1 | #-*- mode: python; mode: fold -*- |
---|
2 | # (C) Copyright 2005 AixtraWare <http://aixtraware.de> |
---|
3 | # Author: Joachim Schmitz <js@aixtraware.de> |
---|
4 | # |
---|
5 | # This program is free software; you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU General Public License version 2 as published |
---|
7 | # by the Free Software Foundation. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program; if not, write to the Free Software |
---|
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
17 | # 02111-1307, USA. |
---|
18 | # |
---|
19 | # $Id: WAeUPTables.py 2069 2007-07-27 16:04:30Z joachim $ |
---|
20 | |
---|
21 | from zope.interface import implements |
---|
22 | from Globals import InitializeClass |
---|
23 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
24 | from Products.ZCatalog.ProgressHandler import ZLogHandler |
---|
25 | from AccessControl import ClassSecurityInfo |
---|
26 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
27 | import urllib |
---|
28 | import DateTime,time |
---|
29 | import csv,re |
---|
30 | import logging |
---|
31 | import Globals |
---|
32 | p_home = Globals.package_home(globals()) |
---|
33 | i_home = Globals.INSTANCE_HOME |
---|
34 | from Products.CMFCore.CatalogTool import CatalogTool |
---|
35 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
36 | |
---|
37 | from interfaces import IWAeUPTable |
---|
38 | |
---|
39 | class AttributeHolder(object): |
---|
40 | pass |
---|
41 | |
---|
42 | def dict2ob(dict): |
---|
43 | ob = AttributeHolder() |
---|
44 | for key, value in dict.items(): |
---|
45 | setattr(ob, key, value) |
---|
46 | return ob |
---|
47 | |
---|
48 | class WAeUPTable(ZCatalog): ###( |
---|
49 | |
---|
50 | implements(IWAeUPTable) |
---|
51 | security = ClassSecurityInfo() |
---|
52 | |
---|
53 | def refreshCatalog(self, clear=0, pghandler=None): |
---|
54 | """ don't refresh for a normal table """ |
---|
55 | |
---|
56 | if self.REQUEST and self.REQUEST.RESPONSE: |
---|
57 | self.REQUEST.RESPONSE.redirect( |
---|
58 | URL1 + |
---|
59 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20refresh%20not%20implemented') |
---|
60 | |
---|
61 | def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None): |
---|
62 | """ clears the whole enchilada """ |
---|
63 | |
---|
64 | #if REQUEST and RESPONSE: |
---|
65 | # RESPONSE.redirect( |
---|
66 | # URL1 + |
---|
67 | # '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Clearing%20disabled') |
---|
68 | |
---|
69 | |
---|
70 | self._catalog.clear() |
---|
71 | if REQUEST and RESPONSE: |
---|
72 | RESPONSE.redirect( |
---|
73 | URL1 + |
---|
74 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20cleared') |
---|
75 | |
---|
76 | def addRecord(self, **data): |
---|
77 | # The uid is the same as "bed". |
---|
78 | uid = data[self.key] |
---|
79 | res = self.searchResults({"%s" % self.key : uid}) |
---|
80 | if len(res) > 0: |
---|
81 | raise ValueError("More than one record with uid %s" % uid) |
---|
82 | self.catalog_object(dict2ob(data), uid=uid) |
---|
83 | return uid |
---|
84 | |
---|
85 | def deleteRecord(self, uid): |
---|
86 | self.uncatalog_object(uid) |
---|
87 | |
---|
88 | def searchAndSetRecord(self, **data): |
---|
89 | raise NotImplemented |
---|
90 | |
---|
91 | def modifyRecord(self, record=None, **data): |
---|
92 | #records = self.searchResults(uid=uid) |
---|
93 | uid = data[self.key] |
---|
94 | if record is None: |
---|
95 | records = self.searchResults({"%s" % self.key : uid}) |
---|
96 | if len(records) > 1: |
---|
97 | # Can not happen, but anyway... |
---|
98 | raise ValueError("More than one record with uid %s" % uid) |
---|
99 | if len(records) == 0: |
---|
100 | raise KeyError("No record for uid %s" % uid) |
---|
101 | record = records[0] |
---|
102 | record_data = {} |
---|
103 | for field in self.schema() + self.indexes(): |
---|
104 | record_data[field] = getattr(record, field) |
---|
105 | # Add the updated data: |
---|
106 | record_data.update(data) |
---|
107 | self.catalog_object(dict2ob(record_data), uid) |
---|
108 | |
---|
109 | def reindexIndex(self, name, REQUEST,pghandler=None): |
---|
110 | if isinstance(name, str): |
---|
111 | name = (name,) |
---|
112 | paths = self._catalog.uids.items() |
---|
113 | i = 0 |
---|
114 | #import pdb;pdb.set_trace() |
---|
115 | for p,rid in paths: |
---|
116 | i += 1 |
---|
117 | metadata = self.getMetadataForRID(rid) |
---|
118 | record_data = {} |
---|
119 | for field in name: |
---|
120 | record_data[field] = metadata.get(field) |
---|
121 | uid = metadata.get(self.key) |
---|
122 | self.catalog_object(dict2ob(record_data), uid, idxs=name, |
---|
123 | update_metadata=0) |
---|
124 | |
---|
125 | security.declareProtected(ModifyPortalContent,"exportAllRecords") |
---|
126 | def exportAllRecords(self): |
---|
127 | "export a WAeUPTable" |
---|
128 | #import pdb;pdb.set_trace() |
---|
129 | fields = [field for field in self.schema()] |
---|
130 | format = ','.join(['"%%(%s)s"' % fn for fn in fields]) |
---|
131 | csv = [] |
---|
132 | csv.append(','.join(['"%s"' % fn for fn in fields])) |
---|
133 | for uid in self._catalog.uids: |
---|
134 | records = self.searchResults({"%s" % self.key : uid}) |
---|
135 | if len(records) > 1: |
---|
136 | # Can not happen, but anyway... |
---|
137 | raise ValueError("More than one record with uid %s" % uid) |
---|
138 | if len(records) == 0: |
---|
139 | raise KeyError("No record for uid %s" % uid) |
---|
140 | rec = records[0] |
---|
141 | csv.append(format % rec) |
---|
142 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
143 | open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) |
---|
144 | ###) |
---|
145 | |
---|
146 | class AccommodationTable(WAeUPTable): ###( |
---|
147 | |
---|
148 | meta_type = 'WAeUP Accommodation Tool' |
---|
149 | name = "accommodation" |
---|
150 | key = "bed" |
---|
151 | def __init__(self): |
---|
152 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
153 | |
---|
154 | def searchAndReserveBed(self, student_id,bed_type): |
---|
155 | records = self.searchResults({'student' : student_id}) |
---|
156 | if len(records) > 0: |
---|
157 | return -1,"Student with Id %s already booked bed %s." % (student_id,records[0].bed) |
---|
158 | |
---|
159 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
160 | #import pdb;pdb.set_trace() |
---|
161 | if len(records) == 0: |
---|
162 | return -2,"No bed available" |
---|
163 | rec = records[0] |
---|
164 | self.modifyRecord(bed=rec.bed,student=student_id) |
---|
165 | s_logger = logging.getLogger('WAeUPTables.AccommodationTable.searchAndReserveBed') |
---|
166 | s_logger.info('%s reserved bed %s' % (student_id,rec.bed)) |
---|
167 | return 1,rec.bed |
---|
168 | |
---|
169 | |
---|
170 | InitializeClass(AccommodationTable) |
---|
171 | |
---|
172 | ###) |
---|
173 | |
---|
174 | class PinTable(WAeUPTable): ###( |
---|
175 | from ZODB.POSException import ConflictError |
---|
176 | meta_type = 'WAeUP Pin Tool' |
---|
177 | name = "pins" |
---|
178 | key = 'pin' |
---|
179 | def __init__(self): |
---|
180 | WAeUPTable.__init__(self, 'portal_pins') |
---|
181 | |
---|
182 | |
---|
183 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
184 | #records = self.searchResults(uid=uid) |
---|
185 | records = self.searchResults(student = student_id) |
---|
186 | #import pdb;pdb.set_trace() |
---|
187 | if len(records) > 0 and prefix in ('CLR','APP'): |
---|
188 | for r in records: |
---|
189 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
---|
190 | return -2 |
---|
191 | records = self.searchResults({"%s" % self.key : uid}) |
---|
192 | if len(records) > 1: |
---|
193 | # Can not happen, but anyway... |
---|
194 | raise ValueError("More than one record with uid %s" % uid) |
---|
195 | if len(records) == 0: |
---|
196 | return -1 |
---|
197 | record = records[0] |
---|
198 | if record.student == "": |
---|
199 | record_data = {} |
---|
200 | for field in self.schema() + self.indexes(): |
---|
201 | record_data[field] = getattr(record, field) |
---|
202 | # Add the updated data: |
---|
203 | record_data['student'] = student_id |
---|
204 | try: |
---|
205 | self.catalog_object(dict2ob(record_data), uid) |
---|
206 | return 1 |
---|
207 | except ConflictError: |
---|
208 | return 2 |
---|
209 | if record.student.upper() != student_id.upper(): |
---|
210 | return 0 |
---|
211 | if record.student.upper() == student_id.upper(): |
---|
212 | return 2 |
---|
213 | return -3 |
---|
214 | |
---|
215 | InitializeClass(PinTable) |
---|
216 | |
---|
217 | ###) |
---|
218 | |
---|
219 | class PumeResultsTable(WAeUPTable): ###( |
---|
220 | |
---|
221 | meta_type = 'WAeUP PumeResults Tool' |
---|
222 | name = "pumeresults" |
---|
223 | key = "jamb_reg_no" |
---|
224 | def __init__(self): |
---|
225 | WAeUPTable.__init__(self, 'portal_pumeresults') |
---|
226 | |
---|
227 | |
---|
228 | InitializeClass(PumeResultsTable) |
---|
229 | |
---|
230 | ###) |
---|
231 | |
---|
232 | class StudentsCatalog(WAeUPTable): ###( |
---|
233 | security = ClassSecurityInfo() |
---|
234 | |
---|
235 | meta_type = 'WAeUP Students Catalog' |
---|
236 | name = "students_catalog" |
---|
237 | key = "id" |
---|
238 | affected_types = { ###( |
---|
239 | 'StudentApplication': |
---|
240 | {'id': 'application', |
---|
241 | 'fields': |
---|
242 | ('jamb_reg_no', |
---|
243 | 'entry_mode', |
---|
244 | #'entry_level', |
---|
245 | 'entry_session', |
---|
246 | ) |
---|
247 | }, |
---|
248 | 'StudentClearance': |
---|
249 | {'id': 'clearance', |
---|
250 | 'fields': |
---|
251 | ('matric_no', |
---|
252 | 'lga', |
---|
253 | ) |
---|
254 | }, |
---|
255 | 'StudentPersonal': |
---|
256 | {'id': 'personal', |
---|
257 | 'fields': |
---|
258 | ('name', |
---|
259 | 'sex', |
---|
260 | 'perm_address', |
---|
261 | 'email', |
---|
262 | 'phone', |
---|
263 | ) |
---|
264 | }, |
---|
265 | 'StudentStudyCourse': |
---|
266 | {'id': 'study_course', |
---|
267 | 'fields': |
---|
268 | ('course', # study_course |
---|
269 | 'faculty', # from certificate |
---|
270 | 'department', # from certificate |
---|
271 | 'end_level', # from certificate |
---|
272 | 'level', # current_level |
---|
273 | 'mode', # current_mode |
---|
274 | 'session', # current_session |
---|
275 | 'verdict', # current_verdict |
---|
276 | ) |
---|
277 | }, |
---|
278 | } |
---|
279 | ###) |
---|
280 | |
---|
281 | def __init__(self): |
---|
282 | WAeUPTable.__init__(self, 'students_catalog') |
---|
283 | return |
---|
284 | |
---|
285 | def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None): |
---|
286 | """ clears the whole enchilada """ |
---|
287 | self._catalog.clear() |
---|
288 | |
---|
289 | if REQUEST and RESPONSE: |
---|
290 | RESPONSE.redirect( |
---|
291 | URL1 + |
---|
292 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Cleared') |
---|
293 | |
---|
294 | def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###( |
---|
295 | """ clear the catalog, then re-index everything """ |
---|
296 | |
---|
297 | elapse = time.time() |
---|
298 | c_elapse = time.clock() |
---|
299 | |
---|
300 | pgthreshold = self._getProgressThreshold() |
---|
301 | handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None |
---|
302 | self.refreshCatalog(clear=1, pghandler=handler) |
---|
303 | |
---|
304 | elapse = time.time() - elapse |
---|
305 | c_elapse = time.clock() - c_elapse |
---|
306 | |
---|
307 | RESPONSE.redirect( |
---|
308 | URL1 + |
---|
309 | '/manage_catalogAdvanced?manage_tabs_message=' + |
---|
310 | urllib.quote('Catalog Updated \n' |
---|
311 | 'Total time: %s\n' |
---|
312 | 'Total CPU time: %s' % (`elapse`, `c_elapse`))) |
---|
313 | ###) |
---|
314 | |
---|
315 | def get_from_doc_department(self,doc): ###( |
---|
316 | "return the students department" |
---|
317 | if doc is None: |
---|
318 | return None |
---|
319 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
320 | if len(certificate_res) != 1: |
---|
321 | return None |
---|
322 | return certificate_res[0].getPath().split('/')[-3] |
---|
323 | |
---|
324 | def get_from_doc_faculty(self,doc): |
---|
325 | "return the students faculty" |
---|
326 | if doc is None: |
---|
327 | return None |
---|
328 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
329 | if len(certificate_res) != 1: |
---|
330 | return None |
---|
331 | return certificate_res[0].getPath().split('/')[-4] |
---|
332 | |
---|
333 | def get_from_doc_end_level(self,doc): ###( |
---|
334 | "return the students end_level" |
---|
335 | if doc is None: |
---|
336 | return None |
---|
337 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
338 | if len(certificate_res) != 1: |
---|
339 | return None |
---|
340 | return getattr(certificate_res[0].getObject().getContent(),'end_level','unknown') |
---|
341 | |
---|
342 | def get_from_doc_level(self,doc): |
---|
343 | "return the students level" |
---|
344 | if doc is None: |
---|
345 | return None |
---|
346 | return getattr(doc,'current_level',None) |
---|
347 | |
---|
348 | def get_from_doc_mode(self,doc): |
---|
349 | "return the students mode" |
---|
350 | if doc is None: |
---|
351 | return None |
---|
352 | cm = getattr(doc,'current_mode',None) |
---|
353 | return cm |
---|
354 | |
---|
355 | |
---|
356 | def get_from_doc_session(self,doc): |
---|
357 | "return the students current_session" |
---|
358 | if doc is None: |
---|
359 | return None |
---|
360 | return getattr(doc,'current_session',None) |
---|
361 | |
---|
362 | def get_from_doc_entry_session(self,doc): |
---|
363 | "return the students entry_session" |
---|
364 | if doc is None: |
---|
365 | return None |
---|
366 | es = getattr(doc,'entry_session',None) |
---|
367 | if es is not None and len(es) == 2: |
---|
368 | return es |
---|
369 | try: |
---|
370 | digit = int(doc.jamb_reg_no[0]) |
---|
371 | except: |
---|
372 | return "-1" |
---|
373 | if digit < 8: |
---|
374 | return "0%c" % doc.jamb_reg_no[0] |
---|
375 | return "9%c" % doc.jamb_reg_no[0] |
---|
376 | |
---|
377 | def get_from_doc_course(self,doc): |
---|
378 | "return the students study_course" |
---|
379 | if doc is None: |
---|
380 | return None |
---|
381 | return getattr(doc,'study_course',None) |
---|
382 | |
---|
383 | def get_from_doc_name(self,doc): |
---|
384 | "return the students name from the personal" |
---|
385 | if doc is None: |
---|
386 | return None |
---|
387 | return "%s %s %s" % (doc.firstname,doc.middlename,doc.lastname) |
---|
388 | |
---|
389 | def get_from_doc_verdict(self,doc): |
---|
390 | "return the students study_course" |
---|
391 | if doc is None: |
---|
392 | return None |
---|
393 | return getattr(doc,'current_verdict',None) |
---|
394 | ###) |
---|
395 | |
---|
396 | def reindexIndex(self, name, REQUEST,pghandler=None): ###( |
---|
397 | if isinstance(name, str): |
---|
398 | name = (name,) |
---|
399 | reindextypes = {} |
---|
400 | reindex_special = [] |
---|
401 | for n in name: |
---|
402 | if n in ("review_state","registered_courses"): |
---|
403 | reindex_special.append(n) |
---|
404 | else: |
---|
405 | for pt in self.affected_types.keys(): |
---|
406 | if n in self.affected_types[pt]['fields']: |
---|
407 | if reindextypes.has_key(pt): |
---|
408 | reindextypes[pt].append(n) |
---|
409 | else: |
---|
410 | reindextypes[pt]= [n] |
---|
411 | break |
---|
412 | students = self.portal_catalog(portal_type="Student") |
---|
413 | if hasattr(self,'portal_catalog_real'): |
---|
414 | aq_portal = self.portal_catalog_real.evalAdvancedQuery |
---|
415 | else: |
---|
416 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
417 | num_objects = len(students) |
---|
418 | if pghandler: |
---|
419 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
420 | noattr = set(('StudentClearance','StudentPersonal')) & set(reindextypes.keys()) |
---|
421 | for i in xrange(num_objects): |
---|
422 | if pghandler: pghandler.report(i) |
---|
423 | student_brain = students[i] |
---|
424 | student_object = student_brain.getObject() |
---|
425 | data = {} |
---|
426 | modified = False |
---|
427 | sid = data['id'] = student_brain.getId |
---|
428 | if reindex_special and 'review_state' in reindex_special: |
---|
429 | modified = True |
---|
430 | data['review_state'] = student_brain.review_state |
---|
431 | sub_objects = False |
---|
432 | for pt in reindextypes.keys(): |
---|
433 | modified = True |
---|
434 | try: |
---|
435 | doc = getattr(student_object,self.affected_types[pt]['id']).getContent() |
---|
436 | sub_objects = True |
---|
437 | except: |
---|
438 | continue |
---|
439 | for field in self.affected_types[pt]['fields']: |
---|
440 | if hasattr(self,'get_from_doc_%s' % field): |
---|
441 | data[field] = getattr(self,'get_from_doc_%s' % field)(doc) |
---|
442 | else: |
---|
443 | data[field] = getattr(doc,field) |
---|
444 | if not sub_objects and noattr: |
---|
445 | import_res = self.returning_import(id = sid) |
---|
446 | if not import_res: |
---|
447 | continue |
---|
448 | import_record = import_res[0] |
---|
449 | data['matric_no'] = import_record.matric_no |
---|
450 | data['sex'] = import_record.Sex == 'F' |
---|
451 | data['name'] = "%s %s %s" % (import_record.Firstname, |
---|
452 | import_record.Middlename, |
---|
453 | import_record.Lastname) |
---|
454 | data['jamb_reg_no'] = import_record.Entryregno |
---|
455 | if reindex_special and 'registered_courses' in reindex_special: |
---|
456 | try: |
---|
457 | study_course = getattr(student_object,"study_course") |
---|
458 | level_ids = study_course.objectIds() |
---|
459 | except: |
---|
460 | continue |
---|
461 | if not level_ids: |
---|
462 | continue |
---|
463 | modified = True |
---|
464 | level_ids.sort() |
---|
465 | course_ids = getattr(study_course,level_ids[-1]).objectIds() |
---|
466 | courses = [] |
---|
467 | for c in course_ids: |
---|
468 | if c.endswith('_co'): |
---|
469 | courses.append(c[:-3]) |
---|
470 | else: |
---|
471 | courses.append(c) |
---|
472 | data['registered_courses'] = courses |
---|
473 | if modified: |
---|
474 | self.modifyRecord(**data) |
---|
475 | if pghandler: pghandler.finish() |
---|
476 | ###) |
---|
477 | |
---|
478 | def refreshCatalog(self, clear=0, pghandler=None): ###( |
---|
479 | """ re-index everything we can find """ |
---|
480 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
481 | if clear: |
---|
482 | self._catalog.clear() |
---|
483 | students = self.portal_catalog(portal_type="Student") |
---|
484 | num_objects = len(students) |
---|
485 | if pghandler: |
---|
486 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
487 | for i in xrange(num_objects): |
---|
488 | if pghandler: pghandler.report(i) |
---|
489 | student_brain = students[i] |
---|
490 | spath = student_brain.getPath() |
---|
491 | student_object = student_brain.getObject() |
---|
492 | data = {} |
---|
493 | sid = data['id'] = student_brain.getId |
---|
494 | data['review_state'] = student_brain.review_state |
---|
495 | sub_objects = False |
---|
496 | for pt in self.affected_types.keys(): |
---|
497 | modified = True |
---|
498 | try: |
---|
499 | doc = getattr(student_object,self.affected_types[pt]['id']).getContent() |
---|
500 | sub_objects = True |
---|
501 | except: |
---|
502 | #from pdb import set_trace;set_trace() |
---|
503 | continue |
---|
504 | for field in self.affected_types[pt]['fields']: |
---|
505 | if hasattr(self,'get_from_doc_%s' % field): |
---|
506 | data[field] = getattr(self,'get_from_doc_%s' % field)(doc) |
---|
507 | else: |
---|
508 | data[field] = getattr(doc,field,None) |
---|
509 | if not sub_objects: |
---|
510 | import_res = self.returning_import(id = sid) |
---|
511 | if not import_res: |
---|
512 | continue |
---|
513 | import_record = import_res[0] |
---|
514 | data['matric_no'] = import_record.matric_no |
---|
515 | data['sex'] = import_record.Sex == 'F' |
---|
516 | data['name'] = "%s %s %s" % (import_record.Firstname, |
---|
517 | import_record.Middlename, |
---|
518 | import_record.Lastname) |
---|
519 | data['jamb_reg_no'] = import_record.Entryregno |
---|
520 | else: |
---|
521 | study_course = getattr(student_object,'study_course',None) |
---|
522 | current_level = data.get('level',None) |
---|
523 | data['registered_courses'] = [] |
---|
524 | if study_course and current_level and current_level in study_course.objectIds(): |
---|
525 | level_obj = getattr(study_course,current_level) |
---|
526 | courses = [] |
---|
527 | for c in level_obj.objectIds(): |
---|
528 | if c.endswith('_co'): |
---|
529 | courses.append(c[:-3]) |
---|
530 | else: |
---|
531 | courses.append(c) |
---|
532 | data['registered_courses'] = courses |
---|
533 | self.addRecord(**data) |
---|
534 | if pghandler: pghandler.finish() |
---|
535 | ###) |
---|
536 | |
---|
537 | security.declarePrivate('notify_event_listener') ###( |
---|
538 | def notify_event_listener(self,event_type,object,infos): |
---|
539 | "listen for events" |
---|
540 | if not infos.has_key('rpath'): |
---|
541 | return |
---|
542 | pt = getattr(object,'portal_type',None) |
---|
543 | mt = getattr(object,'meta_type',None) |
---|
544 | students_catalog = self |
---|
545 | data = {} |
---|
546 | if pt == 'Student' and\ |
---|
547 | mt == 'CPS Proxy Folder' and\ |
---|
548 | event_type.startswith('workflow'): |
---|
549 | data['id'] = object.getId() |
---|
550 | data['review_state'] = self.portal_workflow.getInfoFor(object,'review_state',None) |
---|
551 | students_catalog.modifyRecord(**data) |
---|
552 | return |
---|
553 | rpl = infos['rpath'].split('/') |
---|
554 | if pt == 'Student' and mt == 'CPS Proxy Folder'\ |
---|
555 | and event_type == "sys_add_object": |
---|
556 | student_id = object.id |
---|
557 | try: |
---|
558 | self.addRecord(id = student_id) |
---|
559 | except ValueError: |
---|
560 | pass |
---|
561 | return |
---|
562 | elif pt == 'StudentCourseResult' and mt == 'CPS Proxy Folder': |
---|
563 | if event_type not in ("sys_add_object","sys_del_object"): |
---|
564 | return |
---|
565 | level_session = getattr(object.aq_parent.getContent(),'session','unknown') |
---|
566 | if level_session not in (self.getSessionId()[-2:],'2006/2007'): |
---|
567 | return |
---|
568 | course_id = object.getId() |
---|
569 | if course_id.endswith('_co'): |
---|
570 | course_id = course_id[:-3] |
---|
571 | student_id = object.absolute_url_path().split('/')[-4] |
---|
572 | res = students_catalog(id = student_id) |
---|
573 | if not res: |
---|
574 | return |
---|
575 | student_rec = res[0] |
---|
576 | registered_courses = getattr(student_rec,'registered_courses',None) |
---|
577 | if not registered_courses: |
---|
578 | registered_courses = [] |
---|
579 | #import pdb;pdb.set_trace() |
---|
580 | if event_type == "sys_add_object": |
---|
581 | if course_id not in registered_courses: |
---|
582 | registered_courses.append(course_id) |
---|
583 | else: |
---|
584 | return |
---|
585 | elif registered_courses and event_type == "sys_del_object": |
---|
586 | removed = False |
---|
587 | while course_id in registered_courses: |
---|
588 | removed = True |
---|
589 | registered_courses.remove(course_id) |
---|
590 | if not removed: |
---|
591 | return |
---|
592 | data['id'] = student_id |
---|
593 | data['registered_courses'] = registered_courses |
---|
594 | self.modifyRecord(record = student_rec, **data) |
---|
595 | return |
---|
596 | if pt not in self.affected_types.keys(): |
---|
597 | return |
---|
598 | if event_type not in ('sys_modify_object'): |
---|
599 | return |
---|
600 | if mt == 'CPS Proxy Folder': |
---|
601 | return |
---|
602 | for field in self.affected_types[pt]['fields']: |
---|
603 | if hasattr(self,'get_from_doc_%s' % field): |
---|
604 | data[field] = getattr(self,'get_from_doc_%s' % field)(object) |
---|
605 | else: |
---|
606 | data[field] = getattr(object,field) |
---|
607 | data['id'] = rpl[2] |
---|
608 | self.modifyRecord(**data) |
---|
609 | ###) |
---|
610 | |
---|
611 | |
---|
612 | InitializeClass(StudentsCatalog) |
---|
613 | |
---|
614 | ###) |
---|
615 | |
---|
616 | class StatisticsCatalog(StudentsCatalog): ###( |
---|
617 | security = ClassSecurityInfo() |
---|
618 | meta_type = 'WAeUP Statistics Catalog' |
---|
619 | name = "statistics" |
---|
620 | affected_types = { ###( |
---|
621 | 'StudentApplication': |
---|
622 | {'id': 'application', |
---|
623 | 'fields': |
---|
624 | ('jamb_reg_no', |
---|
625 | 'entry_mode', |
---|
626 | #'entry_level', |
---|
627 | 'entry_session', |
---|
628 | ) |
---|
629 | }, |
---|
630 | 'StudentClearance': |
---|
631 | {'id': 'clearance', |
---|
632 | 'fields': |
---|
633 | ('matric_no', |
---|
634 | ) |
---|
635 | }, |
---|
636 | 'StudentPersonal': |
---|
637 | {'id': 'personal', |
---|
638 | 'fields': |
---|
639 | ('name', |
---|
640 | 'sex', |
---|
641 | 'email', |
---|
642 | 'phone', |
---|
643 | ) |
---|
644 | }, |
---|
645 | 'StudentStudyCourse': |
---|
646 | {'id': 'study_course', |
---|
647 | 'fields': |
---|
648 | ('course', |
---|
649 | 'faculty', |
---|
650 | 'department', |
---|
651 | 'level', |
---|
652 | 'mode', |
---|
653 | 'session', |
---|
654 | 'verdict', |
---|
655 | ) |
---|
656 | }, |
---|
657 | } |
---|
658 | ###) |
---|
659 | |
---|
660 | |
---|
661 | InitializeClass(StudentsCatalog) |
---|
662 | ###) |
---|
663 | |
---|
664 | class CoursesCatalog(WAeUPTable): ###( |
---|
665 | security = ClassSecurityInfo() |
---|
666 | |
---|
667 | meta_type = 'WAeUP Courses Catalog' |
---|
668 | name = "courses_catalog" |
---|
669 | key = "code" |
---|
670 | def __init__(self): |
---|
671 | WAeUPTable.__init__(self, 'courses_catalog') |
---|
672 | |
---|
673 | def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###( |
---|
674 | """ clear the catalog, then re-index everything """ |
---|
675 | |
---|
676 | elapse = time.time() |
---|
677 | c_elapse = time.clock() |
---|
678 | |
---|
679 | pgthreshold = self._getProgressThreshold() |
---|
680 | handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None |
---|
681 | self.refreshCatalog(clear=1, pghandler=handler) |
---|
682 | |
---|
683 | elapse = time.time() - elapse |
---|
684 | c_elapse = time.clock() - c_elapse |
---|
685 | |
---|
686 | RESPONSE.redirect( |
---|
687 | URL1 + |
---|
688 | '/manage_catalogAdvanced?manage_tabs_message=' + |
---|
689 | urllib.quote('Catalog Updated \n' |
---|
690 | 'Total time: %s\n' |
---|
691 | 'Total CPU time: %s' % (`elapse`, `c_elapse`))) |
---|
692 | ###) |
---|
693 | |
---|
694 | def reindexIndex(self, name, REQUEST,pghandler=None): ###( |
---|
695 | if isinstance(name, str): |
---|
696 | name = (name,) |
---|
697 | courses = self.portal_catalog(portal_type="Course") |
---|
698 | num_objects = len(courses) |
---|
699 | if pghandler: |
---|
700 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
701 | for i in xrange(num_objects): |
---|
702 | if pghandler: pghandler.report(i) |
---|
703 | course_brain = courses[i] |
---|
704 | course_object = course_brain.getObject() |
---|
705 | pl = course_brain.getPath().split('/') |
---|
706 | data = {} |
---|
707 | cid = data[self.key] = course_brain.getId |
---|
708 | data['faculty'] = pl[-4] |
---|
709 | data['department'] = pl[-3] |
---|
710 | doc = course_object.getContent() |
---|
711 | for field in name: |
---|
712 | if field not in (self.key,'faculty','department'): |
---|
713 | data[field] = getattr(doc,field) |
---|
714 | self.modifyRecord(**data) |
---|
715 | if pghandler: pghandler.finish() |
---|
716 | ###) |
---|
717 | |
---|
718 | def refreshCatalog(self, clear=0, pghandler=None): ###( |
---|
719 | """ re-index everything we can find """ |
---|
720 | if clear: |
---|
721 | self._catalog.clear() |
---|
722 | courses = self.portal_catalog(portal_type="Course") |
---|
723 | num_objects = len(courses) |
---|
724 | if pghandler: |
---|
725 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
726 | #from pdb import set_trace;set_trace() |
---|
727 | for i in xrange(num_objects): |
---|
728 | if pghandler: pghandler.report(i) |
---|
729 | course_brain = courses[i] |
---|
730 | course_doc = course_brain.getObject().getContent() |
---|
731 | pl = course_brain.getPath().split('/') |
---|
732 | data = {} |
---|
733 | for field in self.schema(): |
---|
734 | data[field] = getattr(course_doc,field,None) |
---|
735 | data[self.key] = course_brain.getId |
---|
736 | ai = pl.index('academics') |
---|
737 | data['faculty'] = pl[ai +1] |
---|
738 | data['department'] = pl[ai +2] |
---|
739 | if clear: |
---|
740 | self.addRecord(**data) |
---|
741 | else: |
---|
742 | self.modifyRecord(**data) |
---|
743 | if pghandler: pghandler.finish() |
---|
744 | ###) |
---|
745 | |
---|
746 | security.declarePrivate('notify_event_listener') ###( |
---|
747 | def notify_event_listener(self,event_type,object,infos): |
---|
748 | "listen for events" |
---|
749 | if not infos.has_key('rpath'): |
---|
750 | return |
---|
751 | pt = getattr(object,'portal_type',None) |
---|
752 | mt = getattr(object,'meta_type',None) |
---|
753 | if pt != 'Course': |
---|
754 | return |
---|
755 | data = {} |
---|
756 | rpl = infos['rpath'].split('/') |
---|
757 | if event_type not in ("sys_add_object","sys_modify_object","sys_del_object"): |
---|
758 | return |
---|
759 | course_id = object.getId() |
---|
760 | data[self.key] = course_id |
---|
761 | if event_type == "sys_add_object" and mt == 'CPS Proxy Folder': |
---|
762 | try: |
---|
763 | self.addRecord(**data) |
---|
764 | except ValueError: |
---|
765 | return |
---|
766 | course_id = object.getId() |
---|
767 | doc = object.getContent() |
---|
768 | if doc is None: |
---|
769 | return |
---|
770 | for field in self.schema(): |
---|
771 | data[field] = getattr(doc,field,None) |
---|
772 | data[self.key] = course_id |
---|
773 | ai = rpl.index('academics') |
---|
774 | data['faculty'] = rpl[ai +1] |
---|
775 | data['department'] = rpl[ai +2] |
---|
776 | self.modifyRecord(**data) |
---|
777 | return |
---|
778 | if event_type == "sys_del_object": |
---|
779 | self.deleteRecord(course_id) |
---|
780 | return |
---|
781 | if event_type == "sys_modify_object" and mt == 'Course': |
---|
782 | #from pdb import set_trace;set_trace() |
---|
783 | for field in self.schema(): |
---|
784 | data[field] = getattr(object,field,None) |
---|
785 | course_id = object.aq_parent.getId() |
---|
786 | data[self.key] = course_id |
---|
787 | ai = rpl.index('academics') |
---|
788 | data['faculty'] = rpl[ai +1] |
---|
789 | data['department'] = rpl[ai +2] |
---|
790 | self.modifyRecord(**data) |
---|
791 | ###) |
---|
792 | |
---|
793 | |
---|
794 | InitializeClass(CoursesCatalog) |
---|
795 | ###) |
---|
796 | |
---|
797 | class ResultsCatalog(WAeUPTable): ###( |
---|
798 | security = ClassSecurityInfo() |
---|
799 | |
---|
800 | meta_type = 'WAeUP Results Catalog' |
---|
801 | name = "course_results" |
---|
802 | key = "key" |
---|
803 | def __init__(self): |
---|
804 | WAeUPTable.__init__(self, 'course_results') |
---|
805 | |
---|
806 | def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###( |
---|
807 | """ clear the catalog, then re-index everything """ |
---|
808 | |
---|
809 | elapse = time.time() |
---|
810 | c_elapse = time.clock() |
---|
811 | |
---|
812 | pgthreshold = self._getProgressThreshold() |
---|
813 | handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None |
---|
814 | #self.refreshCatalog(clear=1, pghandler=handler) |
---|
815 | |
---|
816 | elapse = time.time() - elapse |
---|
817 | c_elapse = time.clock() - c_elapse |
---|
818 | |
---|
819 | RESPONSE.redirect( |
---|
820 | URL1 + |
---|
821 | '/manage_catalogAdvanced?manage_tabs_message=' + |
---|
822 | urllib.quote('Catalog Updated \n' |
---|
823 | 'Total time: %s\n' |
---|
824 | 'Total CPU time: %s' % (`elapse`, `c_elapse`))) |
---|
825 | ###) |
---|
826 | |
---|
827 | |
---|
828 | InitializeClass(ResultsCatalog) |
---|
829 | ###) |
---|
830 | |
---|
831 | class OnlinePaymentsImport(WAeUPTable): ###( |
---|
832 | |
---|
833 | meta_type = 'WAeUP Online Payment Transactions' |
---|
834 | name = "online_payments_import" |
---|
835 | key = "order_id" |
---|
836 | def __init__(self): |
---|
837 | WAeUPTable.__init__(self, self.name) |
---|
838 | |
---|
839 | |
---|
840 | InitializeClass(OnlinePaymentsImport) |
---|
841 | ###) |
---|
842 | |
---|
843 | class ReturningImport(WAeUPTable): ###( |
---|
844 | |
---|
845 | meta_type = 'Returning Import Table' |
---|
846 | name = "returning_import" |
---|
847 | key = "matric_no" |
---|
848 | def __init__(self): |
---|
849 | WAeUPTable.__init__(self, 'returning_import') |
---|
850 | |
---|
851 | |
---|
852 | InitializeClass(ReturningImport) |
---|
853 | ###) |
---|
854 | |
---|
855 | class ResultsImport(WAeUPTable): ###( |
---|
856 | |
---|
857 | meta_type = 'Results Import Table' |
---|
858 | name = "results_import" |
---|
859 | key = "key" |
---|
860 | def __init__(self): |
---|
861 | WAeUPTable.__init__(self, 'results_import') |
---|
862 | |
---|
863 | |
---|
864 | InitializeClass(ResultsImport) |
---|
865 | |
---|
866 | ###) |
---|
867 | |
---|
868 | class PaymentsCatalog(WAeUPTable): ###( |
---|
869 | |
---|
870 | meta_type = 'WAeUP Payments Catalog' |
---|
871 | name = "students_catalog" |
---|
872 | key = "id" |
---|
873 | def __init__(self): |
---|
874 | WAeUPTable.__init__(self, 'payments_catalog') |
---|
875 | |
---|
876 | |
---|
877 | InitializeClass(PaymentsCatalog) |
---|
878 | |
---|
879 | ###) |
---|
880 | |
---|
881 | # BBB: |
---|
882 | AccomodationTable = AccommodationTable |
---|