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