source: WAeUP_SRP/branches/regebro-noskins/WAeUPTool.py @ 12691

Last change on this file since 12691 was 1467, checked in by joachim, 18 years ago

add change password functionality

  • Property svn:keywords set to Id
File size: 19.0 KB
Line 
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 1467 2007-02-22 10:48:52Z joachim $
20"""The WAeUP Tool Box.
21"""
22
23from AccessControl import ClassSecurityInfo
24from Acquisition import aq_inner
25from Acquisition import aq_parent
26from Globals import DTMLFile
27from Globals import InitializeClass
28from OFS.SimpleItem import SimpleItem
29
30from Products.CMFCore.ActionProviderBase import ActionProviderBase
31from Products.CMFCore.permissions import View
32from Products.ZCatalog.ZCatalog import ZCatalog
33from Products.CMFCore.permissions import ModifyPortalContent
34from Products.CMFCore.utils import UniqueObject
35from Products.CMFCore.URLTool import URLTool
36from Students import makeCertificateCode
37from Globals import package_home,INSTANCE_HOME
38p_home = package_home(globals())
39i_home = INSTANCE_HOME
40import logging,os
41import transaction
42
43
44
45
46class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase):
47    """WAeUP tool"""
48
49    id = 'waeup_tool'
50    meta_type = 'WAeUP Tool'
51    _actions = ()
52
53    security = ClassSecurityInfo()
54    security.declareObjectProtected(View)
55
56    manage_options = ( ActionProviderBase.manage_options
57                     + SimpleItem.manage_options
58                     )
59
60
61    def generateStudentId(self,letter): ###(
62        import random
63        r = random
64        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
65        if letter == '?':
66            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
67        sid = "%c%d" % (letter,r.randint(99999,1000000))
68##        students = self.portal_url.getPortalObject().campus.students
69##        while hasattr(students, sid):
70##            sid = "%c%d" % (letter,r.randint(99999,1000000))
71        while self.students_catalog(id = sid):
72            sid = "%c%d" % (letter,r.randint(99999,1000000))
73        return sid
74    ###)
75
76    def generatePassword(self,s=None): ###(
77        import random
78        r = random
79        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
80        if s is None:
81            s = 'abcdefghklmnpqrstuvwxy23456789'
82        pw = ''
83        while len(pw) < 6:
84            pw += r.choice(s)
85        return pw
86    ###)
87
88    security.declareProtected(ModifyPortalContent,'getCredential') ###(
89    def getCredential(self,student_id):
90        "return a student password"
91        student_entry = getattr(self.portal_directories.students,student_id,None)
92        if student_entry is None:
93            return None
94        return getattr(student_entry,"password","not set")
95    ###)
96
97    security.declarePublic('checkPassword') ###(
98    def checkPassword(self,student_id,password):
99        "return a student password"
100        student_entry = getattr(self.portal_directories.students,student_id,None)
101        if student_entry is None:
102            return False
103        return getattr(student_entry,"password","not set") == password
104    ###)
105
106    security.declarePublic('editPassword') ###(
107    def editPassword(self,student_id,password):
108        "edit a student password"
109        student_entry = getattr(self.portal_directories.students,student_id,None)
110        if student_entry is None:
111            return
112        setattr(student_entry,'password',password)
113    ###)
114
115    security.declareProtected(ModifyPortalContent,'doCommit') ###(
116    def doCommit(self,logger=None):
117        "commit some transactions"
118        transaction.commit()
119    ###)
120
121    security.declarePublic('loadStudentFoto') ###(
122    def loadStudentFoto(self,student):
123        "return a student passport picture"
124        app_doc = student.application.getContent()
125        clear = student.clearance
126        clear_doc = clear.getContent()
127        matric_no = clear_doc.matric_no.upper()
128        picture1 ="%s/import/pictures_returning/%s.jpg" % (i_home,matric_no)
129        picture2 ="%s/import/pictures_returning/%s.JPG" % (i_home,matric_no)
130        #import pdb;pdb.set_trace()
131        if os.path.exists(picture1):
132            file = open(picture1)
133        elif os.path.exists(picture2):
134            file = open(picture2)
135        else:
136            return "passport picture not found %s" % picture1
137
138        outfile = file.read()
139        app_doc.manage_addFile('passport',
140                               file=outfile,
141                               title="%s.jpg" % matric_no)
142        return "successfully loaded passport picture"
143    ###)
144
145    security.declareProtected(ModifyPortalContent,'createOne') ###(
146    def createOne(self,students_folder,student_brain,letter,commit=False):
147        sid = self.waeup_tool.generateStudentId(letter)
148        students_folder.invokeFactory('Student', sid)
149        student = getattr(students_folder,sid)
150        self.portal_workflow.doActionFor(student,'return')
151        student.manage_setLocalRoles(sid, ['Owner',])
152        matric_no = student_brain.matric_no
153        jamb_reg_no = student_brain.Entryregno
154        self.students_catalog.addRecord(id = sid,
155                                           matric_no = matric_no,
156                                           jamb_reg_no = jamb_reg_no,
157                                           sex = student_brain.Sex == "F",
158                                           name = "%s %s %s" % (student_brain.Firstname,
159                                                                student_brain.Middlename,
160                                                                student_brain.Lastname)
161                                        )
162        if commit:
163            transaction.commit()
164        return sid,jamb_reg_no
165    ###)
166
167    security.declareProtected(ModifyPortalContent,'createStudent') ###(
168    def createStudent(self,dict):
169        students_folder = self.portal_url.getPortalObject().campus.students
170        sid = self.waeup_tool.generateStudentId('?')
171        students_folder.invokeFactory('Student', sid)
172        student_obj = getattr(students_folder,sid)
173        password = self.generatePassword()
174        self.makeStudentMember(sid,password)
175        status,entry_mode = dict.get('entry_mode').split('_')
176        wfaction = 'return'
177        if status == "NEW":
178            wfaction = 'admit'
179        matric_no = dict.get('matric_no')
180        email = dict.get('email')
181        level = dict.get('level')
182        jamb_reg_no = dict.get('jamb_reg_no')
183        study_course = dict.get('study_course')
184        self.portal_workflow.doActionFor(student_obj,wfaction)
185        student_obj.manage_setLocalRoles(sid, ['Owner',])
186        student_obj.invokeFactory('StudentApplication','application')
187        application = student_obj.application
188        self.portal_workflow.doActionFor(application,'open',dest_container=application)
189        da = {'Title': 'Application Data'}
190        student_obj.invokeFactory('StudentPersonal','personal')
191        da['entry_mode'] = entry_mode
192        personal = student_obj.personal
193        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
194        dp = {'Title': 'Personal Data'}
195        student_obj.invokeFactory('StudentClearance','clearance')
196        clearance = student_obj.clearance
197        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
198        dc = {'Title': 'Clearance/Eligibility Record'}
199        dc['matric_no'] = matric_no
200        da['app_email'] = dp['email'] = email
201        da['jamb_reg_no'] = jamb_reg_no
202        dp['firstname'] = dict.get('firstname')
203        dp['middlename'] = dict.get('middlename')
204        dp['lastname'] = dict.get('lastname')
205        da['jamb_lastname'] = "%(firstname)s %(middlename)s %(lastname)s" % dict
206        sex = dict.get('sex')
207        if sex:
208            da['jamb_sex'] = 'F'
209        else:
210            da['jamb_sex'] = 'M'
211        dp['sex'] = sex
212        application.getContent().edit(mapping=da)
213        self.portal_workflow.doActionFor(application,'close',dest_container=application)
214        personal.getContent().edit(mapping=dp)
215        clearance.getContent().edit(mapping=dc)
216        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
217        catd = {}
218        catd['id'] = sid
219        catd['entry_mode']= entry_mode
220        catd['email'] = email
221        catd['jamb_reg_no'] = jamb_reg_no
222        catd['matric_no'] = matric_no
223        catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
224        catd['sex'] = dp['sex']
225        catd['level'] = level
226        certificate_brain = self.getCertificateBrain(study_course)
227        if certificate_brain:
228            cpath = certificate_brain.getPath().split('/')
229            catd['faculty'] = cpath[-4]
230            catd['department'] = cpath[-3]
231            catd['course'] = study_course
232        self.students_catalog.addRecord(**catd)
233        #
234        # Study Course
235        #
236        student_obj.invokeFactory('StudentStudyCourse','study_course')
237        sc = student_obj.study_course
238        self.portal_workflow.doActionFor(sc,'open',dest_container=sc)
239        dsc = {}
240        dsc['study_course'] = study_course
241        dsc['current_level'] = level
242        sc.getContent().edit(mapping=dsc)
243
244        return sid,password
245    ###)
246
247    security.declarePublic('getCertificateBrain') ###(
248    def getCertificateBrain(self,cert_id):
249        "do it"
250        res = ZCatalog.searchResults(self.portal_catalog,
251                                {'portal_type':"Certificate",
252                                      'id': cert_id})
253        if res:
254            return res[0]
255        return None
256    ###)
257
258    security.declarePublic('findStudentByMatricelNo') ###(
259    def findStudentByMatricelNo(self,matric_no):
260        "do it"
261        res = ZCatalog.searchResults(self.portal_catalog,
262                                {'portal_type':"StudentClearance",
263                                 'SearchableText': matric_no})
264        if res:
265            return res[0]
266        return None
267    ###)
268
269    security.declarePublic('makeStudentMember') ###(
270    def makeStudentMember(self,sid,password='uNsEt'):
271        """make the student a member"""
272        membership = self.portal_membership
273        membership.addMember(sid,
274                             password ,
275                             roles=('Member',
276                                     'Student',
277                                     ),
278                             domains='',
279                             properties = {'memberareaCreationFlag': False,
280                                           'homeless': True},)
281        member = membership.getMemberById(sid)
282        self.portal_registration.afterAdd(member, sid, password, None)
283        #self.manage_setLocalRoles(sid, ['Owner',])
284    ###)
285
286    security.declarePublic('makeStudentData') ###(
287    def makeStudentData(self,student_id,email=None,phone_nr=None):
288        "create Datastructure for a returning Student"
289        #import pdb;pdb.set_trace()
290        logger = logging.getLogger('Student.CreateData')
291        students_folder = self.portal_url.getPortalObject().campus.students
292        res = self.students_catalog(id=student_id)
293        if res:
294            st = res[0]
295        res = self.returning_import(matric_no = st.matric_no)
296        if res:
297            student = res[0]
298        logger.info('"%s", "creating Datastructure"' % student_id)
299        s_results = self.results_import(matric_no = st.matric_no)
300        lnr = self.getLevelFromResultsCosCode(s_results)
301        level = "%d00" % lnr
302        verdict,elegible = self.getVerdict(s_results[0].Verdict)
303        if elegible:
304            level = "%d00" % (lnr + 1)
305##        level = s_results[0].Level
306##        for result in s_results:
307##            if level != result.Level:
308##                logger.info('"%s", "Levels differ","%s != %s"' % (student_id,level,result.Level))
309        #student should not be allowed to perform this transition
310        #wftool = self.portal_workflow
311        #wftool.doActionFor(student,'return')
312        certcode_org = student.Coursemajorcode
313        certcode = makeCertificateCode(certcode_org)
314        certificate_brain = self.getCertificateBrain(certcode)
315        if not certificate_brain:
316            em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org)
317            logger.info(em)
318        matric_no = student.matric_no
319        sid = student_id
320        student_obj = getattr(students_folder,sid)
321        student_obj.invokeFactory('StudentApplication','application')
322        application = student_obj.application
323        self.portal_workflow.doActionFor(application,'open',dest_container=application)
324        da = {'Title': 'Application Data'}
325        student_obj.invokeFactory('StudentPersonal','personal')
326        da['jamb_reg_no'] = student.Entryregno
327        em = self.getEntryMode(student.Entryregno)
328##        em = student.Mode_of_Entry
329##        if em in ('DIRECT', 'DIRECT ENTRY',):
330##            em = 'DE'
331##        elif em in ('U.M.E', 'UNE',):
332##            em = 'UME'
333##        elif not em:
334##            em = "unknown"
335        da['entry_mode'] = em
336        personal = student_obj.personal
337        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
338        dp = {'Title': 'Personal Data'}
339        student_obj.invokeFactory('StudentClearance','clearance')
340        clearance = student_obj.clearance
341        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
342        dc = {'Title': 'Clearance/Eligibility Record'}
343        dc['matric_no'] = matric_no
344        state = student.State
345        lga = student.LGA
346        if state and lga:
347            lga =  state + ' / ' + lga
348        else:
349            lga = "None"
350        da['jamb_lga'] = dc['lga'] = lga
351        da['app_email'] = dp['email'] = email
352        da['app_mobile'] = dp['phone'] = phone_nr
353        dp['firstname'] = student.Firstname
354        dp['middlename'] = student.Middlename
355        dp['lastname'] = student.Lastname
356        da['jamb_lastname'] = "%s %s %s" % (student.Firstname,student.Middlename,student.Lastname)
357        da['jamb_sex'] = student.Sex
358        dp['sex'] = student.Sex == 'F'
359        dp['perm_address'] = student.Permanent_Address
360        application.getContent().edit(mapping=da)
361        self.portal_workflow.doActionFor(application,'close',dest_container=application)
362        personal.getContent().edit(mapping=dp)
363        clearance.getContent().edit(mapping=dc)
364        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
365        catd = {}
366        catd['id'] = sid
367        catd['entry_mode']= da['entry_mode']
368        catd['matric_no'] = matric_no
369        catd['jamb_reg_no'] = da['jamb_reg_no']
370        catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
371        catd['sex'] = dp['sex']
372        catd['level'] = level
373        catd['verdict'] = verdict
374        if certificate_brain:
375            cpath = certificate_brain.getPath().split('/')
376            catd['faculty'] = cpath[-4]
377            catd['department'] = cpath[-3]
378            catd['course'] = certcode
379        self.students_catalog.modifyRecord(**catd)
380        #
381        # Study Course
382        #
383        student_obj.invokeFactory('StudentStudyCourse','study_course')
384        studycourse = student_obj.study_course
385        self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse)
386        dsc = {}
387        dsc['study_course'] = certcode
388        dsc['current_level'] = level
389        dsc['current_verdict'] = verdict
390        studycourse.getContent().edit(mapping=dsc)
391        #
392        # Level
393        #
394##        l = getattr(studycourse,level,None)
395##        if l is None:
396##            studycourse.invokeFactory('StudentStudyLevel', level)
397##            l = getattr(studycourse, level)
398##            self.portal_workflow.doActionFor(l,'open',dest_container=l)
399##            l.getContent().edit(mapping={'Title': "Level %s" % level})
400###)
401
402    security.declarePublic('makeStudentLevel') ###(
403    def makeStudentLevel(self,student_id):
404        "create the StudyLevel for a returning Student"
405        #import pdb;pdb.set_trace()
406        logger = logging.getLogger('Student.CreateLevel')
407        students_folder = self.portal_url.getPortalObject().campus.students
408        res = self.students_catalog(id=student_id)
409        if res:
410            st = res[0]
411        course = st.course
412        matric_no = st.matric_no
413        level = st.level
414        res = self.results_import(matric_no = matric_no)
415        if res:
416            results = res
417        logger.info('"%s", "creating Level", "%s"' % (student_id,level))
418        #
419        # Level
420        #
421        student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id)
422        studycourse = getattr(student_obj,"study_course",None)
423        self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse)
424        l = getattr(studycourse,level,None)
425        if l is None:
426            studycourse.invokeFactory('StudentStudyLevel', level)
427            l = getattr(studycourse, level)
428            self.portal_workflow.doActionFor(l,'open',dest_container=l)
429            l.getContent().edit(mapping={'Title': "Level %s" % level})
430        ###)
431
432    security.declarePublic('getAccommodationInfo') ###(
433    def getAccommodationInfo(self,bed):
434        """return Accommodation Info"""
435        info = {}
436        hall,block,room,letter = bed.split('_')
437        res = ZCatalog.searchResults(self.portal_catalog,portal_type="AccoHall",id=hall)
438        if res and len(res) == 1:
439            hall_brain = res[0]
440            hall_doc = hall_brain.getObject().getContent()
441        else:
442            return info
443        info['hall_title'] = hall_brain.Title
444        info['maintenance_code'] = hall_doc.maintenance_code
445        res = ZCatalog.searchResults(self.portal_catalog,portal_type="ScratchCardBatch")
446        batch_doc = None
447        for brain in res:
448            if brain.id.startswith(info['maintenance_code']):
449                batch_doc = brain.getObject().getContent()
450                break
451        if batch_doc is None:
452            info['maintenance_fee'] = None
453        else:
454            info['maintenance_fee'] = batch_doc.cost
455        return info
456    ###)
457
458    security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###(
459    def deleteAllCourses(self,department="All"):
460        ''' delete the courses'''
461        pm = self.portal_membership
462        member = pm.getAuthenticatedMember()
463
464        if str(member) not in ("henrik","joachim"):
465            return "not possible"
466        if department == "All":
467            res = self.portal_catalog({'meta_type': 'Department'})
468        if len(res) < 1:
469            return "No Departments found"
470
471        deleted = []
472        for dep in res:
473            cf = dep.getObject().courses
474            if cf:
475                cf.manage_delObjects(ids=cf.objectIds())
476                deleted.append("deleted Courses in %s" % dep.getId)
477        return "\r".join(deleted)
478    ###)
479
480
481InitializeClass(WAeUPTool)
Note: See TracBrowser for help on using the repository browser.