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