[920] | 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 |
---|
[1033] | 15 | form = request.form |
---|
| 16 | fget = form.get |
---|
[1065] | 17 | #wftool = context.portal_workflow |
---|
[920] | 18 | mtool = context.portal_membership |
---|
| 19 | member = mtool.getAuthenticatedMember() |
---|
[1096] | 20 | is_anon = mtool.isAnonymousUser() |
---|
[1065] | 21 | #roles = member.getRolesInContext(context) |
---|
| 22 | lt = context.portal_layouts |
---|
| 23 | #pr = context.portal_registration |
---|
| 24 | path_info = request.get('PATH_INFO').split('/') |
---|
[1096] | 25 | |
---|
| 26 | allowed = True |
---|
| 27 | if is_anon: |
---|
| 28 | allowed = False |
---|
| 29 | |
---|
[920] | 30 | try: |
---|
| 31 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 32 | evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery |
---|
[1065] | 33 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
[920] | 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 | |
---|
[1065] | 48 | #student_objects = student_subobjects + ("Student",) |
---|
| 49 | student_objects = ("Student",) |
---|
[920] | 50 | |
---|
[1065] | 51 | user_info = {} |
---|
[920] | 52 | items = [] |
---|
| 53 | validate = request.has_key("cpsdocument_edit_button") |
---|
[1006] | 54 | |
---|
| 55 | state = "all" |
---|
[1065] | 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 |
---|
[1006] | 62 | if "ClearanceOfficers" in member.getGroups(): |
---|
| 63 | state = "clearance_requested" |
---|
[1065] | 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 |
---|
[1071] | 80 | default = {'search_mode': 'student_id', |
---|
[1006] | 81 | 'review_state': state, |
---|
[920] | 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', |
---|
[971] | 90 | formaction="search_students", |
---|
| 91 | commit = False, |
---|
[920] | 92 | ) |
---|
| 93 | if psm == '': |
---|
| 94 | return context.search_students_form(rendered = rend, |
---|
| 95 | psm = psm, |
---|
| 96 | #psm = "%s, %s" % (psm,ds), |
---|
[1065] | 97 | info = user_info, |
---|
[971] | 98 | students = [], |
---|
[1096] | 99 | allowed = allowed, |
---|
[920] | 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), |
---|
[1065] | 118 | info = user_info, |
---|
[920] | 119 | students = items, |
---|
[1096] | 120 | allowed = allowed, |
---|
[920] | 121 | ) |
---|
| 122 | st_queries = ('jamb_reg_no','matric_no','name') |
---|
| 123 | review_res = None |
---|
| 124 | query = None |
---|
[1071] | 125 | items = [] |
---|
| 126 | res = [] |
---|
[1034] | 127 | review_set = [] |
---|
[1043] | 128 | search_set = [] |
---|
[920] | 129 | if len(term) > 0: |
---|
| 130 | if what == "student_id": |
---|
[1065] | 131 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
[1033] | 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 = [], |
---|
[1096] | 137 | allowed = allowed, |
---|
[1033] | 138 | ) |
---|
[1034] | 139 | elif what == "department": |
---|
| 140 | res = context.students_catalog(department=term.strip()) |
---|
| 141 | search_set = [r.id for r in res] |
---|
[920] | 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()) |
---|
[1033] | 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]) |
---|
[1065] | 158 | elif co_view: |
---|
| 159 | #from Products.zdb import set_trace |
---|
| 160 | #set_trace() |
---|
| 161 | only_review = False |
---|
[1096] | 162 | query = In('faculty',faculties) | In('department',departments) |
---|
[1065] | 163 | res = aq_students(query) |
---|
| 164 | search_set = [r.id for r in res] |
---|
[1095] | 165 | if only_review or with_review: |
---|
[1071] | 166 | query = Eq('review_state',state) |
---|
| 167 | review_res = evalAdvancedQuery(query) |
---|
[1034] | 168 | review_set = [] |
---|
| 169 | if review_res: |
---|
| 170 | for r in review_res: |
---|
[1084] | 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) |
---|
[1065] | 176 | #set_trace() |
---|
[1073] | 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 |
---|
[1065] | 186 | items = all[:500] |
---|
[920] | 187 | students = [] |
---|
| 188 | if items: |
---|
| 189 | for item in items: |
---|
[1006] | 190 | stcat = context.students_catalog |
---|
| 191 | record = stcat(id = item)[0] |
---|
[971] | 192 | info = {} |
---|
[1006] | 193 | for field in stcat.schema() + stcat.indexes(): |
---|
| 194 | info[field] = getattr(record, field) |
---|
[1065] | 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) |
---|
[920] | 204 | else: |
---|
[1011] | 205 | students.append(info) |
---|
[920] | 206 | |
---|
| 207 | return context.search_students_form(rendered = rend, |
---|
| 208 | psm = "", |
---|
[1065] | 209 | info = user_info, |
---|
[920] | 210 | students = students, |
---|
[1096] | 211 | allowed = allowed, |
---|
[920] | 212 | co_view = co_view, |
---|
| 213 | ) |
---|
| 214 | return context.search_students_form(rendered = rend, |
---|
| 215 | psm = "No student found!", |
---|
[1065] | 216 | info = user_info, |
---|
[920] | 217 | students = students, |
---|
[1096] | 218 | allowed = allowed, |
---|
[920] | 219 | ) |
---|
| 220 | |
---|
| 221 | |
---|