source: WAeUP_SRP/trunk/skins/waeup_student/search_students.py @ 1037

Last change on this file since 1037 was 1034, checked in by joachim, 18 years ago

fixed some bugs

File size: 6.2 KB
Line 
1## Script (Python) "search_students"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=REQUEST
8##title=
9##
10# $Id: search_students.py 911 2006-11-20 15:11:29Z henrik $
11"""
12list Students for ClearanceOfficers
13"""
14request = REQUEST
15form = request.form
16fget = form.get
17wftool = context.portal_workflow
18mtool = context.portal_membership
19member = mtool.getAuthenticatedMember()
20roles = member.getRolesInContext(context)
21students_folder = context.portal_url.getPortalObject().campus.students
22try:
23    from Products.AdvancedQuery import Eq, Between, Le,In
24    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
25except:
26    evalAdvancedQuery = None
27
28def cmp_id(a,b):
29    if a.getId() > b.getId():
30        return 1
31    return -1
32
33student_subobjects = ("StudentApplication",
34                      "StudentPersonal",
35                      "StudentStudyCourse",
36                      "StudentAccommodation",
37                      "StudentStudyLevel",)
38
39student_objects = student_subobjects + ("Student",)
40
41info = {}
42items = []
43wf = context.portal_workflow
44#student_wf_states = wf.waeup_student_wf.states.keys()
45#info['wf_states'] = student_wf_states
46#info['wf_states'][0] = " ----- "
47lt = context.portal_layouts
48pr = context.portal_registration
49path_info = request.get('PATH_INFO').split('/')
50validate = request.has_key("cpsdocument_edit_button")
51
52state = "all"
53if "ClearanceOfficers" in member.getGroups():
54    state = "clearance_requested"
55default = {'search_mode': 'name',
56        'review_state': state,
57        'search_string': ''
58        }
59rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
60                      schema_id= 'student_search',
61                      context=context,
62                      mapping=validate and request,
63                      ob=default,
64                      layout_mode='edit',
65                      formaction="search_students",
66                      commit = False,
67                      )
68if psm == '':
69    return context.search_students_form(rendered = rend,
70                             psm = psm,
71                             #psm = "%s, %s" % (psm,ds),
72                             students = [],
73                             allowed = True,
74                             )
75what = ds.get('search_mode')
76state = ds.get('review_state')
77st = term = ds.get('search_string')
78err = False
79with_review = state != "all"
80only_review = with_review and not term
81bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
82if not term and not with_review:
83    psm = "You must specify a search string when searching 'all states'!"
84    err = True
85elif '*' in term:
86    psm = "Wildcards are not supported!"
87    err = True
88if err:
89    return context.search_students_form(rendered = rend,
90                             psm = psm,
91                             #psm = "%s, %s" % (psm,ds),
92                             students = items,
93                             allowed = True,
94                             )
95items = []
96res = []
97portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']}
98st_queries = ('jamb_reg_no','matric_no','name')
99query_step = 0
100review_res = None
101query = None
102review_set = []
103if len(term) > 0:
104    if what == "student_id":
105        query_step = 1
106        if hasattr(students_folder,term.strip()):
107            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
108        return context.search_students_form(rendered = rend,
109                             psm = "No student found!",
110                             students = [],
111                             allowed = True,
112                             )
113    elif what == "department":
114        res = context.students_catalog(department=term.strip())
115        search_set = [r.id for r in res]
116    elif what in st_queries:
117        if what == "jamb_reg_no":
118            query_step = 2
119            pt = ('StudentApplication',)
120            st = "%s" % term.strip().lower()
121        elif what == "matric_no":
122            query_step = 3
123            pt = ('StudentClearance',)
124            st = "%s" % term.strip().lower()
125        elif what == "name":
126            query_step = 4
127            pt = ('StudentPersonal')
128            st = "%s" % term.strip()
129        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
130        res = evalAdvancedQuery(query)
131        search_set = []
132        if res:
133            for r in res:
134                pl = r.getPath().split('/')
135                search_set.append(pl[pl.index('students') + 1])
136if with_review:
137    query_step += 10
138    review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state))
139review_set = []
140if review_res:
141    for r in review_res:
142        pl = r.getPath().split('/')
143        review_set.append(pl[pl.index('students') + 1])
144all = []
145if only_review:
146    all = review_set
147elif with_review:
148    for i in search_set:
149        if i in review_set:
150            all.append(i)
151else:
152    all = search_set
153for a in all[:500]:
154    if a in items:
155        continue
156    items.append(a)
157students = []
158items.sort()
159co_view = False
160if items:
161    for item in items:
162        stcat = context.students_catalog
163        record = stcat(id = item)[0]
164        info = {}
165        for field in stcat.schema() + stcat.indexes():
166            info[field] = getattr(record, field)
167        if "ClearanceOfficers" in member.getGroups():
168            co_view = True
169            res = context.portal_catalog(portal_type='Student', id = item)
170            if len(res) != 1:
171                continue
172            droles = member.getRolesInContext(res[0].getObject())
173            info['review_state'] = res[0].review_state
174            if "ClearanceOfficer" in droles:
175                students.append(info)
176        else:
177            students.append(info)
178
179    return context.search_students_form(rendered = rend,
180                             psm = "",
181                             students = students,
182                             allowed = True,
183                             co_view = co_view,
184                             )
185return context.search_students_form(rendered = rend,
186                             psm = "No student found!",
187                             students = students,
188                             allowed = True,
189                             )
190
191
Note: See TracBrowser for help on using the repository browser.