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 | default = {'search_mode': 'name', |
---|
52 | 'review_state': 'clearance_requested', |
---|
53 | 'search_string': '' |
---|
54 | } |
---|
55 | #from Products.zdb import set_trace |
---|
56 | #set_trace() |
---|
57 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
58 | schema_id= 'student_search', |
---|
59 | context=context, |
---|
60 | mapping=validate and request, |
---|
61 | ob=default, |
---|
62 | layout_mode='edit', |
---|
63 | formaction="search_students", |
---|
64 | commit = False, |
---|
65 | ) |
---|
66 | if psm == '': |
---|
67 | return context.search_students_form(rendered = rend, |
---|
68 | psm = psm, |
---|
69 | #psm = "%s, %s" % (psm,ds), |
---|
70 | students = [], |
---|
71 | allowed = True, |
---|
72 | ) |
---|
73 | #set_trace() |
---|
74 | what = ds.get('search_mode') |
---|
75 | state = ds.get('review_state') |
---|
76 | st = term = ds.get('search_string') |
---|
77 | err = False |
---|
78 | with_review = state != "all" |
---|
79 | only_review = with_review and not term |
---|
80 | ##with_level_results = state.startswith("category") or\ |
---|
81 | ## state in ('content_addable',) and\ |
---|
82 | ## evalAdvancedQuery is not None |
---|
83 | bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review) |
---|
84 | if not term and not with_review: |
---|
85 | psm = "You must specify a search string when searching 'all states'!" |
---|
86 | err = True |
---|
87 | elif '*' in term: |
---|
88 | psm = "Wildcards are not supported!" |
---|
89 | err = True |
---|
90 | if err: |
---|
91 | return context.search_students_form(rendered = rend, |
---|
92 | psm = psm, |
---|
93 | #psm = "%s, %s" % (psm,ds), |
---|
94 | students = items, |
---|
95 | allowed = True, |
---|
96 | ) |
---|
97 | items = [] |
---|
98 | res = [] |
---|
99 | portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']} |
---|
100 | st_queries = ('jamb_reg_no','matric_no','name') |
---|
101 | query_step = 0 |
---|
102 | review_res = None |
---|
103 | query = None |
---|
104 | if len(term) > 0: |
---|
105 | if what == "student_id": |
---|
106 | query_step = 1 |
---|
107 | query = Eq('portal_type','Student') & Eq('id', term.strip()) |
---|
108 | elif what in st_queries: |
---|
109 | if what == "jamb_reg_no": |
---|
110 | query_step = 2 |
---|
111 | pt = ('StudentApplication',) |
---|
112 | st = "%s" % term.strip().lower() |
---|
113 | elif what == "matric_no": |
---|
114 | query_step = 3 |
---|
115 | pt = ('StudentClearance',) |
---|
116 | st = "%s" % term.strip().lower() |
---|
117 | elif what == "name": |
---|
118 | query_step = 4 |
---|
119 | pt = ('StudentPersonal') |
---|
120 | st = "%s" % term.strip() |
---|
121 | query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip()) |
---|
122 | res = evalAdvancedQuery(query) |
---|
123 | if with_review: |
---|
124 | query_step += 10 |
---|
125 | review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state)) |
---|
126 | search_set = [] |
---|
127 | if res: |
---|
128 | for r in res: |
---|
129 | pl = r.getPath().split('/') |
---|
130 | search_set.append(pl[pl.index('students') + 1]) |
---|
131 | review_set = [] |
---|
132 | if review_res: |
---|
133 | for r in review_res: |
---|
134 | pl = r.getPath().split('/') |
---|
135 | review_set.append(pl[pl.index('students') + 1]) |
---|
136 | all = [] |
---|
137 | if only_review: |
---|
138 | all = review_set |
---|
139 | elif with_review: |
---|
140 | for i in search_set: |
---|
141 | if i in review_set: |
---|
142 | all.append(i) |
---|
143 | else: |
---|
144 | all = search_set |
---|
145 | for a in all: |
---|
146 | if a in items: |
---|
147 | continue |
---|
148 | items.append(a) |
---|
149 | students = [] |
---|
150 | items.sort() |
---|
151 | co_view = False |
---|
152 | if items: |
---|
153 | for item in items: |
---|
154 | #if context.isClearanceOfficer(info): |
---|
155 | info = {} |
---|
156 | if "ClearanceOfficers" in member.getGroups(): |
---|
157 | co_view = True |
---|
158 | ## droles = member.getRolesInContext(item) |
---|
159 | ## if "ClearanceOfficer" in droles: |
---|
160 | ## info = context.getStudentInfo(item) |
---|
161 | ## students.append(info) |
---|
162 | else: |
---|
163 | #info = context.getStudentInfo(item) |
---|
164 | info = context.students_catalog(id = item) |
---|
165 | if len(info) == 1: |
---|
166 | students.append(info[0]) |
---|
167 | |
---|
168 | return context.search_students_form(rendered = rend, |
---|
169 | psm = "", |
---|
170 | #psm = "%d,%d matching Students found QS = %s" %\ |
---|
171 | # (len(review_set),len(search_set),query_step), |
---|
172 | #psm = "%d found QS = %s items: %s" % (len(items),query_step,items), |
---|
173 | students = students, |
---|
174 | allowed = True, |
---|
175 | co_view = co_view, |
---|
176 | ) |
---|
177 | return context.search_students_form(rendered = rend, |
---|
178 | psm = "No student found!", |
---|
179 | #psm = """Step: %s found: %s Your search for "%s" in %s with state %s failed.<br\>%s""" % (query_step,len(items),st,what,state,bools), |
---|
180 | students = students, |
---|
181 | allowed = True, |
---|
182 | ) |
---|
183 | |
---|
184 | |
---|