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