source: WAeUP_SRP/trunk/skins/waeup_student/search_students.py @ 932

Last change on this file since 932 was 920, checked in by Henrik Bettermann, 18 years ago

search students methods renamed in accordance with search members

File size: 6.3 KB
Line 
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"""
12list Students for ClearanceOfficers
13"""
14request = REQUEST
15wftool = context.portal_workflow
16mtool = context.portal_membership
17member = mtool.getAuthenticatedMember()
18roles = member.getRolesInContext(context)
19try:
20    from Products.AdvancedQuery import Eq, Between, Le,In
21    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
22except:
23    evalAdvancedQuery = None
24
25def cmp_id(a,b):
26    if a.getId() > b.getId():
27        return 1
28    return -1
29
30request = context.REQUEST
31form = request.form
32fget = form.get
33student_subobjects = ("StudentApplication",
34                      "StudentPersonal",
35                      "StudentStudyCourse",
36                      "StudentAccommodation",
37                      "StudentStudyLevel",)
38
39student_objects = student_subobjects + ("Student",)
40
41info = {}
42items = []
43wf = context.portal_workflow
44catalog = context.portal_catalog
45#student_wf_states = wf.waeup_student_wf.states.keys()
46#info['wf_states'] = student_wf_states
47#info['wf_states'][0] = " ----- "
48lt = context.portal_layouts
49pr = context.portal_registration
50path_info = request.get('PATH_INFO').split('/')
51validate = request.has_key("cpsdocument_edit_button")
52default = {'search_mode': 'name',
53        'review_state': 'clearance_requested',
54        'search_string': ''
55        }
56
57rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
58                      schema_id= 'student_search',
59                      context=context,
60                      mapping=validate and request,
61                      ob=default,
62                      layout_mode='edit',
63                      formaction="search_students"
64                      )
65if psm == '':
66    return context.search_students_form(rendered = rend,
67                             psm = psm,
68                             #psm = "%s, %s" % (psm,ds),
69                             students = items,
70                             allowed = True,
71                             )
72what = ds.get('search_mode')
73state = ds.get('review_state')
74st = term = ds.get('search_string')
75err = False
76with_review = state != "all"
77only_review = with_review and not term
78##with_level_results = state.startswith("category") or\
79##                     state in ('content_addable',) and\
80##                     evalAdvancedQuery is not None
81bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
82if not term and not with_review:
83    psm = "You must specify a search string when searching 'all states'!"
84    err = True
85elif '*' in term:
86    psm = "Wildcards are not supported!"
87    err = True
88if 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                             )
95items = []
96res = []
97portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']}
98st_queries = ('jamb_reg_no','matric_no','name')
99query_step = 0
100review_res = None
101query = None
102if len(term) > 0:
103    if what == "student_id":
104        query_step = 1
105        query = Eq('portal_type','Student') & Eq('id', term.strip())
106    elif what in st_queries:
107        if what == "jamb_reg_no":
108            query_step = 2
109            pt = ('StudentApplication',)
110            st = "%s" % term.strip().lower()
111        elif what == "matric_no":
112            query_step = 3
113            pt = ('StudentClearance',)
114            st = "%s" % term.strip().lower()
115        elif what == "name":
116            query_step = 4
117            pt = ('StudentPersonal')
118            st = "%s" % term.strip()
119        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
120    res = evalAdvancedQuery(query)
121if with_review:
122    query_step += 10
123    review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state))
124search_set = []
125if res:
126    for r in res:
127        if r.portal_type in ("StudentStudyLevel",):
128            student = r.getObject().aq_parent.aq_parent
129        elif r.portal_type in student_subobjects:
130            student = r.getObject().aq_parent
131        else:
132            student = r.getObject()
133        search_set.append(student)
134review_set = []
135if review_res:
136    for r in review_res:
137        if r.portal_type in ("StudentStudyLevel",):
138            student = r.getObject().aq_parent.aq_parent
139        elif r.portal_type in student_subobjects:
140            student = r.getObject().aq_parent
141        else:
142            student = r.getObject()
143        review_set.append(student)
144all = []
145if only_review:
146    all = review_set
147elif with_review:
148    for i in search_set:
149        if i in review_set:
150            all.append(i)
151else:
152    all = search_set
153for a in all:
154    if a in items:
155        continue
156    items.append(a)
157students = []
158items.sort(cmp_id)
159co_view = False
160if items:
161    for item in items:
162        #if context.isClearanceOfficer(info):
163        if "ClearanceOfficers" in member.getGroups():
164            co_view = True
165            droles = member.getRolesInContext(item)
166            if "ClearanceOfficer" in droles:
167                info = context.getStudentInfo(item)
168                students.append(info)
169        else:
170            info = context.getStudentInfo(item)
171            students.append(info)
172
173    return context.search_students_form(rendered = rend,
174                             psm = "",
175                             #psm = "%d,%d matching Students found QS = %s" %\
176                             #       (len(review_set),len(search_set),query_step),
177                             #psm = "%d found QS = %s items: %s" % (len(items),query_step,items),
178                             students = students,
179                             allowed = True,
180                             co_view = co_view,
181                             )
182return context.search_students_form(rendered = rend,
183                             psm = "No student found!",
184                             #psm = """Step: %s found: %s Your search for "%s" in %s with state %s failed.<br\>%s""" % (query_step,len(items),st,what,state,bools),
185                             students = students,
186                             allowed = True,
187                             )
188
189
Note: See TracBrowser for help on using the repository browser.