source: WAeUP_SRP/trunk/WAeUPTool.py @ 1198

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

don't create levels

  • Property svn:keywords set to Id
File size: 12.3 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 1198 2007-01-05 07:50:17Z 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
[828]30from Products.CMFCore.ActionProviderBase import ActionProviderBase
31from Products.CMFCore.permissions import View
32from Products.ZCatalog.ZCatalog import ZCatalog
33from Products.CMFCore.permissions import ModifyPortalContent
[197]34from Products.CMFCore.utils import UniqueObject
[1194]35from Products.CMFCore.URLTool import URLTool
[1151]36from Students import makeCertificateCode
37import logging
[1170]38import transaction
[197]39
[1151]40
[1171]41
42
[828]43class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase):
[197]44    """WAeUP tool"""
45
[828]46    id = 'waeup_tool'
[197]47    meta_type = 'WAeUP Tool'
[828]48    _actions = ()
[197]49
50    security = ClassSecurityInfo()
[828]51    security.declareObjectProtected(View)
[197]52
[828]53    manage_options = ( ActionProviderBase.manage_options
54                     + SimpleItem.manage_options
55                     )
56
[1174]57
[1151]58    def generateStudentId(self,letter): ###(
59        import random
60        r = random
[1194]61        ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
62        if letter == '?':
[1151]63            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
64        sid = "%c%d" % (letter,r.randint(99999,1000000))
[1194]65##        students = self.portal_url.getPortalObject().campus.students
66##        while hasattr(students, sid):
67##            sid = "%c%d" % (letter,r.randint(99999,1000000))
68        while self.students_catalog(id = sid):
[1151]69            sid = "%c%d" % (letter,r.randint(99999,1000000))
70        return sid
71    ###)
[828]72
[1170]73    security.declareProtected(ModifyPortalContent,'createOne') ###(
[1194]74    def createOne(self,students_folder,student_brain,letter,commit=False):
75        sid = self.waeup_tool.generateStudentId(letter)
[1170]76        students_folder.invokeFactory('Student', sid)
77        student = getattr(students_folder,sid)
78        self.portal_workflow.doActionFor(student,'return')
79        student.manage_setLocalRoles(sid, ['Owner',])
80        matric_no = student_brain.matric_no
81        jamb_reg_no = student_brain.Entryregno
82        self.students_catalog.addRecord(id = sid,
83                                           matric_no = matric_no,
84                                           jamb_reg_no = jamb_reg_no,
85                                           sex = student_brain.Sex == "F",
86                                           name = "%s %s %s" % (student_brain.Firstname,
87                                                                student_brain.Middlename,
88                                                                student_brain.Lastname)
89                                        )
90        if commit:
91            transaction.commit()
92        return sid,jamb_reg_no
93    ###)
94
[1151]95    security.declarePublic('getCertificateBrain') ###(
96    def getCertificateBrain(self,cert_id):
97        "do it"
98        res = ZCatalog.searchResults(self.portal_catalog,
99                                {'portal_type':"Certificate",
100                                      'id': cert_id})
101        if res:
102            return res[0]
103        return None
104    ###)
[1160]105
[1151]106    security.declarePublic('findStudentByMatricelNo') ###(
107    def findStudentByMatricelNo(self,matric_no):
108        "do it"
109        res = ZCatalog.searchResults(self.portal_catalog,
110                                {'portal_type':"StudentClearance",
111                                 'SearchableText': matric_no})
112        if res:
113            return res[0]
114        return None
115    ###)
116
117    security.declarePublic('makeStudentMember') ###(
118    def makeStudentMember(self,sid,password='uNsEt'):
119        """make the student a member"""
120        membership = self.portal_membership
121        membership.addMember(sid,
122                             password ,
123                             roles=('Member',
124                                     'Student',
125                                     ),
126                             domains='',
127                             properties = {'memberareaCreationFlag': False,
128                                           'homeless': True},)
129        member = membership.getMemberById(sid)
130        self.portal_registration.afterAdd(member, sid, password, None)
131        self.manage_setLocalRoles(sid, ['Owner',])
132    ###)
[1160]133
[1151]134    security.declarePublic('makeStudentData') ###(
[1158]135    def makeStudentData(self,student_id,email=None,phone_nr=None):
[1151]136        "create Datastructure for a returning Student"
137        #import pdb;pdb.set_trace()
138        logger = logging.getLogger('Student.CreateData')
139        students_folder = self.portal_url.getPortalObject().campus.students
140        res = self.students_catalog(id=student_id)
141        if res:
142            st = res[0]
143        res = self.returning_import(matric_no = st.matric_no)
144        if res:
[1160]145            student = res[0]
[1158]146        logger.info('"%s", "creating Datastructure"' % student_id)
[1171]147
148        #student should not be allowed to perform this transition
[1174]149        #wftool = self.portal_workflow
150        #wftool.doActionFor(student,'return')
[1171]151
[1151]152        certcode_org = student.Coursemajorcode
153        certcode = makeCertificateCode(certcode_org)
154        certificate_brain = self.getCertificateBrain(certcode)
155        if not certificate_brain:
156            em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org)
157            logger.info(em)
158        level = student.Level
159        try:
160            int(level)
161        except:
162            em = '"%(matric_no)s","invalid Level","%(Level)s"' % student
163            logger.info(em)
164        matric_no = student.matric_no
165        sid = student_id
166        student_obj = getattr(students_folder,sid)
167        student_obj.invokeFactory('StudentApplication','application')
168        application = student_obj.application
[1169]169        self.portal_workflow.doActionFor(application,'open',dest_container=application)
[1151]170        da = {'Title': 'Application Data'}
171        student_obj.invokeFactory('StudentPersonal','personal')
172        da['jamb_reg_no'] = student.Entryregno
173        da['entry_mode'] = student.Mode_of_Entry
174        personal = student_obj.personal
175        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
176        dp = {'Title': 'Personal Data'}
177        student_obj.invokeFactory('StudentClearance','clearance')
178        clearance = student_obj.clearance
[1169]179        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
[1151]180        dc = {'Title': 'Clearance/Eligibility Record'}
181        dc['matric_no'] = matric_no
182        state = student.State
183        lga = student.LGA
184        if state and lga:
185            lga =  state + ' / ' + lga
186        else:
187            lga = "None"
[1174]188        da['jamb_lga'] = dc['lga'] = lga
[1173]189        da['app_email'] = dp['email'] = email
190        da['app_mobile'] = dp['phone'] = phone_nr
[1174]191        da['jamb_firstname'] = dp['firstname'] = student.Firstname
192        da['jamb_middlename'] = dp['middlename'] = student.Middlename
193        da['jamb_lastname'] = dp['lastname'] = student.Lastname
194        da['jamb_sex'] = student.Sex
[1151]195        dp['sex'] = student.Sex == 'F'
196        dp['perm_address'] = student.Permanent_Address
197        application.getContent().edit(mapping=da)
[1169]198        self.portal_workflow.doActionFor(application,'close',dest_container=application)
[1151]199        personal.getContent().edit(mapping=dp)
200        clearance.getContent().edit(mapping=dc)
[1169]201        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
[1158]202        catd = {}
203        catd['id'] = sid
204        catd['entry_mode']= da['entry_mode']
205        catd['matric_no'] = matric_no
[1171]206        catd['jamb_reg_no'] = da['jamb_reg_no']
207        catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
208        catd['sex'] = dp['sex']
209        catd['level'] = level
[1158]210        if certificate_brain:
211            cpath = certificate_brain.getPath().split('/')
[1171]212            catd['faculty'] = cpath[-4]
213            catd['department'] = cpath[-3]
[1158]214            catd['course'] = certcode
215        self.students_catalog.modifyRecord(**catd)
[1151]216        #
217        # Study Course
218        #
219        student_obj.invokeFactory('StudentStudyCourse','study_course')
220        studycourse = student_obj.study_course
221        self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse)
222        dsc = {}
223        dsc['study_course'] = certcode
224        studycourse.getContent().edit(mapping=dsc)
225        #
226        # Level
227        #
[1198]228##        l = getattr(studycourse,level,None)
229##        if l is None:
230##            studycourse.invokeFactory('StudentStudyLevel', level)
231##            l = getattr(studycourse, level)
232##            self.portal_workflow.doActionFor(l,'open',dest_container=l)
233##            l.getContent().edit(mapping={'Title': "Level %s" % level})
[1194]234###)
[1160]235
[1194]236    security.declarePublic('makeStudentLevel') ###(
237    def makeStudentLevel(self,student_id):
238        "create the StudyLevel for a returning Student"
239        #import pdb;pdb.set_trace()
240        logger = logging.getLogger('Student.CreateLevel')
241        students_folder = self.portal_url.getPortalObject().campus.students
242        res = self.students_catalog(id=student_id)
243        if res:
244            st = res[0]
245        course = st.course
246        matric_no = st.matric_no
247        level = st.level
248        res = self.results_import(matric_no = matric_no)
249        if res:
250            results = res
251        logger.info('"%s", "creating Level", "%s"' % (student_id,level))
252        #
253        # Level
254        #
255        student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id)
256        studycourse = getattr(student_obj,"study_course",None)
257        self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse)
258        l = getattr(studycourse,level,None)
259        if l is None:
260            studycourse.invokeFactory('StudentStudyLevel', level)
261            l = getattr(studycourse, level)
262            self.portal_workflow.doActionFor(l,'open',dest_container=l)
263            l.getContent().edit(mapping={'Title': "Level %s" % level})
264        ###)
265
[1151]266    security.declarePublic('getAccommodationInfo') ###(
[828]267    def getAccommodationInfo(self,bed):
268        """return Accommodation Info"""
269        info = {}
270        hall,block,room,letter = bed.split('_')
271        res = ZCatalog.searchResults(self.portal_catalog,portal_type="AccoHall",id=hall)
272        if res and len(res) == 1:
273            hall_brain = res[0]
274            hall_doc = hall_brain.getObject().getContent()
275        else:
276            return info
277        info['hall_title'] = hall_brain.Title
278        info['maintenance_code'] = hall_doc.maintenance_code
279        res = ZCatalog.searchResults(self.portal_catalog,portal_type="ScratchCardBatch")
280        batch_doc = None
281        for brain in res:
282            if brain.id.startswith(info['maintenance_code']):
283                batch_doc = brain.getObject().getContent()
284                break
285        if batch_doc is None:
286            info['maintenance_fee'] = None
287        else:
288            info['maintenance_fee'] = batch_doc.cost
289        return info
[1151]290    ###)
[828]291
[1151]292    security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###(
293    def deleteAllCourses(self,department="All"):
294        ''' delete the courses'''
295        pm = self.portal_membership
296        member = pm.getAuthenticatedMember()
[1160]297
[1151]298        if str(member) not in ("henrik","joachim"):
299            return "not possible"
300        if department == "All":
301            res = self.portal_catalog({'meta_type': 'Department'})
302        if len(res) < 1:
303            return "No Departments found"
[1160]304
[1151]305        deleted = []
306        for dep in res:
307            cf = dep.getObject().courses
308            if cf:
309                cf.manage_delObjects(ids=cf.objectIds())
310                deleted.append("deleted Courses in %s" % dep.getId)
311        return "\r".join(deleted)
[1160]312    ###)
[1151]313
[1160]314
[828]315InitializeClass(WAeUPTool)
Note: See TracBrowser for help on using the repository browser.