source: WAeUP_SRP/base/WAeUPTables.py @ 3429

Last change on this file since 3429 was 3408, checked in by Henrik Bettermann, 17 years ago

enable bed change for reserved beds

  • Property svn:keywords set to Id
File size: 64.0 KB
RevLine 
[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 3408 2008-04-02 16:07:32Z henrik $
20
21from zope.interface import implements
22from Globals import InitializeClass
23from Products.ZCatalog.ZCatalog import ZCatalog
[1620]24from Products.ZCatalog.ProgressHandler import ZLogHandler
[780]25from AccessControl import ClassSecurityInfo
26from Products.CMFCore.permissions import ModifyPortalContent
[2094]27from Products.CMFCore.utils import getToolByName
28from Products.CMFCore.CatalogTool import CatalogTool
29from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter
30from Products.CPSSchemas.DataStructure import DataStructure
31from Products.CPSSchemas.DataModel import DataModel
32from Products.AdvancedQuery import Eq, Between, Le,In
[1700]33import urllib
[1620]34import DateTime,time
[780]35import csv,re
36import logging
37import Globals
38p_home = Globals.package_home(globals())
39i_home = Globals.INSTANCE_HOME
40
[2084]41ADDING_SHEDULED = "adding_sheduled"
42OBJECT_CREATED = "object_created"
[2845]43NOT_OCCUPIED = 'not_occupied'
[2084]44
[363]45from interfaces import IWAeUPTable
46
47class AttributeHolder(object):
48    pass
49
50def dict2ob(dict):
51    ob = AttributeHolder()
52    for key, value in dict.items():
53        setattr(ob, key, value)
54    return ob
55
[1146]56class 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]479class AccommodationTable(WAeUPTable): ###(
[834]480
[404]481    meta_type = 'WAeUP Accommodation Tool'
[2094]482    name = "portal_accommodation"
[502]483    key = "bed"
[3043]484    not_occupied = NOT_OCCUPIED
[2094]485    def __init__(self,name=None):
486        if name ==  None:
487            name = self.name
488        WAeUPTable.__init__(self, name)
[2866]489
[3043]490    def searchAndReserveBed(self, student_id,bed_type): ###(
[3406]491        logger = logging.getLogger('WAeUPTables.AccommodationTable.searchAndReserveBed')
[2845]492        records = self.evalAdvancedQuery(Eq('student',student_id))
[3406]493        if len(records) == 1:
494            #return -1,"Student with Id %s already booked bed %s." % (student_id,records[0].bed)
495            logger.info('%s found (reserved) bed %s' % (student_id,records[0].bed))
496            return -1,records[0].bed
497        elif len(records) > 1:
498            logger.info('%s found more than one (reserved) bed' % (student_id))
[3408]499            return -3,'more than one bed'
[2845]500        query = Eq('bed_type',bed_type) & Eq('student',NOT_OCCUPIED)
501        records = self.evalAdvancedQuery(query,sortSpecs=('sort_id','bed'))
[635]502        if len(records) == 0:
[3408]503            logger.info('no bed %s available for %s' % (bed_type,student_id))
[3406]504            return -2,"no bed"
[635]505        rec = records[0]
506        self.modifyRecord(bed=rec.bed,student=student_id)
[3406]507        logger.info('%s booked bed %s' % (student_id,rec.bed))
[635]508        return 1,rec.bed
[3043]509    ###)
[363]510
[834]511
[404]512InitializeClass(AccommodationTable)
[411]513
[1146]514###)
515
516class PinTable(WAeUPTable): ###(
[1030]517    from ZODB.POSException import ConflictError
[2973]518    security = ClassSecurityInfo()
[440]519    meta_type = 'WAeUP Pin Tool'
[2094]520    name = "portal_pins"
[502]521    key = 'pin'
[2859]522
[2094]523    def __init__(self,name=None):
524        if name ==  None:
525            name = self.name
526        WAeUPTable.__init__(self, name)
[1082]527
[2973]528    security.declareProtected(ModifyPortalContent,"dumpAll")###(
529    def dumpAll(self,include_unused=None):
530        """dump all data in the table to a csv"""
531        member = self.portal_membership.getAuthenticatedMember()
[2974]532        logger = logging.getLogger('WAeUPTables.PinTable.dumpAll')
[2973]533        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
534        export_file = "%s/export/%s_%s.csv" % (i_home,self.__name__,current,)
535        res_list = []
536        lines = []
537        if hasattr(self,"export_keys"):
538            fields = self.export_keys
539        else:
540            fields = []
541            for f in self.schema():
542                fields.append(f)
543        headline = ','.join(fields)
544        out = open(export_file,"wb")
545        out.write(headline +'\n')
546        out.close()
547        out = open(export_file,"a")
548        csv_writer = csv.DictWriter(out,fields,)
549        if include_unused is not None and str(member) not in ('admin','joachim'):
550            logger.info('%s tries to dump pintable with unused pins' % (member))
551            return
552        if include_unused is not None:
553            records = self()
554        else:
555            records = self.evalAdvancedQuery(~Eq('student',''))
556        nr2export = len(records)
557        logger.info('%s starts dumping, %s records to export' % (member,nr2export))
558        chunk = 2000
559        total = 0
560        start = DateTime.DateTime().timeTime()
561        start_chunk = DateTime.DateTime().timeTime()
562        for record in records:
563            not_all = False
564            d = self.record2dict(fields,record)
565            lines.append(d)
566            total += 1
567            if total and not total % chunk or total == len(records):
568                csv_writer.writerows(lines)
569                anz = len(lines)
570                logger.info("wrote %(anz)d  total written %(total)d" % vars())
571                end_chunk = DateTime.DateTime().timeTime()
572                duration = end_chunk-start_chunk
573                per_record = duration/anz
574                till_now = end_chunk - start
575                avarage_per_record = till_now/total
576                estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export)
577                estimated_end = estimated_end.strftime("%H:%M:%S")
578                logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars())
579                start_chunk = DateTime.DateTime().timeTime()
580                lines = []
581        end = DateTime.DateTime().timeTime()
582        logger.info('total time %6.2f m' % ((end-start)/60))
583        import os
584        filename, extension = os.path.splitext(export_file)
585        from subprocess import call
586        msg = "wrote %(total)d records to %(export_file)s" % vars()
587        #try:
588        #    retcode = call('gzip %s' % (export_file),shell=True)
589        #    if retcode == 0:
590        #        msg = "wrote %(total)d records to %(export_file)s.gz" % vars()
591        #except OSError, e:
592        #    retcode = -99
593        #    logger.info("zip failed with %s" % e)
594        logger.info(msg)
595        args = {'portal_status_message': msg}
596        #url = self.REQUEST.get('URL1') + '?' + urlencode(args)
597        url = self.REQUEST.get('URL2')
598        return self.REQUEST.RESPONSE.redirect(url)
599    ###)
[1082]600
[2973]601
602
[710]603    def searchAndSetRecord(self, uid, student_id,prefix):
[2191]604
605        # The following line must be activated after resetting the
606        # the portal_pins table. This is to avoid duplicate entries
[2184]607        # and disable duplicate payments.
[2191]608
[2184]609        #student_id = student_id.upper()
610
[2716]611        #records = self.searchResults(student = student_id)
[2579]612        #if len(records) > 0 and prefix in ('CLR','APP'):
613        #    for r in records:
614        #        if r.pin != uid and r.prefix_batch.startswith(prefix):
615        #            return -2
[502]616        records = self.searchResults({"%s" % self.key : uid})
617        if len(records) > 1:
618            # Can not happen, but anyway...
619            raise ValueError("More than one record with uid %s" % uid)
620        if len(records) == 0:
[2766]621            return -1,None
[502]622        record = records[0]
623        if record.student == "":
624            record_data = {}
625            for field in self.schema() + self.indexes():
626                record_data[field] = getattr(record, field)
627            # Add the updated data:
[635]628            record_data['student'] = student_id
[1030]629            try:
630                self.catalog_object(dict2ob(record_data), uid)
[2766]631                return 1,record
[1030]632            except ConflictError:
[2766]633                return 2,record
[990]634        if record.student.upper() != student_id.upper():
[2766]635            return 0,record
[997]636        if record.student.upper() == student_id.upper():
[2766]637            return 2,record
638        return -3,record
[440]639InitializeClass(PinTable)
[1146]640###)
[966]641
[1146]642class PumeResultsTable(WAeUPTable): ###(
643
[966]644    meta_type = 'WAeUP PumeResults Tool'
[2094]645    name = "portal_pumeresults"
[966]646    key = "jamb_reg_no"
[2094]647    def __init__(self,name=None):
648        if name ==  None:
649            name = self.name
650        WAeUPTable.__init__(self, name)
[966]651
652
653InitializeClass(PumeResultsTable)
654
[1146]655###)
[971]656
[2094]657class ApplicantsCatalog(WAeUPTable): ###(
658
[2113]659    meta_type = 'WAeUP Applicants Catalog'
[2094]660    name = "applicants_catalog"
661    key = "reg_no"
662    security = ClassSecurityInfo()
[2537]663    #export_keys = (
664    #               "reg_no",
665    #               "status",
666    #               "lastname",
667    #               "sex",
668    #               "date_of_birth",
669    #               "lga",
670    #               "email",
671    #               "phone",
672    #               "passport",
673    #               "entry_mode",
674    #               "pin",
675    #               "screening_type",
676    #               "registration_date",
677    #               "testdate",
678    #               "application_date",
679    #               "screening_date",
680    #               "faculty",
681    #               "department",
682    #               "course1",
683    #               "course2",
684    #               "course3",
685    #               "eng_score",
686    #               "subj1",
687    #               "subj1score",
688    #               "subj2",
689    #               "subj2score",
690    #               "subj3",
691    #               "subj3score",
692    #               "aggregate",
693    #               "course_admitted",
694    #               )
[2632]695
[2094]696    def __init__(self,name=None):
697        if name ==  None:
698            name = self.name
699        WAeUPTable.__init__(self, name)
700
[2185]701    security.declareProtected(ModifyPortalContent,"new_importCSV")###(
702    def new_importCSV(self,filename="JAMB_data",
703                  schema_id="application",
[2503]704                  layout_id="import_application",
[2185]705                  mode='add'):
706        """ import JAMB data """
707        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
708        pm = self.portal_membership
709        member = pm.getAuthenticatedMember()
710        logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV')
711        lock_fn = "%s/import/%s_import_lock" % (i_home,filename)
712        import_fn = "%s/import/%s.csv" % (i_home,filename)
713        if mode not in ('add','edit'):
714            logger.info("invalid mode: %s" % mode)
715        if os.path.exists(lock_fn):
716            logger.info("import of %(import_fn)s already in progress" % vars())
717            return
718        lock_file = open(lock_fn,"w")
719        lock_file.write("%(current)s \n" % vars())
720        lock_file.close()
721        invalid_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current)
722        duplicate_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current)
723        stool = getToolByName(self, 'portal_schemas')
724        ltool = getToolByName(self, 'portal_layouts')
725        schema = stool._getOb(schema_id)
726        if schema is None:
727            em = 'No such schema %s' % schema_id
728            logger.error(em)
729            return
730        for postfix in ('_import',''):
731            layout_name = "%(layout_id)s%(postfix)s" % vars()
732            if hasattr(ltool,layout_name):
733                break
734        layout = ltool._getOb(layout_name)
735        if layout is None:
736            em = 'No such layout %s' % layout_id
737            logger.error(em)
738            return
739        try:
740            csv_file = csv.DictReader(open(import_fn,"rb"))
741        except:
742            em = 'Error reading %s.csv' % filename
743            logger.error(em)
[2191]744            return
[2185]745        d = self._import_new(csv_items,schema,layout,mode,logger)
746        imported = []
747        edited = []
748        duplicates = []
749        not_found = []
750        if len(d['valid_records']) > 0:
751            for record in d['valid_records']:
752                #import pdb;pdb.set_trace()
753                if mode == "add":
754                    try:
755                        self.addRecord(**dict(record.items()))
756                        imported.append(**dict(record.items()))
757                        logger.info("added %s" % record.items())
758                    except ValueError:
759                        dupplicate.append(**dict(record.items()))
760                        logger.info("duplicate %s" % record.items())
761                elif mode == "edit":
762                    try:
763                        self.modifyRecord(**dict(record.items()))
764                        edited.append(**dict(record.items()))
765                        logger.info("edited %s" % record.items())
766                    except KeyError:
767                        not_found.append(**dict(record.items()))
768                        logger.info("not found %s" % record.items())
769        invalid = d['invalid_records']
770        for itype in ("imported","edited","not_found","duplicate","invalid"):
771            outlist = locals[itype]
772            if len(outlist):
773                d = {}
774                for k in outlist[0].keys():
775                    d[k] = k
[2191]776                outlist[0] = d
[2185]777                outfile = open("file_name_%s" % itype,'w')
778                csv.DictWriter(outfile,outlist[0].keys()).writerows(outlist)
779                logger.info("wrote %(itype)s records to %(, written to %(not_imported_fn)s" % d)
780###)
781
[2094]782    security.declareProtected(ModifyPortalContent,"importCSV")###(
783    def importCSV(self,filename="JAMB_data",
784                  schema_id="application",
[2508]785                  layout_id="application_pce",
[2094]786                  mode='add'):
787        """ import JAMB data """
788        stool = getToolByName(self, 'portal_schemas')
789        ltool = getToolByName(self, 'portal_layouts')
790        schema = stool._getOb(schema_id)
791        if schema is None:
792            em = 'No such schema %s' % schema_id
793            logger.error(em)
794            return
795        layout = ltool._getOb(layout_id)
796        if layout is None:
797            em = 'No such layout %s' % layout_id
798            logger.error(em)
799            return
[2099]800        logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV')
[2185]801        d = self._import_old(filename,schema,layout,mode,logger)
[2094]802        if len(d['valid_records']) > 0:
803            for record in d['valid_records']:
804                #import pdb;pdb.set_trace()
805                if mode == "add":
806                    self.addRecord(**dict(record.items()))
807                    logger.info("added %s" % record.items())
808                elif mode == "edit":
809                    self.modifyRecord(**dict(record.items()))
810                    logger.info("edited %s" % record.items())
811                else:
812                    logger.info("invalid mode: %s" % mode)
813        logger.info("%(mode)sed %(imported)d records, invalid written to %(not_imported_fn)s" % d)
[2632]814    ###)
[2094]815
816InitializeClass(ApplicantsCatalog)
817
818###)
819
[1146]820class StudentsCatalog(WAeUPTable): ###(
[1620]821    security = ClassSecurityInfo()
[1146]822
[971]823    meta_type = 'WAeUP Students Catalog'
824    name = "students_catalog"
825    key = "id"
[1700]826    affected_types = {   ###(
[1749]827                      'StudentApplication':
[2069]828                      {'id': 'application',
829                       'fields':
830                       ('jamb_reg_no',
831                        'entry_mode',
832                        #'entry_level',
833                        'entry_session',
834                       )
835                      },
[1700]836                      'StudentClearance':
[2069]837                      {'id': 'clearance',
838                       'fields':
839                       ('matric_no',
840                        'lga',
841                       )
842                      },
843                      'StudentPersonal':
844                      {'id': 'personal',
845                       'fields':
846                       ('name',
847                        'sex',
848                        'perm_address',
849                        'email',
850                        'phone',
851                       )
852                      },
853                      'StudentStudyCourse':
854                      {'id': 'study_course',
855                       'fields':
856                       ('course', # study_course
857                        'faculty', # from certificate
858                        'department', # from certificate
859                        'end_level', # from certificate
860                        'level', # current_level
861                        'mode',  # current_mode
862                        'session', # current_session
863                        'verdict', # current_verdict
864                       )
865                      },
866                     }
[1700]867    ###)
[1625]868
[2094]869    def __init__(self,name=None):
870        if name ==  None:
871            name = self.name
872        WAeUPTable.__init__(self, name)
[1620]873        return
[1625]874
[1700]875    def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None):
876        """ clears the whole enchilada """
877        self._catalog.clear()
[971]878
[1700]879        if REQUEST and RESPONSE:
880            RESPONSE.redirect(
881              URL1 +
882              '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Cleared')
[971]883
[1700]884    def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###(
885        """ clear the catalog, then re-index everything """
886
887        elapse = time.time()
888        c_elapse = time.clock()
889
890        pgthreshold = self._getProgressThreshold()
891        handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None
892        self.refreshCatalog(clear=1, pghandler=handler)
893
894        elapse = time.time() - elapse
895        c_elapse = time.clock() - c_elapse
896
897        RESPONSE.redirect(
898            URL1 +
899            '/manage_catalogAdvanced?manage_tabs_message=' +
900            urllib.quote('Catalog Updated \n'
901                         'Total time: %s\n'
902                         'Total CPU time: %s' % (`elapse`, `c_elapse`)))
903    ###)
904
[2084]905    def fill_certificates_dict(self): ###(
[2078]906        "return certificate data in  dict"
907        certificates_brains = self.portal_catalog(portal_type ='Certificate')
908        d = {}
909        for cb in certificates_brains:
910            certificate_doc = cb.getObject().getContent()
911            cb_path = cb.getPath().split('/')
912            ld = {}
913            ld['faculty'] = cb_path[-4]
914            ld['department'] = cb_path[-3]
915            ld['end_level'] = getattr(certificate_doc,'end_level','999')
[3348]916            ld['study_mode'] = getattr(certificate_doc,'study_mode','')
[2078]917            d[cb.getId] = ld
918        return d
[2084]919    ###)
920
[2078]921    def get_from_doc_department(self,doc,cached_data={}): ###(
[1620]922        "return the students department"
[1700]923        if doc is None:
[1620]924            return None
[3354]925        if hasattr(self,"_v_certificates") and self._v_certificates.has_key(doc.study_course):
[3348]926            return self._v_certificates[doc.study_course]['department']
[1700]927        certificate_res = self.portal_catalog(id = doc.study_course)
[1620]928        if len(certificate_res) != 1:
929            return None
930        return certificate_res[0].getPath().split('/')[-3]
931
[2078]932    def get_from_doc_faculty(self,doc,cached_data={}):
[1700]933        "return the students faculty"
934        if doc is None:
[1620]935            return None
[3354]936        if hasattr(self,"_v_certificates") and self._v_certificates.has_key(doc.study_course):
[3348]937            return self._v_certificates[doc.study_course]['faculty']
[1700]938        certificate_res = self.portal_catalog(id = doc.study_course)
939        if len(certificate_res) != 1:
940            return None
941        return certificate_res[0].getPath().split('/')[-4]
[1620]942
[2099]943    def get_from_doc_end_level(self,doc,cached_data={}):
[2069]944        "return the students end_level"
945        if doc is None:
946            return None
[3354]947        if hasattr(self,"_v_certificates") and self._v_certificates.has_key(doc.study_course):
[3348]948            return self._v_certificates[doc.study_course]['end_level']
[2069]949        certificate_res = self.portal_catalog(id = doc.study_course)
950        if len(certificate_res) != 1:
951            return None
952        return getattr(certificate_res[0].getObject().getContent(),'end_level','unknown')
953
[2078]954    def get_from_doc_level(self,doc,cached_data={}):
[1700]955        "return the students level"
956        if doc is None:
[1620]957            return None
[1700]958        return getattr(doc,'current_level',None)
[1620]959
[2078]960    def get_from_doc_mode(self,doc,cached_data={}):
[1705]961        "return the students mode"
[1700]962        if doc is None:
[1620]963            return None
[1705]964        cm = getattr(doc,'current_mode',None)
965        return cm
[1625]966
[1749]967
[2078]968    def get_from_doc_session(self,doc,cached_data={}):
[1705]969        "return the students current_session"
970        if doc is None:
971            return None
972        return getattr(doc,'current_session',None)
973
[2078]974    def get_from_doc_entry_session(self,doc,cached_data={}):
[1700]975        "return the students entry_session"
976        if doc is None:
[1620]977            return None
[1705]978        es = getattr(doc,'entry_session',None)
[1729]979        if es is not None and len(es) == 2:
[1705]980            return es
[1700]981        try:
982            digit = int(doc.jamb_reg_no[0])
983        except:
[1986]984            return "-1"
[1700]985        if digit < 8:
986            return "0%c" % doc.jamb_reg_no[0]
987        return "9%c" % doc.jamb_reg_no[0]
988
[2078]989    def get_from_doc_course(self,doc,cached_data={}):
[1620]990        "return the students study_course"
[1700]991        if doc is None:
[1620]992            return None
[1700]993        return getattr(doc,'study_course',None)
[1620]994
[2078]995    def get_from_doc_name(self,doc,cached_data={}):
[1620]996        "return the students name from the personal"
[1700]997        if doc is None:
[1620]998            return None
999        return "%s %s %s" % (doc.firstname,doc.middlename,doc.lastname)
1000
[2078]1001    def get_from_doc_verdict(self,doc,cached_data={}):
[1700]1002        "return the students study_course"
1003        if doc is None:
[1620]1004            return None
[1700]1005        return getattr(doc,'current_verdict',None)
[1702]1006    ###)
[1620]1007
[1702]1008    def reindexIndex(self, name, REQUEST,pghandler=None): ###(
1009        if isinstance(name, str):
1010            name = (name,)
[1749]1011        reindextypes = {}
[1702]1012        reindex_special = []
1013        for n in name:
1014            if n in ("review_state","registered_courses"):
1015                reindex_special.append(n)
1016            else:
1017                for pt in self.affected_types.keys():
[1707]1018                    if n in self.affected_types[pt]['fields']:
[1702]1019                        if reindextypes.has_key(pt):
1020                            reindextypes[pt].append(n)
1021                        else:
1022                            reindextypes[pt]= [n]
1023                        break
[2078]1024        cached_data = {}
1025        if set(name).intersection(set(('faculty','department','end_level'))):
1026            cached_data = self.fill_certificates_dict()
[1702]1027        students = self.portal_catalog(portal_type="Student")
[1954]1028        if hasattr(self,'portal_catalog_real'):
1029            aq_portal = self.portal_catalog_real.evalAdvancedQuery
1030        else:
1031            aq_portal = self.portal_catalog.evalAdvancedQuery
[1702]1032        num_objects = len(students)
1033        if pghandler:
1034            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
1035        noattr = set(('StudentClearance','StudentPersonal')) & set(reindextypes.keys())
[2084]1036        #import pdb;pdb.set_trace()
[1702]1037        for i in xrange(num_objects):
1038            if pghandler: pghandler.report(i)
1039            student_brain = students[i]
[1707]1040            student_object = student_brain.getObject()
[2084]1041            # query = Eq('path',student_brain.getPath())
1042            # sub_brains_list = aq_portal(query)
1043            # sub_brains = {}
1044            # for sub_brain in sub_brains_list:
1045            #     sub_brains[sub_brain.portal_type] = sub_brain
1046            # student_path = student_brain.getPath()
[1702]1047            data = {}
1048            modified = False
1049            sid = data['id'] = student_brain.getId
1050            if reindex_special and 'review_state' in reindex_special:
1051                modified = True
1052                data['review_state'] = student_brain.review_state
[1707]1053            sub_objects = False
1054            for pt in reindextypes.keys():
[1702]1055                modified = True
[1707]1056                try:
1057                    doc = getattr(student_object,self.affected_types[pt]['id']).getContent()
[2084]1058                    #doc = sub_brains[pt].getObject().getContent()
1059                    # path = "%s/%s" % (student_path,self.affected_types[pt]['id'])
1060                    # doc = self.unrestrictedTraverse(path).getContent()
[1707]1061                    sub_objects = True
1062                except:
1063                    continue
[2084]1064                for field in set(name).intersection(self.affected_types[pt]['fields']):
[1707]1065                    if hasattr(self,'get_from_doc_%s' % field):
[2078]1066                        data[field] = getattr(self,'get_from_doc_%s' % field)(doc,
1067                                                                              cached_data=cached_data)
[1707]1068                    else:
1069                        data[field] = getattr(doc,field)
1070            if not sub_objects and noattr:
1071                import_res = self.returning_import(id = sid)
1072                if not import_res:
1073                    continue
1074                import_record = import_res[0]
1075                data['matric_no'] = import_record.matric_no
1076                data['sex'] = import_record.Sex == 'F'
1077                data['name'] = "%s %s %s" % (import_record.Firstname,
1078                                             import_record.Middlename,
1079                                             import_record.Lastname)
[1815]1080                data['jamb_reg_no'] = import_record.Entryregno
[2454]1081            #if reindex_special and 'registered_courses' in reindex_special:
1082            #    try:
1083            #        study_course = getattr(student_object,"study_course")
1084            #        level_ids = study_course.objectIds()
1085            #    except:
1086            #        continue
1087            #    if not level_ids:
1088            #        continue
1089            #    modified = True
1090            #    level_ids.sort()
1091            #    course_ids = getattr(study_course,level_ids[-1]).objectIds()
1092            #    courses = []
1093            #    for c in course_ids:
1094            #        if c.endswith('_co'):
1095            #            courses.append(c[:-3])
1096            #        else:
1097            #            courses.append(c)
1098            #    data['registered_courses'] = courses
[1702]1099            if modified:
1100                self.modifyRecord(**data)
1101        if pghandler: pghandler.finish()
1102    ###)
[1620]1103
1104    def refreshCatalog(self, clear=0, pghandler=None): ###(
1105        """ re-index everything we can find """
1106        students_folder = self.portal_url.getPortalObject().campus.students
1107        if clear:
[1724]1108            self._catalog.clear()
[1700]1109        students = self.portal_catalog(portal_type="Student")
1110        num_objects = len(students)
[2078]1111        cached_data = self.fill_certificates_dict()
[1620]1112        if pghandler:
1113            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
1114        for i in xrange(num_objects):
1115            if pghandler: pghandler.report(i)
[1700]1116            student_brain = students[i]
1117            spath = student_brain.getPath()
[1727]1118            student_object = student_brain.getObject()
[1620]1119            data = {}
[1700]1120            sid = data['id'] = student_brain.getId
1121            data['review_state'] = student_brain.review_state
[1707]1122            sub_objects = False
1123            for pt in self.affected_types.keys():
1124                modified = True
1125                try:
1126                    doc = getattr(student_object,self.affected_types[pt]['id']).getContent()
1127                    sub_objects = True
1128                except:
[1727]1129                    #from pdb import set_trace;set_trace()
[1707]1130                    continue
1131                for field in self.affected_types[pt]['fields']:
1132                    if hasattr(self,'get_from_doc_%s' % field):
[2078]1133                        data[field] = getattr(self,'get_from_doc_%s' % field)(doc,
1134                                                                              cached_data=cached_data)
[1707]1135                    else:
[1727]1136                        data[field] = getattr(doc,field,None)
1137            if not sub_objects:
[1700]1138                import_res = self.returning_import(id = sid)
1139                if not import_res:
[1620]1140                    continue
[1700]1141                import_record = import_res[0]
1142                data['matric_no'] = import_record.matric_no
1143                data['sex'] = import_record.Sex == 'F'
1144                data['name'] = "%s %s %s" % (import_record.Firstname,
1145                                             import_record.Middlename,
1146                                             import_record.Lastname)
[1815]1147                data['jamb_reg_no'] = import_record.Entryregno
[1700]1148            self.addRecord(**data)
[1620]1149        if pghandler: pghandler.finish()
1150    ###)
1151
[1700]1152    security.declarePrivate('notify_event_listener') ###(
[1620]1153    def notify_event_listener(self,event_type,object,infos):
1154        "listen for events"
[1716]1155        if not infos.has_key('rpath'):
1156            return
[1702]1157        pt = getattr(object,'portal_type',None)
1158        mt = getattr(object,'meta_type',None)
[1954]1159        students_catalog = self
[1702]1160        data = {}
1161        if pt == 'Student' and\
1162           mt == 'CPS Proxy Folder' and\
1163           event_type.startswith('workflow'):
1164            data['id'] = object.getId()
1165            data['review_state'] = self.portal_workflow.getInfoFor(object,'review_state',None)
1166            students_catalog.modifyRecord(**data)
1167            return
[1700]1168        rpl = infos['rpath'].split('/')
[2396]1169        if pt == 'Student' and mt == 'CPS Proxy Folder':
[1700]1170            student_id = object.id
[2396]1171            if event_type == "sys_add_object":
1172                try:
1173                    self.addRecord(id = student_id)
1174                except ValueError:
1175                    pass
1176                return
1177            elif event_type == 'sys_del_object':
1178                self.deleteRecord(student_id)
[1716]1179        if pt not in self.affected_types.keys():
[1700]1180            return
[1716]1181        if event_type not in ('sys_modify_object'):
1182            return
[1700]1183        if mt == 'CPS Proxy Folder':
1184            return
[3354]1185        if not hasattr(self,'_v_certificates'):
1186            self._v_certificates = self.fill_certificates_dict()
[1716]1187        for field in self.affected_types[pt]['fields']:
[1700]1188            if hasattr(self,'get_from_doc_%s' % field):
1189                data[field] = getattr(self,'get_from_doc_%s' % field)(object)
1190            else:
1191                data[field] = getattr(object,field)
1192        data['id'] = rpl[2]
[1716]1193        self.modifyRecord(**data)
[1700]1194    ###)
[1620]1195
[1625]1196
[971]1197InitializeClass(StudentsCatalog)
1198
[1146]1199###)
1200
[3354]1201class CertificatesCatalog(WAeUPTable): ###(
1202    security = ClassSecurityInfo()
1203
1204    meta_type = 'WAeUP Certificates Catalog'
1205    name =  "certificates_catalog"
1206    key = "code"
1207    def __init__(self,name=None):
1208        if name ==  None:
1209            name =  self.name
1210        WAeUPTable.__init__(self, name)
1211
1212    def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###(
1213        """ clear the catalog, then re-index everything """
1214
1215        elapse = time.time()
1216        c_elapse = time.clock()
1217
1218        pgthreshold = self._getProgressThreshold()
1219        handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None
1220        self.refreshCatalog(clear=1, pghandler=handler)
1221
1222        elapse = time.time() - elapse
1223        c_elapse = time.clock() - c_elapse
1224
1225        RESPONSE.redirect(
1226            URL1 +
1227            '/manage_catalogAdvanced?manage_tabs_message=' +
1228            urllib.quote('Catalog Updated \n'
1229                         'Total time: %s\n'
1230                         'Total CPU time: %s' % (`elapse`, `c_elapse`)))
1231    ###)
1232
1233    def reindexIndex(self, name, REQUEST,pghandler=None): ###(
1234        if isinstance(name, str):
1235            name = (name,)
1236        certificates = self.portal_catalog(portal_type="Certificate")
1237        num_objects = len(Certificates)
1238        if pghandler:
1239            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
1240        for i in xrange(num_objects):
1241            if pghandler: pghandler.report(i)
1242            certificate_brain = certificates[i]
1243            certificate_object = certificate_brain.getObject()
1244            pl = certificate_brain.getPath().split('/')
1245            data = {}
1246            cid = data[self.key] = certificate_brain.getId
1247            data['faculty'] = pl[-4]
1248            data['department'] = pl[-3]
1249            doc = certificate_object.getContent()
1250            for field in name:
1251                if field not in (self.key,'faculty','department'):
1252                    data[field] = getattr(doc,field)
1253            self.modifyRecord(**data)
1254        if pghandler: pghandler.finish()
1255    ###)
1256
1257    def refreshCatalog(self, clear=0, pghandler=None): ###(
1258        """ re-index everything we can find """
1259        if clear:
1260            self._catalog.clear()
1261        certificates = self.portal_catalog(portal_type="Certificate")
1262        num_objects = len(certificates)
1263        if pghandler:
1264            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
1265        #from pdb import set_trace;set_trace()
1266        for i in xrange(num_objects):
1267            if pghandler: pghandler.report(i)
1268            certificate_brain = certificates[i]
1269            certificate_doc = certificate_brain.getObject().getContent()
1270            pl = certificate_brain.getPath().split('/')
1271            data = {}
1272            for field in self.schema():
1273                data[field] = getattr(certificate_doc,field,None)
1274            data[self.key] = certificate_brain.getId
1275            ai = pl.index('academics')
1276            data['faculty'] = pl[ai +1]
1277            data['department'] = pl[ai +2]
1278            if clear:
1279                self.addRecord(**data)
1280            else:
1281                self.modifyRecord(**data)
1282        if pghandler: pghandler.finish()
1283    ###)
1284
1285    security.declarePrivate('notify_event_listener') ###(
1286    def notify_event_listener(self,event_type,object,infos):
1287        "listen for events"
1288        if not infos.has_key('rpath'):
1289            return
1290        pt = getattr(object,'portal_type',None)
1291        mt = getattr(object,'meta_type',None)
1292        if pt != 'Certificate':
1293            return
1294        data = {}
1295        rpl = infos['rpath'].split('/')
1296        if event_type not in ("sys_add_object","sys_modify_object","sys_del_object"):
1297            return
1298        certificate_id = object.getId()
1299        data[self.key] = certificate_id
1300        if event_type == "sys_add_object" and mt == 'CPS Proxy Folder':
1301            try:
1302                self.addRecord(**data)
1303            except ValueError:
1304                return
1305            certificate_id = object.getId()
1306            doc = object.getContent()
1307            if doc is None:
1308                return
1309            for field in self.schema():
1310                data[field] = getattr(doc,field,None)
1311            data[self.key] = certificate_id
1312            ai = rpl.index('academics')
1313            data['faculty'] = rpl[ai +1]
1314            data['department'] = rpl[ai +2]
1315            self.modifyRecord(**data)
1316            return
1317        if event_type == "sys_del_object":
1318            self.deleteRecord(certificate_id)
1319            return
1320        if event_type == "sys_modify_object" and mt == 'Certificate':
1321            #from pdb import set_trace;set_trace()
1322            for field in self.schema():
1323                data[field] = getattr(object,field,None)
1324            certificate_id = object.aq_parent.getId()
1325            data[self.key] = certificate_id
1326            ai = rpl.index('academics')
1327            data['faculty'] = rpl[ai +1]
1328            data['department'] = rpl[ai +2]
1329            self.modifyRecord(**data)
1330    ###)
1331
1332
1333InitializeClass(CertificatesCatalog)
1334###)
1335
[1146]1336class CoursesCatalog(WAeUPTable): ###(
[1716]1337    security = ClassSecurityInfo()
[1146]1338
1339    meta_type = 'WAeUP Courses Catalog'
[2094]1340    name =  "courses_catalog"
[1146]1341    key = "code"
[2094]1342    def __init__(self,name=None):
1343        if name ==  None:
1344            name =  self.name
1345        WAeUPTable.__init__(self, name)
[1146]1346
[1716]1347    def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###(
1348        """ clear the catalog, then re-index everything """
[1146]1349
[1716]1350        elapse = time.time()
1351        c_elapse = time.clock()
1352
1353        pgthreshold = self._getProgressThreshold()
1354        handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None
1355        self.refreshCatalog(clear=1, pghandler=handler)
1356
1357        elapse = time.time() - elapse
1358        c_elapse = time.clock() - c_elapse
1359
1360        RESPONSE.redirect(
1361            URL1 +
1362            '/manage_catalogAdvanced?manage_tabs_message=' +
1363            urllib.quote('Catalog Updated \n'
1364                         'Total time: %s\n'
1365                         'Total CPU time: %s' % (`elapse`, `c_elapse`)))
1366    ###)
1367
1368    def reindexIndex(self, name, REQUEST,pghandler=None): ###(
1369        if isinstance(name, str):
1370            name = (name,)
1371        courses = self.portal_catalog(portal_type="Course")
1372        num_objects = len(courses)
1373        if pghandler:
1374            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
1375        for i in xrange(num_objects):
1376            if pghandler: pghandler.report(i)
1377            course_brain = courses[i]
1378            course_object = course_brain.getObject()
1379            pl = course_brain.getPath().split('/')
1380            data = {}
1381            cid = data[self.key] = course_brain.getId
1382            data['faculty'] = pl[-4]
1383            data['department'] = pl[-3]
1384            doc = course_object.getContent()
1385            for field in name:
1386                if field not in (self.key,'faculty','department'):
1387                    data[field] = getattr(doc,field)
1388            self.modifyRecord(**data)
1389        if pghandler: pghandler.finish()
1390    ###)
1391
1392    def refreshCatalog(self, clear=0, pghandler=None): ###(
1393        """ re-index everything we can find """
[1724]1394        if clear:
1395            self._catalog.clear()
[1716]1396        courses = self.portal_catalog(portal_type="Course")
1397        num_objects = len(courses)
1398        if pghandler:
1399            pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
[1724]1400        #from pdb import set_trace;set_trace()
[1716]1401        for i in xrange(num_objects):
1402            if pghandler: pghandler.report(i)
1403            course_brain = courses[i]
[1724]1404            course_doc = course_brain.getObject().getContent()
[1716]1405            pl = course_brain.getPath().split('/')
1406            data = {}
[1724]1407            for field in self.schema():
[1749]1408                data[field] = getattr(course_doc,field,None)
[1716]1409            data[self.key] = course_brain.getId
[1724]1410            ai = pl.index('academics')
1411            data['faculty'] = pl[ai +1]
1412            data['department'] = pl[ai +2]
1413            if clear:
1414                self.addRecord(**data)
1415            else:
1416                self.modifyRecord(**data)
[1716]1417        if pghandler: pghandler.finish()
1418    ###)
1419
1420    security.declarePrivate('notify_event_listener') ###(
1421    def notify_event_listener(self,event_type,object,infos):
1422        "listen for events"
1423        if not infos.has_key('rpath'):
1424            return
1425        pt = getattr(object,'portal_type',None)
1426        mt = getattr(object,'meta_type',None)
1427        if pt != 'Course':
1428            return
1429        data = {}
1430        rpl = infos['rpath'].split('/')
1431        if event_type not in ("sys_add_object","sys_modify_object","sys_del_object"):
1432            return
1433        course_id = object.getId()
1434        data[self.key] = course_id
[1724]1435        if event_type == "sys_add_object" and mt == 'CPS Proxy Folder':
[1716]1436            try:
1437                self.addRecord(**data)
1438            except ValueError:
[1724]1439                return
1440            course_id = object.getId()
1441            doc = object.getContent()
1442            if doc is None:
1443                return
1444            for field in self.schema():
[1749]1445                data[field] = getattr(doc,field,None)
[1724]1446            data[self.key] = course_id
1447            ai = rpl.index('academics')
1448            data['faculty'] = rpl[ai +1]
1449            data['department'] = rpl[ai +2]
1450            self.modifyRecord(**data)
1451            return
[1716]1452        if event_type == "sys_del_object":
1453            self.deleteRecord(course_id)
[1724]1454            return
[1716]1455        if event_type == "sys_modify_object" and mt == 'Course':
[1724]1456            #from pdb import set_trace;set_trace()
[1716]1457            for field in self.schema():
[1749]1458                data[field] = getattr(object,field,None)
[1716]1459            course_id = object.aq_parent.getId()
1460            data[self.key] = course_id
[1724]1461            ai = rpl.index('academics')
1462            data['faculty'] = rpl[ai +1]
1463            data['department'] = rpl[ai +2]
[1716]1464            self.modifyRecord(**data)
1465    ###)
1466
1467
[1146]1468InitializeClass(CoursesCatalog)
[1151]1469###)
[1146]1470
[2084]1471class CourseResults(WAeUPTable): ###(
[2069]1472    security = ClassSecurityInfo()
1473
1474    meta_type = 'WAeUP Results Catalog'
1475    name = "course_results"
[2084]1476    key = "key" #student_id + level + course_id
[2094]1477    def __init__(self,name=None):
1478        if name ==  None:
1479            name = self.name
1480        WAeUPTable.__init__(self, name)
[2084]1481        self._queue = []
[2099]1482
[2094]1483    def addMultipleRecords(self, records): ###(
1484        """add many records"""
[3362]1485        existing_uids = []
[2094]1486        for data in records:
[3362]1487            uid = "%(student_id)s|%(level_id)s|%(course_id)s" % data
[2094]1488            data['%s' % self.key] = uid
[3362]1489            query = Eq(self.key, uid)
1490            res = self.course_results.evalAdvancedQuery(query)
[2094]1491            if len(res) > 0:
[3362]1492                rec = res[0]
1493                equal = True
1494                for attr in ('student_id','level_id','course_id'):
1495                    if getattr(rec,attr,'') != data[attr]:
1496                        equal = False
1497                        break
1498                if equal:
1499                    existing_uids += uid,
1500                    continue
[2094]1501            self.catalog_object(dict2ob(data), uid=uid)
[3362]1502        return existing_uids
[2094]1503    ###)
1504
[2434]1505    def deleteResultsHere(self,level_id,student_id): ###(
1506        query = Eq('student_id',student_id) & Eq('level_id', level_id)
1507        course_results = self.course_results.evalAdvancedQuery(query)
[3362]1508        #import pdb;pdb.set_trace()
[2434]1509        for result in course_results:
1510            self.deleteRecord(result.key)
[2084]1511    ###)
1512
[2434]1513    def moveResultsHere(self,level,student_id): ###(
1514        #import pdb;pdb.set_trace()
1515        level_id = level.getId()
1516        query = Eq('student_id',student_id) & Eq('level_id', level_id)
1517        course_results = self.course_results.evalAdvancedQuery(query)
[2437]1518        existing_courses = [cr.code for cr in course_results]
[2434]1519        to_delete = []
1520        for code,obj in level.objectItems():
[2437]1521            to_delete.append(code)
[2434]1522            carry_over = False
1523            if code.endswith('_co'):
1524                carry_over = True
[2437]1525                code  = code[:-3]
[2434]1526            if code in existing_courses:
[2094]1527                continue
[2434]1528            course_result_doc = obj.getContent()
[2094]1529            data = {}
[2434]1530            course_id = code
[2094]1531            for field in self.schema():
1532                data[field] = getattr(course_result_doc,field,'')
1533            data['key'] = key = "%(student_id)s|%(level_id)s|%(course_id)s" % vars()
[2099]1534            data['student_id'] = student_id
1535            data['level_id'] = level_id
[2439]1536            session_id = self.getLevelSession(level.getContent(),student_id,level_id)
[2442]1537            data['session_id'] = session_id
[2434]1538            #data['queue_status'] = OBJECT_CREATED
[2099]1539            data['code'] = course_id
[2434]1540            data['carry_over'] = carry_over
[2094]1541            self.catalog_object(dict2ob(data), uid=key)
[2434]1542        level.manage_delObjects(to_delete)
1543    ###)
1544
1545    def getCourses(self,student_id,level_id): ###(
[2094]1546        query = Eq('student_id',student_id) & Eq('level_id', level_id)
[2434]1547        course_results = self.course_results.evalAdvancedQuery(query)
1548        carry_overs = []
[2606]1549        normal1 = []
1550        normal2 = []
[2761]1551        normal3 = []
[2757]1552        total_credits = 0
1553        gpa = 0
[2434]1554        for brain in course_results:
1555            d = {}
[2781]1556
[2761]1557            for field in self.schema():
1558                d[field] = getattr(brain,field,'')
1559
1560            d['weight'] = ''
1561            d['grade'] = ''
1562            d['score'] = ''
1563
[2750]1564            if str(brain.credits).isdigit():
[2757]1565                credits = int(brain.credits)
1566                total_credits += credits
[2780]1567                score = getattr(brain,'score',0)
1568                if score and str(score).isdigit() and int(score) > 0:
1569                    score = int(score)
[2757]1570                    grade,weight = self.getGradesFromScore(score)
[2781]1571                    gpa += weight * credits
1572                    d['weight'] = weight
1573                    d['grade'] = grade
1574                    d['score'] = score
[2757]1575            d['coe'] = ''
[2434]1576            if brain.core_or_elective:
1577                d['coe'] = 'Core'
[2757]1578            elif brain.core_or_elective == False:
1579                d['coe'] = 'Elective'
[2434]1580            id = code = d['id'] = brain.code
1581            d['code'] = code
[2864]1582            res = self.courses_catalog.evalAdvancedQuery(Eq('code',code))
1583            if res:
1584                course = res[0]
1585                d['title'] = course.title
1586                # The courses_catalog contains strings and integers in its semester field.
1587                # Maybe this can be fixed by reindexing the catalog. The schema of course says 'CPS Int Field'.
1588                d['semester'] = str(course.semester)
1589            else:
[2866]1590                d['title'] = "Course has been removed from course list"
[2864]1591                d['semester'] = ''
[2448]1592            if brain.carry_over:
[2761]1593                d['coe'] = 'CO'
[2434]1594                carry_overs.append(d)
1595            else:
[2614]1596                if d['semester'] == '1':
[2606]1597                    normal1.append(d)
[2614]1598
1599                elif d['semester'] == '2':
[2606]1600                    normal2.append(d)
1601                else:
1602                    normal3.append(d)
1603        #normal.sort(cmp=lambda x,y: cmp("%(semester)s%(code)s" % x,
1604        #                                "%(semester)s%(code)s" % y))
[2503]1605        carry_overs.sort(cmp=lambda x,y: cmp("%(semester)s%(code)s" % x,
[2460]1606                                             "%(semester)s%(code)s" % y))
[2757]1607        return total_credits,gpa,carry_overs,normal1,normal2,normal3
[2094]1608    ###)
1609
[2084]1610InitializeClass(CourseResults)
[2069]1611###)
1612
[1625]1613class OnlinePaymentsImport(WAeUPTable): ###(
[1620]1614
1615    meta_type = 'WAeUP Online Payment Transactions'
[1625]1616    name = "online_payments_import"
[1620]1617    key = "order_id"
[2094]1618    def __init__(self,name=None):
1619        if name ==  None:
1620            name = self.name
1621        WAeUPTable.__init__(self, name)
[1620]1622
1623
[2069]1624InitializeClass(OnlinePaymentsImport)
[1620]1625###)
1626
[1151]1627class ReturningImport(WAeUPTable): ###(
[1146]1628
[1151]1629    meta_type = 'Returning Import Table'
1630    name = "returning_import"
[1146]1631    key = "matric_no"
[2094]1632    def __init__(self,name=None):
1633        if name ==  None:
1634            name = self.name
1635        WAeUPTable.__init__(self, name)
[1146]1636
1637
[1151]1638InitializeClass(ReturningImport)
1639###)
[1146]1640
1641class ResultsImport(WAeUPTable): ###(
1642
1643    meta_type = 'Results Import Table'
1644    name = "results_import"
1645    key = "key"
[2094]1646    def __init__(self,name=None):
1647        if name ==  None:
1648            name = self.name
1649        WAeUPTable.__init__(self, name)
[1146]1650
1651
1652InitializeClass(ResultsImport)
1653
1654###)
1655
1656class PaymentsCatalog(WAeUPTable): ###(
[2738]1657    security = ClassSecurityInfo()
[1146]1658
1659    meta_type = 'WAeUP Payments Catalog'
[2868]1660    name = "payments_catalog"
1661    key = "order_id"
[2094]1662    def __init__(self,name=None):
1663        if name ==  None:
1664            name = self.name
1665        WAeUPTable.__init__(self, name)
[1146]1666
[2859]1667
[2738]1668    security.declarePrivate('notify_event_listener') ###(
1669    def notify_event_listener(self,event_type,object,infos):
1670        "listen for events"
1671        if not infos.has_key('rpath'):
1672            return
1673        pt = getattr(object,'portal_type',None)
1674        mt = getattr(object,'meta_type',None)
1675        data = {}
[2904]1676        if pt != 'Payment':
1677            return
1678        if mt == 'CPS Proxy Folder':
[2911]1679            return # is handled only for the real object
1680        if event_type == 'sys_del_object':
1681            self.deleteRecord(object.order_id)
[2738]1682        if event_type not in ('sys_modify_object'):
1683            return
1684        for field in self.schema():
[2859]1685            data[field] = getattr(object,field,'')
[2738]1686        rpl = infos['rpath'].split('/')
[2904]1687        #import pdb;pdb.set_trace()
1688        student_id = rpl[-4]
[2738]1689        data['student_id'] = student_id
[2907]1690        modified = False
[2859]1691        try:
1692            self.modifyRecord(**data)
[2907]1693            modified = True
[2859]1694        except KeyError:
[2926]1695            #logger = logging.getLogger('WAeUPTables.PaymentsCatalog.%s' % self.__name__)
1696            #logger.info("could not modify entry for %(student_id)s with %(order_id)s" % data)
1697            pass
[2907]1698        if not modified:
1699            try:
1700                self.addRecord(**data)
1701            except:
[2976]1702                logger = logging.getLogger('WAeUPTables.PaymentsCatalog.notify_event_listener')
[2907]1703                logger.info("could not add or modify entry for %(student_id)s with %(order_id)s" % data)
1704        ###)
[1146]1705
[2738]1706
[1146]1707InitializeClass(PaymentsCatalog)
1708
1709###)
1710
[414]1711# BBB:
1712AccomodationTable = AccommodationTable
Note: See TracBrowser for help on using the repository browser.