source: WAeUP_SRP/base/WAeUPTool.py @ 2984

Last change on this file since 2984 was 2975, checked in by joachim, 17 years ago
  • Property svn:keywords set to Id
File size: 85.9 KB
RevLine 
[1991]1#-*- mode: python; mode: fold -*-
[197]2# (C) Copyright 2005 The WAeUP group  <http://www.waeup.org>
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: WAeUPTool.py 2975 2008-01-03 17:20:02Z joachim $
[1174]20"""The WAeUP Tool Box.
[197]21"""
22
23from AccessControl import ClassSecurityInfo
[828]24from Acquisition import aq_inner
25from Acquisition import aq_parent
26from Globals import DTMLFile
27from Globals import InitializeClass
28from OFS.SimpleItem import SimpleItem
[2283]29from zExceptions import BadRequest
[197]30
[1890]31from Products.CMFCore.utils import getToolByName
[1747]32from Products.CPSSchemas.DataStructure import DataStructure
33from Products.CPSSchemas.DataModel import DataModel
34from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter
[828]35from Products.CMFCore.ActionProviderBase import ActionProviderBase
36from Products.CMFCore.permissions import View
37from Products.ZCatalog.ZCatalog import ZCatalog
38from Products.CMFCore.permissions import ModifyPortalContent
[1890]39from Products.CMFCore.permissions import ManagePortal
[197]40from Products.CMFCore.utils import UniqueObject
[1194]41from Products.CMFCore.URLTool import URLTool
[1747]42from Products.CMFCore.utils import getToolByName
[1151]43from Students import makeCertificateCode
[1285]44from Globals import package_home,INSTANCE_HOME
[2094]45import DateTime,time
[1620]46import logging
[1170]47import transaction
[2193]48import csv,re,os,sys
[2413]49from shutil import copy2
[1707]50from Products.AdvancedQuery import Eq, Between, Le,In
[197]51
[2413]52p_home = package_home(globals())
53i_home = INSTANCE_HOME
54images_base = os.path.join(i_home,"images")
55
[2675]56def getImagesDir(student_id):
57    return os.path.join("%s" % images_base,student_id[0],student_id)
[2413]58
[1707]59def getObject(object,name):
60    if object.hasObject(name):
61        return getattr(object,name)
62    return None
[1720]63
[828]64class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase):
[197]65    """WAeUP tool"""
66
[828]67    id = 'waeup_tool'
[197]68    meta_type = 'WAeUP Tool'
[828]69    _actions = ()
[197]70    security = ClassSecurityInfo()
[828]71    security.declareObjectProtected(View)
72    manage_options = ( ActionProviderBase.manage_options
73                     + SimpleItem.manage_options
74                     )
75
[1818]76    def rwrite(self,s): ###(
[1707]77        response = self.REQUEST.RESPONSE
78        response.setHeader('Content-type','text/html; charset=ISO-8859-15')
79        response.write("%s<br />\r\n" % s)
[1818]80    ###)
[1174]81
[2695]82    def addtodict(self,d,key,item): ###(
83        d[key].append(item)
84        return d[key]
85    ###)
86
[1818]87    def sleep(self,secs): ###(
88        "sleep"
89        import time
90        time.sleep(secs)
91        return
[1827]92
[1818]93###)
94
[2695]95    security.declareProtected(View,'updateRoleMappingsFor') ###(
96    def updateRoleMappingsFor(self,wf_definition,ob):
97        "do so for public"
98        wf_def = getattr(self.portal_workflow,wf_definition)
99        wf_def.updateRoleMappingsFor(ob)
100    ###)
[2699]101
[2975]102    security.declareProtected(View,'getAccessInfo') ###(
103    def getAccessInfo(self,context):
104        "return a dict with access_info"
105        logger = logging.getLogger('WAeUPTool.getAccessInfo')
106        mtool = self.portal_membership
107        member = mtool.getAuthenticatedMember()
108        member_id = str(member)
109        info = {}
110        is_anonymous = info['is_anonymous'] = mtool.isAnonymousUser()
111        is_student = info['is_student'] = ord(member_id[1]) > 48 and ord(member_id[1]) <= 57
112        is_staff = info['is_staff'] = not is_anonymous and not is_student
113        roles = member.getRolesInContext(context)
114        is_sectionofficer = info['is_sectionofficer'] = not is_student and ("SectionOfficer" in roles or
115                                                                            "SectionManager" in roles or
116                                                                            "Manager" in roles)
117        is_allowed = info['is_allowed'] = not is_anonymous
118        requested_id = context.getStudentId()
119        student_id  = None
120        if not is_student and requested_id:
121            student_id  = requested_id
122        elif is_anonymous or member_id != requested_id:
123            logger.info('%s tried to access %s of %s' % (member_id,context.portal_type,requested_id))
124        else:
125            student_id = member_id
126        info['student_id'] = student_id
127        return info
128    ###)
129
[1818]130    security.declareProtected(ModifyPortalContent,'openLog') ###(
[1716]131    def openLog(self,name):
132        """open a log file"""
133        version = 1
134        path = "%s/log/%s_%d.log" % (i_home,name,version)
135        while os.path.exists(path):
136            version += 1
137            path = "%s/log/%s_%d.log" % (i_home,name,version)
138        log = open(path,"w")
139        return log
[2259]140    ###)
[1716]141
[2259]142    security.declareProtected(ModifyPortalContent,'bypassQueueCatalog') ###(
143    def bypassQueueCatalog(self,enable=True):
144        """bypass the QueueCatalog by setting all indexes to process imediate,
145        if enable is True (default) the old settings are restored
146        """
[1818]147
[2259]148    ###)
149
[2094]150    security.declareProtected(ModifyPortalContent,'measureOaT') ###(
151    def measureOaT(self,method="a",probe="1000",nr_pts="1"):
152        """measure Object access Time"""
153        import random
154        if hasattr(self,'portal_catalog_real'):
155            aq_portal = self.portal_catalog_real.evalAdvancedQuery
156        else:
157            aq_portal = self.portal_catalog.evalAdvancedQuery
158        nr_pts = int(nr_pts)
159        probe = int(probe)
160        intervall = probe/10
161        objects = ("application","clearance","personal")
162        portal_types = ("StudentApplication","StudentClearance","StudentPersonal")
163        #i = random.randrange(num_objects)
164        count = 0
165        found = 0
166        not_found = 0
167        t_found = 0
168        t_not_found = 0
169        time_found = time_not_found = 0.0
170        t_time_found = t_time_not_found = 0.0
171        accessed = []
172        t_min = 1000
173        t_max = 0
174        #import pdb;pdb.set_trace()
175        students = self.portal_catalog(portal_type="Student")
176        num_students = len(students)
177        if method == "d":
178            query = Eq('path','/uniben/campus/students') & In('portal_type',portal_types[:nr_pts])
179            res = aq_portal(query)
180            brains = {}
181            for r in res:
182                sid = r.relative_path.split('/')[-2]
183                if brains.has_key(sid):
184                    brains[sid][r.portal_type] = r
185                else:
186                    brains[sid] = {r.portal_type : r}
187            brains_list = brains.keys()
188            num_objects = len(brains_list)
189        else:
190            num_objects = num_students
191        print "="*40
192        print "method: %s probes: %d nr_pts: %d num_objects: %d" % (method,
193                                                                        probe,
194                                                                        nr_pts,
195                                                                        num_objects)
196        print "nr found/not time found/not min/max"
197        elapse = time.time()
198        i_elapse = time.time()
199        c_elapse = time.clock()
200        for c in range(1,probe + 1):
201            i = random.randrange(num_objects)
202            if method in ('a','b','c'):
203                student_brain = students[i]
204            elif method == "d":
205                #import pdb;pdb.set_trace()
206                student_brain = brains[brains_list[i]]
207            if method == "c":
208                query = Eq('path',student_brain.getPath()) & In('portal_type',portal_types[:nr_pts])
209                res = aq_portal(query)
210                this_portal_types = [r.portal_type for r in res]
211            for i in range(nr_pts):
212                oid = objects[i]
213                if method == "a":
214                    try:
215                        student_path = student_brain.getPath()
216                        path = "%s/%s" % (student_path,oid)
217                        doc = self.unrestrictedTraverse(path).getContent()
218                        found += 1
219                        i_time = time.time() - i_elapse
220                        time_found += i_time
221                    except:
222                        not_found += 1
223                        i_time = time.time() - i_elapse
224                        time_not_found += i_time
225                        pass
226                elif method == "b":
227                    try:
228                        student_object = student_brain.getObject()
229                        doc = getattr(student_object,oid).getContent()
230                        found += 1
231                        i_time = time.time() - i_elapse
232                        time_found += i_time
233                    except:
234                        i_time = time.time() - i_elapse
235                        time_not_found += i_time
236                        not_found += 1
237                        pass
238                elif method == "c":
239                    if portal_types[i] in this_portal_types:
240                        found += 1
241                        doc = res[this_portal_types.index(portal_types[i])].getObject().getContent()
242                        i_time = time.time() - i_elapse
243                        time_found += i_time
244                    else:
245                        not_found += 1
246                        i_time = time.time() - i_elapse
247                        time_not_found += i_time
248                elif method == "d":
249                    if student_brain.has_key(portal_types[i]):
250                        found += 1
251                        doc = student_brain[portal_types[i]].getObject().getContent()
252                        i_time = time.time() - i_elapse
253                        time_found += i_time
254                    else:
255                        not_found += 1
256                        i_time = time.time() - i_elapse
257                        time_not_found += i_time
258                i_elapse = time.time()
259            if c and (c % intervall == 0):
260                #i_time = time.time() - i_elapse
261                t_per = 0.0
262                if found:
263                    t_per = time_found/found
264                if t_per > t_max:
265                    t_max = t_per
266                if t_per > 0.0 and t_per < t_min:
[2119]267                    t_min = t_per
[2094]268                itf = 0.0
269                if found:
270                    itf = time_found/found
271                itnf = 0.0
272                if not_found :
273                    itnf = time_not_found / not_found
[2119]274                interval_time = time_found + time_not_found
275                s = "%(c)d: %(found)d/%(not_found)d " % vars()
[2094]276                s += "%(interval_time)6.2f %(itf)6.4f/%(itnf)6.4f " % vars()
277                s += "%(t_min)6.4f/%(t_max)6.4f" %  vars()
278                print s
279                t_found += found
280                t_not_found += not_found
281                t_time_found += time_found
282                t_time_not_found += time_not_found
283                time_found = time_not_found = 0.0
284                found = not_found = 0
285        # t_found += found
286        # t_not_found += not_found
287        elapse = time.time() - elapse
288        itf = 0.0
289        if t_found:
290            itf = t_time_found/t_found
291        itnf = 0.0
292        if t_not_found:
293            itnf = t_time_not_found / t_not_found
294        #c_elapse = time.clock() - c_elapse
[2119]295        s = "%(probe)d: %(t_found)d/%(t_not_found)d " % vars()
[2094]296        s += "%(elapse)6.2f %(itf)6.4f/%(itnf)6.4f " % vars()
297        s += "%(t_min)6.4f/%(t_max)6.4f" %  vars()
298        print "-"*40
299        print s
300        rel_found = float(t_found)/probe
301        rel_not_found = float(t_not_found)/probe
302        estimated_total_time = num_objects*(rel_found*itf + rel_not_found*itnf)
303        print estimated_total_time
304    ###)
305
[1818]306    security.declareProtected(ModifyPortalContent,'writeLog') ###(
[1716]307    def writeLog(self,logfile,s):
308        """write to the log file"""
309        logfile.write(s)
310
[1818]311###)
[1720]312
[1151]313    def generateStudentId(self,letter): ###(
314        import random
315        r = random
[1194]316        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
317        if letter == '?':
[2695]318            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
[1151]319        sid = "%c%d" % (letter,r.randint(99999,1000000))
[1720]320        students = self.portal_url.getPortalObject().campus.students
[1721]321##        while hasattr(students, sid):
322##            sid = "%c%d" % (letter,r.randint(99999,1000000))
323        while self.students_catalog(id = sid):
[1720]324            sid = "%c%d" % (letter,r.randint(99999,1000000))
[1151]325        return sid
[1460]326    ###)
[1415]327
328    def generatePassword(self,s=None): ###(
329        import random
330        r = random
331        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
332        if s is None:
333            s = 'abcdefghklmnpqrstuvwxy23456789'
334        pw = ''
335        while len(pw) < 6:
336            pw += r.choice(s)
337        return pw
[1151]338    ###)
[828]339
[2645]340    security.declareProtected(ModifyPortalContent, 'dumpSchoolfeePayments') ###(
341    def dumpSchoolfeePayments(self):
342        "dump paid schoolfees"
[2666]343        mtool = self.portal_membership
344        member = mtool.getAuthenticatedMember()
[2645]345        logger = logging.getLogger('WAeUPTool.dumpSchoolfees')
346        aq_student = self.students_catalog.evalAdvancedQuery
347        query = In('review_state',('schoolfee_paid',
348                                   'courses_registered',
349                                   'courses_validated',
350                                   ))
351        res = aq_student(query)
[2666]352        #import pdb;pdb.set_trace()
[2645]353        l = []
354        logger.info("start for %d" % len(res))
355        count = 1
356        log_after = 100
357        for student in res:
[2656]358            if not count % log_after:
[2645]359                logger.info("processed %d total %d" % (log_after,count))
360            count += 1
361            fee_dict =self.getSchoolFee(student)
362            fulltime = student.mode.endswith('_ft')
363            d = {}
364            d['student_id'] = student.id
[2666]365            d['name'] = student.name
366            d['amount'] = fee_dict.get(new_returning)
[2645]367            l += d,
[2666]368        csv_name = self.dumpListToCSV(l,'payments')
369        logger.info('%s dumped payments to %s' % (member,export_file))
[2645]370    ###)
371
[2666]372    security.declarePublic('dumpListToCSV') ###(
373    def dumpListToCSV(self,l,filename,fields=None):
374        """dump a list of dicts to a CSV file"""
375        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
376        export_file = "%s/export/%s_%s.csv" % (i_home,filename,current,)
377        if fields is None:
378            fields = l[0].keys()
379        headline = ','.join(fields)
380        out = open(export_file,"wb")
381        out.write(headline +'\n')
382        out.close()
383        out = open(export_file,"a")
384        csv_writer = csv.DictWriter(out,fields,)
385        csv_writer.writerows(l)
386        return export_file
387    ###)
388
[2643]389    security.declareProtected(ManagePortal, 'listMembers') ###(
390    def listMembers(self):
391        "list all members"
392        mtool = self.portal_membership
393        member = mtool.getAuthenticatedMember()
394        logger = logging.getLogger('WAeUPTool.listMembers')
395        if str(member) not in ('admin','joachim'):
396            logger.info('%s tried to list members' % (member))
397            return None
398        members = self.portal_directories.members
399        all = members.listEntryIdsAndTitles()
400        l = []
401        for user_id,name in all:
402            d = {}
403            d['user_id'] = user_id
404            d['name'] = name
405            d['pw'] = getattr(getattr(members,user_id),'password')
[2645]406            d['email'] = getattr(getattr(members,user_id),'email')
407            d['groups'] = " ".join(getattr(getattr(members,user_id),'groups'))
408            d['roles'] = " ".join(getattr(getattr(members,user_id),'roles'))
[2643]409            l += d,
410        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
411        export_file = "%s/export/member_list_%s.csv" % (i_home,current,)
412        logger.info('%s dumped memberlist to %s' % (member,export_file))
413        fields = l[0].keys()
414        headline = ','.join(fields)
415        out = open(export_file,"wb")
416        out.write(headline +'\n')
417        out.close()
418        out = open(export_file,"a")
419        csv_writer = csv.DictWriter(out,fields,)
420        csv_writer.writerows(l)
421    ###)
[2656]422
[1890]423    security.declareProtected(ManagePortal, 'removeDeletedDocIds') ###(
424    def removeDeletedDocIds(self, max=1000):
425        """
426        remove deleted docids from repository commit after max
427        """
428        logger = logging.getLogger('WAeUPTool.removeDeletedDocIds')
429        repository = getToolByName(self, 'portal_repository')
430        pxtool = getToolByName(self, 'portal_proxies')
[2632]431        logger.info('start')
[1890]432        pxtool_infos = pxtool.getRevisionsUsed()
[2632]433        logger.info('found  %d used revisions ' % (len(pxtool_infos)))
[1895]434
[1890]435        nb_revs = 0
436        docids_d = {} # all docids
437        unused_docids_d = {} # all docids that are unused
438        ids_unused_revs_docids = [] # ids for revs of unused docids
[2632]439        unused_ids = [] # ids for unused revs
[1890]440        total = 0
[1895]441        idlist = repository.objectIds()
[2632]442        to_delete = 0
443        found = False
[1895]444        for id in idlist:
[1890]445            docid, rev = repository._splitId(id)
446            if docid is None:
447                logger.info("invalid doc_id %s" % docid)
448                continue
449            nb_revs += 1
450            if not pxtool_infos.has_key(docid):
[2632]451                found = True
452                to_delete += 1
453                unused_ids.append(id)
[1890]454            elif not pxtool_infos[docid].has_key(rev):
[2632]455                found = True
456                to_delete += 1
457                unused_ids.append(id)
458            if found and not to_delete % max:
459                found = False
[1890]460                #import pdb;pdb.set_trace()
[2632]461                repository.manage_delObjects(unused_ids)
[1890]462                transaction.commit()
[2632]463                logger.info('removed %d total %d unused docids ' % (max,to_delete))
464        else:
465            if unused_ids:
466                repository.manage_delObjects(unused_ids)
467                transaction.commit()
468        logger.info('finished removing %d unused docids ' % (to_delete))
[1890]469
[1895]470
[1890]471###)
472
[2562]473    security.declareProtected(View,'getCredential') ###(
[1250]474    def getCredential(self,student_id):
475        student_entry = getattr(self.portal_directories.students,student_id,None)
[2555]476        if not self.isStaff():
[2563]477            mtool = self.portal_membership
478            member = mtool.getAuthenticatedMember()
479            logger = logging.getLogger('WAeUPTool.getCredential')
480            logger.info('%s tried to access password of %s' % (member,student_id))
[2555]481            return None
[1250]482        if student_entry is None:
483            return None
[1263]484        return getattr(student_entry,"password","not set")
[1261]485    ###)
[1250]486
[2747]487    security.declarePublic('checkPassword') ###(
[1467]488    def checkPassword(self,student_id,password):
[1460]489        student_entry = getattr(self.portal_directories.students,student_id,None)
490        if student_entry is None:
491            return False
492        return getattr(student_entry,"password","not set") == password
[2747]493    ###)
[2712]494
[2747]495    security.declarePublic('checkGenericPassword') ###(
[2710]496    def checkGenericPassword(self,member_id):
497        member_entry = getattr(self.portal_directories.members,member_id,None)
498        if member_entry is None:
499            return False
[2712]500        ltool = getToolByName(self, 'portal_layouts')
[2710]501        unsecure_words = ltool._getOb('members')['w__password'].check_words
[2712]502        password = getattr(member_entry,"password","not set")
503        is_unsecure = password in unsecure_words
504        if is_unsecure:
[2713]505            logger = logging.getLogger('WAeUPTool.checkGenericPassword')
[2712]506            logger.info('Member %s tried to log in with unsecure password %s' %(member_id,password))
507        return is_unsecure
[2747]508    ###)
[1460]509
[2556]510    security.declareProtected(ModifyPortalContent,'editPassword') ###(
[1467]511    def editPassword(self,student_id,password):
512        "edit a student password"
513        student_entry = getattr(self.portal_directories.students,student_id,None)
514        if student_entry is None:
[1571]515            return
[1467]516        setattr(student_entry,'password',password)
517    ###)
518
[2555]519    security.declareProtected(ModifyPortalContent,'doCommit') ###(
[1401]520    def doCommit(self,logger=None):
521        "commit some transactions"
522        transaction.commit()
523    ###)
524
[1285]525    security.declarePublic('loadStudentFoto') ###(
[1813]526    def loadStudentFoto(self,student,filename,folder):
[1285]527        "return a student passport picture"
[2413]528        #import pdb;pdb.set_trace()
529        picture ="%s/import/%s/%s" % (i_home,folder,filename)
530        student_id = student.getId()
[2675]531        images_dir = getImagesDir(student_id)
[2413]532        if not os.path.exists(images_dir):
533            os.mkdir(images_dir)
534        image_name = os.path.join(images_dir,"passport_%(student_id)s.jpg" % vars())
535        for extension in ('.jpg','.JPG'):
536            fullname = "%(picture)s%(extension)s" % vars()
537            if os.path.exists(fullname):
538                copy2(fullname,image_name)
539                return "successfully copied passport picture"
[2467]540        return "passport picture not found: %s.jpg or .JPG" % picture
[2413]541    ###)
542
[2420]543    def old____loadStudentFoto(self,student,filename,folder): ###(
[2413]544        "return a student passport picture"
[1921]545        app = student.application
546        app_doc = app.getContent()
[1813]547        #clear = student.clearance
548        #clear_doc = clear.getContent()
549        #matric_no = clear_doc.matric_no.upper()
550        picture1 ="%s/import/%s/%s.jpg" % (i_home,folder,filename)
551        picture2 ="%s/import/%s/%s.JPG" % (i_home,folder,filename)
[1285]552        #import pdb;pdb.set_trace()
[1286]553        if os.path.exists(picture1):
554            file = open(picture1)
555        elif os.path.exists(picture2):
[1287]556            file = open(picture2)
[1286]557        else:
[1287]558            return "passport picture not found %s" % picture1
[1921]559        reopened = False
560        if self.portal_workflow.getInfoFor(app,'review_state',None) !='opened':
561            self.portal_workflow.doActionFor(app,'open')
562            reopened = True
[1285]563        outfile = file.read()
564        app_doc.manage_addFile('passport',
565                               file=outfile,
[1818]566                               title="%s.jpg" % filename)
[1921]567        if reopened:
568            self.portal_workflow.doActionFor(app,'close')
[1286]569        return "successfully loaded passport picture"
[1285]570    ###)
571
[1170]572    security.declareProtected(ModifyPortalContent,'createOne') ###(
[1194]573    def createOne(self,students_folder,student_brain,letter,commit=False):
574        sid = self.waeup_tool.generateStudentId(letter)
[1170]575        students_folder.invokeFactory('Student', sid)
576        student = getattr(students_folder,sid)
577        self.portal_workflow.doActionFor(student,'return')
578        student.manage_setLocalRoles(sid, ['Owner',])
579        matric_no = student_brain.matric_no
580        jamb_reg_no = student_brain.Entryregno
581        self.students_catalog.addRecord(id = sid,
582                                           matric_no = matric_no,
583                                           jamb_reg_no = jamb_reg_no,
584                                           sex = student_brain.Sex == "F",
585                                           name = "%s %s %s" % (student_brain.Firstname,
586                                                                student_brain.Middlename,
587                                                                student_brain.Lastname)
588                                        )
589        if commit:
590            transaction.commit()
591        return sid,jamb_reg_no
592    ###)
593
[1752]594    security.declareProtected(ModifyPortalContent,'addStudent') ###(
595    def addStudent(self,dict):
[1415]596        students_folder = self.portal_url.getPortalObject().campus.students
597        sid = self.waeup_tool.generateStudentId('?')
598        students_folder.invokeFactory('Student', sid)
599        student_obj = getattr(students_folder,sid)
[1728]600        f2t = self.student_field2types
[1772]601        #from pdb import set_trace; set_trace()
602        d = {}
[2297]603        #d['jamb_sex']  = 'M'
604        #if dict.get('sex'):
605        #    d['jamb_sex']  = 'F'
[1904]606
[1898]607        entry_session = dict.get('entry_session')
[2454]608        if entry_session == self.getSessionId()[0]:
[1898]609            wfaction = 'admit'
[1991]610            wft = 'wf_transition_admit'
[1900]611            password = None
[1904]612        else:
[1900]613            wfaction = 'return'
[1991]614            wft = 'wf_transition_return'
[1900]615            password = self.generatePassword()
[1904]616            self.makeStudentMember(sid,password)
617
[1728]618        for pt in f2t.keys():
619            student_obj.invokeFactory(pt,f2t[pt]['id'])
620            sub_obj = getattr(student_obj,f2t[pt]['id'])
621            sub_doc = sub_obj.getContent()
[1898]622            #self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj)
[1772]623            d['Title'] = f2t[pt]['title']
[1728]624            for field in f2t[pt]['fields']:
[1749]625                d[field] = dict.get(field,'')
[1728]626            sub_doc.edit(mapping = d)
[1898]627            new_state = f2t[pt][wft]
[1772]628            if new_state != "remain":
629                self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
[1415]630        self.portal_workflow.doActionFor(student_obj,wfaction)
631        student_obj.manage_setLocalRoles(sid, ['Owner',])
632        return sid,password
633    ###)
634
[1151]635    security.declarePublic('getCertificateBrain') ###(
636    def getCertificateBrain(self,cert_id):
637        "do it"
[1849]638        res = ZCatalog.searchResults(self.portal_catalog_real,
[1151]639                                {'portal_type':"Certificate",
640                                      'id': cert_id})
641        if res:
642            return res[0]
643        return None
644    ###)
[1160]645
[1756]646    security.declareProtected(ModifyPortalContent,'get_csv_filenames') ###(
647    def get_csv_filenames(self):
648        "do it"
[1759]649        files = [file for file in os.listdir("%s/import/" % (i_home))
[1756]650                 if file.endswith('.csv') and file.find('imported') == -1]
651        return files
652    ###)
653
[1151]654    security.declarePublic('findStudentByMatricelNo') ###(
655    def findStudentByMatricelNo(self,matric_no):
656        "do it"
[1849]657        res = ZCatalog.searchResults(self.portal_catalog_real,
[1151]658                                {'portal_type':"StudentClearance",
659                                 'SearchableText': matric_no})
660        if res:
661            return res[0]
662        return None
663    ###)
664
[2557]665    security.declarePublic('makeStudentMember') ###(
[1151]666    def makeStudentMember(self,sid,password='uNsEt'):
667        """make the student a member"""
668        membership = self.portal_membership
669        membership.addMember(sid,
670                             password ,
671                             roles=('Member',
672                                     'Student',
673                                     ),
674                             domains='',
675                             properties = {'memberareaCreationFlag': False,
676                                           'homeless': True},)
677        member = membership.getMemberById(sid)
678        self.portal_registration.afterAdd(member, sid, password, None)
[1261]679        #self.manage_setLocalRoles(sid, ['Owner',])
[1151]680    ###)
[1160]681
[2560]682    security.declareProtected(View,'makeStudentData') ###(
[1158]683    def makeStudentData(self,student_id,email=None,phone_nr=None):
[1151]684        "create Datastructure for a returning Student"
[1406]685        #import pdb;pdb.set_trace()
[1571]686        logger = logging.getLogger('WAeUPTool.makeStudentData')
[1151]687        students_folder = self.portal_url.getPortalObject().campus.students
[1794]688        #res = self.students_catalog(id=student_id)
689        #if res:
690        #    st = res[0]
691        #res = self.returning_import(matric_no = st.matric_no)
[1798]692        res = self.returning_import(id = student_id)
[1151]693        if res:
[1160]694            student = res[0]
[1794]695        else:
[1816]696            logger.info('Id %s not found in returning_import' % student_id)
[1794]697            return
[1580]698        logger.info('%s creates data structure' % student_id)
[1794]699        s_results = self.results_import(matric_no = student.matric_no)
[1816]700        if s_results:
701            lnr = self.getLevelFromResultsCosCode(s_results)
702            level = "%d00" % lnr
703            verdict,eligible = self.getVerdict(s_results[0].Verdict)
[2454]704            #if eligible:
705            #    level = "%d00" % (lnr + 1)
[1816]706        else:
707            logger.info('matric_no %s not found in results_import' % student.matric_no)
[2656]708            level = ''
709            verdict = ''
[1171]710        #student should not be allowed to perform this transition
[1174]711        #wftool = self.portal_workflow
712        #wftool.doActionFor(student,'return')
[1151]713        certcode_org = student.Coursemajorcode
714        certcode = makeCertificateCode(certcode_org)
715        certificate_brain = self.getCertificateBrain(certcode)
716        if not certificate_brain:
717            em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org)
718            logger.info(em)
719        matric_no = student.matric_no
720        sid = student_id
721        student_obj = getattr(students_folder,sid)
722        student_obj.invokeFactory('StudentApplication','application')
723        application = student_obj.application
[1169]724        self.portal_workflow.doActionFor(application,'open',dest_container=application)
[1151]725        da = {'Title': 'Application Data'}
726        student_obj.invokeFactory('StudentPersonal','personal')
727        da['jamb_reg_no'] = student.Entryregno
[1462]728        em = self.getEntryMode(student.Entryregno)
[1401]729        da['entry_mode'] = em
[1151]730        personal = student_obj.personal
731        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
732        dp = {'Title': 'Personal Data'}
733        student_obj.invokeFactory('StudentClearance','clearance')
734        clearance = student_obj.clearance
[1169]735        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
[1151]736        dc = {'Title': 'Clearance/Eligibility Record'}
737        dc['matric_no'] = matric_no
738        state = student.State
739        lga = student.LGA
740        if state and lga:
741            lga =  state + ' / ' + lga
742        else:
743            lga = "None"
[1174]744        da['jamb_lga'] = dc['lga'] = lga
[1173]745        da['app_email'] = dp['email'] = email
746        da['app_mobile'] = dp['phone'] = phone_nr
[1411]747        dp['firstname'] = student.Firstname
748        dp['middlename'] = student.Middlename
749        dp['lastname'] = student.Lastname
750        da['jamb_lastname'] = "%s %s %s" % (student.Firstname,student.Middlename,student.Lastname)
[1174]751        da['jamb_sex'] = student.Sex
[1151]752        dp['sex'] = student.Sex == 'F'
753        dp['perm_address'] = student.Permanent_Address
754        application.getContent().edit(mapping=da)
[1169]755        self.portal_workflow.doActionFor(application,'close',dest_container=application)
[1151]756        personal.getContent().edit(mapping=dp)
757        clearance.getContent().edit(mapping=dc)
[1169]758        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
[1151]759        #
760        # Study Course
761        #
762        student_obj.invokeFactory('StudentStudyCourse','study_course')
763        studycourse = student_obj.study_course
764        self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse)
765        dsc = {}
766        dsc['study_course'] = certcode
[1401]767        dsc['current_level'] = level
768        dsc['current_verdict'] = verdict
[1827]769        dsc['current_mode'] = em
[2466]770        dsc['current_session'] = '05'
[1151]771        studycourse.getContent().edit(mapping=dsc)
772        #
773        # Level
774        #
[1198]775##        l = getattr(studycourse,level,None)
776##        if l is None:
777##            studycourse.invokeFactory('StudentStudyLevel', level)
778##            l = getattr(studycourse, level)
779##            self.portal_workflow.doActionFor(l,'open',dest_container=l)
780##            l.getContent().edit(mapping={'Title': "Level %s" % level})
[1194]781###)
[1160]782
[2556]783    security.declareProtected(ModifyPortalContent,'admitOneStudent') ###(
[2576]784    def admitOneStudent(self,brain,entry_session,pin_password):
[2514]785        "create Datastructure for an admitted Student"
786        #import pdb;pdb.set_trace()
[2727]787        if not hasattr(self,"_v_certificates"):
788            self._v_certificates = [entry[0] for entry in self.getCertificatesVoc()]
[2514]789        students_folder = self.portal_url.getPortalObject().campus.students
[2716]790        logger = logging.getLogger('WAeUPTool.admitOneStudent')
[2514]791        if brain.status != "admitted":
792            logger.info('status of %s is %s' % (brain.reg_no,brain.status))
[2540]793            return
[2514]794        pin_parts = brain.pin.split('-')
[2755]795        if pin_parts and len(pin_parts) != 3:
[2695]796            logger.info('invalid pin %s for %s' % (brain.pin,brain.reg_no))
[2540]797            return
[2727]798        if brain.course_admitted not in self._v_certificates:
799            logger.info('certificate %s not found for %s' % (brain.course_admitted,brain.reg_no))
800            return
[2959]801        if brain.sex not in (True,False):
802            logger.info('sex of %s not available' % (brain.reg_no))
803            return           
[2540]804        student_id = self.generateStudentId('?')
[2514]805        students_folder.invokeFactory('Student', student_id)
806        student_object = getattr(students_folder,student_id)
[2576]807        if pin_password:
808            password = pin_parts[2]
809            self.makeStudentMember(student_id,password = password)
[2540]810        student_object.manage_setLocalRoles(student_id, ['Owner',])
811        #logger.info("creating %s reg_no %s" % (student_id,brain.reg_no))
[2514]812        #
813        # application
814        #
[2540]815        student_object.invokeFactory('StudentApplication','application')
816        application = student_object.application
[2575]817        #self.portal_workflow.doActionFor(application,'open',dest_container=application)
[2514]818        da = {'Title': 'Application Data'}
819        da['jamb_reg_no'] = brain.reg_no
[2576]820
[2540]821        sex = 'M'
822        if brain.sex:
823            sex = 'F'
824        da['jamb_sex'] = sex
[2670]825        da['jamb_age'] = brain.jamb_age
[2575]826        #da['app_ac_pin'] = brain.pin
[2670]827        da['jamb_lga'] = brain.jamb_lga
828        da['jamb_state'] = brain.jamb_state
[2575]829        da['jamb_score'] = brain.aggregate
[2514]830        da['app_email'] = brain.email
[2540]831        da['app_mobile'] = brain.phone
[2575]832        if brain.entry_mode:
833            da['entry_mode'] = brain.entry_mode
834        elif brain.screening_type == 'pume':
835            da['entry_mode'] = 'ume_ft'
836        elif brain.screening_type == 'pde':
837            da['entry_mode'] = 'de_ft'
[2950]838        elif brain.screening_type == 'pce':
[2959]839            da['entry_mode'] = 'pce'
[2950]840        elif brain.screening_type == 'prence':
[2959]841            da['entry_mode'] = 'prence'
[2575]842        else:
[2921]843            da['entry_mode'] = ''
[2575]844        da['entry_session'] = entry_session
845        da['jamb_lastname'] = brain.lastname
846        da['jamb_middlename'] = brain.middlenames   # different field names!
[2670]847        da['jamb_firstname'] = brain.firstname
[2575]848        da['screening_application_date'] = brain.application_date
849        da['date_of_birth'] = brain.date_of_birth
850        da['jamb_first_cos'] = brain.course1
851        da['jamb_second_cos'] = brain.course2
852        da['course3'] = brain.course3
853        da['screening_type'] = brain.screening_type
854        da['screening_score'] = brain.screening_score
855        da['screening_date'] = brain.screening_date
856        da['hq_type'] = brain.hq_type
857        da['hq_grade'] = brain.hq_grade
858        da['aos'] = brain.aos
[2576]859
[2514]860        application.getContent().edit(mapping=da)
[2575]861        #self.portal_workflow.doActionFor(application,'close',dest_container=application)
[2514]862        #
863        # personal
864        #
[2540]865        student_object.invokeFactory('StudentPersonal','personal')
866        personal = student_object.personal
[2575]867        #self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
[2514]868        dp = {'Title': 'Personal Data'}
869        dp['sex'] = brain.sex
870        dp['email'] = brain.email
[2540]871        dp['phone'] = brain.phone
[2514]872        dp['lastname'] = brain.lastname
[2575]873        dp['middlename'] = brain.middlenames   # different field names!
[2576]874        dp['firstname'] = brain.firstname
[2514]875        personal.getContent().edit(mapping=dp)
876        #
877        # clearance
878        #
[2540]879        student_object.invokeFactory('StudentClearance','clearance')
880        clearance = student_object.clearance
[2575]881        #self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
[2514]882        dc = {'Title': 'Clearance/Eligibility Record'}
883        dc['lga'] = brain.lga
[2670]884        dc['birthday'] = brain.date_of_birth
[2514]885        clearance.getContent().edit(mapping=dc)
[2575]886        #self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
[2514]887        #
888        # study Course
889        #
[2540]890        student_object.invokeFactory('StudentStudyCourse','study_course')
891        studycourse = student_object.study_course
[2575]892        #self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse)
[2514]893        dsc = {}
894        dsc['study_course'] = brain.course_admitted
895        dsc['current_verdict'] = ''
[2575]896        dsc['current_mode'] = da['entry_mode']
897        if da['entry_mode'].startswith('de'):
898            dsc['current_level'] = '200'
899        else:
900            dsc['current_level'] = '100'
901        dsc['current_session'] = entry_session
[2514]902        studycourse.getContent().edit(mapping=dsc)
903        #
904        # payments folder
[2540]905        student_object.invokeFactory('PaymentsFolder','payments')
906        payments = getattr(student_object,'payments')
[2514]907        dpay = {}
908        dpay['Title'] = 'Payments'
909        payments.getContent().edit(mapping=dpay)
[2540]910        self.portal_workflow.doActionFor(payments,'open')
[2580]911        #
912        # passport foto
913        app_picture ="%s/import/images/%s/%s_passport.jpg" % (i_home,
914                                                              brain.screening_type,
915                                                              brain.reg_no)
[2675]916        images_dir = getImagesDir(student_id)
917        #images_dir = os.path.join("%s" % images_base,student_id)
918        letter_dir,student_dir = os.path.split(images_dir)
919        if not os.path.exists(letter_dir):
920            os.mkdir(letter_dir)
[2580]921        if not os.path.exists(images_dir):
922            os.mkdir(images_dir)
923        image_name = os.path.join(images_dir,"passport_%(student_id)s.jpg" % vars())
924        if os.path.exists(app_picture):
925            copy2(app_picture,image_name)
926        else:
927            logger.info('passport of %s/%s not found: %s' % (student_id,
928                                                             brain.reg_no,
929                                                             app_picture))
[2656]930
[2514]931        return student_id
932    ###)
933
[2556]934    security.declareProtected(ModifyPortalContent,'makeStudentLevel') ###(
[1194]935    def makeStudentLevel(self,student_id):
936        "create the StudyLevel for a returning Student"
937        #import pdb;pdb.set_trace()
[1571]938        logger = logging.getLogger('WAeUPTool.makeStudentLevel')
[1194]939        students_folder = self.portal_url.getPortalObject().campus.students
940        res = self.students_catalog(id=student_id)
941        if res:
942            st = res[0]
943        course = st.course
944        matric_no = st.matric_no
945        level = st.level
946        res = self.results_import(matric_no = matric_no)
947        if res:
948            results = res
[1571]949        logger.info('%s creating Level %s' % (student_id,level))
[1194]950        #
951        # Level
952        #
953        student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id)
954        studycourse = getattr(student_obj,"study_course",None)
955        self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse)
956        l = getattr(studycourse,level,None)
957        if l is None:
958            studycourse.invokeFactory('StudentStudyLevel', level)
959            l = getattr(studycourse, level)
960            self.portal_workflow.doActionFor(l,'open',dest_container=l)
961            l.getContent().edit(mapping={'Title': "Level %s" % level})
962        ###)
963
[2000]964    security.declarePublic('getHallInfo') ###(
965    def getHallInfo(self,bed):
966        """return Hall Info"""
[828]967        info = {}
968        hall,block,room,letter = bed.split('_')
[1846]969        res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="AccoHall",id=hall)
[828]970        if res and len(res) == 1:
971            hall_brain = res[0]
972            hall_doc = hall_brain.getObject().getContent()
973        else:
974            return info
975        info['hall_title'] = hall_brain.Title
976        info['maintenance_code'] = hall_doc.maintenance_code
[1846]977        res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="ScratchCardBatch")
[828]978        batch_doc = None
979        for brain in res:
980            if brain.id.startswith(info['maintenance_code']):
981                batch_doc = brain.getObject().getContent()
982                break
983        if batch_doc is None:
[2913]984            info['maintenance_fee'] = ''
[828]985        else:
986            info['maintenance_fee'] = batch_doc.cost
987        return info
[1151]988    ###)
[828]989
[2396]990    security.declareProtected(ModifyPortalContent,'removePictureFolder') ###(
991    def removePictureFolder(self,student_id):
992        """remove picture_folder by renaming it"""
993        path = 'images'
994        picture_path = os.path.join(i_home,path,student_id)
995        if not os.path.exists(picture_path):
996            return False
997        os.rename(picture_path,picture_path + "_removed")
998        return True
999    ###)
1000
1001    security.declareProtected(ModifyPortalContent,'restorePictureFolder') ###(
1002    def restorePictureFolder(self,student_id):
1003        """restore picture_folder by renaming it"""
1004        path = 'images'
1005        picture_path = os.path.join(i_home,path,student_id)
1006        if not os.path.exists(picture_path + "_removed"):
1007            return False
1008        os.rename(picture_path + "_removed",picture_path)
1009        return True
1010    ###)
1011
[2363]1012    security.declarePublic('picturesExist') ###(
[2420]1013    def picturesExist(self, ids,student_id=None):
[2363]1014        """check if pictures exist in the filesystem"""
[2420]1015        if student_id is None:
1016            student_id = self.getStudentId()
1017        if student_id is None:
1018            return False
[2675]1019        picture_path = getImagesDir(student_id)
1020        #picture_path = os.path.join(images_base,student_id)
[2363]1021        if not os.path.exists(picture_path):
1022            return False
1023        pictures  = [picture[:picture.rfind('_')] for picture in os.listdir(picture_path)]
1024        return set(ids).issubset(set(pictures))
1025    ###)
1026
[2364]1027    security.declarePublic('picturesList') ###(
1028    def picturesList(self):
1029        """check if pictures exist in the filesystem"""
1030        path = 'images'
1031        student_id = self.getStudentId()
[2677]1032        #picture_path = os.path.join(i_home,path,student_id)
1033        picture_path = getImagesDir(student_id)
[2364]1034        if not os.path.exists(picture_path):
1035            return []
1036        return [picture[:picture.rfind('_')] for picture in os.listdir(picture_path)]
1037    ###)
1038
[2114]1039    security.declarePublic('showFsPicture') ###(
1040    def showFsPicture(self,path):
1041        """return a picture from the filesystem"""
[2675]1042        #picture_path = os.path.join(i_home,path)
1043        picture_path = os.path.join(images_base,path)
[2335]1044        response = self.REQUEST.RESPONSE
[2342]1045        #import pdb;pdb.set_trace()
[2354]1046        registry = getToolByName(self, 'mimetypes_registry')
1047        mimetype = str(registry.lookupExtension(path.lower()) or
1048                    registry.lookupExtension('file.bin'))
[2114]1049        if os.path.exists(picture_path):
[2354]1050            response.setHeader('Content-type',mimetype)
[2114]1051            return open(picture_path).read()
[2335]1052        picture_path = os.path.join(i_home,'import',path)
1053        if os.path.exists(picture_path):
1054            return open(picture_path).read()
[2114]1055    ###)
1056
[1151]1057    security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###(
1058    def deleteAllCourses(self,department="All"):
1059        ''' delete the courses'''
1060        pm = self.portal_membership
1061        member = pm.getAuthenticatedMember()
[1160]1062
[1151]1063        if str(member) not in ("henrik","joachim"):
1064            return "not possible"
1065        if department == "All":
1066            res = self.portal_catalog({'meta_type': 'Department'})
1067        if len(res) < 1:
1068            return "No Departments found"
[1160]1069
[1151]1070        deleted = []
1071        for dep in res:
1072            cf = dep.getObject().courses
1073            if cf:
1074                cf.manage_delObjects(ids=cf.objectIds())
1075                deleted.append("deleted Courses in %s" % dep.getId)
1076        return "\r".join(deleted)
[1160]1077    ###)
[1151]1078
[1572]1079    security.declareProtected(ModifyPortalContent,'getLogfileLines') ###(
1080    def getLogfileLines(self,filename="event.log",numlines=20):
1081        """Get last NUMLINES lines of logfile FILENAME.
[1160]1082
[1572]1083        Return last lines' of a file in the instances logfile directory as
1084        a list. The number of returned lines equals `numlines' or less. If
1085        less than `numlines' lines are available, the whole file ist
1086        returned. If the file can not be opened or some other error
1087        occurs, empty list is returend.
1088        """
1089        result = []
1090        lines_hit = 0
1091
1092        # We only handle files in instances' log directory...
1093        logpath = os.path.join(i_home, "log")
1094        filename = str(os.path.abspath( os.path.join( logpath, filename )))
1095        if not filename.startswith( logpath ):
1096            # Attempt to access file outside log-dir...
1097            return []
1098
1099        try:
1100            fd = file( filename, "rb" )
1101        except IOError:
1102            return []
1103        if not fd:
1104            return []
1105
1106        if os.linesep == None:
1107            linesep = '\n'
1108        else:
1109            linesep = os.linesep
1110
1111        # Try to find 'numlines' times a lineseparator, searching from end
1112        # and moving to the beginning of file...
1113        fd.seek( 0, 2) # Move to end of file...
1114        while lines_hit < numlines:
1115            if fd.read(1) == linesep[-1]: # This moves filedescriptor
1116                                          # one step forward...
1117                lines_hit += 1
1118            try:
1119                fd.seek( -2, 1) # Go two bytes back from current pos...
1120            except IOError:
1121                # We cannot go back two bytes. Maybe the file is too small...
1122                break
1123        fd.seek(2,1)
1124
1125        # Read all lines from current position...
1126        result = fd.readlines()
1127        # Remove line endings...
1128        result = [x.strip() for x in result]
1129        fd.close()
1130        return result
1131    ###)
1132
[1700]1133    security.declareProtected(ModifyPortalContent,"getCallbacksFromLog")###(
[1665]1134    def getCallbacksFromLog(self,filename):
1135        """fix Online Payment Transactions from Z2.log entries"""
1136        import transaction
1137        import random
1138        from cgi import parse_qs
1139        from urlparse import urlparse
1140        #from pdb import set_trace
1141        wftool = self.portal_workflow
1142        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1143        students_folder = self.portal_url.getPortalObject().campus.students
1144        s = r'(?P<client_ip>\S+) - (?P<member_id>\S+) \['
1145        s += r'(?P<date>.*)\] "(?P<get>.*)" (?P<codes>\d+ \d+) "'
1146        s += r'(?P<intersw>.*)" "(?P<agent>.*)"'
1147        data = re.compile(s)
1148        start = True
1149        tr_count = 1
1150        total = 0
1151        #name = 'pume_results'
1152        #name = 'epaymentsuccessful_z2log2'
1153        name = filename
1154        no_import = []
1155        imported = []
1156        logger = logging.getLogger('WAeUPTool.getFailedTransactions')
1157        try:
1158            transactions = open("%s/import/%s" % (i_home,name),"rb").readlines()
1159        except:
1160            logger.error('Error reading %s' % name)
1161            return
1162        tas = []
1163        for line in transactions:
1164            dict = {}
1165            items = data.search(line)
1166            dict['idict'] = idict = items.groupdict()
1167            #print idict
1168            #from pdb import set_trace;set_trace()
1169            urlparsed = urlparse(idict['get'][4:])
1170            #print urlparsed
1171            path = urlparsed[2].split('/')
1172            dict['student_id'] = student_id = path[8]
1173            dict['payment_id'] = payment_id = path[10]
1174            dict['qs_dict'] = qs_dict = parse_qs(urlparsed[4])
1175            tas.append(dict)
1176            tr_count += 1
1177        return tas
1178    ###)
1179
[1620]1180    security.declareProtected(ModifyPortalContent,"importOnlinePaymentTransactions")###(
1181    def importOnlinePaymentTransactions(self):
1182        """load Online Payment Transactions from CSV values"""
1183        import transaction
1184        import random
1185        #from pdb import set_trace
1186        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
[1625]1187        opt = self.online_payments_import
[1620]1188        students_folder = self.portal_url.getPortalObject().campus.students
1189        start = True
1190        tr_count = 1
1191        total = 0
1192        #name = 'pume_results'
1193        name = 'OnlineTransactions'
1194        no_import = []
1195        imported = []
1196        logger = logging.getLogger('WAeUPTool.importOnlinePaymentTransactions')
1197        try:
1198            transactions = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1199        except:
1200            logger.error('Error reading %s.csv' % name)
1201            return
1202        for pay_transaction in transactions:
1203            if start:
1204                start = False
1205                logger.info('Start loading from %s.csv' % name)
1206                s = ','.join(['"%s"' % fn for fn in pay_transaction.keys()])
1207                no_import.append('%s,"Error"' % s)
1208                format = ','.join(['"%%(%s)s"' % fn for fn in pay_transaction.keys()])
1209                format_error = format + ',"%(Error)s"'
1210            data = {}
[1644]1211
[1781]1212            # format of the first file sent by Tayo
[1643]1213            #data['datetime'] = date = DateTime.DateTime(pay_transaction['Date'])
1214            #data['student_id'] = student_id = pay_transaction['Payer ID']
1215            #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)']
1216            #data['response_code'] = response_code = pay_transaction['Resp Code']
1217            #data['amount'] = amount = pay_transaction['Amount']
[1644]1218
[1781]1219            # format of the second file sent by Tayo
[1798]1220            #data['datetime'] = date = 0
1221            #data['student_id'] = student_id = pay_transaction['Payer ID']
1222            #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)']
1223            #data['response_code'] = response_code = '00'
1224            #data['amount'] = amount = pay_transaction['Amount']
[1813]1225
[1798]1226            # format of the third file sent by Kehinde
[1643]1227            data['datetime'] = date = 0
[1798]1228            data['student_id'] = student_id = pay_transaction['customer_id']
1229            data['order_id'] = order_id = pay_transaction['merchant_reference']
[1644]1230            data['response_code'] = response_code = '00'
[1813]1231            data['amount'] = amount = pay_transaction['Amount']
[1644]1232
[1620]1233            dup = False
1234            if response_code == "12":
1235                continue
1236            try:
1237                opt.addRecord(**data)
1238            except ValueError:
1239                dup = True
1240            #from pdb import set_trace;set_trace()
1241            if dup:
1242                if response_code == "00":
[1798]1243                    try:
1244                        opt.modifyRecord(**data)
1245                    except:
1246                        logger.info("duplicate uid, order_id %(order_id)s, student_id %(student_id)s, response_code %(response_code)s" % data)
1247                        continue
[1620]1248                else:
[1674]1249                    pay_transaction['Error'] = "Duplicate order_id"
[1620]1250                    no_import.append( format_error % pay_transaction)
[1674]1251                    logger.info("duplicate order_id %(order_id)s for %(student_id)s %(response_code)s" % data)
[1620]1252                    continue
1253            tr_count += 1
1254            if tr_count > 1000:
1255                if len(no_import) > 0:
1256                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1257                             '\n'.join(no_import) + '\n')
1258                    no_import = []
[1645]1259                em = '%d transactions committed\n' % (tr_count)
[1620]1260                transaction.commit()
1261                regs = []
1262                logger.info(em)
1263                total += tr_count
1264                tr_count = 0
1265        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1266                                                '\n'.join(no_import))
1267        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1268    ###)
[2237]1269
[2264]1270    security.declareProtected(ModifyPortalContent,'mass_create_faculty') ###(
1271    def mass_create_faculty(self,mapping):
1272        "create a faculty"
1273        logger = logging.getLogger('WAeUPTool.mass_create_faculty')
1274        academics_folder = self.portal_url.getPortalObject().campus.academics
1275        fid = mapping['code']
1276        if getattr(academics_folder,fid,None) is not None:
1277            return '', "Faculty with ID: %s exists" % fid
[2267]1278        logger.info('Creating Faculty %(code)s, %(title)s' % mapping)
[2283]1279        try:
1280            academics_folder.invokeFactory('Faculty', fid)
1281        except BadRequest,E:
1282            return '', "%s" % E
[2264]1283        f = getattr(academics_folder,fid,None)
1284        f.getContent().edit(mapping=mapping)
1285        return fid,''
1286    ###)
1287
[2288]1288    security.declareProtected(ModifyPortalContent,'mass_edit_faculty') ###(
[2264]1289    def mass_edit_faculty(self,mapping):
1290        "edit a faculty"
1291        logger = logging.getLogger('WAeUPTool.mass_edit_faculty')
1292        academics_folder = self.portal_url.getPortalObject().campus.academics
1293        fid = mapping['code']
1294        f = getattr(academics_folder,fid,None)
1295        if f is None:
1296            return '', "Faculty with ID: %s does not exist" % fid
[2267]1297        logger.info('Editing Faculty %(code)s, %(title)s' % mapping)
[2264]1298        f.getContent().edit(mapping=mapping)
1299        return fid,''
1300    ###)
1301
[2234]1302    security.declareProtected(ModifyPortalContent,'mass_create_department') ###(
[2193]1303    def mass_create_department(self,mapping):
1304        "create a department in the correct faculty"
1305        logger = logging.getLogger('WAeUPTool.mass_create_department')
1306        fid = mapping['faculty_code']
1307        if getattr(self,'_v_faculties',None) is None:
1308            res = self.portal_catalog(portal_type = "Faculty")
1309            self._v_faculties = {}
1310            for f in res:
1311                self._v_faculties[f.getId] = f.getObject()
1312        f = self._v_faculties.get(fid,None)
1313        if f is None:
1314            return '', "No Faculty with ID: %s" % fid
1315        else:
1316            did = mapping.get('code')
1317            d = getattr(f,did,None)
1318            if d is None or d.portal_type == "Faculty":
[2267]1319                logger.info('Creating Department %(code)s, %(title)s' % mapping)
[2283]1320                try:
1321                    f.invokeFactory('Department', did)
1322                except BadRequest,E:
1323                    return '', "%s" % E
[2193]1324                d = getattr(f,did)
1325                d.invokeFactory('CoursesFolder','courses')
1326                courses = getattr(d,'courses')
1327                dict = {'Title': 'Courses'}
1328                courses.getContent().edit(mapping=dict)
1329                d.invokeFactory('CertificatesFolder','certificates')
1330                certificates = getattr(d,'certificates')
1331                dict = {'Title': 'Certificates'}
1332                certificates.getContent().edit(mapping=dict)
1333            d.getContent().edit(mapping=mapping)
1334        return did,''
1335    ###)
[1620]1336
[2234]1337    security.declareProtected(ModifyPortalContent,'mass_edit_department') ###(
[2193]1338    def mass_edit_department(self,mapping):
1339        "create a department in the correct faculty"
1340        logger = logging.getLogger('WAeUPTool.mass_create_department')
1341        academics_folder = self.portal_url.getPortalObject().campus.academics
1342        fid = mapping['faculty_code']
1343        did = mapping.get('code')
1344        try:
1345            d = getattr(getattr(academics_folder,fid),did,None)
1346        except KeyError:
1347            return '', "Department %s or Faculty %s wrong" % (did,fid)
1348        else:
1349            if d is None or d.portal_type == "Faculty":
[2267]1350                logger.info('Editing Department %(code)s, %(title)s' % mapping)
[2193]1351            d.getContent().edit(mapping=mapping)
1352        return did,''
1353    ###)
[1786]1354
[2265]1355    security.declareProtected(ModifyPortalContent,'mass_create_course') ###(
1356    def mass_create_course(self,mapping):
[2315]1357        #import pdb;pdb.set_trace()
[2265]1358        if getattr(self,'_v_course_list',None) is None:
1359            self._v_course_list = []
1360        if getattr(self,'_v_departments',None) is None:
1361            res = self.portal_catalog(portal_type = "Department")
[2313]1362            self._v_department_courses = {}
[2265]1363            for d in res:
[2313]1364                self._v_department_courses[d.getId] = getattr(d.getObject(),"courses",None)
[2265]1365        did = mapping['department_code']
[2313]1366        d = self._v_department_courses.get(did,None)
[2265]1367        if d is None:
[2282]1368            return '', "No Department with ID: %s" % did
[2265]1369        course_id = mapping.get('code')
1370        if course_id in self._v_course_list:
[2313]1371            return '', "Duplicate Course ID: %s" % did
[2265]1372        c = getattr(d,course_id,None)
1373        if c is not None:
[2313]1374            return '', "Duplicate Course ID: %s" % did
[2283]1375        try:
1376            d.invokeFactory('Course', course_id)
1377        except BadRequest,E:
1378            return '', "%s" % E
[2265]1379        self._v_course_list.append(course_id)
1380        c = getattr(d,course_id)
1381        c.getContent().edit(mapping=mapping)
1382        return course_id,''
1383    ###)
1384
[2282]1385    security.declareProtected(ModifyPortalContent,'mass_edit_course') ###(
1386    def mass_edit_course(self,mapping):
1387        #import pdb;pdb.set_trace()
1388        course_id = mapping.get('code')
1389        res = self.portal_catalog(id=course_id)
1390        if not res:
[2289]1391            return '', "No Course with ID: %s" % course_id
[2282]1392        c = res[0].getObject()
1393        c.getContent().edit(mapping=mapping)
1394        return course_id,''
1395    ###)
1396
1397    security.declareProtected(ModifyPortalContent,'mass_create_certificate') ###(
1398    def mass_create_certificate(self,mapping):
1399        if getattr(self,'_v_certificate_list',None) is None:
1400            self._v_certificate_list = []
[2313]1401        if getattr(self,'_v_department_certificates',None) is None:
[2282]1402            res = self.portal_catalog(portal_type = "Department")
[2313]1403            self._v_department_certificates = {}
[2282]1404            for d in res:
[2313]1405                self._v_department_certificates[d.getId] = getattr(d.getObject(),"certificates",None)
[2282]1406        did = mapping['department_code']
[2313]1407        d = self._v_department_certificates.get(did,None)
[2282]1408        if d is None:
1409            return '', "No Department with ID: %s" % did
1410        certificate_id = mapping.get('code')
1411        if certificate_id in self._v_certificate_list:
[2313]1412            return '', "Duplicate Certificate ID: %s" % did
[2282]1413        c = getattr(d,certificate_id,None)
1414        if c is not None:
[2313]1415            return '', "Duplicate Certificate ID: %s" % did
[2283]1416        try:
1417            d.invokeFactory('Certificate', certificate_id)
1418        except BadRequest,E:
1419            return '', "%s" % E
[2282]1420        self._v_certificate_list.append(certificate_id)
1421        c = getattr(d,certificate_id)
1422        c.getContent().edit(mapping=mapping)
1423        return certificate_id,''
1424    ###)
1425
1426    security.declareProtected(ModifyPortalContent,'mass_edit_certificate') ###(
1427    def mass_edit_certificate(self,mapping):
1428        #import pdb;pdb.set_trace()
1429        certificate_id = mapping.get('code')
1430        res = self.portal_catalog(id=certificate_id)
1431        if not res:
1432            return '', "No Certificate with ID: %s" % did
1433        c = res[0].getObject()
1434        c.getContent().edit(mapping=mapping)
1435        return certificate_id,''
1436    ###)
1437
[2520]1438    security.declareProtected(ModifyPortalContent,'mass_create_application') ###(
1439    def mass_create_application(self,mapping):
[2514]1440        #import pdb;pdb.set_trace()
[2547]1441        reg_no = mapping.get('reg_no')
[2514]1442        try:
1443            self.applicants_catalog.addRecord(**mapping)
1444        except ValueError:
[2548]1445            return '', "applicant record with reg_no %s already exists" % reg_no
[2546]1446        return reg_no,''
[2514]1447    ###)
1448
[2520]1449    security.declareProtected(ModifyPortalContent,'mass_edit_application') ###(
1450    def mass_edit_application(self,mapping):
[2514]1451        #import pdb;pdb.set_trace()
1452        reg_no = mapping.get('reg_no')
[2731]1453        status = mapping.get('status')
[2699]1454        res = self.applicants_catalog(reg_no = reg_no)
1455        if len(res):
[2731]1456            if res[0].status == 'created' and status != 'created':
1457                return '', "student object with id %s for %s already created, status cannot be changed" % (res[0].student_id, reg_no)
1458            elif status == 'created' and res[0].status != 'created':
1459                return '', "student object for %s has not yet been created, status cannot be set to 'created'" % (reg_no)
[2699]1460            else:
1461                self.applicants_catalog.modifyRecord(**mapping)
1462                return reg_no,''
1463        else:
[2548]1464            return '', "applicant record with reg_no %s does not exist" % reg_no
[2701]1465
[2514]1466    ###)
1467
[2501]1468    security.declareProtected(ModifyPortalContent,'mass_create_course_result') ###(
1469    def mass_create_course_result(self,mapping):
[2500]1470        #import pdb;pdb.set_trace()
[2747]1471        students_folder = self.portal_url.getPortalObject().campus.students
[2501]1472        if getattr(self,'_v_courses',None) is None:
1473            res = self.courses_catalog()
1474            self._v_courses = {}
1475            for brain in res:
[2520]1476                self._v_courses[brain.code] = brain
[2747]1477        if getattr(self,'_v_level_created',None) is None:
1478            self._v_level_created = []
[2500]1479        course_id = mapping.get('code')
[2501]1480        if course_id not in self._v_courses.keys():
[2754]1481            return '', "No course with ID: %s" % course_id
[2773]1482        id_key = ''
1483        for id_key in ('student_id','matric_no'):
1484            id_field = mapping.get(id_key,None)
1485            if id_field is not None:
1486                student_id = id_field
1487                break
1488        query = Eq(id_key,id_field)
1489        res = self.students_catalog.evalAdvancedQuery(query)
1490        if not res:
[2750]1491            return '', "no student with %(id_key)s %(id_field)s" % vars()
[2773]1492        student_rec = res[0]
[2776]1493        if id_field != "student_id":
1494            mapping['student_id'] = student_rec.id
[2766]1495        student_id = student_rec.id
1496        level_id = mapping['level_id']
1497        code = mapping['code']
[2774]1498        if student_id not in self._v_level_created:
[2747]1499            try:
1500                context = getattr(getattr(students_folder,
[2766]1501                                          "%(student_id)s" % vars()),
[2747]1502                                  'study_course')
1503            except:
[2766]1504                return '', "could not create level %(level_id)s for %(student_id)s" % vars()
1505            if level_id not in context.objectIds():
1506                context.invokeFactory('StudentStudyLevel',"%(level_id)s" % vars())
1507                level = getattr(context,"%(level_id)s" % vars())
[2747]1508                self.portal_workflow.doActionFor(level,'open')
[2754]1509                # the session string must not be copied into the level object
[2775]1510                current_verdict = getattr(student_rec,'current_verdict','')
1511                current_session = getattr(student_rec,'current_session','')
1512                if current_verdict and student_rec.current_level == level_id:
[2771]1513                    level.getContent().edit(mapping={'verdict': "%s" %
[2775]1514                                                     current_verdict,
[2771]1515                                                     'session': "%s" %
[2775]1516                                                     current_session,
[2766]1517                                                    })
1518                    self.portal_workflow.doActionFor(level,'close')
1519            self._v_level_created += student_id,
1520        mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars()
[2746]1521        for k in ('semester','credits',):
[2501]1522            mapping[k] = getattr(self._v_courses[course_id],k)
[2500]1523        try:
[2501]1524            self.course_results.addRecord(**mapping)
1525        except ValueError:
1526            return '', "course result already exists: %s" % key
1527        return key,''
[2500]1528    ###)
1529
1530    security.declareProtected(ModifyPortalContent,'mass_edit_course_result') ###(
1531    def mass_edit_course_result(self,mapping):
1532        #import pdb;pdb.set_trace()
1533        # if getattr(self,'_v_courses',None) is None:
1534        #     res = self.courses_catalog()
1535        #     self._v_courses = {}
1536        #     for brain in res:
[2520]1537        #         self._v_courses[brain.code] = brain
[2500]1538        # course_id = mapping.get('code')
1539        # if course_id not in self._v_courses.keys():
1540        #     return '', "No course with ID: %s" % did
[2776]1541        id_key = ''
1542        for id_key in ('student_id','matric_no'):
1543            id_field = mapping.get(id_key,None)
1544            if id_field is not None:
1545                student_id = id_field
1546                break
1547        query = Eq(id_key,id_field)
1548        res = self.students_catalog.evalAdvancedQuery(query)
1549        if not res:
1550            return '', "No student with %(id_field)s: %(id_key)s" % vars()
1551        if id_field != "student_id":
1552            mapping['student_id'] = res[0].id
1553        if not res:
[2766]1554            return '', "no student with %(id_key)s %(id_field)s" % vars()
[2776]1555        student_rec = res[0]
[2766]1556        student_id = student_rec.id
1557        level_id = mapping['level_id']
1558        code = mapping['code']
1559        mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars()
[2500]1560        # for k in ('semester',):
1561        #     mapping[k] = getattr(self._v_courses[course_id],k)
1562        try:
1563            self.course_results.modifyRecord(**mapping)
1564        except KeyError:
1565            return '', "No course result to edit: %s" % key
1566        return key,''
1567    ###)
1568
[2282]1569    security.declareProtected(ModifyPortalContent,'mass_create_certificate_course') ###(
1570    def mass_create_certificate_course(self,mapping):
[2320]1571        if getattr(self,'_v_courses',None) is None:
1572            res = self.courses_catalog()
[2351]1573            self._v_courses= [course.code for course in res]
[2282]1574        if getattr(self,'_v_ceritficates',None) is None:
1575            res = self.portal_catalog(portal_type = "Certificate")
1576            self._v_certificates = {}
1577            for cert in res:
1578                self._v_certificates[cert.getId] = cert.getObject()
[2284]1579        certificate_course_id = mapping.get('code')
[2351]1580        if certificate_course_id not in self._v_courses:
[2284]1581            return '', "No Course with ID: %s" % certificate_course_id
[2282]1582        cert_id = mapping['certificate_code']
1583        cert = self._v_certificates.get(cert_id,None)
1584        if cert is None:
[2284]1585            return '', "No Certificate with ID: %s" % cert_id
[2282]1586        level_id = mapping.get('level')
1587        level = getattr(cert,level_id,None)
1588        if level is None:
1589            cert.invokeFactory('StudyLevel', level_id)
1590            level = getattr(cert,level_id,None)
1591        elif hasattr(level,certificate_course_id):
1592            return '', "Duplicate CertificateCourse ID: %s in %s/ %s" %\
1593            (certificate_course_id,cert_id,level_id)
1594        level.invokeFactory('CertificateCourse', certificate_course_id)
1595        c = getattr(level,certificate_course_id)
1596        c.getContent().edit(mapping=mapping)
1597        return certificate_course_id,''
1598    ###)
1599
[2193]1600    field2types_student = {   ###(
[1756]1601                      'StudentApplication':
1602                          {'id': 'application',
1603                           'title': 'Application Data',
[1991]1604                           'wf_transition_return': 'close',
1605                           'wf_transition_admit': 'remain',
[1756]1606                           'fields':
1607                             ('jamb_reg_no',
1608                              'entry_mode',
1609                              'entry_session',
1610                              'jamb_score',
[1786]1611                              'app_email',
[2008]1612                              'jamb_age',
[2428]1613                              'jamb_state',
1614                              'jamb_lga',
[1756]1615                              )
1616                              },
[1786]1617                      #'StudentPume':
1618                      #    {'id': 'pume',
1619                      #     'title': 'Pume Data',
[1991]1620                      #     'wf_transition_return': 'close',
1621                      #     'wf_transition_admit': 'close',
[1786]1622                      #     'fields':
1623                      #       ('pume_score',
1624                      #        )
1625                      #        },
[1756]1626                      'StudentClearance':
1627                          {'id': 'clearance',
[2009]1628                           'title': 'Clearance/Eligibility Record',
[1991]1629                           'wf_transition_return': 'close',
1630                           'wf_transition_admit': 'remain',
[1756]1631                           'fields':
1632                             ('matric_no',
1633                              'nationality',
1634                              'lga',
[1804]1635                              'birthday',
[1756]1636                              )
1637                              },
1638                         'StudentPersonal':
1639                          {'id': 'personal',
1640                           'title': 'Personal Data',
[1991]1641                           'wf_transition_return': 'open',
1642                           'wf_transition_admit': 'remain',
[1756]1643                           'fields':
1644                             ('firstname',
1645                              'middlename',
1646                              'lastname',
1647                              'sex',
1648                              'email',
1649                              'phone',
1650                              'perm_address',
1651                              )
1652                              },
1653                         'StudentStudyCourse':
1654                          {'id': 'study_course',
1655                           'title': 'Study Course',
[1991]1656                           'wf_transition_return': 'open',
1657                           'wf_transition_admit': 'remain',
[1756]1658                           'fields':
1659                             ('study_course',
1660                              'current_level',
1661                              'current_session',
1662                              'current_mode',
1663                              'current_verdict',
[2478]1664                              'previous_verdict',
[1756]1665                              )
1666                              },
[2846]1667                         # 'StudentStudyLevel':
1668                         #  {'id': 'current_level',
1669                         #   'title': '',
1670                         #   'wf_transition_return': 'open',
1671                         #   'wf_transition_admit': 'remain',
1672                         #   'fields':
1673                         #     ('verdict',
1674                         #      'session',
1675                         #      )
1676                         #      },
[1898]1677                         'PaymentsFolder':
1678                          {'id': 'payments',
1679                           'title': 'Payments',
[1991]1680                           'wf_transition_return': 'open',
1681                           'wf_transition_admit': 'open',
[1898]1682                           'fields':
1683                             ()
1684                              },
[1756]1685                         }
1686    ###)
1687
[2193]1688    security.declareProtected(ModifyPortalContent,'mass_create_student') ###(
1689    def mass_create_student(self,mapping):
[1781]1690        "create a students record due import"
[2193]1691        logger = logging.getLogger('WAeUPTool.mass_create_student')
[1747]1692        students_folder = self.portal_url.getPortalObject().campus.students
[1818]1693        jamb_reg_no = mapping.get('jamb_reg_no',None)
[1747]1694        if jamb_reg_no:
1695            res = self.students_catalog(jamb_reg_no = jamb_reg_no)
1696            if res:
[1749]1697                return '',"jamb_reg_no exists"
[1818]1698        matric_no = mapping.get('matric_no',None)
[1747]1699        if matric_no:
1700            res = self.students_catalog(matric_no = matric_no)
1701            if res:
[1749]1702                return '',"matric_no exists"
[1747]1703        sid = self.waeup_tool.generateStudentId('?')
1704        students_folder.invokeFactory('Student', sid)
1705        student_obj = getattr(students_folder,sid)
[2193]1706        f2t = self.field2types_student
[1772]1707        d = {}
[1990]1708        transition = mapping.get('reg_transition','admit')
1709        if transition not in ('admit','return'):
[2008]1710            return '',"no valid transition provided"
[1747]1711        for pt in f2t.keys():
1712            student_obj.invokeFactory(pt,f2t[pt]['id'])
1713            sub_obj = getattr(student_obj,f2t[pt]['id'])
1714            sub_doc = sub_obj.getContent()
[1772]1715            d['Title'] = f2t[pt]['title']
[1747]1716            for field in f2t[pt]['fields']:
[2702]1717                d[field] = mapping.get(field,'')
[2454]1718
1719            if pt == "StudentApplication":
[2436]1720                d['jamb_sex']  = 'M'
1721                if mapping.get('sex'):
1722                    d['jamb_sex']  = 'F'
1723                d['jamb_firstname'] = mapping.get('firstname',None)
1724                d['jamb_middlename'] = mapping.get('middlename',None)
1725                d['jamb_lastname'] = mapping.get('lastname',None)
1726
[2433]1727            # if pt == "StudyCourse":
1728            #     for von,zu in (('entry_mode','current_mode'),
1729            #                    ('entry_session','current_session')):
1730            #         if mapping.get(zu,None) is None and mapping.get(von,None) is not None:
1731            #             d[zu] = mapping[von]
[1747]1732            sub_doc.edit(mapping = d)
[1990]1733
[1987]1734            #import pdb;pdb.set_trace()
1735            new_state = f2t[pt]['wf_transition_%(transition)s' % vars()]
[1898]1736            if new_state != "remain":
1737                self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
[1990]1738        self.portal_workflow.doActionFor(student_obj,transition)
[1747]1739        student_obj.manage_setLocalRoles(sid, ['Owner',])
1740        return sid,''
1741    ###)
1742
[2193]1743    security.declareProtected(ModifyPortalContent,'mass_edit_student') ###(
1744    def mass_edit_student(self,mapping):
[2754]1745        wftool = self.portal_workflow
[1781]1746        "edit a students record due import"
[2193]1747        logger = logging.getLogger('WAeUPTool.mass_edit_student')
[1774]1748        students_folder = self.portal_url.getPortalObject().campus.students
[1818]1749        sid = mapping.get('id',None)
1750        jamb_reg_no = mapping.get('jamb_reg_no',None)
1751        matric_no = mapping.get('matric_no',None)
[1875]1752        editable_keys = mapping.keys()
[1774]1753        if sid:
1754            res = self.students_catalog(id = sid)
1755            if not res:
1756                return '',"no student with id %s" % sid
[2268]1757            if matric_no and res[0].matric_no and\
[1774]1758              matric_no != res[0].matric_no:
[2268]1759                logger.info("%s, old matric_no %s overwritten with %s" % (res[0].id,res[0].matric_no,matric_no))
1760            if jamb_reg_no and res[0].jamb_reg_no and\
[1774]1761              jamb_reg_no != res[0].jamb_reg_no:
[2268]1762                logger.info("%s, old reg_no %s overwritten with %s" % (res[0].id,res[0].jamb_reg_no,jamb_reg_no))
[1844]1763        elif jamb_reg_no:
1764            res = self.students_catalog(jamb_reg_no = jamb_reg_no)
1765            if not res:
1766                return '',"no student with jamb_reg_no %s" % jamb_reg_no
[1875]1767            editable_keys.remove('jamb_reg_no')
[1774]1768        elif matric_no:
1769            res = self.students_catalog(matric_no = matric_no)
1770            if not res:
1771                return '',"no student with matric_no %s" % matric_no
[1875]1772            editable_keys.remove('matric_no')
[1965]1773
[2268]1774        ## included only to change wf state from admitted to returning
1775        #if res[0].review_state not in ('admitted','objection_raised'):
1776        #    return '%s' % res[0].id ,"student is not in state admitted or objection_raised"
1777        ## end inclusion
[1965]1778
[1774]1779        sid = res[0].id
1780        student_obj = getattr(students_folder,sid)
[2193]1781        f2t = self.field2types_student
[1774]1782        d = {}
[2265]1783        #import pdb;pdb.set_trace()
[1875]1784        any_change = False
[2846]1785        #special treatment for StudentStudyLevel
1786        d['verdict']  = mapping.get('current_verdict','')
1787        d['session']  = mapping.get('current_session','')
1788        current_level = mapping.get('current_level','')
1789        while d['session'] and d['verdict'] and current_level:
1790            sub_obj = getattr(student_obj,'study_course',None)
1791            if sub_obj is None:
1792                break
1793            level_obj = getattr(sub_obj,current_level,None)
1794            if  level_obj is None:
1795                break
1796            any_change = True
1797            level_obj.getContent().edit(mapping = d)
1798            try:
1799                wftool.doActionFor(level_obj,'close')
1800            except:
1801                pass
1802            break
[1774]1803        for pt in f2t.keys():
[2753]1804            if pt == "StudentApplication":
[1875]1805                d['jamb_sex']  = 'M'
1806                if mapping.get('sex'):
1807                    d['jamb_sex']  = 'F'
1808            intersect = set(f2t[pt]['fields']).intersection(set(editable_keys))
[2753]1809            if intersect and pt not in ('StudentStudyLevel',):
1810                object_id = f2t[pt]['id']
1811                sub_obj = getattr(student_obj,object_id,None)
[1875]1812                if sub_obj is None:
1813                    try:
[2753]1814                        student_obj.invokeFactory(pt,object_id)
[1875]1815                    except:
1816                        continue
[2753]1817                    sub_obj = getattr(student_obj,object_id)
1818                    if f2t[pt]['title'] != '':
1819                        d['Title'] = f2t[pt]['title']
[1875]1820                sub_doc = sub_obj.getContent()
1821                for field in intersect:
1822                    changed = False
[2478]1823                    if getattr(sub_doc,field,None) != mapping.get(field,''):
[1875]1824                        any_change = True
1825                        changed = True
1826                        d[field] = mapping.get(field,'')
1827                    if changed:
1828                        sub_doc.edit(mapping = d)
[1904]1829
[1905]1830
[2268]1831        ## included only to change wf state from admitted to returning
1832        #    if res[0].review_state in ('admitted','objection_raised'):
1833        #        new_state = f2t[pt]['wf_transition_return']
1834        #        sub_obj = getattr(student_obj,f2t[pt]['id'],None)
1835        #        if sub_obj and new_state != "remain":
1836        #            try:
1837        #                self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
1838        #            except:
1839        #                #logger.info('%s, wf transition %s of %s failed' % (sid,new_state,sub_obj.id))
1840        #                pass
1841        #if res[0].review_state in ('admitted','objection_raised'):
1842        #    wfaction = 'return'
1843        #    try:
1844        #        self.portal_workflow.doActionFor(student_obj,wfaction)
1845        #        logger.info('%s, wf state changed' % sid)
1846        #        any_change = True
1847        #    except:
1848        #        logger.info('%s, wf transition failed, old state = %s' % (sid,res[0].review_state))
1849        #        pass
1850        ## end inclusion
[1904]1851
[1905]1852
[1875]1853        if any_change:
1854            return sid,''
1855        else:
1856            return sid,'not modified'
[1774]1857    ###)
[1905]1858
[1730]1859    security.declareProtected(ModifyPortalContent,"importData")###(
[2259]1860    def importData(self,filename,name,edit=False,bypass_queue_catalog=False):
[1730]1861        """load data from CSV values"""
1862        import transaction
1863        import random
[1775]1864
[1773]1865        pm = self.portal_membership
[1775]1866        member = pm.getAuthenticatedMember()
1867
[1747]1868        logger = logging.getLogger('WAeUPTool.importData')
[1730]1869        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1870        students_folder = self.portal_url.getPortalObject().campus.students
1871        start = True
[2495]1872        tr_count = 0
[1756]1873        total_imported = 0
1874        total_not_imported = 0
[1730]1875        total = 0
[1749]1876        iname = "import_%s" % name
[2754]1877        if iname in ('import_application','import_course_result',):
[2528]1878            commit_after = 2000
1879        else:
1880            commit_after = 100
[1747]1881        stool = getToolByName(self, 'portal_schemas')
1882        ltool = getToolByName(self, 'portal_layouts')
1883        schema = stool._getOb(iname)
[1730]1884        if schema is None:
[1772]1885            em = 'No such schema %s' % iname
1886            logger.error('No such schema %s' % iname)
[1756]1887            return em
[1747]1888        layout = ltool._getOb(iname)
[1730]1889        if layout is None:
[1772]1890            em = 'No such layout %s' % iname
[1756]1891            logger.error(em)
1892            return em
[1730]1893        validators = {}
1894        for widget in layout.keys():
[1749]1895            validators[widget] = layout[widget].validate
[2289]1896        mode = "create"
[1774]1897        if edit:
[2289]1898            mode = "edit"
1899        importer_name = "mass_%(mode)s_%(name)s" % vars()
[1772]1900        importer = getattr(self, '%s' % importer_name,None)
1901        if importer is None:
1902            em = 'No importer function %s' % importer_name
[1756]1903            logger.error(em)
1904            return em
[1747]1905        not_imported = []
[1730]1906        imported = []
1907        try:
[1747]1908            items = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb"))
[1730]1909        except:
[1756]1910            em = 'Error reading %s.csv' % filename
1911            logger.error(em)
1912            return em
[1730]1913        for item in items:
[2704]1914            #import pdb;pdb.set_trace()
[2703]1915            item = dict((k.strip(),l.strip()) for (k,l) in item.items())
[1730]1916            if start:
1917                start = False
[1747]1918                adapters = [MappingStorageAdapter(schema, item)]
[2315]1919                logger.info('%s starts import from %s.csv in %s mode with schema and layout %s' % (member,filename,mode,iname))
[2094]1920                attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb")).next()
[2702]1921                import_keys = [k.strip() for k in attrs if not k.startswith('ignore')]
[1804]1922                diff2schema = set(import_keys).difference(set(schema.keys()))
1923                diff2layout = set(import_keys).difference(set(layout.keys()))
1924                if diff2schema:
[2282]1925                    em = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema)
[1804]1926                    return em
[2433]1927                if mode == "create":
[2436]1928                    required_keys = [layout.getIdUnprefixed(id)
[2433]1929                                     for id,widget in layout.objectItems()
1930                                     if widget.is_required]
1931                    if not set(required_keys).issubset(set(import_keys)):
1932                        diff2import = set(required_keys).difference(set(import_keys))
1933                        em = 'required key(s): "%s" not found in heading' % ", ".join(diff2import)
1934                        return em
[1753]1935                s = ','.join(['"%s"' % fn for fn in import_keys])
[1772]1936                open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(s + ',"Error"'+ '\n')
1937                s = '"id",' + s
[1756]1938                open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(s + '\n')
[1730]1939                format = ','.join(['"%%(%s)s"' % fn for fn in import_keys])
1940                format_error = format + ',"%(Error)s"'
[1756]1941                format = '"%(id)s",'+ format
[2267]1942
[2094]1943            dm = DataModel(item, adapters,context=self)
[1747]1944            ds = DataStructure(data=item,datamodel=dm)
1945            error_string = ""
[2495]1946            total += 1
[2704]1947            error_count = 0
[1730]1948            for k in import_keys:
[2289]1949                if not validators[k](ds,mode=mode):
[2704]1950                    if error_count:
[2710]1951                        error_string += ' ++ '
[2704]1952                    error_string += "%s: %s" % (k,
[2264]1953                                                  self.translation_service(ds.getError(k),
1954                                                                           ds.getErrorMapping(k)))
[2704]1955                    error_count += 1
[1747]1956            if not error_string:
[2433]1957                temp_item = item.copy()
1958                temp_item.update(dm)
[2435]1959                temp_item['id'],error = importer(temp_item)
[1747]1960                if error:
1961                    error_string += error
[2433]1962                else:
1963                    item = temp_item
[2436]1964            if error_string:
[1747]1965                item['Error'] = error_string
1966                not_imported.append(format_error % item)
[1756]1967                total_not_imported += 1
[1747]1968            else:
[1821]1969                em = format % item
1970                imported.append(em)
[1875]1971                tr_count += 1
[1756]1972                total_imported += 1
[2495]1973                logger.info("%(total_imported)d of %(total)d %(em)s" % vars())
[2520]1974
[2528]1975            if total and not total % commit_after:
[1875]1976                transaction.commit()
[1747]1977                if len(not_imported) > 0:
1978                    open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
1979                             '\n'.join(not_imported) + '\n')
1980                    not_imported = []
1981                if len(imported) > 0:
1982                    open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
[1772]1983                             '\n'.join(imported) + '\n')
[1747]1984                    imported = []
[1730]1985                em = '%d transactions committed\n' % (tr_count)
1986                regs = []
1987                logger.info(em)
1988                tr_count = 0
[2528]1989                #if total > 100:
1990                #    return
[1756]1991        if len(imported) > 0:
[1747]1992            open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
1993                                                '\n'.join(imported))
[1756]1994        if len(not_imported) > 0:
[1747]1995            open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
1996                                                '\n'.join(not_imported))
[2872]1997        em = "Finished import from %s.csv: %d imported, %d not imported (of total %d)" % (filename,total_imported,total_not_imported,total)
[2495]1998        logger.info(em)
[1756]1999        return em
[1730]2000    ###)
2001
[2292]2002    security.declareProtected(ModifyPortalContent,"moveImagesToFS")###(
2003    def moveImagesToFS(self,student_id="O738726"):
2004        "move the images to the filesystem"
[2675]2005        images_dir = getImagesDir(student_id)
2006        #images_dir = os.path.join("%s" % images_base,student_id)
[2292]2007        student_folder = getattr(self.portal_url.getPortalObject().campus.students,student_id)
2008        stool = getToolByName(self, 'portal_schemas')
2009        schemas = ['student_application',
2010                   'student_clearance',
2011                   ]
2012        created = False
2013        for schema_id in schemas:
2014            schema = stool._getOb(schema_id)
2015            object = getattr(student_folder,schema_id[len('student_'):],None)
2016            if object is None:
2017                continue
2018            doc = object.getContent()
2019            for key in schema.keys():
2020                if schema[key].meta_type != "CPS Image Field":
2021                    continue
2022                #import pdb;pdb.set_trace()
[2351]2023                image = getattr(doc,key,None)
2024                if not image or not hasattr(image,"data"):
[2292]2025                    continue
2026                if not created:
2027                    if not os.path.exists(images_dir):
2028                        os.mkdir(images_dir)
2029                    created = True
2030                filename = os.path.join(images_dir,"%(key)s_%(student_id)s.jpg" % vars())
2031                open(filename,"wb").write(str(image.data))
[2500]2032    ###)
[2292]2033
[2300]2034    security.declareProtected(ModifyPortalContent,"movePassportToFS")###(
2035    def movePassportToFS(self,student_id="O738726"):
2036        "move the passports to the filesystem"
2037        images_dir = os.path.join("%s" % i_home,'passports')
2038        student_folder = getattr(self.portal_url.getPortalObject().campus.students,student_id)
2039        stool = getToolByName(self, 'portal_schemas')
2040        schemas = ['student_application',
2041                   #'student_clearance',
2042                   ]
2043        created = False
2044        for schema_id in schemas:
2045            schema = stool._getOb(schema_id)
2046            object = getattr(student_folder,schema_id[len('student_'):],None)
2047            if object is None:
2048                continue
2049            doc = object.getContent()
2050            for key in schema.keys():
2051                if schema[key].meta_type != "CPS Image Field":
2052                    continue
2053                #import pdb;pdb.set_trace()
2054                image = getattr(doc,key)
2055                if not hasattr(image,"data"):
2056                    continue
2057                if not created:
2058                    if not os.path.exists(images_dir):
2059                        os.mkdir(images_dir)
2060                    created = True
2061                filename = os.path.join(images_dir,"%(student_id)s.jpg" % vars())
2062                open(filename,"wb").write(str(image.data))
[2500]2063    ###)
[2300]2064
[828]2065InitializeClass(WAeUPTool)
Note: See TracBrowser for help on using the repository browser.