[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 | """ |
---|
[2873] | 14 | try: |
---|
| 15 | from Products.zdb import set_trace |
---|
| 16 | except: |
---|
| 17 | def set_trace(): |
---|
| 18 | pass |
---|
[1278] | 19 | import logging |
---|
[1571] | 20 | logger = logging.getLogger('Skins.search_students') |
---|
[2249] | 21 | #from DateTime import DateTime |
---|
[1278] | 22 | # |
---|
| 23 | #with_timer = True |
---|
[2249] | 24 | #with_timer = False |
---|
[1278] | 25 | # |
---|
[920] | 26 | request = REQUEST |
---|
[1033] | 27 | form = request.form |
---|
| 28 | fget = form.get |
---|
[920] | 29 | mtool = context.portal_membership |
---|
| 30 | member = mtool.getAuthenticatedMember() |
---|
[1096] | 31 | is_anon = mtool.isAnonymousUser() |
---|
[1065] | 32 | lt = context.portal_layouts |
---|
| 33 | path_info = request.get('PATH_INFO').split('/') |
---|
[1096] | 34 | |
---|
| 35 | allowed = True |
---|
[2431] | 36 | if is_anon or context.isStudent(): |
---|
[1096] | 37 | allowed = False |
---|
[1845] | 38 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
[920] | 39 | try: |
---|
[1845] | 40 | aq_portal = context.portal_catalog.evalAdvancedQuery |
---|
[920] | 41 | except: |
---|
[1845] | 42 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
[3022] | 43 | students_catalog = context.students_catalog |
---|
[3024] | 44 | aq_students = students_catalog.evalAdvancedQuery |
---|
[920] | 45 | |
---|
[3024] | 46 | #def cmp_id(a,b): |
---|
| 47 | # if a.getId() > b.getId(): |
---|
| 48 | # return 1 |
---|
| 49 | # return -1 |
---|
[920] | 50 | |
---|
[1065] | 51 | user_info = {} |
---|
[920] | 52 | validate = request.has_key("cpsdocument_edit_button") |
---|
[1006] | 53 | |
---|
[3024] | 54 | default_state = "all" |
---|
[1065] | 55 | user_info['member'] = str(member) |
---|
| 56 | user_info['departments'] = [] |
---|
| 57 | user_info['faculties'] = [] |
---|
| 58 | co_view = False |
---|
[1557] | 59 | ca_view = False |
---|
[1278] | 60 | faculties = fget('faculties') |
---|
| 61 | departments = fget('departments') |
---|
[2253] | 62 | certificate_levels = fget('certificate_levels') |
---|
| 63 | dep_str = fac_str = certificate_levels_str = '' |
---|
[3024] | 64 | |
---|
| 65 | # determine local roles |
---|
| 66 | |
---|
[1006] | 67 | if "ClearanceOfficers" in member.getGroups(): |
---|
[3024] | 68 | default_state = "clearance_requested" |
---|
[1065] | 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,)) |
---|
[1845] | 81 | res = aq_portal(query) |
---|
[1571] | 82 | logger.info('ClearanceOfficer %s initiated student_search' % member) |
---|
[1278] | 83 | faculties = [] |
---|
| 84 | if res: |
---|
| 85 | faculties = [f.getId for f in res] |
---|
| 86 | user_info['faculties'] = faculties |
---|
| 87 | fac_str = " ".join(faculties) |
---|
| 88 | query = In('portal_type',('Department',)) &\ |
---|
| 89 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
[1845] | 90 | res = aq_portal(query) |
---|
[1278] | 91 | departments = [] |
---|
| 92 | if res: |
---|
| 93 | departments = [f.getId for f in res] |
---|
| 94 | user_info['departments'] = departments |
---|
| 95 | dep_str = " ".join(departments) |
---|
[1557] | 96 | elif "CourseAdvisers" in member.getGroups(): |
---|
[3024] | 97 | default_state = "courses_registered" |
---|
[1557] | 98 | ca_view = True |
---|
[2253] | 99 | if certificate_levels: |
---|
| 100 | certificate_levels_str = certificate_levels |
---|
| 101 | certificate_levels = certificate_levels.split() |
---|
[1557] | 102 | else: |
---|
| 103 | query = In('portal_type',('StudyLevel',)) &\ |
---|
| 104 | In('localUsersWithRoles', ("user:%s" % member,)) |
---|
[1845] | 105 | res = aq_portal(query) |
---|
[1571] | 106 | logger.info('CourseAdviser %s initiated student_search' % member) |
---|
[2253] | 107 | certificate_levels = [] |
---|
[1557] | 108 | if res: |
---|
[2253] | 109 | certificate_levels = ['/'.join(f.getPath().split('/')[-2:]) for f in res] |
---|
[2873] | 110 | cert_ids = [] |
---|
| 111 | level_ids = [] |
---|
| 112 | for cl in certificate_levels: |
---|
| 113 | c,l = cl.split('/') |
---|
| 114 | level_ids += l, |
---|
| 115 | if c not in cert_ids: |
---|
| 116 | cert_ids += c, |
---|
| 117 | cert_brains = aq_portal(In("id",cert_ids)) |
---|
| 118 | end_levels = {} |
---|
| 119 | for cert_brain in cert_brains: |
---|
| 120 | end_levels[cert_brain.getId] = cert_brain.getObject().getContent().end_level |
---|
| 121 | for cl in certificate_levels[:]: |
---|
| 122 | c,l = cl.split('/') |
---|
| 123 | for prob in (10,20): |
---|
| 124 | certificate_levels += "%s/%s" % (c,int(l) + prob), |
---|
| 125 | if l == end_levels[c]: |
---|
| 126 | certificate_levels += "%s/%s" % (c,int(l) + 100), |
---|
[2253] | 127 | user_info['certificate_levels'] = certificate_levels |
---|
| 128 | certificate_levels_str = " ".join(certificate_levels) |
---|
| 129 | |
---|
[1071] | 130 | default = {'search_mode': 'student_id', |
---|
[3024] | 131 | 'review_state': default_state, |
---|
[920] | 132 | 'search_string': '' |
---|
| 133 | } |
---|
| 134 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
| 135 | schema_id= 'student_search', |
---|
| 136 | context=context, |
---|
[1192] | 137 | mapping=validate and REQUEST, |
---|
[920] | 138 | ob=default, |
---|
| 139 | layout_mode='edit', |
---|
[971] | 140 | formaction="search_students", |
---|
[1278] | 141 | faculties = fac_str, |
---|
| 142 | departments = dep_str, |
---|
[2253] | 143 | certificate_levels = certificate_levels_str, |
---|
[971] | 144 | commit = False, |
---|
[920] | 145 | ) |
---|
| 146 | if psm == '': |
---|
| 147 | return context.search_students_form(rendered = rend, |
---|
| 148 | psm = psm, |
---|
| 149 | #psm = "%s, %s" % (psm,ds), |
---|
[1065] | 150 | info = user_info, |
---|
[971] | 151 | students = [], |
---|
[1096] | 152 | allowed = allowed, |
---|
[920] | 153 | ) |
---|
[3024] | 154 | |
---|
| 155 | # return to search form if form was not properly filled |
---|
| 156 | |
---|
[920] | 157 | what = ds.get('search_mode') |
---|
| 158 | state = ds.get('review_state') |
---|
| 159 | st = term = ds.get('search_string') |
---|
| 160 | err = False |
---|
[3024] | 161 | if not term and state == "all": |
---|
| 162 | psm = "You must specify a search string when searching for students in all states!" |
---|
[920] | 163 | err = True |
---|
| 164 | elif '*' in term: |
---|
| 165 | psm = "Wildcards are not supported!" |
---|
| 166 | err = True |
---|
| 167 | if err: |
---|
| 168 | return context.search_students_form(rendered = rend, |
---|
| 169 | psm = psm, |
---|
| 170 | #psm = "%s, %s" % (psm,ds), |
---|
[1065] | 171 | info = user_info, |
---|
[3024] | 172 | students = [], |
---|
[1096] | 173 | allowed = allowed, |
---|
[920] | 174 | ) |
---|
[3024] | 175 | |
---|
| 176 | # build query string part 1 |
---|
| 177 | |
---|
[920] | 178 | query = None |
---|
| 179 | if len(term) > 0: |
---|
| 180 | if what == "student_id": |
---|
[1065] | 181 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
[1033] | 182 | if hasattr(students_folder,term.strip()): |
---|
[1571] | 183 | logger.info('%s searches for student with id %s' % (member,term)) |
---|
[1033] | 184 | request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term)) |
---|
| 185 | return context.search_students_form(rendered = rend, |
---|
| 186 | psm = "No student found!", |
---|
| 187 | students = [], |
---|
[1096] | 188 | allowed = allowed, |
---|
[3023] | 189 | info = user_info, |
---|
[1033] | 190 | ) |
---|
[1034] | 191 | elif what == "department": |
---|
[3022] | 192 | query = Eq('department', term.strip()) |
---|
[1571] | 193 | logger.info('%s searches for student in department %s' % (member,term)) |
---|
[1278] | 194 | elif what == "matric_no": |
---|
[3022] | 195 | query = Eq('matric_no', term.strip()) |
---|
[1571] | 196 | logger.info('%s searches for student with matric_no %s' % (member,term)) |
---|
[1479] | 197 | elif what == "jamb_reg_no": |
---|
[1500] | 198 | st_l = "%s" % term.strip().lower() |
---|
| 199 | st_u = "%s" % term.strip().upper() |
---|
[3022] | 200 | query = In('jamb_reg_no',(st_l,st_u)) |
---|
[1571] | 201 | logger.info('%s searches for student with jamb_reg_no %s' % (member,term)) |
---|
[1479] | 202 | elif what == "name": |
---|
[2278] | 203 | if len(term) < 4: |
---|
| 204 | return context.search_students_form(rendered = rend, |
---|
[3023] | 205 | psm = "Search string is too short!", |
---|
[2278] | 206 | students = [], |
---|
[3023] | 207 | info = user_info, |
---|
[2278] | 208 | allowed = allowed, |
---|
| 209 | ) |
---|
[3022] | 210 | query = Eq('name',"%s*" % term.strip()) |
---|
[1571] | 211 | logger.info('%s searches for student with name %s' % (member,term)) |
---|
[1479] | 212 | |
---|
[3024] | 213 | |
---|
| 214 | # build query string part 2 |
---|
| 215 | |
---|
| 216 | if state != 'all': |
---|
[3022] | 217 | if query is not None: |
---|
| 218 | query = Eq('review_state',state) & query |
---|
| 219 | else: |
---|
| 220 | query = Eq('review_state',state) |
---|
[1571] | 221 | logger.info('%s searches for students in review_state %s' % (member,state)) |
---|
[2253] | 222 | |
---|
[3024] | 223 | # build query string part 3 |
---|
| 224 | |
---|
[1278] | 225 | if co_view: |
---|
[3022] | 226 | if query is not None: |
---|
| 227 | query = query & In('faculty',faculties) | In('department',departments) |
---|
| 228 | else: |
---|
| 229 | query = In('faculty',faculties) | In('department',departments) |
---|
[1557] | 230 | elif ca_view: |
---|
[2253] | 231 | courses = [cl.split('/')[0] for cl in certificate_levels] |
---|
| 232 | levels = [cl.split('/')[1] for cl in certificate_levels] |
---|
[3022] | 233 | if query is not None: |
---|
| 234 | query = query & In('course',courses) & In('level',levels) |
---|
[3024] | 235 | |
---|
| 236 | # search students_catalog |
---|
| 237 | |
---|
[3022] | 238 | student_records = aq_students(query) |
---|
[920] | 239 | students = [] |
---|
[3022] | 240 | if student_records: |
---|
| 241 | for student_record in student_records: |
---|
[971] | 242 | info = {} |
---|
[3022] | 243 | for field in students_catalog.schema() + students_catalog.indexes(): |
---|
| 244 | info[field] = getattr(student_record, field) |
---|
[920] | 245 | else: |
---|
[1011] | 246 | students.append(info) |
---|
[920] | 247 | return context.search_students_form(rendered = rend, |
---|
| 248 | psm = "", |
---|
[1065] | 249 | info = user_info, |
---|
[920] | 250 | students = students, |
---|
[1096] | 251 | allowed = allowed, |
---|
[920] | 252 | co_view = co_view, |
---|
[1582] | 253 | ca_view = ca_view |
---|
[920] | 254 | ) |
---|
| 255 | return context.search_students_form(rendered = rend, |
---|
| 256 | psm = "No student found!", |
---|
[1065] | 257 | info = user_info, |
---|
[920] | 258 | students = students, |
---|
[1096] | 259 | allowed = allowed, |
---|
[920] | 260 | ) |
---|
| 261 | |
---|
| 262 | |
---|