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