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

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

fix searchfor beds reserved (in custom)

File size: 7.1 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': 'student_id',
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                             )
116st_queries = ('jamb_reg_no','matric_no','name')
117review_res = None
118query = None
119items = []
120res = []
121review_set = []
122search_set = []
123if len(term) > 0:
124    if what == "student_id":
125        students_folder = context.portal_url.getPortalObject().campus.students
126        if hasattr(students_folder,term.strip()):
127            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
128        return context.search_students_form(rendered = rend,
129                             psm = "No student found!",
130                             students = [],
131                             allowed = True,
132                             )
133    elif what == "department":
134        res = context.students_catalog(department=term.strip())
135        search_set = [r.id for r in res]
136    elif what in st_queries:
137        if what == "jamb_reg_no":
138            pt = ('StudentApplication',)
139            st = "%s" % term.strip().lower()
140        elif what == "matric_no":
141            pt = ('StudentClearance',)
142            st = "%s" % term.strip().lower()
143        elif what == "name":
144            pt = ('StudentPersonal')
145            st = "%s" % term.strip()
146        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
147        res = evalAdvancedQuery(query)
148        if res:
149            for r in res:
150                pl = r.getPath().split('/')
151                search_set.append(pl[pl.index('students') + 1])
152elif co_view:
153    #from Products.zdb import set_trace
154    #set_trace()
155    only_review = False
156    query = In('faculty',faculties) | In('department',departments)
157    res = aq_students(query)
158    search_set = [r.id for r in res]
159elif only_review or with_review:
160    query = Eq('review_state',state)
161    review_res = evalAdvancedQuery(query)
162review_set = []
163if review_res:
164    for r in review_res:
165        if r.portal_type == "StudentAccommodation":
166            start = r.getPath().find('/students/') + 10
167            review_set.append(r.getPath()[start:start+7])
168        else:
169            review_set.append(r.getId)
170#set_trace()
171if only_review:
172    all = review_set
173elif with_review:
174    all = []
175    for i in search_set:
176        if i in review_set:
177            all.append(i)
178else:
179    all = search_set
180items = all[:500]
181students = []
182if items:
183    for item in items:
184        stcat = context.students_catalog
185        record = stcat(id = item)[0]
186        info = {}
187        for field in stcat.schema() + stcat.indexes():
188            info[field] = getattr(record, field)
189##        if "ClearanceOfficers" in member.getGroups():
190##            co_view = True
191##            res = context.portal_catalog(portal_type='Student', id = item)
192##            if len(res) != 1:
193##                continue
194##            droles = member.getRolesInContext(res[0].getObject())
195##            info['review_state'] = res[0].review_state
196##            if "ClearanceOfficer" in droles:
197##                students.append(info)
198        else:
199            students.append(info)
200
201    return context.search_students_form(rendered = rend,
202                             psm = "",
203                             info = user_info,
204                             students = students,
205                             allowed = True,
206                             co_view = co_view,
207                             )
208return context.search_students_form(rendered = rend,
209                             psm = "No student found!",
210                             info = user_info,
211                             students = students,
212                             allowed = True,
213                             )
214
215
Note: See TracBrowser for help on using the repository browser.