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

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

searches also for jamb_reg_no in students_catalog

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