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

Last change on this file since 1065 was 1065, checked in by joachim, 18 years ago

improved search, modify studycourse, patchfor attribute error

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