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 | request = REQUEST |
---|
15 | form = request.form |
---|
16 | fget = form.get |
---|
17 | #wftool = context.portal_workflow |
---|
18 | mtool = context.portal_membership |
---|
19 | member = mtool.getAuthenticatedMember() |
---|
20 | #roles = member.getRolesInContext(context) |
---|
21 | lt = context.portal_layouts |
---|
22 | #pr = context.portal_registration |
---|
23 | path_info = request.get('PATH_INFO').split('/') |
---|
24 | try: |
---|
25 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
26 | evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery |
---|
27 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
28 | except: |
---|
29 | evalAdvancedQuery = None |
---|
30 | |
---|
31 | def cmp_id(a,b): |
---|
32 | if a.getId() > b.getId(): |
---|
33 | return 1 |
---|
34 | return -1 |
---|
35 | |
---|
36 | student_subobjects = ("StudentApplication", |
---|
37 | "StudentPersonal", |
---|
38 | "StudentStudyCourse", |
---|
39 | "StudentAccommodation", |
---|
40 | "StudentStudyLevel",) |
---|
41 | |
---|
42 | #student_objects = student_subobjects + ("Student",) |
---|
43 | student_objects = ("Student",) |
---|
44 | |
---|
45 | user_info = {} |
---|
46 | items = [] |
---|
47 | validate = request.has_key("cpsdocument_edit_button") |
---|
48 | |
---|
49 | state = "all" |
---|
50 | user_info['member'] = str(member) |
---|
51 | user_info['departments'] = [] |
---|
52 | user_info['faculties'] = [] |
---|
53 | #from Products.zdb import set_trace |
---|
54 | #set_trace() |
---|
55 | co_view = False |
---|
56 | if "ClearanceOfficers" in member.getGroups(): |
---|
57 | state = "clearance_requested" |
---|
58 | only_review = False |
---|
59 | co_view = True |
---|
60 | query = In('portal_type',('Faculty',)) &\ |
---|
61 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
62 | res = evalAdvancedQuery(query) |
---|
63 | faculties = [] |
---|
64 | if res: |
---|
65 | faculties = [f.getId for f in res] |
---|
66 | user_info['faculties'] = faculties |
---|
67 | query = In('portal_type',('Department',)) &\ |
---|
68 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
69 | res = evalAdvancedQuery(query) |
---|
70 | departments = [] |
---|
71 | if res: |
---|
72 | departments = [f.getId for f in res] |
---|
73 | user_info['departments'] = departments |
---|
74 | default = {'search_mode': 'name', |
---|
75 | 'review_state': state, |
---|
76 | 'search_string': '' |
---|
77 | } |
---|
78 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
79 | schema_id= 'student_search', |
---|
80 | context=context, |
---|
81 | mapping=validate and request, |
---|
82 | ob=default, |
---|
83 | layout_mode='edit', |
---|
84 | formaction="search_students", |
---|
85 | commit = False, |
---|
86 | ) |
---|
87 | if psm == '': |
---|
88 | return context.search_students_form(rendered = rend, |
---|
89 | psm = psm, |
---|
90 | #psm = "%s, %s" % (psm,ds), |
---|
91 | info = user_info, |
---|
92 | students = [], |
---|
93 | allowed = True, |
---|
94 | ) |
---|
95 | what = ds.get('search_mode') |
---|
96 | state = ds.get('review_state') |
---|
97 | st = term = ds.get('search_string') |
---|
98 | err = False |
---|
99 | with_review = state != "all" |
---|
100 | only_review = with_review and not term |
---|
101 | bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review) |
---|
102 | if not term and not with_review: |
---|
103 | psm = "You must specify a search string when searching 'all states'!" |
---|
104 | err = True |
---|
105 | elif '*' in term: |
---|
106 | psm = "Wildcards are not supported!" |
---|
107 | err = True |
---|
108 | if err: |
---|
109 | return context.search_students_form(rendered = rend, |
---|
110 | psm = psm, |
---|
111 | #psm = "%s, %s" % (psm,ds), |
---|
112 | info = user_info, |
---|
113 | students = items, |
---|
114 | allowed = True, |
---|
115 | ) |
---|
116 | items = [] |
---|
117 | res = [] |
---|
118 | portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']} |
---|
119 | st_queries = ('jamb_reg_no','matric_no','name') |
---|
120 | query_step = 0 |
---|
121 | review_res = None |
---|
122 | query = None |
---|
123 | review_set = [] |
---|
124 | search_set = [] |
---|
125 | if len(term) > 0: |
---|
126 | if what == "student_id": |
---|
127 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
128 | query_step = 1 |
---|
129 | if hasattr(students_folder,term.strip()): |
---|
130 | request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term)) |
---|
131 | return context.search_students_form(rendered = rend, |
---|
132 | psm = "No student found!", |
---|
133 | students = [], |
---|
134 | allowed = True, |
---|
135 | ) |
---|
136 | elif what == "department": |
---|
137 | res = context.students_catalog(department=term.strip()) |
---|
138 | search_set = [r.id for r in res] |
---|
139 | elif what in st_queries: |
---|
140 | if what == "jamb_reg_no": |
---|
141 | query_step = 2 |
---|
142 | pt = ('StudentApplication',) |
---|
143 | st = "%s" % term.strip().lower() |
---|
144 | elif what == "matric_no": |
---|
145 | query_step = 3 |
---|
146 | pt = ('StudentClearance',) |
---|
147 | st = "%s" % term.strip().lower() |
---|
148 | elif what == "name": |
---|
149 | query_step = 4 |
---|
150 | pt = ('StudentPersonal') |
---|
151 | st = "%s" % term.strip() |
---|
152 | query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip()) |
---|
153 | res = evalAdvancedQuery(query) |
---|
154 | search_set = [] |
---|
155 | if res: |
---|
156 | for r in res: |
---|
157 | pl = r.getPath().split('/') |
---|
158 | search_set.append(pl[pl.index('students') + 1]) |
---|
159 | elif co_view: |
---|
160 | #from Products.zdb import set_trace |
---|
161 | #set_trace() |
---|
162 | only_review = False |
---|
163 | query = In('faculty',faculties) | In('department',departments) |
---|
164 | res = aq_students(query) |
---|
165 | search_set = [r.id for r in res] |
---|
166 | ## res = context.students_catalog(faculty=faculties[0],) |
---|
167 | ## res = context.students_catalog(faculty=faculties,department=departments) |
---|
168 | #set_trace() |
---|
169 | if with_review: |
---|
170 | query_step += 10 |
---|
171 | #review_res = evalAdvancedQuery(('portal_type',student_objects) & Eq('review_state',state)) |
---|
172 | review_res = evalAdvancedQuery(Eq('review_state',state)) |
---|
173 | review_set = [] |
---|
174 | if review_res: |
---|
175 | for r in review_res: |
---|
176 | #pl = r.getPath().split('/') |
---|
177 | #review_set.append(pl[pl.index('students') + 1]) |
---|
178 | review_set.append(r.getId) |
---|
179 | all = [] |
---|
180 | #set_trace() |
---|
181 | if only_review: |
---|
182 | all = review_set |
---|
183 | elif with_review: |
---|
184 | for i in search_set: |
---|
185 | if i in review_set: |
---|
186 | all.append(i) |
---|
187 | else: |
---|
188 | all = search_set |
---|
189 | ##for a in all[:50]: |
---|
190 | ## if a in items: |
---|
191 | ## continue |
---|
192 | ## items.append(a) |
---|
193 | items = all[:500] |
---|
194 | students = [] |
---|
195 | #items.sort() |
---|
196 | |
---|
197 | if items: |
---|
198 | for item in items: |
---|
199 | stcat = context.students_catalog |
---|
200 | record = stcat(id = item)[0] |
---|
201 | info = {} |
---|
202 | for field in stcat.schema() + stcat.indexes(): |
---|
203 | info[field] = getattr(record, field) |
---|
204 | ## if "ClearanceOfficers" in member.getGroups(): |
---|
205 | ## co_view = True |
---|
206 | ## res = context.portal_catalog(portal_type='Student', id = item) |
---|
207 | ## if len(res) != 1: |
---|
208 | ## continue |
---|
209 | ## droles = member.getRolesInContext(res[0].getObject()) |
---|
210 | ## info['review_state'] = res[0].review_state |
---|
211 | ## if "ClearanceOfficer" in droles: |
---|
212 | ## students.append(info) |
---|
213 | else: |
---|
214 | students.append(info) |
---|
215 | |
---|
216 | return context.search_students_form(rendered = rend, |
---|
217 | psm = "", |
---|
218 | info = user_info, |
---|
219 | students = students, |
---|
220 | allowed = True, |
---|
221 | co_view = co_view, |
---|
222 | ) |
---|
223 | return context.search_students_form(rendered = rend, |
---|
224 | psm = "No student found!", |
---|
225 | info = user_info, |
---|
226 | students = students, |
---|
227 | allowed = True, |
---|
228 | ) |
---|
229 | |
---|
230 | |
---|