[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 |
---|
[920] | 17 | wftool = context.portal_workflow |
---|
| 18 | mtool = context.portal_membership |
---|
| 19 | member = mtool.getAuthenticatedMember() |
---|
| 20 | roles = member.getRolesInContext(context) |
---|
[1033] | 21 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
[920] | 22 | try: |
---|
| 23 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 24 | evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery |
---|
| 25 | except: |
---|
| 26 | evalAdvancedQuery = None |
---|
| 27 | |
---|
| 28 | def cmp_id(a,b): |
---|
| 29 | if a.getId() > b.getId(): |
---|
| 30 | return 1 |
---|
| 31 | return -1 |
---|
| 32 | |
---|
| 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") |
---|
[1006] | 51 | |
---|
| 52 | state = "all" |
---|
| 53 | if "ClearanceOfficers" in member.getGroups(): |
---|
| 54 | state = "clearance_requested" |
---|
[920] | 55 | default = {'search_mode': 'name', |
---|
[1006] | 56 | 'review_state': state, |
---|
[920] | 57 | 'search_string': '' |
---|
| 58 | } |
---|
| 59 | rend,psm,ds = lt.renderLayout(layout_id= 'student_search', |
---|
| 60 | schema_id= 'student_search', |
---|
| 61 | context=context, |
---|
| 62 | mapping=validate and request, |
---|
| 63 | ob=default, |
---|
| 64 | layout_mode='edit', |
---|
[971] | 65 | formaction="search_students", |
---|
| 66 | commit = False, |
---|
[920] | 67 | ) |
---|
| 68 | if psm == '': |
---|
| 69 | return context.search_students_form(rendered = rend, |
---|
| 70 | psm = psm, |
---|
| 71 | #psm = "%s, %s" % (psm,ds), |
---|
[971] | 72 | students = [], |
---|
[920] | 73 | allowed = True, |
---|
| 74 | ) |
---|
| 75 | what = ds.get('search_mode') |
---|
| 76 | state = ds.get('review_state') |
---|
| 77 | st = term = ds.get('search_string') |
---|
| 78 | err = False |
---|
| 79 | with_review = state != "all" |
---|
| 80 | only_review = with_review and not term |
---|
| 81 | bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review) |
---|
| 82 | if not term and not with_review: |
---|
| 83 | psm = "You must specify a search string when searching 'all states'!" |
---|
| 84 | err = True |
---|
| 85 | elif '*' in term: |
---|
| 86 | psm = "Wildcards are not supported!" |
---|
| 87 | err = True |
---|
| 88 | if err: |
---|
| 89 | return context.search_students_form(rendered = rend, |
---|
| 90 | psm = psm, |
---|
| 91 | #psm = "%s, %s" % (psm,ds), |
---|
| 92 | students = items, |
---|
| 93 | allowed = True, |
---|
| 94 | ) |
---|
| 95 | items = [] |
---|
| 96 | res = [] |
---|
| 97 | portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']} |
---|
| 98 | st_queries = ('jamb_reg_no','matric_no','name') |
---|
| 99 | query_step = 0 |
---|
| 100 | review_res = None |
---|
| 101 | query = None |
---|
[1034] | 102 | review_set = [] |
---|
[1043] | 103 | search_set = [] |
---|
[920] | 104 | if len(term) > 0: |
---|
| 105 | if what == "student_id": |
---|
| 106 | query_step = 1 |
---|
[1033] | 107 | if hasattr(students_folder,term.strip()): |
---|
| 108 | request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term)) |
---|
| 109 | return context.search_students_form(rendered = rend, |
---|
| 110 | psm = "No student found!", |
---|
| 111 | students = [], |
---|
| 112 | allowed = True, |
---|
| 113 | ) |
---|
[1034] | 114 | elif what == "department": |
---|
| 115 | res = context.students_catalog(department=term.strip()) |
---|
| 116 | search_set = [r.id for r in res] |
---|
[920] | 117 | elif what in st_queries: |
---|
| 118 | if what == "jamb_reg_no": |
---|
| 119 | query_step = 2 |
---|
| 120 | pt = ('StudentApplication',) |
---|
| 121 | st = "%s" % term.strip().lower() |
---|
| 122 | elif what == "matric_no": |
---|
| 123 | query_step = 3 |
---|
| 124 | pt = ('StudentClearance',) |
---|
| 125 | st = "%s" % term.strip().lower() |
---|
| 126 | elif what == "name": |
---|
| 127 | query_step = 4 |
---|
| 128 | pt = ('StudentPersonal') |
---|
| 129 | st = "%s" % term.strip() |
---|
| 130 | query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip()) |
---|
[1033] | 131 | res = evalAdvancedQuery(query) |
---|
| 132 | search_set = [] |
---|
| 133 | if res: |
---|
| 134 | for r in res: |
---|
| 135 | pl = r.getPath().split('/') |
---|
| 136 | search_set.append(pl[pl.index('students') + 1]) |
---|
[1034] | 137 | if with_review: |
---|
| 138 | query_step += 10 |
---|
| 139 | review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state)) |
---|
| 140 | review_set = [] |
---|
| 141 | if review_res: |
---|
| 142 | for r in review_res: |
---|
| 143 | pl = r.getPath().split('/') |
---|
| 144 | review_set.append(pl[pl.index('students') + 1]) |
---|
[920] | 145 | all = [] |
---|
| 146 | if only_review: |
---|
| 147 | all = review_set |
---|
| 148 | elif with_review: |
---|
| 149 | for i in search_set: |
---|
| 150 | if i in review_set: |
---|
| 151 | all.append(i) |
---|
| 152 | else: |
---|
| 153 | all = search_set |
---|
[1034] | 154 | for a in all[:500]: |
---|
[920] | 155 | if a in items: |
---|
| 156 | continue |
---|
| 157 | items.append(a) |
---|
| 158 | students = [] |
---|
[971] | 159 | items.sort() |
---|
[920] | 160 | co_view = False |
---|
| 161 | if items: |
---|
| 162 | for item in items: |
---|
[1006] | 163 | stcat = context.students_catalog |
---|
| 164 | record = stcat(id = item)[0] |
---|
[971] | 165 | info = {} |
---|
[1006] | 166 | for field in stcat.schema() + stcat.indexes(): |
---|
| 167 | info[field] = getattr(record, field) |
---|
[920] | 168 | if "ClearanceOfficers" in member.getGroups(): |
---|
| 169 | co_view = True |
---|
[1006] | 170 | res = context.portal_catalog(portal_type='Student', id = item) |
---|
| 171 | if len(res) != 1: |
---|
| 172 | continue |
---|
| 173 | droles = member.getRolesInContext(res[0].getObject()) |
---|
| 174 | info['review_state'] = res[0].review_state |
---|
| 175 | if "ClearanceOfficer" in droles: |
---|
| 176 | students.append(info) |
---|
[920] | 177 | else: |
---|
[1011] | 178 | students.append(info) |
---|
[920] | 179 | |
---|
| 180 | return context.search_students_form(rendered = rend, |
---|
| 181 | psm = "", |
---|
| 182 | students = students, |
---|
| 183 | allowed = True, |
---|
| 184 | co_view = co_view, |
---|
| 185 | ) |
---|
| 186 | return context.search_students_form(rendered = rend, |
---|
| 187 | psm = "No student found!", |
---|
| 188 | students = students, |
---|
| 189 | allowed = True, |
---|
| 190 | ) |
---|
| 191 | |
---|
| 192 | |
---|