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 | """ |
---|
12 | list Students for ClearanceOfficers |
---|
13 | """ |
---|
14 | try: |
---|
15 | from Products.zdb import set_trace |
---|
16 | except: |
---|
17 | def set_trace(): |
---|
18 | pass |
---|
19 | import logging |
---|
20 | logger = logging.getLogger('Skins.search_students') |
---|
21 | #from DateTime import DateTime |
---|
22 | # |
---|
23 | #with_timer = True |
---|
24 | #with_timer = False |
---|
25 | # |
---|
26 | request = REQUEST |
---|
27 | form = request.form |
---|
28 | fget = form.get |
---|
29 | mtool = context.portal_membership |
---|
30 | member = mtool.getAuthenticatedMember() |
---|
31 | is_anon = mtool.isAnonymousUser() |
---|
32 | lt = context.portal_layouts |
---|
33 | path_info = request.get('PATH_INFO').split('/') |
---|
34 | |
---|
35 | allowed = True |
---|
36 | if is_anon or context.isStudent(): |
---|
37 | allowed = False |
---|
38 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
39 | try: |
---|
40 | aq_portal = context.portal_catalog.evalAdvancedQuery |
---|
41 | except: |
---|
42 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
43 | students_catalog = context.students_catalog |
---|
44 | aq_students = students_catalog.evalAdvancedQuery |
---|
45 | |
---|
46 | #def cmp_id(a,b): |
---|
47 | # if a.getId() > b.getId(): |
---|
48 | # return 1 |
---|
49 | # return -1 |
---|
50 | |
---|
51 | user_info = {} |
---|
52 | validate = request.has_key("cpsdocument_edit_button") |
---|
53 | |
---|
54 | default_state = "all" |
---|
55 | user_info['member'] = str(member) |
---|
56 | user_info['departments'] = [] |
---|
57 | user_info['faculties'] = [] |
---|
58 | co_view = False |
---|
59 | ca_view = False |
---|
60 | session_data = request.SESSION |
---|
61 | faculties = session_data.get('faculties',None) |
---|
62 | departments = session_data.get('departments',None) |
---|
63 | certificate_levels = session_data.get('certificate_levels',None) |
---|
64 | # determine local roles |
---|
65 | if "ClearanceOfficers" in member.getGroups(): |
---|
66 | default_state = "clearance_requested" |
---|
67 | co_view = True |
---|
68 | if faculties is None or departments is None: |
---|
69 | logger.info('ClearanceOfficer %s initiated student_search' % member) |
---|
70 | query = In('portal_type',('Faculty','Department')) &\ |
---|
71 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
72 | res = aq_portal(query) |
---|
73 | faculties = [f.getId for f in res if f.portal_type == 'Faculty'] |
---|
74 | departments = [f.getId for f in res if f.portal_type == 'Department'] |
---|
75 | user_info['faculties'] = session_data['faculties'] = faculties |
---|
76 | user_info['departments'] = session_data['departments'] = departments |
---|
77 | elif "CourseAdvisers" in member.getGroups(): |
---|
78 | default_state = "courses_registered" |
---|
79 | ca_view = True |
---|
80 | if certificate_levels is None: |
---|
81 | logger.info('CourseAdviser %s initiated student_search' % member) |
---|
82 | query = In('portal_type',('StudyLevel',)) &\ |
---|
83 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
84 | res = aq_portal(query) |
---|
85 | certificate_levels = ['/'.join(f.getPath().split('/')[-2:]) for f in res] |
---|
86 | cert_ids = [] |
---|
87 | level_ids = [] |
---|
88 | for cl in certificate_levels: |
---|
89 | c,l = cl.split('/') |
---|
90 | level_ids += l, |
---|
91 | if c not in cert_ids: |
---|
92 | cert_ids += c, |
---|
93 | cert_brains = aq_portal(In("id",cert_ids)) |
---|
94 | end_levels = {} |
---|
95 | for cert_brain in cert_brains: |
---|
96 | end_levels[cert_brain.getId] = cert_brain.getObject().getContent().end_level |
---|
97 | for cl in certificate_levels[:]: |
---|
98 | c,l = cl.split('/') |
---|
99 | for prob in (10,20): |
---|
100 | certificate_levels += "%s/%s" % (c,int(l) + prob), |
---|
101 | if l == end_levels[c]: |
---|
102 | certificate_levels += "%s/%s" % (c,int(l) + 100), |
---|
103 | user_info['certificate_levels'] = session_data['certificate_levels'] = certificate_levels |
---|
104 | |
---|
105 | default = {'search_mode': 'student_id', |
---|
106 | 'review_state': default_state, |
---|
107 | 'search_string': '' |
---|
108 | } |
---|
109 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
110 | schema_id= 'student_search', |
---|
111 | context=context, |
---|
112 | mapping=validate and REQUEST, |
---|
113 | ob=default, |
---|
114 | layout_mode='edit', |
---|
115 | formaction="search_students", |
---|
116 | commit = False, |
---|
117 | ) |
---|
118 | if psm == '': |
---|
119 | return context.search_students_form(rendered = rend, |
---|
120 | psm = psm, |
---|
121 | #psm = "%s, %s" % (psm,ds), |
---|
122 | info = user_info, |
---|
123 | students = [], |
---|
124 | allowed = allowed, |
---|
125 | ) |
---|
126 | |
---|
127 | # return to search form if form was not properly filled |
---|
128 | |
---|
129 | what = ds.get('search_mode') |
---|
130 | state = ds.get('review_state') |
---|
131 | st = term = ds.get('search_string') |
---|
132 | err = False |
---|
133 | if not term and state == "all": |
---|
134 | psm = "You must specify a search string when searching for students in all states!" |
---|
135 | err = True |
---|
136 | elif '*' in term: |
---|
137 | psm = "Wildcards are not supported!" |
---|
138 | err = True |
---|
139 | if err: |
---|
140 | return context.search_students_form(rendered = rend, |
---|
141 | psm = psm, |
---|
142 | #psm = "%s, %s" % (psm,ds), |
---|
143 | info = user_info, |
---|
144 | students = [], |
---|
145 | allowed = allowed, |
---|
146 | ) |
---|
147 | |
---|
148 | # build query string part 1 |
---|
149 | |
---|
150 | query = None |
---|
151 | if len(term) > 0: |
---|
152 | if what == "student_id": |
---|
153 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
154 | if hasattr(students_folder,term.strip()): |
---|
155 | logger.info('%s searches for student with id %s' % (member,term)) |
---|
156 | request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term)) |
---|
157 | return context.search_students_form(rendered = rend, |
---|
158 | psm = "No student found!", |
---|
159 | students = [], |
---|
160 | allowed = allowed, |
---|
161 | info = user_info, |
---|
162 | ) |
---|
163 | elif what == "department": |
---|
164 | query = Eq('department', term.strip()) |
---|
165 | logger.info('%s searches for student in department %s' % (member,term)) |
---|
166 | elif what == "matric_no": |
---|
167 | query = Eq('matric_no', term.strip()) |
---|
168 | logger.info('%s searches for student with matric_no %s' % (member,term)) |
---|
169 | elif what == "jamb_reg_no": |
---|
170 | st_l = "%s" % term.strip().lower() |
---|
171 | st_u = "%s" % term.strip().upper() |
---|
172 | query = In('jamb_reg_no',(st_l,st_u)) |
---|
173 | logger.info('%s searches for student with jamb_reg_no %s' % (member,term)) |
---|
174 | elif what == "name": |
---|
175 | if len(term) < 4: |
---|
176 | return context.search_students_form(rendered = rend, |
---|
177 | psm = "Search string is too short!", |
---|
178 | students = [], |
---|
179 | info = user_info, |
---|
180 | allowed = allowed, |
---|
181 | ) |
---|
182 | query = Eq('name',"%s*" % term.strip()) |
---|
183 | logger.info('%s searches for student with name %s' % (member,term)) |
---|
184 | |
---|
185 | |
---|
186 | # build query string part 2 |
---|
187 | |
---|
188 | if state != 'all': |
---|
189 | if query is not None: |
---|
190 | query = Eq('review_state',state) & query |
---|
191 | else: |
---|
192 | query = Eq('review_state',state) |
---|
193 | logger.info('%s searches for students in review_state %s' % (member,state)) |
---|
194 | |
---|
195 | # build query string part 3 |
---|
196 | if co_view: |
---|
197 | if query is not None: |
---|
198 | query = query & In('faculty',faculties) | In('department',departments) |
---|
199 | else: |
---|
200 | query = In('faculty',faculties) | In('department',departments) |
---|
201 | elif ca_view: |
---|
202 | courses = [cl.split('/')[0] for cl in certificate_levels] |
---|
203 | levels = [cl.split('/')[1] for cl in certificate_levels] |
---|
204 | if query is not None: |
---|
205 | query = query & In('course',courses) & In('level',levels) |
---|
206 | |
---|
207 | # search students_catalog |
---|
208 | |
---|
209 | student_records = aq_students(query,('matric_no','jamb_reg_no')) |
---|
210 | students = [] |
---|
211 | if student_records: |
---|
212 | for student_record in student_records: |
---|
213 | info = {} |
---|
214 | for field in students_catalog.schema() + students_catalog.indexes(): |
---|
215 | info[field] = getattr(student_record, field) |
---|
216 | else: |
---|
217 | students.append(info) |
---|
218 | return context.search_students_form(rendered = rend, |
---|
219 | psm = "", |
---|
220 | info = user_info, |
---|
221 | students = students, |
---|
222 | allowed = allowed, |
---|
223 | co_view = co_view, |
---|
224 | ca_view = ca_view |
---|
225 | ) |
---|
226 | return context.search_students_form(rendered = rend, |
---|
227 | psm = "No student found!", |
---|
228 | info = user_info, |
---|
229 | students = students, |
---|
230 | allowed = allowed, |
---|
231 | ) |
---|
232 | |
---|
233 | |
---|