[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 3018 2008-01-13 09:35:17Z 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 |
---|
[2094] | 27 | from Products.CMFCore.utils import getToolByName |
---|
| 28 | from Products.CMFCore.CatalogTool import CatalogTool |
---|
| 29 | from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter |
---|
| 30 | from Products.CPSSchemas.DataStructure import DataStructure |
---|
| 31 | from Products.CPSSchemas.DataModel import DataModel |
---|
| 32 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
[1700] | 33 | import urllib |
---|
[1620] | 34 | import DateTime,time |
---|
[780] | 35 | import csv,re |
---|
| 36 | import logging |
---|
| 37 | import Globals |
---|
| 38 | p_home = Globals.package_home(globals()) |
---|
| 39 | i_home = Globals.INSTANCE_HOME |
---|
| 40 | |
---|
[2084] | 41 | ADDING_SHEDULED = "adding_sheduled" |
---|
| 42 | OBJECT_CREATED = "object_created" |
---|
[2845] | 43 | NOT_OCCUPIED = 'not_occupied' |
---|
[2084] | 44 | |
---|
[363] | 45 | from interfaces import IWAeUPTable |
---|
| 46 | |
---|
| 47 | class AttributeHolder(object): |
---|
| 48 | pass |
---|
| 49 | |
---|
| 50 | def dict2ob(dict): |
---|
| 51 | ob = AttributeHolder() |
---|
| 52 | for key, value in dict.items(): |
---|
| 53 | setattr(ob, key, value) |
---|
| 54 | return ob |
---|
| 55 | |
---|
[1146] | 56 | class WAeUPTable(ZCatalog): ###( |
---|
[834] | 57 | |
---|
[363] | 58 | implements(IWAeUPTable) |
---|
[780] | 59 | security = ClassSecurityInfo() |
---|
[2094] | 60 | meta_type = None |
---|
[2099] | 61 | |
---|
[2094] | 62 | def __init__(self,name=None): |
---|
| 63 | if name == None: |
---|
| 64 | name = self.name |
---|
| 65 | ZCatalog.__init__(self,name) |
---|
[2099] | 66 | |
---|
[2094] | 67 | def refreshCatalog(self, clear=0, pghandler=None): ###( |
---|
[1620] | 68 | """ don't refresh for a normal table """ |
---|
| 69 | |
---|
| 70 | if self.REQUEST and self.REQUEST.RESPONSE: |
---|
| 71 | self.REQUEST.RESPONSE.redirect( |
---|
| 72 | URL1 + |
---|
| 73 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20refresh%20not%20implemented') |
---|
| 74 | |
---|
[2094] | 75 | ###) |
---|
| 76 | |
---|
| 77 | def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None): ###( |
---|
[1620] | 78 | """ clears the whole enchilada """ |
---|
[1986] | 79 | |
---|
[1916] | 80 | #if REQUEST and RESPONSE: |
---|
| 81 | # RESPONSE.redirect( |
---|
| 82 | # URL1 + |
---|
| 83 | # '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Clearing%20disabled') |
---|
[1620] | 84 | |
---|
[1916] | 85 | self._catalog.clear() |
---|
[1620] | 86 | if REQUEST and RESPONSE: |
---|
| 87 | RESPONSE.redirect( |
---|
| 88 | URL1 + |
---|
[1916] | 89 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20cleared') |
---|
[1620] | 90 | |
---|
[2094] | 91 | ###) |
---|
| 92 | |
---|
[2632] | 93 | def record2dict(self,fields,record): ###( |
---|
[2189] | 94 | d = {} |
---|
| 95 | for key in fields: |
---|
| 96 | v = getattr(record, key, None) |
---|
[3018] | 97 | v_dump = v |
---|
[2192] | 98 | if key == 'sex': |
---|
[3017] | 99 | if v == True: |
---|
| 100 | v_dump = 'F' |
---|
[3018] | 101 | elif v == False: |
---|
[3017] | 102 | v_dump = 'M' |
---|
| 103 | d[key] = v_dump |
---|
[2192] | 104 | elif v: |
---|
[2189] | 105 | if key == 'lga': |
---|
[3017] | 106 | v_dump = self.portal_vocabularies.local_gov_areas.get(v) |
---|
| 107 | if not v_dump: |
---|
| 108 | v_dump = v |
---|
[2627] | 109 | elif key == 'aos': |
---|
[3017] | 110 | v_dump = self.portal_vocabularies.aos.get(v) |
---|
| 111 | d[key] = v_dump |
---|
[2189] | 112 | else: |
---|
| 113 | d[key] = '' |
---|
| 114 | return d |
---|
[2191] | 115 | |
---|
[2632] | 116 | ###) |
---|
| 117 | |
---|
[2094] | 118 | def addRecord(self, **data): ###( |
---|
[502] | 119 | # The uid is the same as "bed". |
---|
| 120 | uid = data[self.key] |
---|
| 121 | res = self.searchResults({"%s" % self.key : uid}) |
---|
| 122 | if len(res) > 0: |
---|
| 123 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 124 | self.catalog_object(dict2ob(data), uid=uid) |
---|
| 125 | return uid |
---|
[834] | 126 | |
---|
[2094] | 127 | ###) |
---|
| 128 | |
---|
[363] | 129 | def deleteRecord(self, uid): |
---|
| 130 | self.uncatalog_object(uid) |
---|
[834] | 131 | |
---|
[2738] | 132 | def getRecordByKey(self,key): |
---|
| 133 | if not key: |
---|
| 134 | return None |
---|
| 135 | res = self.evalAdvancedQuery(Eq(self.key,key)) |
---|
| 136 | if res: |
---|
| 137 | return res[0] |
---|
| 138 | return None |
---|
| 139 | |
---|
[502] | 140 | def searchAndSetRecord(self, **data): |
---|
| 141 | raise NotImplemented |
---|
| 142 | |
---|
[2094] | 143 | def modifyRecord(self, record=None, **data): ###( |
---|
[502] | 144 | #records = self.searchResults(uid=uid) |
---|
| 145 | uid = data[self.key] |
---|
[2069] | 146 | if record is None: |
---|
| 147 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 148 | if len(records) > 1: |
---|
| 149 | # Can not happen, but anyway... |
---|
| 150 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 151 | if len(records) == 0: |
---|
| 152 | raise KeyError("No record for uid %s" % uid) |
---|
| 153 | record = records[0] |
---|
[363] | 154 | record_data = {} |
---|
| 155 | for field in self.schema() + self.indexes(): |
---|
| 156 | record_data[field] = getattr(record, field) |
---|
| 157 | # Add the updated data: |
---|
| 158 | record_data.update(data) |
---|
| 159 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 160 | |
---|
[2094] | 161 | ###) |
---|
| 162 | |
---|
| 163 | def reindexIndex(self, name, REQUEST,pghandler=None): ###( |
---|
[1062] | 164 | if isinstance(name, str): |
---|
[2094] | 165 | name = (name,) |
---|
[1062] | 166 | paths = self._catalog.uids.items() |
---|
| 167 | i = 0 |
---|
| 168 | #import pdb;pdb.set_trace() |
---|
| 169 | for p,rid in paths: |
---|
| 170 | i += 1 |
---|
| 171 | metadata = self.getMetadataForRID(rid) |
---|
| 172 | record_data = {} |
---|
| 173 | for field in name: |
---|
| 174 | record_data[field] = metadata.get(field) |
---|
| 175 | uid = metadata.get(self.key) |
---|
| 176 | self.catalog_object(dict2ob(record_data), uid, idxs=name, |
---|
| 177 | update_metadata=0) |
---|
[1082] | 178 | |
---|
[2094] | 179 | ###) |
---|
| 180 | |
---|
| 181 | security.declareProtected(ModifyPortalContent,"exportAllRecords") ###( |
---|
[780] | 182 | def exportAllRecords(self): |
---|
| 183 | "export a WAeUPTable" |
---|
| 184 | #import pdb;pdb.set_trace() |
---|
| 185 | fields = [field for field in self.schema()] |
---|
| 186 | format = ','.join(['"%%(%s)s"' % fn for fn in fields]) |
---|
| 187 | csv = [] |
---|
| 188 | csv.append(','.join(['"%s"' % fn for fn in fields])) |
---|
| 189 | for uid in self._catalog.uids: |
---|
| 190 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 191 | if len(records) > 1: |
---|
| 192 | # Can not happen, but anyway... |
---|
| 193 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 194 | if len(records) == 0: |
---|
| 195 | raise KeyError("No record for uid %s" % uid) |
---|
| 196 | rec = records[0] |
---|
| 197 | csv.append(format % rec) |
---|
| 198 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 199 | open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) |
---|
[2094] | 200 | |
---|
| 201 | ###) |
---|
| 202 | |
---|
[2189] | 203 | security.declareProtected(ModifyPortalContent,"dumpAll")###( |
---|
| 204 | def dumpAll(self): |
---|
| 205 | """dump all data in the table to a csv""" |
---|
| 206 | member = self.portal_membership.getAuthenticatedMember() |
---|
[2974] | 207 | logger = logging.getLogger('WAeUPTables.WAeUPTable.dumpAll') |
---|
[2189] | 208 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 209 | export_file = "%s/export/%s_%s.csv" % (i_home,self.__name__,current,) |
---|
| 210 | res_list = [] |
---|
| 211 | lines = [] |
---|
| 212 | if hasattr(self,"export_keys"): |
---|
| 213 | fields = self.export_keys |
---|
| 214 | else: |
---|
| 215 | fields = [] |
---|
| 216 | for f in self.schema(): |
---|
| 217 | fields.append(f) |
---|
| 218 | headline = ','.join(fields) |
---|
| 219 | out = open(export_file,"wb") |
---|
| 220 | out.write(headline +'\n') |
---|
| 221 | out.close() |
---|
| 222 | out = open(export_file,"a") |
---|
| 223 | csv_writer = csv.DictWriter(out,fields,) |
---|
| 224 | records = self() |
---|
| 225 | nr2export = len(records) |
---|
| 226 | logger.info('%s starts dumping, %s records to export' % (member,nr2export)) |
---|
| 227 | chunk = 2000 |
---|
| 228 | total = 0 |
---|
| 229 | start = DateTime.DateTime().timeTime() |
---|
| 230 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 231 | for record in records: |
---|
| 232 | not_all = False |
---|
| 233 | d = self.record2dict(fields,record) |
---|
| 234 | lines.append(d) |
---|
| 235 | total += 1 |
---|
| 236 | if total and not total % chunk or total == len(records): |
---|
| 237 | csv_writer.writerows(lines) |
---|
| 238 | anz = len(lines) |
---|
| 239 | logger.info("wrote %(anz)d total written %(total)d" % vars()) |
---|
| 240 | end_chunk = DateTime.DateTime().timeTime() |
---|
| 241 | duration = end_chunk-start_chunk |
---|
| 242 | per_record = duration/anz |
---|
| 243 | till_now = end_chunk - start |
---|
| 244 | avarage_per_record = till_now/total |
---|
| 245 | estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export) |
---|
| 246 | estimated_end = estimated_end.strftime("%H:%M:%S") |
---|
| 247 | logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars()) |
---|
| 248 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 249 | lines = [] |
---|
| 250 | end = DateTime.DateTime().timeTime() |
---|
| 251 | logger.info('total time %6.2f m' % ((end-start)/60)) |
---|
| 252 | import os |
---|
| 253 | filename, extension = os.path.splitext(export_file) |
---|
| 254 | from subprocess import call |
---|
| 255 | msg = "wrote %(total)d records to %(export_file)s" % vars() |
---|
[2561] | 256 | #try: |
---|
| 257 | # retcode = call('gzip %s' % (export_file),shell=True) |
---|
| 258 | # if retcode == 0: |
---|
| 259 | # msg = "wrote %(total)d records to %(export_file)s.gz" % vars() |
---|
| 260 | #except OSError, e: |
---|
| 261 | # retcode = -99 |
---|
| 262 | # logger.info("zip failed with %s" % e) |
---|
[2189] | 263 | logger.info(msg) |
---|
| 264 | args = {'portal_status_message': msg} |
---|
| 265 | #url = self.REQUEST.get('URL1') + '?' + urlencode(args) |
---|
| 266 | url = self.REQUEST.get('URL2') |
---|
| 267 | return self.REQUEST.RESPONSE.redirect(url) |
---|
| 268 | ###) |
---|
| 269 | |
---|
[2185] | 270 | security.declarePrivate("_import_old") ###( |
---|
| 271 | def _import_old(self,filename,schema,layout, mode,logger): |
---|
[2094] | 272 | "import data from csv" |
---|
| 273 | import transaction |
---|
| 274 | import random |
---|
| 275 | pm = self.portal_membership |
---|
| 276 | member = pm.getAuthenticatedMember() |
---|
| 277 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 278 | import_fn = "%s/import/%s.csv" % (i_home,filename) |
---|
| 279 | imported_fn = "%s/import/%s_imported%s.csv" % (i_home,filename,current) |
---|
| 280 | not_imported_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current) |
---|
| 281 | start = True |
---|
| 282 | tr_count = 1 |
---|
| 283 | total_imported = 0 |
---|
| 284 | total_not_imported = 0 |
---|
| 285 | total = 0 |
---|
| 286 | iname = "%s" % filename |
---|
[2112] | 287 | not_imported = [] |
---|
| 288 | imported = [] |
---|
| 289 | valid_records = [] |
---|
| 290 | invalid_records = [] |
---|
| 291 | d = {} |
---|
| 292 | d['mode'] = mode |
---|
| 293 | d['imported'] = total_imported |
---|
| 294 | d['not_imported'] = total_not_imported |
---|
| 295 | d['valid_records'] = valid_records |
---|
| 296 | d['invalid_records'] = invalid_records |
---|
| 297 | d['import_fn'] = import_fn |
---|
| 298 | d['imported_fn'] = imported_fn |
---|
| 299 | d['not_imported_fn'] = not_imported_fn |
---|
[2094] | 300 | if schema is None: |
---|
| 301 | em = 'No schema specified' |
---|
| 302 | logger.error(em) |
---|
[2112] | 303 | return d |
---|
[2094] | 304 | if layout is None: |
---|
| 305 | em = 'No layout specified' |
---|
| 306 | logger.error(em) |
---|
[2112] | 307 | return d |
---|
[2094] | 308 | validators = {} |
---|
| 309 | for widget in layout.keys(): |
---|
[2112] | 310 | try: |
---|
| 311 | validators[widget] = layout[widget].validate |
---|
| 312 | except AttributeError: |
---|
| 313 | logger.info('%s has no validate attribute' % widget) |
---|
| 314 | return d |
---|
[2094] | 315 | # if mode == 'edit': |
---|
| 316 | # importer = self.importEdit |
---|
| 317 | # elif mode == 'add': |
---|
| 318 | # importer = self.importAdd |
---|
| 319 | # else: |
---|
| 320 | # importer = None |
---|
| 321 | try: |
---|
[2185] | 322 | items = csv.DictReader(open(import_fn,"rb"), |
---|
| 323 | dialect="excel", |
---|
| 324 | skipinitialspace=True) |
---|
[2094] | 325 | except: |
---|
| 326 | em = 'Error reading %s.csv' % filename |
---|
| 327 | logger.error(em) |
---|
| 328 | return d |
---|
[2185] | 329 | #import pdb;pdb.set_trace() |
---|
[2094] | 330 | for item in items: |
---|
| 331 | if start: |
---|
| 332 | start = False |
---|
| 333 | logger.info('%s starts import from %s.csv' % (member,filename)) |
---|
| 334 | #import_keys = [k for k in item.keys() if not k.startswith('ignore')] |
---|
[2185] | 335 | attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb"), |
---|
| 336 | dialect="excel", |
---|
| 337 | skipinitialspace=True).next() |
---|
[2094] | 338 | import_keys = [k for k in attrs if not (k.startswith('ignore') or k.isupper())] |
---|
| 339 | diff2schema = set(import_keys).difference(set(schema.keys())) |
---|
| 340 | diff2layout = set(import_keys).difference(set(layout.keys())) |
---|
| 341 | if diff2layout: |
---|
| 342 | em = "not ignorable key(s) %s found in heading" % diff2layout |
---|
| 343 | logger.info(em) |
---|
| 344 | return d |
---|
| 345 | s = ','.join(['"%s"' % fn for fn in import_keys]) |
---|
| 346 | open(not_imported_fn,"a").write(s + ',"Error"'+ '\n') |
---|
| 347 | #s = '"id",' + s |
---|
| 348 | open(imported_fn,"a").write(s + '\n') |
---|
| 349 | format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) |
---|
| 350 | format_error = format + ',"%(Error)s"' |
---|
| 351 | #format = '"%(id)s",'+ format |
---|
| 352 | adapters = [MappingStorageAdapter(schema, item)] |
---|
| 353 | dm = DataModel(item, adapters,context=self) |
---|
| 354 | ds = DataStructure(data=item,datamodel=dm) |
---|
| 355 | error_string = "" |
---|
[2503] | 356 | #import pdb;pdb.set_trace() |
---|
[2094] | 357 | for k in import_keys: |
---|
| 358 | if not validators[k](ds,mode=mode): |
---|
| 359 | error_string += " %s : %s" % (k,ds.getError(k)) |
---|
| 360 | # if not error_string and importer: |
---|
| 361 | # item.update(dm) |
---|
| 362 | # item['id'],error = importer(item) |
---|
| 363 | # if error: |
---|
| 364 | # error_string += error |
---|
| 365 | if error_string: |
---|
| 366 | item['Error'] = error_string |
---|
| 367 | invalid_records.append(dm) |
---|
| 368 | not_imported.append(format_error % item) |
---|
| 369 | total_not_imported += 1 |
---|
| 370 | else: |
---|
| 371 | em = format % item |
---|
| 372 | valid_records.append(dm) |
---|
| 373 | imported.append(em) |
---|
| 374 | #logger.info("%(total_imported)d of %(total)d %(em)s" % vars()) |
---|
| 375 | tr_count += 1 |
---|
| 376 | total_imported += 1 |
---|
| 377 | total += 1 |
---|
| 378 | if len(imported) > 0: |
---|
| 379 | open(imported_fn,"a").write('\n'.join(imported)) |
---|
| 380 | if len(not_imported) > 0: |
---|
| 381 | open(not_imported_fn,"a").write('\n'.join(not_imported)) |
---|
| 382 | #em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) |
---|
| 383 | d['imported'] = total_imported |
---|
| 384 | d['not_imported'] = total_not_imported |
---|
| 385 | d['valid_records'] = valid_records |
---|
| 386 | d['invalid_records'] = invalid_records |
---|
| 387 | d['imported_fn'] = imported_fn |
---|
| 388 | d['not_imported_fn'] = not_imported_fn |
---|
| 389 | #logger.info(em) |
---|
| 390 | return d |
---|
[1935] | 391 | ###) |
---|
[2185] | 392 | |
---|
| 393 | security.declarePrivate("_import") ###( |
---|
| 394 | def _import_new(self,csv_items,schema, layout, mode,logger): |
---|
| 395 | "import data from csv.Dictreader Instance" |
---|
| 396 | start = True |
---|
| 397 | tr_count = 1 |
---|
| 398 | total_imported = 0 |
---|
| 399 | total_not_imported = 0 |
---|
| 400 | total = 0 |
---|
| 401 | iname = "%s" % filename |
---|
| 402 | not_imported = [] |
---|
| 403 | valid_records = [] |
---|
| 404 | invalid_records = [] |
---|
| 405 | duplicate_records = [] |
---|
| 406 | d = {} |
---|
| 407 | d['mode'] = mode |
---|
| 408 | d['valid_records'] = valid_records |
---|
| 409 | d['invalid_records'] = invalid_records |
---|
| 410 | d['invalid_records'] = duplicate_records |
---|
| 411 | # d['import_fn'] = import_fn |
---|
| 412 | # d['imported_fn'] = imported_fn |
---|
| 413 | # d['not_imported_fn'] = not_imported_fn |
---|
| 414 | validators = {} |
---|
| 415 | for widget in layout.keys(): |
---|
| 416 | try: |
---|
| 417 | validators[widget] = layout[widget].validate |
---|
| 418 | except AttributeError: |
---|
| 419 | logger.info('%s has no validate attribute' % widget) |
---|
| 420 | return d |
---|
| 421 | for item in csv_items: |
---|
| 422 | if start: |
---|
| 423 | start = False |
---|
| 424 | logger.info('%s starts import from %s.csv' % (member,filename)) |
---|
| 425 | #import_keys = [k for k in item.keys() if not k.startswith('ignore')] |
---|
| 426 | attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb")).next() |
---|
| 427 | import_keys = [k for k in attrs if not (k.startswith('ignore') or k.isupper())] |
---|
| 428 | diff2schema = set(import_keys).difference(set(schema.keys())) |
---|
| 429 | diff2layout = set(import_keys).difference(set(layout.keys())) |
---|
| 430 | if diff2layout: |
---|
| 431 | em = "not ignorable key(s) %s found in heading" % diff2layout |
---|
| 432 | logger.info(em) |
---|
| 433 | return d |
---|
| 434 | # s = ','.join(['"%s"' % fn for fn in import_keys]) |
---|
| 435 | # open(not_imported_fn,"a").write(s + ',"Error"'+ '\n') |
---|
| 436 | # #s = '"id",' + s |
---|
| 437 | # open(imported_fn,"a").write(s + '\n') |
---|
| 438 | # format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) |
---|
| 439 | # format_error = format + ',"%(Error)s"' |
---|
| 440 | # #format = '"%(id)s",'+ format |
---|
| 441 | adapters = [MappingStorageAdapter(schema, item)] |
---|
| 442 | dm = DataModel(item, adapters,context=self) |
---|
| 443 | ds = DataStructure(data=item,datamodel=dm) |
---|
| 444 | error_string = "" |
---|
| 445 | for k in import_keys: |
---|
| 446 | if not validators[k](ds,mode=mode): |
---|
| 447 | error_string += " %s : %s" % (k,ds.getError(k)) |
---|
| 448 | if error_string: |
---|
| 449 | item['Error'] = error_string |
---|
| 450 | #invalid_records.append(dm) |
---|
| 451 | invalid_records.append(item) |
---|
| 452 | total_not_imported += 1 |
---|
| 453 | else: |
---|
| 454 | em = format % item |
---|
| 455 | valid_records.append(dm) |
---|
| 456 | #logger.info("%(total_imported)d of %(total)d %(em)s" % vars()) |
---|
| 457 | tr_count += 1 |
---|
| 458 | total_imported += 1 |
---|
| 459 | total += 1 |
---|
| 460 | # if len(imported) > 0: |
---|
| 461 | # open(imported_fn,"a").write('\n'.join(imported)) |
---|
| 462 | # if len(not_imported) > 0: |
---|
| 463 | # open(not_imported_fn,"a").write('\n'.join(not_imported)) |
---|
| 464 | #em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) |
---|
| 465 | d['imported'] = total_imported |
---|
| 466 | d['not_imported'] = total_not_imported |
---|
| 467 | d['valid_records'] = valid_records |
---|
| 468 | d['invalid_records'] = invalid_records |
---|
| 469 | return d |
---|
| 470 | ###) |
---|
| 471 | |
---|
[2396] | 472 | security.declarePublic("missingValue")###( |
---|
| 473 | def missingValue(self): |
---|
| 474 | from Missing import MV |
---|
| 475 | return MV |
---|
| 476 | ###) |
---|
[2094] | 477 | ###) |
---|
[834] | 478 | |
---|
[1146] | 479 | class AccommodationTable(WAeUPTable): ###( |
---|
[834] | 480 | |
---|
[404] | 481 | meta_type = 'WAeUP Accommodation Tool' |
---|
[2094] | 482 | name = "portal_accommodation" |
---|
[502] | 483 | key = "bed" |
---|
[2094] | 484 | def __init__(self,name=None): |
---|
| 485 | if name == None: |
---|
| 486 | name = self.name |
---|
| 487 | WAeUPTable.__init__(self, name) |
---|
[2866] | 488 | |
---|
[635] | 489 | def searchAndReserveBed(self, student_id,bed_type): |
---|
[2845] | 490 | #records = self.searchResults({'student' : student_id}) |
---|
[2857] | 491 | #import pdb;pdb.set_trace() |
---|
[2845] | 492 | records = self.evalAdvancedQuery(Eq('student',student_id)) |
---|
[635] | 493 | if len(records) > 0: |
---|
[1293] | 494 | return -1,"Student with Id %s already booked bed %s." % (student_id,records[0].bed) |
---|
[834] | 495 | |
---|
[2845] | 496 | #records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
| 497 | query = Eq('bed_type',bed_type) & Eq('student',NOT_OCCUPIED) |
---|
| 498 | records = self.evalAdvancedQuery(query,sortSpecs=('sort_id','bed')) |
---|
[635] | 499 | if len(records) == 0: |
---|
[1306] | 500 | return -2,"No bed available" |
---|
[635] | 501 | rec = records[0] |
---|
| 502 | self.modifyRecord(bed=rec.bed,student=student_id) |
---|
[1571] | 503 | s_logger = logging.getLogger('WAeUPTables.AccommodationTable.searchAndReserveBed') |
---|
| 504 | s_logger.info('%s reserved bed %s' % (student_id,rec.bed)) |
---|
[635] | 505 | return 1,rec.bed |
---|
[363] | 506 | |
---|
[834] | 507 | |
---|
[404] | 508 | InitializeClass(AccommodationTable) |
---|
[411] | 509 | |
---|
[1146] | 510 | ###) |
---|
| 511 | |
---|
| 512 | class PinTable(WAeUPTable): ###( |
---|
[1030] | 513 | from ZODB.POSException import ConflictError |
---|
[2973] | 514 | security = ClassSecurityInfo() |
---|
[440] | 515 | meta_type = 'WAeUP Pin Tool' |
---|
[2094] | 516 | name = "portal_pins" |
---|
[502] | 517 | key = 'pin' |
---|
[2859] | 518 | |
---|
[2094] | 519 | def __init__(self,name=None): |
---|
| 520 | if name == None: |
---|
| 521 | name = self.name |
---|
| 522 | WAeUPTable.__init__(self, name) |
---|
[1082] | 523 | |
---|
[2973] | 524 | security.declareProtected(ModifyPortalContent,"dumpAll")###( |
---|
| 525 | def dumpAll(self,include_unused=None): |
---|
| 526 | """dump all data in the table to a csv""" |
---|
| 527 | member = self.portal_membership.getAuthenticatedMember() |
---|
[2974] | 528 | logger = logging.getLogger('WAeUPTables.PinTable.dumpAll') |
---|
[2973] | 529 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 530 | export_file = "%s/export/%s_%s.csv" % (i_home,self.__name__,current,) |
---|
| 531 | res_list = [] |
---|
| 532 | lines = [] |
---|
| 533 | if hasattr(self,"export_keys"): |
---|
| 534 | fields = self.export_keys |
---|
| 535 | else: |
---|
| 536 | fields = [] |
---|
| 537 | for f in self.schema(): |
---|
| 538 | fields.append(f) |
---|
| 539 | headline = ','.join(fields) |
---|
| 540 | out = open(export_file,"wb") |
---|
| 541 | out.write(headline +'\n') |
---|
| 542 | out.close() |
---|
| 543 | out = open(export_file,"a") |
---|
| 544 | csv_writer = csv.DictWriter(out,fields,) |
---|
| 545 | if include_unused is not None and str(member) not in ('admin','joachim'): |
---|
| 546 | logger.info('%s tries to dump pintable with unused pins' % (member)) |
---|
| 547 | return |
---|
| 548 | if include_unused is not None: |
---|
| 549 | records = self() |
---|
| 550 | else: |
---|
| 551 | records = self.evalAdvancedQuery(~Eq('student','')) |
---|
| 552 | nr2export = len(records) |
---|
| 553 | logger.info('%s starts dumping, %s records to export' % (member,nr2export)) |
---|
| 554 | chunk = 2000 |
---|
| 555 | total = 0 |
---|
| 556 | start = DateTime.DateTime().timeTime() |
---|
| 557 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 558 | for record in records: |
---|
| 559 | not_all = False |
---|
| 560 | d = self.record2dict(fields,record) |
---|
| 561 | lines.append(d) |
---|
| 562 | total += 1 |
---|
| 563 | if total and not total % chunk or total == len(records): |
---|
| 564 | csv_writer.writerows(lines) |
---|
| 565 | anz = len(lines) |
---|
| 566 | logger.info("wrote %(anz)d total written %(total)d" % vars()) |
---|
| 567 | end_chunk = DateTime.DateTime().timeTime() |
---|
| 568 | duration = end_chunk-start_chunk |
---|
| 569 | per_record = duration/anz |
---|
| 570 | till_now = end_chunk - start |
---|
| 571 | avarage_per_record = till_now/total |
---|
| 572 | estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export) |
---|
| 573 | estimated_end = estimated_end.strftime("%H:%M:%S") |
---|
| 574 | logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars()) |
---|
| 575 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 576 | lines = [] |
---|
| 577 | end = DateTime.DateTime().timeTime() |
---|
| 578 | logger.info('total time %6.2f m' % ((end-start)/60)) |
---|
| 579 | import os |
---|
| 580 | filename, extension = os.path.splitext(export_file) |
---|
| 581 | from subprocess import call |
---|
| 582 | msg = "wrote %(total)d records to %(export_file)s" % vars() |
---|
| 583 | #try: |
---|
| 584 | # retcode = call('gzip %s' % (export_file),shell=True) |
---|
| 585 | # if retcode == 0: |
---|
| 586 | # msg = "wrote %(total)d records to %(export_file)s.gz" % vars() |
---|
| 587 | #except OSError, e: |
---|
| 588 | # retcode = -99 |
---|
| 589 | # logger.info("zip failed with %s" % e) |
---|
| 590 | logger.info(msg) |
---|
| 591 | args = {'portal_status_message': msg} |
---|
| 592 | #url = self.REQUEST.get('URL1') + '?' + urlencode(args) |
---|
| 593 | url = self.REQUEST.get('URL2') |
---|
| 594 | return self.REQUEST.RESPONSE.redirect(url) |
---|
| 595 | ###) |
---|
[1082] | 596 | |
---|
[2973] | 597 | |
---|
| 598 | |
---|
[710] | 599 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
[2191] | 600 | |
---|
| 601 | # The following line must be activated after resetting the |
---|
| 602 | # the portal_pins table. This is to avoid duplicate entries |
---|
[2184] | 603 | # and disable duplicate payments. |
---|
[2191] | 604 | |
---|
[2184] | 605 | #student_id = student_id.upper() |
---|
| 606 | |
---|
[2716] | 607 | #records = self.searchResults(student = student_id) |
---|
[2579] | 608 | #if len(records) > 0 and prefix in ('CLR','APP'): |
---|
| 609 | # for r in records: |
---|
| 610 | # if r.pin != uid and r.prefix_batch.startswith(prefix): |
---|
| 611 | # return -2 |
---|
[502] | 612 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 613 | if len(records) > 1: |
---|
| 614 | # Can not happen, but anyway... |
---|
| 615 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 616 | if len(records) == 0: |
---|
[2766] | 617 | return -1,None |
---|
[502] | 618 | record = records[0] |
---|
| 619 | if record.student == "": |
---|
| 620 | record_data = {} |
---|
| 621 | for field in self.schema() + self.indexes(): |
---|
| 622 | record_data[field] = getattr(record, field) |
---|
| 623 | # Add the updated data: |
---|
[635] | 624 | record_data['student'] = student_id |
---|
[1030] | 625 | try: |
---|
| 626 | self.catalog_object(dict2ob(record_data), uid) |
---|
[2766] | 627 | return 1,record |
---|
[1030] | 628 | except ConflictError: |
---|
[2766] | 629 | return 2,record |
---|
[990] | 630 | if record.student.upper() != student_id.upper(): |
---|
[2766] | 631 | return 0,record |
---|
[997] | 632 | if record.student.upper() == student_id.upper(): |
---|
[2766] | 633 | return 2,record |
---|
| 634 | return -3,record |
---|
[440] | 635 | InitializeClass(PinTable) |
---|
[1146] | 636 | ###) |
---|
[966] | 637 | |
---|
[1146] | 638 | class PumeResultsTable(WAeUPTable): ###( |
---|
| 639 | |
---|
[966] | 640 | meta_type = 'WAeUP PumeResults Tool' |
---|
[2094] | 641 | name = "portal_pumeresults" |
---|
[966] | 642 | key = "jamb_reg_no" |
---|
[2094] | 643 | def __init__(self,name=None): |
---|
| 644 | if name == None: |
---|
| 645 | name = self.name |
---|
| 646 | WAeUPTable.__init__(self, name) |
---|
[966] | 647 | |
---|
| 648 | |
---|
| 649 | InitializeClass(PumeResultsTable) |
---|
| 650 | |
---|
[1146] | 651 | ###) |
---|
[971] | 652 | |
---|
[2094] | 653 | class ApplicantsCatalog(WAeUPTable): ###( |
---|
| 654 | |
---|
[2113] | 655 | meta_type = 'WAeUP Applicants Catalog' |
---|
[2094] | 656 | name = "applicants_catalog" |
---|
| 657 | key = "reg_no" |
---|
| 658 | security = ClassSecurityInfo() |
---|
[2537] | 659 | #export_keys = ( |
---|
| 660 | # "reg_no", |
---|
| 661 | # "status", |
---|
| 662 | # "lastname", |
---|
| 663 | # "sex", |
---|
| 664 | # "date_of_birth", |
---|
| 665 | # "lga", |
---|
| 666 | # "email", |
---|
| 667 | # "phone", |
---|
| 668 | # "passport", |
---|
| 669 | # "entry_mode", |
---|
| 670 | # "pin", |
---|
| 671 | # "screening_type", |
---|
| 672 | # "registration_date", |
---|
| 673 | # "testdate", |
---|
| 674 | # "application_date", |
---|
| 675 | # "screening_date", |
---|
| 676 | # "faculty", |
---|
| 677 | # "department", |
---|
| 678 | # "course1", |
---|
| 679 | # "course2", |
---|
| 680 | # "course3", |
---|
| 681 | # "eng_score", |
---|
| 682 | # "subj1", |
---|
| 683 | # "subj1score", |
---|
| 684 | # "subj2", |
---|
| 685 | # "subj2score", |
---|
| 686 | # "subj3", |
---|
| 687 | # "subj3score", |
---|
| 688 | # "aggregate", |
---|
| 689 | # "course_admitted", |
---|
| 690 | # ) |
---|
[2632] | 691 | |
---|
[2094] | 692 | def __init__(self,name=None): |
---|
| 693 | if name == None: |
---|
| 694 | name = self.name |
---|
| 695 | WAeUPTable.__init__(self, name) |
---|
| 696 | |
---|
[2185] | 697 | security.declareProtected(ModifyPortalContent,"new_importCSV")###( |
---|
| 698 | def new_importCSV(self,filename="JAMB_data", |
---|
| 699 | schema_id="application", |
---|
[2503] | 700 | layout_id="import_application", |
---|
[2185] | 701 | mode='add'): |
---|
| 702 | """ import JAMB data """ |
---|
| 703 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 704 | pm = self.portal_membership |
---|
| 705 | member = pm.getAuthenticatedMember() |
---|
| 706 | logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV') |
---|
| 707 | lock_fn = "%s/import/%s_import_lock" % (i_home,filename) |
---|
| 708 | import_fn = "%s/import/%s.csv" % (i_home,filename) |
---|
| 709 | if mode not in ('add','edit'): |
---|
| 710 | logger.info("invalid mode: %s" % mode) |
---|
| 711 | if os.path.exists(lock_fn): |
---|
| 712 | logger.info("import of %(import_fn)s already in progress" % vars()) |
---|
| 713 | return |
---|
| 714 | lock_file = open(lock_fn,"w") |
---|
| 715 | lock_file.write("%(current)s \n" % vars()) |
---|
| 716 | lock_file.close() |
---|
| 717 | invalid_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current) |
---|
| 718 | duplicate_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current) |
---|
| 719 | stool = getToolByName(self, 'portal_schemas') |
---|
| 720 | ltool = getToolByName(self, 'portal_layouts') |
---|
| 721 | schema = stool._getOb(schema_id) |
---|
| 722 | if schema is None: |
---|
| 723 | em = 'No such schema %s' % schema_id |
---|
| 724 | logger.error(em) |
---|
| 725 | return |
---|
| 726 | for postfix in ('_import',''): |
---|
| 727 | layout_name = "%(layout_id)s%(postfix)s" % vars() |
---|
| 728 | if hasattr(ltool,layout_name): |
---|
| 729 | break |
---|
| 730 | layout = ltool._getOb(layout_name) |
---|
| 731 | if layout is None: |
---|
| 732 | em = 'No such layout %s' % layout_id |
---|
| 733 | logger.error(em) |
---|
| 734 | return |
---|
| 735 | try: |
---|
| 736 | csv_file = csv.DictReader(open(import_fn,"rb")) |
---|
| 737 | except: |
---|
| 738 | em = 'Error reading %s.csv' % filename |
---|
| 739 | logger.error(em) |
---|
[2191] | 740 | return |
---|
[2185] | 741 | d = self._import_new(csv_items,schema,layout,mode,logger) |
---|
| 742 | imported = [] |
---|
| 743 | edited = [] |
---|
| 744 | duplicates = [] |
---|
| 745 | not_found = [] |
---|
| 746 | if len(d['valid_records']) > 0: |
---|
| 747 | for record in d['valid_records']: |
---|
| 748 | #import pdb;pdb.set_trace() |
---|
| 749 | if mode == "add": |
---|
| 750 | try: |
---|
| 751 | self.addRecord(**dict(record.items())) |
---|
| 752 | imported.append(**dict(record.items())) |
---|
| 753 | logger.info("added %s" % record.items()) |
---|
| 754 | except ValueError: |
---|
| 755 | dupplicate.append(**dict(record.items())) |
---|
| 756 | logger.info("duplicate %s" % record.items()) |
---|
| 757 | elif mode == "edit": |
---|
| 758 | try: |
---|
| 759 | self.modifyRecord(**dict(record.items())) |
---|
| 760 | edited.append(**dict(record.items())) |
---|
| 761 | logger.info("edited %s" % record.items()) |
---|
| 762 | except KeyError: |
---|
| 763 | not_found.append(**dict(record.items())) |
---|
| 764 | logger.info("not found %s" % record.items()) |
---|
| 765 | invalid = d['invalid_records'] |
---|
| 766 | for itype in ("imported","edited","not_found","duplicate","invalid"): |
---|
| 767 | outlist = locals[itype] |
---|
| 768 | if len(outlist): |
---|
| 769 | d = {} |
---|
| 770 | for k in outlist[0].keys(): |
---|
| 771 | d[k] = k |
---|
[2191] | 772 | outlist[0] = d |
---|
[2185] | 773 | outfile = open("file_name_%s" % itype,'w') |
---|
| 774 | csv.DictWriter(outfile,outlist[0].keys()).writerows(outlist) |
---|
| 775 | logger.info("wrote %(itype)s records to %(, written to %(not_imported_fn)s" % d) |
---|
| 776 | ###) |
---|
| 777 | |
---|
[2094] | 778 | security.declareProtected(ModifyPortalContent,"importCSV")###( |
---|
| 779 | def importCSV(self,filename="JAMB_data", |
---|
| 780 | schema_id="application", |
---|
[2508] | 781 | layout_id="application_pce", |
---|
[2094] | 782 | mode='add'): |
---|
| 783 | """ import JAMB data """ |
---|
| 784 | stool = getToolByName(self, 'portal_schemas') |
---|
| 785 | ltool = getToolByName(self, 'portal_layouts') |
---|
| 786 | schema = stool._getOb(schema_id) |
---|
| 787 | if schema is None: |
---|
| 788 | em = 'No such schema %s' % schema_id |
---|
| 789 | logger.error(em) |
---|
| 790 | return |
---|
| 791 | layout = ltool._getOb(layout_id) |
---|
| 792 | if layout is None: |
---|
| 793 | em = 'No such layout %s' % layout_id |
---|
| 794 | logger.error(em) |
---|
| 795 | return |
---|
[2099] | 796 | logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV') |
---|
[2185] | 797 | d = self._import_old(filename,schema,layout,mode,logger) |
---|
[2094] | 798 | if len(d['valid_records']) > 0: |
---|
| 799 | for record in d['valid_records']: |
---|
| 800 | #import pdb;pdb.set_trace() |
---|
| 801 | if mode == "add": |
---|
| 802 | self.addRecord(**dict(record.items())) |
---|
| 803 | logger.info("added %s" % record.items()) |
---|
| 804 | elif mode == "edit": |
---|
| 805 | self.modifyRecord(**dict(record.items())) |
---|
| 806 | logger.info("edited %s" % record.items()) |
---|
| 807 | else: |
---|
| 808 | logger.info("invalid mode: %s" % mode) |
---|
| 809 | logger.info("%(mode)sed %(imported)d records, invalid written to %(not_imported_fn)s" % d) |
---|
[2632] | 810 | ###) |
---|
[2094] | 811 | |
---|
| 812 | InitializeClass(ApplicantsCatalog) |
---|
| 813 | |
---|
| 814 | ###) |
---|
| 815 | |
---|
[1146] | 816 | class StudentsCatalog(WAeUPTable): ###( |
---|
[1620] | 817 | security = ClassSecurityInfo() |
---|
[1146] | 818 | |
---|
[971] | 819 | meta_type = 'WAeUP Students Catalog' |
---|
| 820 | name = "students_catalog" |
---|
| 821 | key = "id" |
---|
[1700] | 822 | affected_types = { ###( |
---|
[1749] | 823 | 'StudentApplication': |
---|
[2069] | 824 | {'id': 'application', |
---|
| 825 | 'fields': |
---|
| 826 | ('jamb_reg_no', |
---|
| 827 | 'entry_mode', |
---|
| 828 | #'entry_level', |
---|
| 829 | 'entry_session', |
---|
| 830 | ) |
---|
| 831 | }, |
---|
[1700] | 832 | 'StudentClearance': |
---|
[2069] | 833 | {'id': 'clearance', |
---|
| 834 | 'fields': |
---|
| 835 | ('matric_no', |
---|
| 836 | 'lga', |
---|
| 837 | ) |
---|
| 838 | }, |
---|
| 839 | 'StudentPersonal': |
---|
| 840 | {'id': 'personal', |
---|
| 841 | 'fields': |
---|
| 842 | ('name', |
---|
| 843 | 'sex', |
---|
| 844 | 'perm_address', |
---|
| 845 | 'email', |
---|
| 846 | 'phone', |
---|
| 847 | ) |
---|
| 848 | }, |
---|
| 849 | 'StudentStudyCourse': |
---|
| 850 | {'id': 'study_course', |
---|
| 851 | 'fields': |
---|
| 852 | ('course', # study_course |
---|
| 853 | 'faculty', # from certificate |
---|
| 854 | 'department', # from certificate |
---|
| 855 | 'end_level', # from certificate |
---|
| 856 | 'level', # current_level |
---|
| 857 | 'mode', # current_mode |
---|
| 858 | 'session', # current_session |
---|
| 859 | 'verdict', # current_verdict |
---|
| 860 | ) |
---|
| 861 | }, |
---|
| 862 | } |
---|
[1700] | 863 | ###) |
---|
[1625] | 864 | |
---|
[2094] | 865 | def __init__(self,name=None): |
---|
| 866 | if name == None: |
---|
| 867 | name = self.name |
---|
| 868 | WAeUPTable.__init__(self, name) |
---|
[1620] | 869 | return |
---|
[1625] | 870 | |
---|
[1700] | 871 | def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None): |
---|
| 872 | """ clears the whole enchilada """ |
---|
| 873 | self._catalog.clear() |
---|
[971] | 874 | |
---|
[1700] | 875 | if REQUEST and RESPONSE: |
---|
| 876 | RESPONSE.redirect( |
---|
| 877 | URL1 + |
---|
| 878 | '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Cleared') |
---|
[971] | 879 | |
---|
[1700] | 880 | def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###( |
---|
| 881 | """ clear the catalog, then re-index everything """ |
---|
| 882 | |
---|
| 883 | elapse = time.time() |
---|
| 884 | c_elapse = time.clock() |
---|
| 885 | |
---|
| 886 | pgthreshold = self._getProgressThreshold() |
---|
| 887 | handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None |
---|
| 888 | self.refreshCatalog(clear=1, pghandler=handler) |
---|
| 889 | |
---|
| 890 | elapse = time.time() - elapse |
---|
| 891 | c_elapse = time.clock() - c_elapse |
---|
| 892 | |
---|
| 893 | RESPONSE.redirect( |
---|
| 894 | URL1 + |
---|
| 895 | '/manage_catalogAdvanced?manage_tabs_message=' + |
---|
| 896 | urllib.quote('Catalog Updated \n' |
---|
| 897 | 'Total time: %s\n' |
---|
| 898 | 'Total CPU time: %s' % (`elapse`, `c_elapse`))) |
---|
| 899 | ###) |
---|
| 900 | |
---|
[2084] | 901 | def fill_certificates_dict(self): ###( |
---|
[2078] | 902 | "return certificate data in dict" |
---|
| 903 | certificates_brains = self.portal_catalog(portal_type ='Certificate') |
---|
| 904 | d = {} |
---|
| 905 | for cb in certificates_brains: |
---|
| 906 | certificate_doc = cb.getObject().getContent() |
---|
| 907 | cb_path = cb.getPath().split('/') |
---|
| 908 | ld = {} |
---|
| 909 | ld['faculty'] = cb_path[-4] |
---|
| 910 | ld['department'] = cb_path[-3] |
---|
| 911 | ld['end_level'] = getattr(certificate_doc,'end_level','999') |
---|
| 912 | d[cb.getId] = ld |
---|
| 913 | return d |
---|
[2084] | 914 | ###) |
---|
| 915 | |
---|
[2078] | 916 | def get_from_doc_department(self,doc,cached_data={}): ###( |
---|
[1620] | 917 | "return the students department" |
---|
[1700] | 918 | if doc is None: |
---|
[1620] | 919 | return None |
---|
[2078] | 920 | if cached_data.has_key(doc.study_course): |
---|
| 921 | return cached_data[doc.study_course]['department'] |
---|
[1700] | 922 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
[1620] | 923 | if len(certificate_res) != 1: |
---|
| 924 | return None |
---|
| 925 | return certificate_res[0].getPath().split('/')[-3] |
---|
| 926 | |
---|
[2078] | 927 | def get_from_doc_faculty(self,doc,cached_data={}): |
---|
[1700] | 928 | "return the students faculty" |
---|
| 929 | if doc is None: |
---|
[1620] | 930 | return None |
---|
[2078] | 931 | if cached_data.has_key(doc.study_course): |
---|
| 932 | return cached_data[doc.study_course]['faculty'] |
---|
[1700] | 933 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
| 934 | if len(certificate_res) != 1: |
---|
| 935 | return None |
---|
| 936 | return certificate_res[0].getPath().split('/')[-4] |
---|
[1620] | 937 | |
---|
[2099] | 938 | def get_from_doc_end_level(self,doc,cached_data={}): |
---|
[2069] | 939 | "return the students end_level" |
---|
| 940 | if doc is None: |
---|
| 941 | return None |
---|
[2078] | 942 | if cached_data.has_key(doc.study_course): |
---|
| 943 | return cached_data[doc.study_course]['end_level'] |
---|
[2069] | 944 | certificate_res = self.portal_catalog(id = doc.study_course) |
---|
| 945 | if len(certificate_res) != 1: |
---|
| 946 | return None |
---|
| 947 | return getattr(certificate_res[0].getObject().getContent(),'end_level','unknown') |
---|
| 948 | |
---|
[2078] | 949 | def get_from_doc_level(self,doc,cached_data={}): |
---|
[1700] | 950 | "return the students level" |
---|
| 951 | if doc is None: |
---|
[1620] | 952 | return None |
---|
[1700] | 953 | return getattr(doc,'current_level',None) |
---|
[1620] | 954 | |
---|
[2078] | 955 | def get_from_doc_mode(self,doc,cached_data={}): |
---|
[1705] | 956 | "return the students mode" |
---|
[1700] | 957 | if doc is None: |
---|
[1620] | 958 | return None |
---|
[1705] | 959 | cm = getattr(doc,'current_mode',None) |
---|
| 960 | return cm |
---|
[1625] | 961 | |
---|
[1749] | 962 | |
---|
[2078] | 963 | def get_from_doc_session(self,doc,cached_data={}): |
---|
[1705] | 964 | "return the students current_session" |
---|
| 965 | if doc is None: |
---|
| 966 | return None |
---|
| 967 | return getattr(doc,'current_session',None) |
---|
| 968 | |
---|
[2078] | 969 | def get_from_doc_entry_session(self,doc,cached_data={}): |
---|
[1700] | 970 | "return the students entry_session" |
---|
| 971 | if doc is None: |
---|
[1620] | 972 | return None |
---|
[1705] | 973 | es = getattr(doc,'entry_session',None) |
---|
[1729] | 974 | if es is not None and len(es) == 2: |
---|
[1705] | 975 | return es |
---|
[1700] | 976 | try: |
---|
| 977 | digit = int(doc.jamb_reg_no[0]) |
---|
| 978 | except: |
---|
[1986] | 979 | return "-1" |
---|
[1700] | 980 | if digit < 8: |
---|
| 981 | return "0%c" % doc.jamb_reg_no[0] |
---|
| 982 | return "9%c" % doc.jamb_reg_no[0] |
---|
| 983 | |
---|
[2078] | 984 | def get_from_doc_course(self,doc,cached_data={}): |
---|
[1620] | 985 | "return the students study_course" |
---|
[1700] | 986 | if doc is None: |
---|
[1620] | 987 | return None |
---|
[1700] | 988 | return getattr(doc,'study_course',None) |
---|
[1620] | 989 | |
---|
[2078] | 990 | def get_from_doc_name(self,doc,cached_data={}): |
---|
[1620] | 991 | "return the students name from the personal" |
---|
[1700] | 992 | if doc is None: |
---|
[1620] | 993 | return None |
---|
| 994 | return "%s %s %s" % (doc.firstname,doc.middlename,doc.lastname) |
---|
| 995 | |
---|
[2078] | 996 | def get_from_doc_verdict(self,doc,cached_data={}): |
---|
[1700] | 997 | "return the students study_course" |
---|
| 998 | if doc is None: |
---|
[1620] | 999 | return None |
---|
[1700] | 1000 | return getattr(doc,'current_verdict',None) |
---|
[1702] | 1001 | ###) |
---|
[1620] | 1002 | |
---|
[1702] | 1003 | def reindexIndex(self, name, REQUEST,pghandler=None): ###( |
---|
| 1004 | if isinstance(name, str): |
---|
| 1005 | name = (name,) |
---|
[1749] | 1006 | reindextypes = {} |
---|
[1702] | 1007 | reindex_special = [] |
---|
| 1008 | for n in name: |
---|
| 1009 | if n in ("review_state","registered_courses"): |
---|
| 1010 | reindex_special.append(n) |
---|
| 1011 | else: |
---|
| 1012 | for pt in self.affected_types.keys(): |
---|
[1707] | 1013 | if n in self.affected_types[pt]['fields']: |
---|
[1702] | 1014 | if reindextypes.has_key(pt): |
---|
| 1015 | reindextypes[pt].append(n) |
---|
| 1016 | else: |
---|
| 1017 | reindextypes[pt]= [n] |
---|
| 1018 | break |
---|
[2078] | 1019 | cached_data = {} |
---|
| 1020 | if set(name).intersection(set(('faculty','department','end_level'))): |
---|
| 1021 | cached_data = self.fill_certificates_dict() |
---|
[1702] | 1022 | students = self.portal_catalog(portal_type="Student") |
---|
[1954] | 1023 | if hasattr(self,'portal_catalog_real'): |
---|
| 1024 | aq_portal = self.portal_catalog_real.evalAdvancedQuery |
---|
| 1025 | else: |
---|
| 1026 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
[1702] | 1027 | num_objects = len(students) |
---|
| 1028 | if pghandler: |
---|
| 1029 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
| 1030 | noattr = set(('StudentClearance','StudentPersonal')) & set(reindextypes.keys()) |
---|
[2084] | 1031 | #import pdb;pdb.set_trace() |
---|
[1702] | 1032 | for i in xrange(num_objects): |
---|
| 1033 | if pghandler: pghandler.report(i) |
---|
| 1034 | student_brain = students[i] |
---|
[1707] | 1035 | student_object = student_brain.getObject() |
---|
[2084] | 1036 | # query = Eq('path',student_brain.getPath()) |
---|
| 1037 | # sub_brains_list = aq_portal(query) |
---|
| 1038 | # sub_brains = {} |
---|
| 1039 | # for sub_brain in sub_brains_list: |
---|
| 1040 | # sub_brains[sub_brain.portal_type] = sub_brain |
---|
| 1041 | # student_path = student_brain.getPath() |
---|
[1702] | 1042 | data = {} |
---|
| 1043 | modified = False |
---|
| 1044 | sid = data['id'] = student_brain.getId |
---|
| 1045 | if reindex_special and 'review_state' in reindex_special: |
---|
| 1046 | modified = True |
---|
| 1047 | data['review_state'] = student_brain.review_state |
---|
[1707] | 1048 | sub_objects = False |
---|
| 1049 | for pt in reindextypes.keys(): |
---|
[1702] | 1050 | modified = True |
---|
[1707] | 1051 | try: |
---|
| 1052 | doc = getattr(student_object,self.affected_types[pt]['id']).getContent() |
---|
[2084] | 1053 | #doc = sub_brains[pt].getObject().getContent() |
---|
| 1054 | # path = "%s/%s" % (student_path,self.affected_types[pt]['id']) |
---|
| 1055 | # doc = self.unrestrictedTraverse(path).getContent() |
---|
[1707] | 1056 | sub_objects = True |
---|
| 1057 | except: |
---|
| 1058 | continue |
---|
[2084] | 1059 | for field in set(name).intersection(self.affected_types[pt]['fields']): |
---|
[1707] | 1060 | if hasattr(self,'get_from_doc_%s' % field): |
---|
[2078] | 1061 | data[field] = getattr(self,'get_from_doc_%s' % field)(doc, |
---|
| 1062 | cached_data=cached_data) |
---|
[1707] | 1063 | else: |
---|
| 1064 | data[field] = getattr(doc,field) |
---|
| 1065 | if not sub_objects and noattr: |
---|
| 1066 | import_res = self.returning_import(id = sid) |
---|
| 1067 | if not import_res: |
---|
| 1068 | continue |
---|
| 1069 | import_record = import_res[0] |
---|
| 1070 | data['matric_no'] = import_record.matric_no |
---|
| 1071 | data['sex'] = import_record.Sex == 'F' |
---|
| 1072 | data['name'] = "%s %s %s" % (import_record.Firstname, |
---|
| 1073 | import_record.Middlename, |
---|
| 1074 | import_record.Lastname) |
---|
[1815] | 1075 | data['jamb_reg_no'] = import_record.Entryregno |
---|
[2454] | 1076 | #if reindex_special and 'registered_courses' in reindex_special: |
---|
| 1077 | # try: |
---|
| 1078 | # study_course = getattr(student_object,"study_course") |
---|
| 1079 | # level_ids = study_course.objectIds() |
---|
| 1080 | # except: |
---|
| 1081 | # continue |
---|
| 1082 | # if not level_ids: |
---|
| 1083 | # continue |
---|
| 1084 | # modified = True |
---|
| 1085 | # level_ids.sort() |
---|
| 1086 | # course_ids = getattr(study_course,level_ids[-1]).objectIds() |
---|
| 1087 | # courses = [] |
---|
| 1088 | # for c in course_ids: |
---|
| 1089 | # if c.endswith('_co'): |
---|
| 1090 | # courses.append(c[:-3]) |
---|
| 1091 | # else: |
---|
| 1092 | # courses.append(c) |
---|
| 1093 | # data['registered_courses'] = courses |
---|
[1702] | 1094 | if modified: |
---|
| 1095 | self.modifyRecord(**data) |
---|
| 1096 | if pghandler: pghandler.finish() |
---|
| 1097 | ###) |
---|
[1620] | 1098 | |
---|
| 1099 | def refreshCatalog(self, clear=0, pghandler=None): ###( |
---|
| 1100 | """ re-index everything we can find """ |
---|
| 1101 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
| 1102 | if clear: |
---|
[1724] | 1103 | self._catalog.clear() |
---|
[1700] | 1104 | students = self.portal_catalog(portal_type="Student") |
---|
| 1105 | num_objects = len(students) |
---|
[2078] | 1106 | cached_data = self.fill_certificates_dict() |
---|
[1620] | 1107 | if pghandler: |
---|
| 1108 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
| 1109 | for i in xrange(num_objects): |
---|
| 1110 | if pghandler: pghandler.report(i) |
---|
[1700] | 1111 | student_brain = students[i] |
---|
| 1112 | spath = student_brain.getPath() |
---|
[1727] | 1113 | student_object = student_brain.getObject() |
---|
[1620] | 1114 | data = {} |
---|
[1700] | 1115 | sid = data['id'] = student_brain.getId |
---|
| 1116 | data['review_state'] = student_brain.review_state |
---|
[1707] | 1117 | sub_objects = False |
---|
| 1118 | for pt in self.affected_types.keys(): |
---|
| 1119 | modified = True |
---|
| 1120 | try: |
---|
| 1121 | doc = getattr(student_object,self.affected_types[pt]['id']).getContent() |
---|
| 1122 | sub_objects = True |
---|
| 1123 | except: |
---|
[1727] | 1124 | #from pdb import set_trace;set_trace() |
---|
[1707] | 1125 | continue |
---|
| 1126 | for field in self.affected_types[pt]['fields']: |
---|
| 1127 | if hasattr(self,'get_from_doc_%s' % field): |
---|
[2078] | 1128 | data[field] = getattr(self,'get_from_doc_%s' % field)(doc, |
---|
| 1129 | cached_data=cached_data) |
---|
[1707] | 1130 | else: |
---|
[1727] | 1131 | data[field] = getattr(doc,field,None) |
---|
| 1132 | if not sub_objects: |
---|
[1700] | 1133 | import_res = self.returning_import(id = sid) |
---|
| 1134 | if not import_res: |
---|
[1620] | 1135 | continue |
---|
[1700] | 1136 | import_record = import_res[0] |
---|
| 1137 | data['matric_no'] = import_record.matric_no |
---|
| 1138 | data['sex'] = import_record.Sex == 'F' |
---|
| 1139 | data['name'] = "%s %s %s" % (import_record.Firstname, |
---|
| 1140 | import_record.Middlename, |
---|
| 1141 | import_record.Lastname) |
---|
[1815] | 1142 | data['jamb_reg_no'] = import_record.Entryregno |
---|
[1700] | 1143 | self.addRecord(**data) |
---|
[1620] | 1144 | if pghandler: pghandler.finish() |
---|
| 1145 | ###) |
---|
| 1146 | |
---|
[1700] | 1147 | security.declarePrivate('notify_event_listener') ###( |
---|
[1620] | 1148 | def notify_event_listener(self,event_type,object,infos): |
---|
| 1149 | "listen for events" |
---|
[1716] | 1150 | if not infos.has_key('rpath'): |
---|
| 1151 | return |
---|
[1702] | 1152 | pt = getattr(object,'portal_type',None) |
---|
| 1153 | mt = getattr(object,'meta_type',None) |
---|
[1954] | 1154 | students_catalog = self |
---|
[1702] | 1155 | data = {} |
---|
| 1156 | if pt == 'Student' and\ |
---|
| 1157 | mt == 'CPS Proxy Folder' and\ |
---|
| 1158 | event_type.startswith('workflow'): |
---|
| 1159 | data['id'] = object.getId() |
---|
| 1160 | data['review_state'] = self.portal_workflow.getInfoFor(object,'review_state',None) |
---|
| 1161 | students_catalog.modifyRecord(**data) |
---|
| 1162 | return |
---|
[1700] | 1163 | rpl = infos['rpath'].split('/') |
---|
[2396] | 1164 | if pt == 'Student' and mt == 'CPS Proxy Folder': |
---|
[1700] | 1165 | student_id = object.id |
---|
[2396] | 1166 | if event_type == "sys_add_object": |
---|
| 1167 | try: |
---|
| 1168 | self.addRecord(id = student_id) |
---|
| 1169 | except ValueError: |
---|
| 1170 | pass |
---|
| 1171 | return |
---|
| 1172 | elif event_type == 'sys_del_object': |
---|
| 1173 | self.deleteRecord(student_id) |
---|
[1716] | 1174 | if pt not in self.affected_types.keys(): |
---|
[1700] | 1175 | return |
---|
[1716] | 1176 | if event_type not in ('sys_modify_object'): |
---|
| 1177 | return |
---|
[1700] | 1178 | if mt == 'CPS Proxy Folder': |
---|
| 1179 | return |
---|
[1716] | 1180 | for field in self.affected_types[pt]['fields']: |
---|
[1700] | 1181 | if hasattr(self,'get_from_doc_%s' % field): |
---|
| 1182 | data[field] = getattr(self,'get_from_doc_%s' % field)(object) |
---|
| 1183 | else: |
---|
| 1184 | data[field] = getattr(object,field) |
---|
| 1185 | data['id'] = rpl[2] |
---|
[1716] | 1186 | self.modifyRecord(**data) |
---|
[1700] | 1187 | ###) |
---|
[1620] | 1188 | |
---|
[1625] | 1189 | |
---|
[971] | 1190 | InitializeClass(StudentsCatalog) |
---|
| 1191 | |
---|
[1146] | 1192 | ###) |
---|
| 1193 | |
---|
| 1194 | class CoursesCatalog(WAeUPTable): ###( |
---|
[1716] | 1195 | security = ClassSecurityInfo() |
---|
[1146] | 1196 | |
---|
| 1197 | meta_type = 'WAeUP Courses Catalog' |
---|
[2094] | 1198 | name = "courses_catalog" |
---|
[1146] | 1199 | key = "code" |
---|
[2094] | 1200 | def __init__(self,name=None): |
---|
| 1201 | if name == None: |
---|
| 1202 | name = self.name |
---|
| 1203 | WAeUPTable.__init__(self, name) |
---|
[1146] | 1204 | |
---|
[1716] | 1205 | def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###( |
---|
| 1206 | """ clear the catalog, then re-index everything """ |
---|
[1146] | 1207 | |
---|
[1716] | 1208 | elapse = time.time() |
---|
| 1209 | c_elapse = time.clock() |
---|
| 1210 | |
---|
| 1211 | pgthreshold = self._getProgressThreshold() |
---|
| 1212 | handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None |
---|
| 1213 | self.refreshCatalog(clear=1, pghandler=handler) |
---|
| 1214 | |
---|
| 1215 | elapse = time.time() - elapse |
---|
| 1216 | c_elapse = time.clock() - c_elapse |
---|
| 1217 | |
---|
| 1218 | RESPONSE.redirect( |
---|
| 1219 | URL1 + |
---|
| 1220 | '/manage_catalogAdvanced?manage_tabs_message=' + |
---|
| 1221 | urllib.quote('Catalog Updated \n' |
---|
| 1222 | 'Total time: %s\n' |
---|
| 1223 | 'Total CPU time: %s' % (`elapse`, `c_elapse`))) |
---|
| 1224 | ###) |
---|
| 1225 | |
---|
| 1226 | def reindexIndex(self, name, REQUEST,pghandler=None): ###( |
---|
| 1227 | if isinstance(name, str): |
---|
| 1228 | name = (name,) |
---|
| 1229 | courses = self.portal_catalog(portal_type="Course") |
---|
| 1230 | num_objects = len(courses) |
---|
| 1231 | if pghandler: |
---|
| 1232 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
| 1233 | for i in xrange(num_objects): |
---|
| 1234 | if pghandler: pghandler.report(i) |
---|
| 1235 | course_brain = courses[i] |
---|
| 1236 | course_object = course_brain.getObject() |
---|
| 1237 | pl = course_brain.getPath().split('/') |
---|
| 1238 | data = {} |
---|
| 1239 | cid = data[self.key] = course_brain.getId |
---|
| 1240 | data['faculty'] = pl[-4] |
---|
| 1241 | data['department'] = pl[-3] |
---|
| 1242 | doc = course_object.getContent() |
---|
| 1243 | for field in name: |
---|
| 1244 | if field not in (self.key,'faculty','department'): |
---|
| 1245 | data[field] = getattr(doc,field) |
---|
| 1246 | self.modifyRecord(**data) |
---|
| 1247 | if pghandler: pghandler.finish() |
---|
| 1248 | ###) |
---|
| 1249 | |
---|
| 1250 | def refreshCatalog(self, clear=0, pghandler=None): ###( |
---|
| 1251 | """ re-index everything we can find """ |
---|
[1724] | 1252 | if clear: |
---|
| 1253 | self._catalog.clear() |
---|
[1716] | 1254 | courses = self.portal_catalog(portal_type="Course") |
---|
| 1255 | num_objects = len(courses) |
---|
| 1256 | if pghandler: |
---|
| 1257 | pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects) |
---|
[1724] | 1258 | #from pdb import set_trace;set_trace() |
---|
[1716] | 1259 | for i in xrange(num_objects): |
---|
| 1260 | if pghandler: pghandler.report(i) |
---|
| 1261 | course_brain = courses[i] |
---|
[1724] | 1262 | course_doc = course_brain.getObject().getContent() |
---|
[1716] | 1263 | pl = course_brain.getPath().split('/') |
---|
| 1264 | data = {} |
---|
[1724] | 1265 | for field in self.schema(): |
---|
[1749] | 1266 | data[field] = getattr(course_doc,field,None) |
---|
[1716] | 1267 | data[self.key] = course_brain.getId |
---|
[1724] | 1268 | ai = pl.index('academics') |
---|
| 1269 | data['faculty'] = pl[ai +1] |
---|
| 1270 | data['department'] = pl[ai +2] |
---|
| 1271 | if clear: |
---|
| 1272 | self.addRecord(**data) |
---|
| 1273 | else: |
---|
| 1274 | self.modifyRecord(**data) |
---|
[1716] | 1275 | if pghandler: pghandler.finish() |
---|
| 1276 | ###) |
---|
| 1277 | |
---|
| 1278 | security.declarePrivate('notify_event_listener') ###( |
---|
| 1279 | def notify_event_listener(self,event_type,object,infos): |
---|
| 1280 | "listen for events" |
---|
| 1281 | if not infos.has_key('rpath'): |
---|
| 1282 | return |
---|
| 1283 | pt = getattr(object,'portal_type',None) |
---|
| 1284 | mt = getattr(object,'meta_type',None) |
---|
| 1285 | if pt != 'Course': |
---|
| 1286 | return |
---|
| 1287 | data = {} |
---|
| 1288 | rpl = infos['rpath'].split('/') |
---|
| 1289 | if event_type not in ("sys_add_object","sys_modify_object","sys_del_object"): |
---|
| 1290 | return |
---|
| 1291 | course_id = object.getId() |
---|
| 1292 | data[self.key] = course_id |
---|
[1724] | 1293 | if event_type == "sys_add_object" and mt == 'CPS Proxy Folder': |
---|
[1716] | 1294 | try: |
---|
| 1295 | self.addRecord(**data) |
---|
| 1296 | except ValueError: |
---|
[1724] | 1297 | return |
---|
| 1298 | course_id = object.getId() |
---|
| 1299 | doc = object.getContent() |
---|
| 1300 | if doc is None: |
---|
| 1301 | return |
---|
| 1302 | for field in self.schema(): |
---|
[1749] | 1303 | data[field] = getattr(doc,field,None) |
---|
[1724] | 1304 | data[self.key] = course_id |
---|
| 1305 | ai = rpl.index('academics') |
---|
| 1306 | data['faculty'] = rpl[ai +1] |
---|
| 1307 | data['department'] = rpl[ai +2] |
---|
| 1308 | self.modifyRecord(**data) |
---|
| 1309 | return |
---|
[1716] | 1310 | if event_type == "sys_del_object": |
---|
| 1311 | self.deleteRecord(course_id) |
---|
[1724] | 1312 | return |
---|
[1716] | 1313 | if event_type == "sys_modify_object" and mt == 'Course': |
---|
[1724] | 1314 | #from pdb import set_trace;set_trace() |
---|
[1716] | 1315 | for field in self.schema(): |
---|
[1749] | 1316 | data[field] = getattr(object,field,None) |
---|
[1716] | 1317 | course_id = object.aq_parent.getId() |
---|
| 1318 | data[self.key] = course_id |
---|
[1724] | 1319 | ai = rpl.index('academics') |
---|
| 1320 | data['faculty'] = rpl[ai +1] |
---|
| 1321 | data['department'] = rpl[ai +2] |
---|
[1716] | 1322 | self.modifyRecord(**data) |
---|
| 1323 | ###) |
---|
| 1324 | |
---|
| 1325 | |
---|
[1146] | 1326 | InitializeClass(CoursesCatalog) |
---|
[1151] | 1327 | ###) |
---|
[1146] | 1328 | |
---|
[2084] | 1329 | class CourseResults(WAeUPTable): ###( |
---|
[2069] | 1330 | security = ClassSecurityInfo() |
---|
| 1331 | |
---|
| 1332 | meta_type = 'WAeUP Results Catalog' |
---|
| 1333 | name = "course_results" |
---|
[2084] | 1334 | key = "key" #student_id + level + course_id |
---|
[2094] | 1335 | def __init__(self,name=None): |
---|
| 1336 | if name == None: |
---|
| 1337 | name = self.name |
---|
| 1338 | WAeUPTable.__init__(self, name) |
---|
[2084] | 1339 | self._queue = [] |
---|
[2099] | 1340 | |
---|
[2094] | 1341 | def addMultipleRecords(self, records): ###( |
---|
| 1342 | """add many records""" |
---|
| 1343 | added_keys = [] |
---|
| 1344 | for data in records: |
---|
| 1345 | uid = key = "%(student_id)s|%(level_id)s|%(course_id)s" % data |
---|
| 1346 | data['%s' % self.key] = uid |
---|
| 1347 | res = self.searchResults({"%s" % self.key : uid}) |
---|
| 1348 | if len(res) > 0: |
---|
| 1349 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 1350 | self.catalog_object(dict2ob(data), uid=uid) |
---|
| 1351 | return uid |
---|
| 1352 | ###) |
---|
| 1353 | |
---|
[2434] | 1354 | def deleteResultsHere(self,level_id,student_id): ###( |
---|
| 1355 | #import pdb;pdb.set_trace() |
---|
| 1356 | query = Eq('student_id',student_id) & Eq('level_id', level_id) |
---|
| 1357 | course_results = self.course_results.evalAdvancedQuery(query) |
---|
| 1358 | for result in course_results: |
---|
| 1359 | self.deleteRecord(result.key) |
---|
[2084] | 1360 | ###) |
---|
| 1361 | |
---|
[2434] | 1362 | def moveResultsHere(self,level,student_id): ###( |
---|
| 1363 | #import pdb;pdb.set_trace() |
---|
| 1364 | level_id = level.getId() |
---|
| 1365 | query = Eq('student_id',student_id) & Eq('level_id', level_id) |
---|
| 1366 | course_results = self.course_results.evalAdvancedQuery(query) |
---|
[2437] | 1367 | existing_courses = [cr.code for cr in course_results] |
---|
[2434] | 1368 | to_delete = [] |
---|
| 1369 | for code,obj in level.objectItems(): |
---|
[2437] | 1370 | to_delete.append(code) |
---|
[2434] | 1371 | carry_over = False |
---|
| 1372 | if code.endswith('_co'): |
---|
| 1373 | carry_over = True |
---|
[2437] | 1374 | code = code[:-3] |
---|
[2434] | 1375 | if code in existing_courses: |
---|
[2094] | 1376 | continue |
---|
[2434] | 1377 | course_result_doc = obj.getContent() |
---|
[2094] | 1378 | data = {} |
---|
[2434] | 1379 | course_id = code |
---|
[2094] | 1380 | for field in self.schema(): |
---|
| 1381 | data[field] = getattr(course_result_doc,field,'') |
---|
| 1382 | data['key'] = key = "%(student_id)s|%(level_id)s|%(course_id)s" % vars() |
---|
[2099] | 1383 | data['student_id'] = student_id |
---|
| 1384 | data['level_id'] = level_id |
---|
[2439] | 1385 | session_id = self.getLevelSession(level.getContent(),student_id,level_id) |
---|
[2442] | 1386 | data['session_id'] = session_id |
---|
[2434] | 1387 | #data['queue_status'] = OBJECT_CREATED |
---|
[2099] | 1388 | data['code'] = course_id |
---|
[2434] | 1389 | data['carry_over'] = carry_over |
---|
[2094] | 1390 | self.catalog_object(dict2ob(data), uid=key) |
---|
[2434] | 1391 | level.manage_delObjects(to_delete) |
---|
| 1392 | ###) |
---|
| 1393 | |
---|
| 1394 | def getCourses(self,student_id,level_id): ###( |
---|
[2094] | 1395 | query = Eq('student_id',student_id) & Eq('level_id', level_id) |
---|
[2434] | 1396 | course_results = self.course_results.evalAdvancedQuery(query) |
---|
| 1397 | carry_overs = [] |
---|
[2606] | 1398 | normal1 = [] |
---|
| 1399 | normal2 = [] |
---|
[2761] | 1400 | normal3 = [] |
---|
[2757] | 1401 | total_credits = 0 |
---|
| 1402 | gpa = 0 |
---|
[2434] | 1403 | for brain in course_results: |
---|
| 1404 | d = {} |
---|
[2781] | 1405 | |
---|
[2761] | 1406 | for field in self.schema(): |
---|
| 1407 | d[field] = getattr(brain,field,'') |
---|
| 1408 | |
---|
| 1409 | d['weight'] = '' |
---|
| 1410 | d['grade'] = '' |
---|
| 1411 | d['score'] = '' |
---|
| 1412 | |
---|
[2750] | 1413 | if str(brain.credits).isdigit(): |
---|
[2757] | 1414 | credits = int(brain.credits) |
---|
| 1415 | total_credits += credits |
---|
[2780] | 1416 | score = getattr(brain,'score',0) |
---|
| 1417 | if score and str(score).isdigit() and int(score) > 0: |
---|
| 1418 | score = int(score) |
---|
[2757] | 1419 | grade,weight = self.getGradesFromScore(score) |
---|
[2781] | 1420 | gpa += weight * credits |
---|
| 1421 | d['weight'] = weight |
---|
| 1422 | d['grade'] = grade |
---|
| 1423 | d['score'] = score |
---|
[2757] | 1424 | d['coe'] = '' |
---|
[2434] | 1425 | if brain.core_or_elective: |
---|
| 1426 | d['coe'] = 'Core' |
---|
[2757] | 1427 | elif brain.core_or_elective == False: |
---|
| 1428 | d['coe'] = 'Elective' |
---|
[2434] | 1429 | id = code = d['id'] = brain.code |
---|
| 1430 | d['code'] = code |
---|
[2864] | 1431 | res = self.courses_catalog.evalAdvancedQuery(Eq('code',code)) |
---|
| 1432 | if res: |
---|
| 1433 | course = res[0] |
---|
| 1434 | d['title'] = course.title |
---|
| 1435 | # The courses_catalog contains strings and integers in its semester field. |
---|
| 1436 | # Maybe this can be fixed by reindexing the catalog. The schema of course says 'CPS Int Field'. |
---|
| 1437 | d['semester'] = str(course.semester) |
---|
| 1438 | else: |
---|
[2866] | 1439 | d['title'] = "Course has been removed from course list" |
---|
[2864] | 1440 | d['semester'] = '' |
---|
[2448] | 1441 | if brain.carry_over: |
---|
[2761] | 1442 | d['coe'] = 'CO' |
---|
[2434] | 1443 | carry_overs.append(d) |
---|
| 1444 | else: |
---|
[2614] | 1445 | if d['semester'] == '1': |
---|
[2606] | 1446 | normal1.append(d) |
---|
[2614] | 1447 | |
---|
| 1448 | elif d['semester'] == '2': |
---|
[2606] | 1449 | normal2.append(d) |
---|
| 1450 | else: |
---|
| 1451 | normal3.append(d) |
---|
| 1452 | #normal.sort(cmp=lambda x,y: cmp("%(semester)s%(code)s" % x, |
---|
| 1453 | # "%(semester)s%(code)s" % y)) |
---|
[2503] | 1454 | carry_overs.sort(cmp=lambda x,y: cmp("%(semester)s%(code)s" % x, |
---|
[2460] | 1455 | "%(semester)s%(code)s" % y)) |
---|
[2757] | 1456 | return total_credits,gpa,carry_overs,normal1,normal2,normal3 |
---|
[2094] | 1457 | ###) |
---|
| 1458 | |
---|
[2084] | 1459 | InitializeClass(CourseResults) |
---|
[2069] | 1460 | ###) |
---|
| 1461 | |
---|
[1625] | 1462 | class OnlinePaymentsImport(WAeUPTable): ###( |
---|
[1620] | 1463 | |
---|
| 1464 | meta_type = 'WAeUP Online Payment Transactions' |
---|
[1625] | 1465 | name = "online_payments_import" |
---|
[1620] | 1466 | key = "order_id" |
---|
[2094] | 1467 | def __init__(self,name=None): |
---|
| 1468 | if name == None: |
---|
| 1469 | name = self.name |
---|
| 1470 | WAeUPTable.__init__(self, name) |
---|
[1620] | 1471 | |
---|
| 1472 | |
---|
[2069] | 1473 | InitializeClass(OnlinePaymentsImport) |
---|
[1620] | 1474 | ###) |
---|
| 1475 | |
---|
[1151] | 1476 | class ReturningImport(WAeUPTable): ###( |
---|
[1146] | 1477 | |
---|
[1151] | 1478 | meta_type = 'Returning Import Table' |
---|
| 1479 | name = "returning_import" |
---|
[1146] | 1480 | key = "matric_no" |
---|
[2094] | 1481 | def __init__(self,name=None): |
---|
| 1482 | if name == None: |
---|
| 1483 | name = self.name |
---|
| 1484 | WAeUPTable.__init__(self, name) |
---|
[1146] | 1485 | |
---|
| 1486 | |
---|
[1151] | 1487 | InitializeClass(ReturningImport) |
---|
| 1488 | ###) |
---|
[1146] | 1489 | |
---|
| 1490 | class ResultsImport(WAeUPTable): ###( |
---|
| 1491 | |
---|
| 1492 | meta_type = 'Results Import Table' |
---|
| 1493 | name = "results_import" |
---|
| 1494 | key = "key" |
---|
[2094] | 1495 | def __init__(self,name=None): |
---|
| 1496 | if name == None: |
---|
| 1497 | name = self.name |
---|
| 1498 | WAeUPTable.__init__(self, name) |
---|
[1146] | 1499 | |
---|
| 1500 | |
---|
| 1501 | InitializeClass(ResultsImport) |
---|
| 1502 | |
---|
| 1503 | ###) |
---|
| 1504 | |
---|
| 1505 | class PaymentsCatalog(WAeUPTable): ###( |
---|
[2738] | 1506 | security = ClassSecurityInfo() |
---|
[1146] | 1507 | |
---|
| 1508 | meta_type = 'WAeUP Payments Catalog' |
---|
[2868] | 1509 | name = "payments_catalog" |
---|
| 1510 | key = "order_id" |
---|
[2094] | 1511 | def __init__(self,name=None): |
---|
| 1512 | if name == None: |
---|
| 1513 | name = self.name |
---|
| 1514 | WAeUPTable.__init__(self, name) |
---|
[1146] | 1515 | |
---|
[2859] | 1516 | |
---|
[2738] | 1517 | security.declarePrivate('notify_event_listener') ###( |
---|
| 1518 | def notify_event_listener(self,event_type,object,infos): |
---|
| 1519 | "listen for events" |
---|
| 1520 | if not infos.has_key('rpath'): |
---|
| 1521 | return |
---|
| 1522 | pt = getattr(object,'portal_type',None) |
---|
| 1523 | mt = getattr(object,'meta_type',None) |
---|
| 1524 | data = {} |
---|
[2904] | 1525 | if pt != 'Payment': |
---|
| 1526 | return |
---|
| 1527 | if mt == 'CPS Proxy Folder': |
---|
[2911] | 1528 | return # is handled only for the real object |
---|
| 1529 | if event_type == 'sys_del_object': |
---|
| 1530 | self.deleteRecord(object.order_id) |
---|
[2738] | 1531 | if event_type not in ('sys_modify_object'): |
---|
| 1532 | return |
---|
| 1533 | for field in self.schema(): |
---|
[2859] | 1534 | data[field] = getattr(object,field,'') |
---|
[2738] | 1535 | rpl = infos['rpath'].split('/') |
---|
[2904] | 1536 | #import pdb;pdb.set_trace() |
---|
| 1537 | student_id = rpl[-4] |
---|
[2738] | 1538 | data['student_id'] = student_id |
---|
[2907] | 1539 | modified = False |
---|
[2859] | 1540 | try: |
---|
| 1541 | self.modifyRecord(**data) |
---|
[2907] | 1542 | modified = True |
---|
[2859] | 1543 | except KeyError: |
---|
[2926] | 1544 | #logger = logging.getLogger('WAeUPTables.PaymentsCatalog.%s' % self.__name__) |
---|
| 1545 | #logger.info("could not modify entry for %(student_id)s with %(order_id)s" % data) |
---|
| 1546 | pass |
---|
[2907] | 1547 | if not modified: |
---|
| 1548 | try: |
---|
| 1549 | self.addRecord(**data) |
---|
| 1550 | except: |
---|
[2976] | 1551 | logger = logging.getLogger('WAeUPTables.PaymentsCatalog.notify_event_listener') |
---|
[2907] | 1552 | logger.info("could not add or modify entry for %(student_id)s with %(order_id)s" % data) |
---|
| 1553 | ###) |
---|
[1146] | 1554 | |
---|
[2738] | 1555 | |
---|
[1146] | 1556 | InitializeClass(PaymentsCatalog) |
---|
| 1557 | |
---|
| 1558 | ###) |
---|
| 1559 | |
---|
[414] | 1560 | # BBB: |
---|
| 1561 | AccomodationTable = AccommodationTable |
---|