source: WAeUP_SRP/trunk/WAeUPTool.py @ 1825

Last change on this file since 1825 was 1821, checked in by joachim, 18 years ago

fix for #227 and #226

  • Property svn:keywords set to Id
File size: 39.0 KB
RevLine 
[197]1#-*- mode: python; mode: fold -*-
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 1821 2007-05-25 08:35:49Z 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
[197]29
[1747]30from Products.CPSSchemas.DataStructure import DataStructure
31from Products.CPSSchemas.DataModel import DataModel
32from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter
[828]33from Products.CMFCore.ActionProviderBase import ActionProviderBase
34from Products.CMFCore.permissions import View
35from Products.ZCatalog.ZCatalog import ZCatalog
36from Products.CMFCore.permissions import ModifyPortalContent
[197]37from Products.CMFCore.utils import UniqueObject
[1194]38from Products.CMFCore.URLTool import URLTool
[1747]39from Products.CMFCore.utils import getToolByName
[1151]40from Students import makeCertificateCode
[1285]41from Globals import package_home,INSTANCE_HOME
42p_home = package_home(globals())
43i_home = INSTANCE_HOME
[1620]44import DateTime
45import logging
[1170]46import transaction
[1620]47import csv,re,os
[1707]48from Products.AdvancedQuery import Eq, Between, Le,In
[197]49
[1707]50def getObject(object,name):
51    if object.hasObject(name):
52        return getattr(object,name)
53    return None
[1720]54
[828]55class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase):
[197]56    """WAeUP tool"""
57
[828]58    id = 'waeup_tool'
[197]59    meta_type = 'WAeUP Tool'
[828]60    _actions = ()
[197]61
62    security = ClassSecurityInfo()
[828]63    security.declareObjectProtected(View)
[197]64
[828]65    manage_options = ( ActionProviderBase.manage_options
66                     + SimpleItem.manage_options
67                     )
68
[1818]69    def rwrite(self,s): ###(
[1707]70        response = self.REQUEST.RESPONSE
71        response.setHeader('Content-type','text/html; charset=ISO-8859-15')
72        response.write("%s<br />\r\n" % s)
[1818]73    ###)
[1174]74
[1818]75    def sleep(self,secs): ###(
76        "sleep"
77        import time
78        time.sleep(secs)
79        return
80   
81###)
82
83    security.declareProtected(ModifyPortalContent,'openLog') ###(
[1716]84    def openLog(self,name):
85        """open a log file"""
86        version = 1
87        path = "%s/log/%s_%d.log" % (i_home,name,version)
88        while os.path.exists(path):
89            version += 1
90            path = "%s/log/%s_%d.log" % (i_home,name,version)
91        log = open(path,"w")
92        return log
93
[1818]94###)
95
96    security.declareProtected(ModifyPortalContent,'writeLog') ###(
[1716]97    def writeLog(self,logfile,s):
98        """write to the log file"""
99        logfile.write(s)
100
[1818]101###)
[1720]102
[1151]103    def generateStudentId(self,letter): ###(
104        import random
105        r = random
[1194]106        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
107        if letter == '?':
[1151]108            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
109        sid = "%c%d" % (letter,r.randint(99999,1000000))
[1720]110        students = self.portal_url.getPortalObject().campus.students
[1721]111##        while hasattr(students, sid):
112##            sid = "%c%d" % (letter,r.randint(99999,1000000))
113        while self.students_catalog(id = sid):
[1720]114            sid = "%c%d" % (letter,r.randint(99999,1000000))
[1151]115        return sid
[1460]116    ###)
[1415]117
118    def generatePassword(self,s=None): ###(
119        import random
120        r = random
121        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
122        if s is None:
123            s = 'abcdefghklmnpqrstuvwxy23456789'
124        pw = ''
125        while len(pw) < 6:
126            pw += r.choice(s)
127        return pw
[1151]128    ###)
[828]129
[1261]130    security.declareProtected(ModifyPortalContent,'getCredential') ###(
[1250]131    def getCredential(self,student_id):
132        "return a student password"
133        student_entry = getattr(self.portal_directories.students,student_id,None)
134        if student_entry is None:
135            return None
[1263]136        return getattr(student_entry,"password","not set")
[1261]137    ###)
[1250]138
[1460]139    security.declarePublic('checkPassword') ###(
[1467]140    def checkPassword(self,student_id,password):
[1460]141        "return a student password"
142        student_entry = getattr(self.portal_directories.students,student_id,None)
143        if student_entry is None:
144            return False
145        return getattr(student_entry,"password","not set") == password
146    ###)
147
[1467]148    security.declarePublic('editPassword') ###(
149    def editPassword(self,student_id,password):
150        "edit a student password"
151        student_entry = getattr(self.portal_directories.students,student_id,None)
152        if student_entry is None:
[1571]153            return
[1467]154        setattr(student_entry,'password',password)
155    ###)
156
[1647]157    security.declareProtected(View,'doCommit') ###(
[1401]158    def doCommit(self,logger=None):
159        "commit some transactions"
160        transaction.commit()
161    ###)
162
[1285]163    security.declarePublic('loadStudentFoto') ###(
[1813]164    def loadStudentFoto(self,student,filename,folder):
[1285]165        "return a student passport picture"
166        app_doc = student.application.getContent()
[1813]167        #clear = student.clearance
168        #clear_doc = clear.getContent()
169        #matric_no = clear_doc.matric_no.upper()
170        picture1 ="%s/import/%s/%s.jpg" % (i_home,folder,filename)
171        picture2 ="%s/import/%s/%s.JPG" % (i_home,folder,filename)
[1285]172        #import pdb;pdb.set_trace()
[1286]173        if os.path.exists(picture1):
174            file = open(picture1)
175        elif os.path.exists(picture2):
[1287]176            file = open(picture2)
[1286]177        else:
[1287]178            return "passport picture not found %s" % picture1
[1286]179
[1285]180        outfile = file.read()
181        app_doc.manage_addFile('passport',
182                               file=outfile,
[1818]183                               title="%s.jpg" % filename)
[1286]184        return "successfully loaded passport picture"
[1285]185    ###)
186
[1170]187    security.declareProtected(ModifyPortalContent,'createOne') ###(
[1194]188    def createOne(self,students_folder,student_brain,letter,commit=False):
189        sid = self.waeup_tool.generateStudentId(letter)
[1170]190        students_folder.invokeFactory('Student', sid)
191        student = getattr(students_folder,sid)
192        self.portal_workflow.doActionFor(student,'return')
193        student.manage_setLocalRoles(sid, ['Owner',])
194        matric_no = student_brain.matric_no
195        jamb_reg_no = student_brain.Entryregno
196        self.students_catalog.addRecord(id = sid,
197                                           matric_no = matric_no,
198                                           jamb_reg_no = jamb_reg_no,
199                                           sex = student_brain.Sex == "F",
200                                           name = "%s %s %s" % (student_brain.Firstname,
201                                                                student_brain.Middlename,
202                                                                student_brain.Lastname)
203                                        )
204        if commit:
205            transaction.commit()
206        return sid,jamb_reg_no
207    ###)
208
[1752]209    security.declareProtected(ModifyPortalContent,'addStudent') ###(
210    def addStudent(self,dict):
[1415]211        students_folder = self.portal_url.getPortalObject().campus.students
212        sid = self.waeup_tool.generateStudentId('?')
213        students_folder.invokeFactory('Student', sid)
214        student_obj = getattr(students_folder,sid)
215        password = self.generatePassword()
216        self.makeStudentMember(sid,password)
[1728]217        f2t = self.student_field2types
[1772]218        #from pdb import set_trace; set_trace()
219        d = {}
220        d['jamb_sex']  = 'M'
221        if dict.get('sex'):
222            d['jamb_sex']  = 'F'
[1728]223        for pt in f2t.keys():
224            student_obj.invokeFactory(pt,f2t[pt]['id'])
225            sub_obj = getattr(student_obj,f2t[pt]['id'])
226            sub_doc = sub_obj.getContent()
227            self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj)
[1772]228            d['Title'] = f2t[pt]['title']
[1728]229            for field in f2t[pt]['fields']:
[1749]230                d[field] = dict.get(field,'')
[1728]231            sub_doc.edit(mapping = d)
[1781]232            new_state = f2t[pt]['wf_transition']
[1772]233            if new_state != "remain":
234                self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
[1728]235        entry_session = dict.get('entry_session')
[1415]236        wfaction = 'return'
[1728]237        if entry_session == self.getSessionId():
[1415]238            wfaction = 'admit'
239        self.portal_workflow.doActionFor(student_obj,wfaction)
240        student_obj.manage_setLocalRoles(sid, ['Owner',])
241        return sid,password
242    ###)
243
[1151]244    security.declarePublic('getCertificateBrain') ###(
245    def getCertificateBrain(self,cert_id):
246        "do it"
247        res = ZCatalog.searchResults(self.portal_catalog,
248                                {'portal_type':"Certificate",
249                                      'id': cert_id})
250        if res:
251            return res[0]
252        return None
253    ###)
[1160]254
[1756]255    security.declareProtected(ModifyPortalContent,'get_csv_filenames') ###(
256    def get_csv_filenames(self):
257        "do it"
[1759]258        files = [file for file in os.listdir("%s/import/" % (i_home))
[1756]259                 if file.endswith('.csv') and file.find('imported') == -1]
260        return files
261    ###)
262
[1151]263    security.declarePublic('findStudentByMatricelNo') ###(
264    def findStudentByMatricelNo(self,matric_no):
265        "do it"
266        res = ZCatalog.searchResults(self.portal_catalog,
267                                {'portal_type':"StudentClearance",
268                                 'SearchableText': matric_no})
269        if res:
270            return res[0]
271        return None
272    ###)
273
274    security.declarePublic('makeStudentMember') ###(
275    def makeStudentMember(self,sid,password='uNsEt'):
276        """make the student a member"""
277        membership = self.portal_membership
278        membership.addMember(sid,
279                             password ,
280                             roles=('Member',
281                                     'Student',
282                                     ),
283                             domains='',
284                             properties = {'memberareaCreationFlag': False,
285                                           'homeless': True},)
286        member = membership.getMemberById(sid)
287        self.portal_registration.afterAdd(member, sid, password, None)
[1261]288        #self.manage_setLocalRoles(sid, ['Owner',])
[1151]289    ###)
[1160]290
[1151]291    security.declarePublic('makeStudentData') ###(
[1158]292    def makeStudentData(self,student_id,email=None,phone_nr=None):
[1151]293        "create Datastructure for a returning Student"
[1406]294        #import pdb;pdb.set_trace()
[1571]295        logger = logging.getLogger('WAeUPTool.makeStudentData')
[1151]296        students_folder = self.portal_url.getPortalObject().campus.students
[1794]297        #res = self.students_catalog(id=student_id)
298        #if res:
299        #    st = res[0]
300        #res = self.returning_import(matric_no = st.matric_no)
[1798]301        res = self.returning_import(id = student_id)
[1151]302        if res:
[1160]303            student = res[0]
[1794]304        else:
[1816]305            logger.info('Id %s not found in returning_import' % student_id)
[1794]306            return
[1580]307        logger.info('%s creates data structure' % student_id)
[1794]308        s_results = self.results_import(matric_no = student.matric_no)
[1816]309        if s_results:
310            lnr = self.getLevelFromResultsCosCode(s_results)
311            level = "%d00" % lnr
312            verdict,eligible = self.getVerdict(s_results[0].Verdict)
313            if eligible:
314                level = "%d00" % (lnr + 1)
315        else:
316            logger.info('matric_no %s not found in results_import' % student.matric_no)
317            level = 0
318            verdict = 'N/A'
[1171]319        #student should not be allowed to perform this transition
[1174]320        #wftool = self.portal_workflow
321        #wftool.doActionFor(student,'return')
[1151]322        certcode_org = student.Coursemajorcode
323        certcode = makeCertificateCode(certcode_org)
324        certificate_brain = self.getCertificateBrain(certcode)
325        if not certificate_brain:
326            em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org)
327            logger.info(em)
328        matric_no = student.matric_no
329        sid = student_id
330        student_obj = getattr(students_folder,sid)
331        student_obj.invokeFactory('StudentApplication','application')
332        application = student_obj.application
[1169]333        self.portal_workflow.doActionFor(application,'open',dest_container=application)
[1151]334        da = {'Title': 'Application Data'}
335        student_obj.invokeFactory('StudentPersonal','personal')
336        da['jamb_reg_no'] = student.Entryregno
[1462]337        em = self.getEntryMode(student.Entryregno)
[1460]338##        em = student.Mode_of_Entry
339##        if em in ('DIRECT', 'DIRECT ENTRY',):
340##            em = 'DE'
341##        elif em in ('U.M.E', 'UNE',):
342##            em = 'UME'
343##        elif not em:
344##            em = "unknown"
[1401]345        da['entry_mode'] = em
[1151]346        personal = student_obj.personal
347        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
348        dp = {'Title': 'Personal Data'}
349        student_obj.invokeFactory('StudentClearance','clearance')
350        clearance = student_obj.clearance
[1169]351        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
[1151]352        dc = {'Title': 'Clearance/Eligibility Record'}
353        dc['matric_no'] = matric_no
354        state = student.State
355        lga = student.LGA
356        if state and lga:
357            lga =  state + ' / ' + lga
358        else:
359            lga = "None"
[1174]360        da['jamb_lga'] = dc['lga'] = lga
[1173]361        da['app_email'] = dp['email'] = email
362        da['app_mobile'] = dp['phone'] = phone_nr
[1411]363        dp['firstname'] = student.Firstname
364        dp['middlename'] = student.Middlename
365        dp['lastname'] = student.Lastname
366        da['jamb_lastname'] = "%s %s %s" % (student.Firstname,student.Middlename,student.Lastname)
[1174]367        da['jamb_sex'] = student.Sex
[1151]368        dp['sex'] = student.Sex == 'F'
369        dp['perm_address'] = student.Permanent_Address
370        application.getContent().edit(mapping=da)
[1169]371        self.portal_workflow.doActionFor(application,'close',dest_container=application)
[1151]372        personal.getContent().edit(mapping=dp)
373        clearance.getContent().edit(mapping=dc)
[1169]374        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
[1759]375##        catd = {}
376##        catd['id'] = sid
377##        catd['entry_mode']= da['entry_mode']
378##        catd['matric_no'] = matric_no
379##        catd['jamb_reg_no'] = da['jamb_reg_no']
380##        catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
381##        catd['sex'] = dp['sex']
382##        catd['level'] = level
383##        catd['verdict'] = verdict
384##        if certificate_brain:
385##            cpath = certificate_brain.getPath().split('/')
386##            catd['faculty'] = cpath[-4]
387##            catd['department'] = cpath[-3]
388##            catd['course'] = certcode
389##        self.students_catalog.modifyRecord(**catd)
[1151]390        #
391        # Study Course
392        #
393        student_obj.invokeFactory('StudentStudyCourse','study_course')
394        studycourse = student_obj.study_course
395        self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse)
396        dsc = {}
397        dsc['study_course'] = certcode
[1401]398        dsc['current_level'] = level
399        dsc['current_verdict'] = verdict
[1759]400        dsc['current_session'] = '06'
[1151]401        studycourse.getContent().edit(mapping=dsc)
402        #
403        # Level
404        #
[1198]405##        l = getattr(studycourse,level,None)
406##        if l is None:
407##            studycourse.invokeFactory('StudentStudyLevel', level)
408##            l = getattr(studycourse, level)
409##            self.portal_workflow.doActionFor(l,'open',dest_container=l)
410##            l.getContent().edit(mapping={'Title': "Level %s" % level})
[1194]411###)
[1160]412
[1194]413    security.declarePublic('makeStudentLevel') ###(
414    def makeStudentLevel(self,student_id):
415        "create the StudyLevel for a returning Student"
416        #import pdb;pdb.set_trace()
[1571]417        logger = logging.getLogger('WAeUPTool.makeStudentLevel')
[1194]418        students_folder = self.portal_url.getPortalObject().campus.students
419        res = self.students_catalog(id=student_id)
420        if res:
421            st = res[0]
422        course = st.course
423        matric_no = st.matric_no
424        level = st.level
425        res = self.results_import(matric_no = matric_no)
426        if res:
427            results = res
[1571]428        logger.info('%s creating Level %s' % (student_id,level))
[1194]429        #
430        # Level
431        #
432        student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id)
433        studycourse = getattr(student_obj,"study_course",None)
434        self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse)
435        l = getattr(studycourse,level,None)
436        if l is None:
437            studycourse.invokeFactory('StudentStudyLevel', level)
438            l = getattr(studycourse, level)
439            self.portal_workflow.doActionFor(l,'open',dest_container=l)
440            l.getContent().edit(mapping={'Title': "Level %s" % level})
441        ###)
442
[1151]443    security.declarePublic('getAccommodationInfo') ###(
[828]444    def getAccommodationInfo(self,bed):
445        """return Accommodation Info"""
446        info = {}
447        hall,block,room,letter = bed.split('_')
448        res = ZCatalog.searchResults(self.portal_catalog,portal_type="AccoHall",id=hall)
449        if res and len(res) == 1:
450            hall_brain = res[0]
451            hall_doc = hall_brain.getObject().getContent()
452        else:
453            return info
454        info['hall_title'] = hall_brain.Title
455        info['maintenance_code'] = hall_doc.maintenance_code
456        res = ZCatalog.searchResults(self.portal_catalog,portal_type="ScratchCardBatch")
457        batch_doc = None
458        for brain in res:
459            if brain.id.startswith(info['maintenance_code']):
460                batch_doc = brain.getObject().getContent()
461                break
462        if batch_doc is None:
463            info['maintenance_fee'] = None
464        else:
465            info['maintenance_fee'] = batch_doc.cost
466        return info
[1151]467    ###)
[828]468
[1151]469    security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###(
470    def deleteAllCourses(self,department="All"):
471        ''' delete the courses'''
472        pm = self.portal_membership
473        member = pm.getAuthenticatedMember()
[1160]474
[1151]475        if str(member) not in ("henrik","joachim"):
476            return "not possible"
477        if department == "All":
478            res = self.portal_catalog({'meta_type': 'Department'})
479        if len(res) < 1:
480            return "No Departments found"
[1160]481
[1151]482        deleted = []
483        for dep in res:
484            cf = dep.getObject().courses
485            if cf:
486                cf.manage_delObjects(ids=cf.objectIds())
487                deleted.append("deleted Courses in %s" % dep.getId)
488        return "\r".join(deleted)
[1160]489    ###)
[1151]490
[1572]491    security.declareProtected(ModifyPortalContent,'getLogfileLines') ###(
492    def getLogfileLines(self,filename="event.log",numlines=20):
493        """Get last NUMLINES lines of logfile FILENAME.
[1160]494
[1572]495        Return last lines' of a file in the instances logfile directory as
496        a list. The number of returned lines equals `numlines' or less. If
497        less than `numlines' lines are available, the whole file ist
498        returned. If the file can not be opened or some other error
499        occurs, empty list is returend.
500        """
501        result = []
502        lines_hit = 0
503
504        # We only handle files in instances' log directory...
505        logpath = os.path.join(i_home, "log")
506        filename = str(os.path.abspath( os.path.join( logpath, filename )))
507        if not filename.startswith( logpath ):
508            # Attempt to access file outside log-dir...
509            return []
510
511        try:
512            fd = file( filename, "rb" )
513        except IOError:
514            return []
515        if not fd:
516            return []
517
518        if os.linesep == None:
519            linesep = '\n'
520        else:
521            linesep = os.linesep
522
523        # Try to find 'numlines' times a lineseparator, searching from end
524        # and moving to the beginning of file...
525        fd.seek( 0, 2) # Move to end of file...
526        while lines_hit < numlines:
527            if fd.read(1) == linesep[-1]: # This moves filedescriptor
528                                          # one step forward...
529                lines_hit += 1
530            try:
531                fd.seek( -2, 1) # Go two bytes back from current pos...
532            except IOError:
533                # We cannot go back two bytes. Maybe the file is too small...
534                break
535        fd.seek(2,1)
536
537        # Read all lines from current position...
538        result = fd.readlines()
539        # Remove line endings...
540        result = [x.strip() for x in result]
541        fd.close()
542        return result
543    ###)
544
[1700]545    security.declareProtected(ModifyPortalContent,"getCallbacksFromLog")###(
[1665]546    def getCallbacksFromLog(self,filename):
547        """fix Online Payment Transactions from Z2.log entries"""
548        import transaction
549        import random
550        from cgi import parse_qs
551        from urlparse import urlparse
552        #from pdb import set_trace
553        wftool = self.portal_workflow
554        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
555        students_folder = self.portal_url.getPortalObject().campus.students
556        s = r'(?P<client_ip>\S+) - (?P<member_id>\S+) \['
557        s += r'(?P<date>.*)\] "(?P<get>.*)" (?P<codes>\d+ \d+) "'
558        s += r'(?P<intersw>.*)" "(?P<agent>.*)"'
559        data = re.compile(s)
560        start = True
561        tr_count = 1
562        total = 0
563        #name = 'pume_results'
564        #name = 'epaymentsuccessful_z2log2'
565        name = filename
566        no_import = []
567        imported = []
568        logger = logging.getLogger('WAeUPTool.getFailedTransactions')
569        try:
570            transactions = open("%s/import/%s" % (i_home,name),"rb").readlines()
571        except:
572            logger.error('Error reading %s' % name)
573            return
574        tas = []
575        for line in transactions:
576            dict = {}
577            items = data.search(line)
578            dict['idict'] = idict = items.groupdict()
579            #print idict
580            #from pdb import set_trace;set_trace()
581            urlparsed = urlparse(idict['get'][4:])
582            #print urlparsed
583            path = urlparsed[2].split('/')
584            dict['student_id'] = student_id = path[8]
585            dict['payment_id'] = payment_id = path[10]
586            dict['qs_dict'] = qs_dict = parse_qs(urlparsed[4])
587            tas.append(dict)
588            tr_count += 1
589        return tas
590    ###)
591
[1620]592    security.declareProtected(ModifyPortalContent,"importOnlinePaymentTransactions")###(
593    def importOnlinePaymentTransactions(self):
594        """load Online Payment Transactions from CSV values"""
595        import transaction
596        import random
597        #from pdb import set_trace
598        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
[1625]599        opt = self.online_payments_import
[1620]600        students_folder = self.portal_url.getPortalObject().campus.students
601        start = True
602        tr_count = 1
603        total = 0
604        #name = 'pume_results'
605        name = 'OnlineTransactions'
606        no_import = []
607        imported = []
608        logger = logging.getLogger('WAeUPTool.importOnlinePaymentTransactions')
609        try:
610            transactions = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
611        except:
612            logger.error('Error reading %s.csv' % name)
613            return
614        for pay_transaction in transactions:
615            if start:
616                start = False
617                logger.info('Start loading from %s.csv' % name)
618                s = ','.join(['"%s"' % fn for fn in pay_transaction.keys()])
619                no_import.append('%s,"Error"' % s)
620                format = ','.join(['"%%(%s)s"' % fn for fn in pay_transaction.keys()])
621                format_error = format + ',"%(Error)s"'
622            data = {}
[1644]623
[1781]624            # format of the first file sent by Tayo
[1643]625            #data['datetime'] = date = DateTime.DateTime(pay_transaction['Date'])
626            #data['student_id'] = student_id = pay_transaction['Payer ID']
627            #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)']
628            #data['response_code'] = response_code = pay_transaction['Resp Code']
629            #data['amount'] = amount = pay_transaction['Amount']
[1644]630
[1781]631            # format of the second file sent by Tayo
[1798]632            #data['datetime'] = date = 0
633            #data['student_id'] = student_id = pay_transaction['Payer ID']
634            #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)']
635            #data['response_code'] = response_code = '00'
636            #data['amount'] = amount = pay_transaction['Amount']
[1813]637
[1798]638            # format of the third file sent by Kehinde
[1643]639            data['datetime'] = date = 0
[1798]640            data['student_id'] = student_id = pay_transaction['customer_id']
641            data['order_id'] = order_id = pay_transaction['merchant_reference']
[1644]642            data['response_code'] = response_code = '00'
[1813]643            data['amount'] = amount = pay_transaction['Amount']
[1644]644
[1620]645            dup = False
646            if response_code == "12":
647                continue
648            try:
649                opt.addRecord(**data)
650            except ValueError:
651                dup = True
652            #from pdb import set_trace;set_trace()
653            if dup:
654                if response_code == "00":
[1798]655                    try:
656                        opt.modifyRecord(**data)
657                    except:
658                        logger.info("duplicate uid, order_id %(order_id)s, student_id %(student_id)s, response_code %(response_code)s" % data)
659                        continue
[1620]660                else:
[1674]661                    pay_transaction['Error'] = "Duplicate order_id"
[1620]662                    no_import.append( format_error % pay_transaction)
[1674]663                    logger.info("duplicate order_id %(order_id)s for %(student_id)s %(response_code)s" % data)
[1620]664                    continue
665            tr_count += 1
666            if tr_count > 1000:
667                if len(no_import) > 0:
668                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
669                             '\n'.join(no_import) + '\n')
670                    no_import = []
[1645]671                em = '%d transactions committed\n' % (tr_count)
[1620]672                transaction.commit()
673                regs = []
674                logger.info(em)
675                total += tr_count
676                tr_count = 0
677        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
678                                                '\n'.join(no_import))
679        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
680    ###)
681
[1786]682
[1781]683    # all workflow transitions refer to students in state returning
[1756]684    student_field2types = {   ###(
685                      'StudentApplication':
686                          {'id': 'application',
687                           'title': 'Application Data',
[1781]688                           'wf_transition': 'close',
[1756]689                           'fields':
690                             ('jamb_reg_no',
691                              'entry_mode',
692                              'entry_session',
693                              'jamb_score',
[1786]694                              'app_email',
[1756]695                              )
696                              },
[1786]697                      #'StudentPume':
698                      #    {'id': 'pume',
699                      #     'title': 'Pume Data',
700                      #     'wf_transition': 'close',
701                      #     'fields':
702                      #       ('pume_score',
703                      #        )
704                      #        },
[1756]705                      'StudentClearance':
706                          {'id': 'clearance',
707                           'title': 'Clearance Data',
[1781]708                           'wf_transition': 'close',
[1756]709                           'fields':
710                             ('matric_no',
711                              'nationality',
712                              'lga',
[1804]713                              'birthday',
[1756]714                              )
715                              },
716                         'StudentPersonal':
717                          {'id': 'personal',
718                           'title': 'Personal Data',
[1781]719                           'wf_transition': 'open',
[1756]720                           'fields':
721                             ('firstname',
722                              'middlename',
723                              'lastname',
724                              'sex',
725                              'email',
726                              'phone',
727                              'perm_address',
728                              )
729                              },
730                         'StudentStudyCourse':
731                          {'id': 'study_course',
732                           'title': 'Study Course',
[1781]733                           'wf_transition': 'open',
[1756]734                           'fields':
735                             ('study_course',
736                              'current_level',
737                              'current_session',
738                              'current_mode',
739                              'current_verdict',
740                              )
741                              },
[1775]742                         'PaymentsFolder':
743                          {'id': 'payments',
744                           'title': 'Payments',
[1781]745                           'wf_transition': 'open',
[1775]746                           'fields':
747                             ()
748                              },
[1756]749                         }
750    ###)
751
752
[1772]753    security.declareProtected(ModifyPortalContent,'importStudent') ###(
[1818]754    def importStudent(self,mapping):
[1781]755        "create a students record due import"
756        logger = logging.getLogger('WAeUPTool.importStudent')
[1747]757        students_folder = self.portal_url.getPortalObject().campus.students
[1818]758        jamb_reg_no = mapping.get('jamb_reg_no',None)
[1747]759        if jamb_reg_no:
760            res = self.students_catalog(jamb_reg_no = jamb_reg_no)
761            if res:
[1749]762                return '',"jamb_reg_no exists"
[1818]763        matric_no = mapping.get('matric_no',None)
[1747]764        if matric_no:
765            res = self.students_catalog(matric_no = matric_no)
766            if res:
[1749]767                return '',"matric_no exists"
[1747]768        sid = self.waeup_tool.generateStudentId('?')
769        students_folder.invokeFactory('Student', sid)
770        student_obj = getattr(students_folder,sid)
771        f2t = self.student_field2types
[1772]772        d = {}
773        d['jamb_sex']  = 'M'
[1818]774        if mapping.get('sex'):
[1772]775            d['jamb_sex']  = 'F'
[1747]776        for pt in f2t.keys():
777            student_obj.invokeFactory(pt,f2t[pt]['id'])
778            sub_obj = getattr(student_obj,f2t[pt]['id'])
779            sub_doc = sub_obj.getContent()
[1772]780            d['Title'] = f2t[pt]['title']
[1747]781            for field in f2t[pt]['fields']:
[1818]782                d[field] = mapping.get(field,'')
783            if pt == "StudyCourse":
784                for von,zu in (('entry_mode','current_mode'),
785                               ('entry_session','current_session')):
786                    if mapping.get(zu,None) is None and mapping.get(von,None) is not None:
787                        d[zu] = mapping[von]
[1747]788            sub_doc.edit(mapping = d)
[1781]789            new_state = f2t[pt]['wf_transition']
[1758]790            if new_state != "remain":
791                self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
[1752]792        wfaction = 'return'
793        self.portal_workflow.doActionFor(student_obj,wfaction)
[1747]794        student_obj.manage_setLocalRoles(sid, ['Owner',])
795        return sid,''
796    ###)
797
[1774]798    security.declareProtected(ModifyPortalContent,'importEditStudent') ###(
[1818]799    def importEditStudent(self,mapping):
[1781]800        "edit a students record due import"
801        logger = logging.getLogger('WAeUPTool.importEditStudent')
[1774]802        students_folder = self.portal_url.getPortalObject().campus.students
[1818]803        sid = mapping.get('id',None)
804        jamb_reg_no = mapping.get('jamb_reg_no',None)
805        matric_no = mapping.get('matric_no',None)
[1774]806        if sid:
807            res = self.students_catalog(id = sid)
808            if not res:
809                return '',"no student with id %s" % sid
810            elif matric_no and res[0].matric_no and\
811              matric_no != res[0].matric_no:
812                return '%s' % res[0].id ,"student has no matric_no %s" % matric_no
813            elif jamb_reg_no and res[0].jamb_reg_no and\
814              jamb_reg_no != res[0].jamb_reg_no:
815                return '%s' % res[0].id ,"student has no jamb_reg_no %s" % jamb_reg_no
816        elif matric_no:
817            res = self.students_catalog(matric_no = matric_no)
818            if not res:
819                return '',"no student with matric_no %s" % matric_no
820            elif jamb_reg_no and res[0].jamb_reg_no and\
821              jamb_reg_no != res[0].jamb_reg_no:
822                return '%s' % res[0].id ,"student has no jamb_reg_no %s" % jamb_reg_no
823        elif jamb_reg_no:
824            res = self.students_catalog(jamb_reg_no = jamb_reg_no)
825            if not res:
826                return '',"no student with jamb_reg_no %s" % jamb_reg_no
827        sid = res[0].id
828        student_obj = getattr(students_folder,sid)
829        f2t = self.student_field2types
830        d = {}
831        d['jamb_sex']  = 'M'
[1818]832        if mapping.get('sex'):
[1774]833            d['jamb_sex']  = 'F'
834        for pt in f2t.keys():
835            sub_obj = getattr(student_obj,f2t[pt]['id'],None)
836            if sub_obj is None:
[1786]837                try:
838                    student_obj.invokeFactory(pt,f2t[pt]['id'])
839                except:
840                    continue
[1774]841                sub_obj = getattr(student_obj,f2t[pt]['id'])
842            sub_doc = sub_obj.getContent()
[1781]843            # self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj)
[1774]844            d['Title'] = f2t[pt]['title']
845            for field in f2t[pt]['fields']:
[1818]846                d[field] = mapping.get(field,'')
[1774]847            sub_doc.edit(mapping = d)
[1781]848            # new_state = f2t[pt]['wf_transition']
[1774]849            # if new_state != "remain":
850            #     self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj)
851        # wfaction = 'return'
852        # self.portal_workflow.doActionFor(student_obj,wfaction)
853        # student_obj.manage_setLocalRoles(sid, ['Owner',])
854        return sid,''
855    ###)
856
[1730]857    security.declareProtected(ModifyPortalContent,"importData")###(
[1774]858    def importData(self,filename,name,edit=False):
[1730]859        """load data from CSV values"""
860        import transaction
861        import random
[1775]862
[1773]863        pm = self.portal_membership
[1775]864        member = pm.getAuthenticatedMember()
865
[1747]866        logger = logging.getLogger('WAeUPTool.importData')
[1730]867        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
868        students_folder = self.portal_url.getPortalObject().campus.students
869        start = True
870        tr_count = 1
[1756]871        total_imported = 0
872        total_not_imported = 0
[1730]873        total = 0
[1749]874        iname = "import_%s" % name
[1747]875        stool = getToolByName(self, 'portal_schemas')
876        ltool = getToolByName(self, 'portal_layouts')
877        schema = stool._getOb(iname)
[1730]878        if schema is None:
[1772]879            em = 'No such schema %s' % iname
880            logger.error('No such schema %s' % iname)
[1756]881            return em
[1747]882        layout = ltool._getOb(iname)
[1730]883        if layout is None:
[1772]884            em = 'No such layout %s' % iname
[1756]885            logger.error(em)
886            return em
[1730]887        validators = {}
888        for widget in layout.keys():
[1749]889            validators[widget] = layout[widget].validate
[1774]890        if edit:
891            importer_name = "importEdit%s" % name.capitalize()
892        else:
893            importer_name = "import%s" % name.capitalize()
[1772]894        importer = getattr(self, '%s' % importer_name,None)
895        if importer is None:
896            em = 'No importer function %s' % importer_name
[1756]897            logger.error(em)
898            return em
[1747]899        not_imported = []
[1730]900        imported = []
901        try:
[1747]902            items = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb"))
[1730]903        except:
[1756]904            em = 'Error reading %s.csv' % filename
905            logger.error(em)
906            return em
[1730]907        for item in items:
908            tr_count += 1
909            if start:
910                start = False
[1747]911                adapters = [MappingStorageAdapter(schema, item)]
912                dm = DataModel(item, adapters,context=self)
[1773]913                logger.info('%s starts import from %s.csv' % (member,filename))
[1804]914                import_keys = [k for k in item.keys() if not k.startswith('ignore')]
915                diff2schema = set(import_keys).difference(set(schema.keys()))
916                diff2layout = set(import_keys).difference(set(layout.keys()))
917                if diff2schema:
918                    em = "not ignorable key(s) %s found in heading" % diff2schema
919                    return em
[1753]920                s = ','.join(['"%s"' % fn for fn in import_keys])
[1772]921                open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(s + ',"Error"'+ '\n')
922                s = '"id",' + s
[1756]923                open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(s + '\n')
[1730]924                format = ','.join(['"%%(%s)s"' % fn for fn in import_keys])
925                format_error = format + ',"%(Error)s"'
[1756]926                format = '"%(id)s",'+ format
[1747]927            ds = DataStructure(data=item,datamodel=dm)
928            error_string = ""
[1730]929            for k in import_keys:
[1747]930                if not validators[k](ds):
931                    error_string += " %s : %s" % (k,ds.getError(k))
932            if not error_string:
[1756]933                item.update(dm)
[1772]934                item['id'],error = importer(item)
[1747]935                if error:
936                    error_string += error
937            if error_string:
938                item['Error'] = error_string
939                not_imported.append(format_error % item)
[1756]940                total_not_imported += 1
[1747]941            else:
[1821]942                em = format % item
943                imported.append(em)
944                logger.info("%(total_imported)d of %(total)d %(em)s" % vars())
[1756]945                total_imported += 1
946            total += 1
[1730]947            if tr_count > 1000:
[1747]948                if len(not_imported) > 0:
949                    open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
950                             '\n'.join(not_imported) + '\n')
951                    not_imported = []
952                if len(imported) > 0:
953                    open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
[1772]954                             '\n'.join(imported) + '\n')
[1747]955                    imported = []
[1730]956                em = '%d transactions committed\n' % (tr_count)
957                transaction.commit()
958                regs = []
959                logger.info(em)
960                tr_count = 0
[1756]961        if len(imported) > 0:
[1747]962            open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
963                                                '\n'.join(imported))
[1756]964        if len(not_imported) > 0:
[1747]965            open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
966                                                '\n'.join(not_imported))
[1759]967        em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total)
[1756]968        logger.info(em)
969        return em
[1730]970    ###)
971
[828]972InitializeClass(WAeUPTool)
Note: See TracBrowser for help on using the repository browser.