[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 |
---|
[1596] | 15 | logger = logging.getLogger('Skins.search_students') |
---|
[1278] | 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 |
---|
[1558] | 63 | ca_view = False |
---|
[1278] | 64 | faculties = fget('faculties') |
---|
| 65 | departments = fget('departments') |
---|
[1558] | 66 | certificate_level = fget('certificate_level') |
---|
| 67 | dep_str = fac_str = certificate_level_str = '' |
---|
[1006] | 68 | if "ClearanceOfficers" in member.getGroups(): |
---|
| 69 | state = "clearance_requested" |
---|
[1065] | 70 | only_review = False |
---|
| 71 | co_view = True |
---|
[1278] | 72 | if faculties or departments: |
---|
| 73 | #from Products.zdb import set_trace;set_trace() |
---|
| 74 | if faculties: |
---|
| 75 | fac_str = faculties |
---|
| 76 | faculties = faculties.split() |
---|
| 77 | if departments: |
---|
| 78 | dep_str = departments |
---|
| 79 | departments = departments.split() |
---|
| 80 | else: |
---|
| 81 | query = In('portal_type',('Faculty',)) &\ |
---|
| 82 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
| 83 | res = evalAdvancedQuery(query) |
---|
[1596] | 84 | logger.info('ClearanceOfficer %s initiated student_search' % member) |
---|
[1278] | 85 | faculties = [] |
---|
| 86 | if res: |
---|
| 87 | faculties = [f.getId for f in res] |
---|
| 88 | user_info['faculties'] = faculties |
---|
| 89 | fac_str = " ".join(faculties) |
---|
| 90 | query = In('portal_type',('Department',)) &\ |
---|
| 91 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
| 92 | res = evalAdvancedQuery(query) |
---|
| 93 | departments = [] |
---|
| 94 | if res: |
---|
| 95 | departments = [f.getId for f in res] |
---|
| 96 | user_info['departments'] = departments |
---|
| 97 | dep_str = " ".join(departments) |
---|
[1558] | 98 | elif "CourseAdvisers" in member.getGroups(): |
---|
| 99 | state = "courses_registered" |
---|
| 100 | only_review = False |
---|
| 101 | ca_view = True |
---|
| 102 | if certificate_level: |
---|
| 103 | #from Products.zdb import set_trace;set_trace() |
---|
| 104 | certificate_level_str = certificate_level |
---|
| 105 | certificate_level = certificate_level.split() |
---|
| 106 | else: |
---|
| 107 | query = In('portal_type',('StudyLevel',)) &\ |
---|
| 108 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
| 109 | res = evalAdvancedQuery(query) |
---|
[1596] | 110 | logger.info('CourseAdviser %s initiated student_search' % member) |
---|
[1558] | 111 | certificate_level = [] |
---|
| 112 | if res: |
---|
| 113 | certificate_level = ['/'.join(f.getPath().split('/')[-2:]) for f in res] |
---|
| 114 | user_info['certificate_level'] = certificate_level |
---|
| 115 | certificate_level_str = " ".join(certificate_level) |
---|
[1071] | 116 | default = {'search_mode': 'student_id', |
---|
[1006] | 117 | 'review_state': state, |
---|
[920] | 118 | 'search_string': '' |
---|
| 119 | } |
---|
| 120 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
| 121 | schema_id= 'student_search', |
---|
| 122 | context=context, |
---|
[1192] | 123 | mapping=validate and REQUEST, |
---|
[920] | 124 | ob=default, |
---|
| 125 | layout_mode='edit', |
---|
[971] | 126 | formaction="search_students", |
---|
[1278] | 127 | faculties = fac_str, |
---|
| 128 | departments = dep_str, |
---|
[1558] | 129 | certificate_level = certificate_level_str, |
---|
[971] | 130 | commit = False, |
---|
[920] | 131 | ) |
---|
| 132 | if psm == '': |
---|
| 133 | return context.search_students_form(rendered = rend, |
---|
| 134 | psm = psm, |
---|
| 135 | #psm = "%s, %s" % (psm,ds), |
---|
[1065] | 136 | info = user_info, |
---|
[971] | 137 | students = [], |
---|
[1096] | 138 | allowed = allowed, |
---|
[920] | 139 | ) |
---|
| 140 | what = ds.get('search_mode') |
---|
| 141 | state = ds.get('review_state') |
---|
| 142 | st = term = ds.get('search_string') |
---|
| 143 | err = False |
---|
| 144 | with_review = state != "all" |
---|
| 145 | only_review = with_review and not term |
---|
| 146 | bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review) |
---|
| 147 | if not term and not with_review: |
---|
| 148 | psm = "You must specify a search string when searching 'all states'!" |
---|
| 149 | err = True |
---|
| 150 | elif '*' in term: |
---|
| 151 | psm = "Wildcards are not supported!" |
---|
| 152 | err = True |
---|
| 153 | if err: |
---|
| 154 | return context.search_students_form(rendered = rend, |
---|
| 155 | psm = psm, |
---|
| 156 | #psm = "%s, %s" % (psm,ds), |
---|
[1065] | 157 | info = user_info, |
---|
[920] | 158 | students = items, |
---|
[1096] | 159 | allowed = allowed, |
---|
[920] | 160 | ) |
---|
[1479] | 161 | #st_queries = ('jamb_reg_no','matric_no','name') |
---|
[920] | 162 | review_res = None |
---|
| 163 | query = None |
---|
[1071] | 164 | items = [] |
---|
| 165 | res = [] |
---|
[1034] | 166 | review_set = [] |
---|
[1043] | 167 | search_set = [] |
---|
[920] | 168 | if len(term) > 0: |
---|
| 169 | if what == "student_id": |
---|
[1065] | 170 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
[1033] | 171 | if hasattr(students_folder,term.strip()): |
---|
[1596] | 172 | logger.info('%s searches for student with id %s' % (member,term)) |
---|
[1033] | 173 | request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term)) |
---|
| 174 | return context.search_students_form(rendered = rend, |
---|
| 175 | psm = "No student found!", |
---|
| 176 | students = [], |
---|
[1096] | 177 | allowed = allowed, |
---|
[1033] | 178 | ) |
---|
[1034] | 179 | elif what == "department": |
---|
| 180 | res = context.students_catalog(department=term.strip()) |
---|
[1596] | 181 | logger.info('%s searches for student in department %s' % (member,term)) |
---|
[1034] | 182 | search_set = [r.id for r in res] |
---|
[1278] | 183 | elif what == "matric_no": |
---|
| 184 | res = context.students_catalog(matric_no=term.strip()) |
---|
[1596] | 185 | logger.info('%s searches for student with matric_no %s' % (member,term)) |
---|
[1278] | 186 | search_set = [r.id for r in res] |
---|
[1479] | 187 | elif what == "jamb_reg_no": |
---|
[1500] | 188 | st_l = "%s" % term.strip().lower() |
---|
| 189 | st_u = "%s" % term.strip().upper() |
---|
| 190 | res = aq_students(In('jamb_reg_no',(st_l,st_u))) |
---|
[1596] | 191 | logger.info('%s searches for student with jamb_reg_no %s' % (member,term)) |
---|
[1479] | 192 | search_set = [r.id for r in res] |
---|
| 193 | elif what == "name": |
---|
| 194 | pt = ('StudentPersonal') |
---|
[920] | 195 | query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip()) |
---|
[1596] | 196 | logger.info('%s searches for student with name %s' % (member,term)) |
---|
[1033] | 197 | res = evalAdvancedQuery(query) |
---|
| 198 | if res: |
---|
| 199 | for r in res: |
---|
| 200 | pl = r.getPath().split('/') |
---|
| 201 | search_set.append(pl[pl.index('students') + 1]) |
---|
[1479] | 202 | |
---|
| 203 | |
---|
[1278] | 204 | if only_review or with_review or co_view: |
---|
| 205 | if with_timer: |
---|
| 206 | start = DateTime().timeTime() |
---|
| 207 | logger.info('"%s","start 100"' % member) |
---|
| 208 | query = Eq('review_state',state) |
---|
| 209 | review_res = evalAdvancedQuery(query) |
---|
[1596] | 210 | |
---|
| 211 | logger.info('%s searches for students in review_state %s' % (member,state)) |
---|
| 212 | |
---|
[1278] | 213 | if with_timer: |
---|
| 214 | end = DateTime().timeTime() |
---|
| 215 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
| 216 | if co_view: |
---|
[1065] | 217 | only_review = False |
---|
[1278] | 218 | with_review = True |
---|
| 219 | if with_timer: |
---|
| 220 | start = DateTime().timeTime() |
---|
| 221 | logger.info('"%s","start 200"' % member) |
---|
[1096] | 222 | query = In('faculty',faculties) | In('department',departments) |
---|
[1065] | 223 | res = aq_students(query) |
---|
[1278] | 224 | if with_timer: |
---|
| 225 | end = DateTime().timeTime() |
---|
| 226 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
| 227 | start = DateTime().timeTime() |
---|
[1065] | 228 | search_set = [r.id for r in res] |
---|
[1278] | 229 | if with_timer: |
---|
| 230 | end = DateTime().timeTime() |
---|
| 231 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
[1558] | 232 | elif ca_view: |
---|
| 233 | only_review = False |
---|
| 234 | with_review = True |
---|
| 235 | if with_timer: |
---|
| 236 | start = DateTime().timeTime() |
---|
| 237 | logger.info('"%s","start 200"' % member) |
---|
| 238 | courses = [cl.split('/')[0] for cl in certificate_level] |
---|
| 239 | levels = [cl.split('/')[1] for cl in certificate_level] |
---|
| 240 | query = In('course',courses) & In('level',levels) |
---|
| 241 | res = aq_students(query) |
---|
| 242 | if with_timer: |
---|
| 243 | end = DateTime().timeTime() |
---|
| 244 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
| 245 | start = DateTime().timeTime() |
---|
| 246 | search_set = [r.id for r in res] |
---|
| 247 | if with_timer: |
---|
| 248 | end = DateTime().timeTime() |
---|
| 249 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
[1278] | 250 | if with_timer: |
---|
| 251 | logger.info('"%s","start 300"' % member) |
---|
| 252 | start = DateTime().timeTime() |
---|
[1034] | 253 | if review_res: |
---|
[1278] | 254 | review_set = [r.getId for r in review_res] |
---|
| 255 | if with_timer: |
---|
| 256 | end = DateTime().timeTime() |
---|
| 257 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
| 258 | logger.info('"%s","start 400"' % member) |
---|
| 259 | start = DateTime().timeTime() |
---|
[1073] | 260 | if only_review: |
---|
| 261 | all = review_set |
---|
| 262 | elif with_review: |
---|
| 263 | all = [] |
---|
| 264 | for i in search_set: |
---|
| 265 | if i in review_set: |
---|
| 266 | all.append(i) |
---|
| 267 | else: |
---|
| 268 | all = search_set |
---|
[1278] | 269 | if with_timer: |
---|
| 270 | end = DateTime().timeTime() |
---|
| 271 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
[1065] | 272 | items = all[:500] |
---|
[920] | 273 | students = [] |
---|
[1278] | 274 | if with_timer: |
---|
| 275 | logger.info('"%s","start 500"' % member) |
---|
| 276 | start = DateTime().timeTime() |
---|
[920] | 277 | if items: |
---|
| 278 | for item in items: |
---|
[1006] | 279 | stcat = context.students_catalog |
---|
[1446] | 280 | record = stcat(id = item) |
---|
| 281 | if record: |
---|
| 282 | record = record[0] |
---|
| 283 | else: |
---|
| 284 | return item+' not found in students_catalog' |
---|
[971] | 285 | info = {} |
---|
[1006] | 286 | for field in stcat.schema() + stcat.indexes(): |
---|
| 287 | info[field] = getattr(record, field) |
---|
[920] | 288 | else: |
---|
[1011] | 289 | students.append(info) |
---|
[1278] | 290 | if with_timer: |
---|
| 291 | end = DateTime().timeTime() |
---|
| 292 | logger.info('"%s","searchtime","%6.2f"' % (member,end-start)) |
---|
[920] | 293 | |
---|
| 294 | return context.search_students_form(rendered = rend, |
---|
| 295 | psm = "", |
---|
[1065] | 296 | info = user_info, |
---|
[920] | 297 | students = students, |
---|
[1096] | 298 | allowed = allowed, |
---|
[920] | 299 | co_view = co_view, |
---|
[1596] | 300 | ca_view = ca_view |
---|
[920] | 301 | ) |
---|
| 302 | return context.search_students_form(rendered = rend, |
---|
| 303 | psm = "No student found!", |
---|
[1065] | 304 | info = user_info, |
---|
[920] | 305 | students = students, |
---|
[1096] | 306 | allowed = allowed, |
---|
[920] | 307 | ) |
---|
| 308 | |
---|
| 309 | |
---|