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

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

redirect to illegal_view.py

File size: 7.2 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
15form = request.form
16fget = form.get
17#wftool = context.portal_workflow
18mtool = context.portal_membership
19member = mtool.getAuthenticatedMember()
20is_anon = mtool.isAnonymousUser()
21#roles = member.getRolesInContext(context)
22lt = context.portal_layouts
23#pr = context.portal_registration
24path_info = request.get('PATH_INFO').split('/')
25
26allowed = True
27if is_anon:
28    allowed = False
29
30try:
31    from Products.AdvancedQuery import Eq, Between, Le,In
32    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
33    aq_students = context.students_catalog.evalAdvancedQuery
34except:
35    evalAdvancedQuery = None
36
37def cmp_id(a,b):
38    if a.getId() > b.getId():
39        return 1
40    return -1
41
42student_subobjects = ("StudentApplication",
43                      "StudentPersonal",
44                      "StudentStudyCourse",
45                      "StudentAccommodation",
46                      "StudentStudyLevel",)
47
48#student_objects = student_subobjects + ("Student",)
49student_objects = ("Student",)
50
51user_info = {}
52items = []
53validate = request.has_key("cpsdocument_edit_button")
54
55state = "all"
56user_info['member'] = str(member)
57user_info['departments'] = []
58user_info['faculties'] = []
59#from Products.zdb import set_trace
60#set_trace()
61co_view = False
62if "ClearanceOfficers" in member.getGroups():
63    state = "clearance_requested"
64    only_review = False
65    co_view = True
66    query = In('portal_type',('Faculty',)) &\
67             In('localUsersWithRoles', ("user:%s" % member,))
68    res = evalAdvancedQuery(query)
69    faculties = []
70    if res:
71        faculties = [f.getId for f in res]
72    user_info['faculties'] = faculties
73    query = In('portal_type',('Department',)) &\
74             In('localUsersWithRoles', ("user:%s" % member,))
75    res = evalAdvancedQuery(query)
76    departments = []
77    if res:
78        departments = [f.getId for f in res]
79    user_info['departments'] = departments
80default = {'search_mode': 'student_id',
81        'review_state': state,
82        'search_string': ''
83        }
84rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
85                      schema_id= 'student_search',
86                      context=context,
87                      mapping=validate and request,
88                      ob=default,
89                      layout_mode='edit',
90                      formaction="search_students",
91                      commit = False,
92                      )
93if psm == '':
94    return context.search_students_form(rendered = rend,
95                             psm = psm,
96                             #psm = "%s, %s" % (psm,ds),
97                             info = user_info,
98                             students = [],
99                             allowed = allowed,
100                             )
101what = ds.get('search_mode')
102state = ds.get('review_state')
103st = term = ds.get('search_string')
104err = False
105with_review = state != "all"
106only_review = with_review and not term
107bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
108if not term and not with_review:
109    psm = "You must specify a search string when searching 'all states'!"
110    err = True
111elif '*' in term:
112    psm = "Wildcards are not supported!"
113    err = True
114if err:
115    return context.search_students_form(rendered = rend,
116                             psm = psm,
117                             #psm = "%s, %s" % (psm,ds),
118                             info = user_info,
119                             students = items,
120                             allowed = allowed,
121                             )
122st_queries = ('jamb_reg_no','matric_no','name')
123review_res = None
124query = None
125items = []
126res = []
127review_set = []
128search_set = []
129if len(term) > 0:
130    if what == "student_id":
131        students_folder = context.portal_url.getPortalObject().campus.students
132        if hasattr(students_folder,term.strip()):
133            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
134        return context.search_students_form(rendered = rend,
135                             psm = "No student found!",
136                             students = [],
137                             allowed = allowed,
138                             )
139    elif what == "department":
140        res = context.students_catalog(department=term.strip())
141        search_set = [r.id for r in res]
142    elif what in st_queries:
143        if what == "jamb_reg_no":
144            pt = ('StudentApplication',)
145            st = "%s" % term.strip().lower()
146        elif what == "matric_no":
147            pt = ('StudentClearance',)
148            st = "%s" % term.strip().lower()
149        elif what == "name":
150            pt = ('StudentPersonal')
151            st = "%s" % term.strip()
152        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
153        res = evalAdvancedQuery(query)
154        if res:
155            for r in res:
156                pl = r.getPath().split('/')
157                search_set.append(pl[pl.index('students') + 1])
158elif co_view:
159    #from Products.zdb import set_trace
160    #set_trace()
161    only_review = False
162    query = In('faculty',faculties) | In('department',departments)
163    res = aq_students(query)
164    search_set = [r.id for r in res]
165if only_review or with_review:
166    query = Eq('review_state',state)
167    review_res = evalAdvancedQuery(query)
168review_set = []
169if review_res:
170    for r in review_res:
171        if r.portal_type == "StudentAccommodation":
172            start = r.getPath().find('/students/') + 10
173            review_set.append(r.getPath()[start:start+7])
174        else:
175            review_set.append(r.getId)
176#set_trace()
177if only_review:
178    all = review_set
179elif with_review:
180    all = []
181    for i in search_set:
182        if i in review_set:
183            all.append(i)
184else:
185    all = search_set
186items = all[:500]
187students = []
188if items:
189    for item in items:
190        stcat = context.students_catalog
191        record = stcat(id = item)[0]
192        info = {}
193        for field in stcat.schema() + stcat.indexes():
194            info[field] = getattr(record, field)
195##        if "ClearanceOfficers" in member.getGroups():
196##            co_view = True
197##            res = context.portal_catalog(portal_type='Student', id = item)
198##            if len(res) != 1:
199##                continue
200##            droles = member.getRolesInContext(res[0].getObject())
201##            info['review_state'] = res[0].review_state
202##            if "ClearanceOfficer" in droles:
203##                students.append(info)
204        else:
205            students.append(info)
206
207    return context.search_students_form(rendered = rend,
208                             psm = "",
209                             info = user_info,
210                             students = students,
211                             allowed = allowed,
212                             co_view = co_view,
213                             )
214return context.search_students_form(rendered = rend,
215                             psm = "No student found!",
216                             info = user_info,
217                             students = students,
218                             allowed = allowed,
219                             )
220
221
Note: See TracBrowser for help on using the repository browser.