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