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 2264 2007-09-20 17:36:42Z joachim $ |
---|
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.utils import getToolByName |
---|
31 | from Products.CPSSchemas.DataStructure import DataStructure |
---|
32 | from Products.CPSSchemas.DataModel import DataModel |
---|
33 | from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter |
---|
34 | from Products.CMFCore.ActionProviderBase import ActionProviderBase |
---|
35 | from Products.CMFCore.permissions import View |
---|
36 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
37 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
38 | from Products.CMFCore.permissions import ManagePortal |
---|
39 | from Products.CMFCore.utils import UniqueObject |
---|
40 | from Products.CMFCore.URLTool import URLTool |
---|
41 | from Products.CMFCore.utils import getToolByName |
---|
42 | from Students import makeCertificateCode |
---|
43 | from Globals import package_home,INSTANCE_HOME |
---|
44 | p_home = package_home(globals()) |
---|
45 | i_home = INSTANCE_HOME |
---|
46 | import DateTime,time |
---|
47 | import logging |
---|
48 | import transaction |
---|
49 | import csv,re,os,sys |
---|
50 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
51 | |
---|
52 | def getObject(object,name): |
---|
53 | if object.hasObject(name): |
---|
54 | return getattr(object,name) |
---|
55 | return None |
---|
56 | |
---|
57 | class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase): |
---|
58 | """WAeUP tool""" |
---|
59 | |
---|
60 | id = 'waeup_tool' |
---|
61 | meta_type = 'WAeUP Tool' |
---|
62 | _actions = () |
---|
63 | security = ClassSecurityInfo() |
---|
64 | security.declareObjectProtected(View) |
---|
65 | manage_options = ( ActionProviderBase.manage_options |
---|
66 | + SimpleItem.manage_options |
---|
67 | ) |
---|
68 | |
---|
69 | def rwrite(self,s): ###( |
---|
70 | response = self.REQUEST.RESPONSE |
---|
71 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
72 | response.write("%s<br />\r\n" % s) |
---|
73 | ###) |
---|
74 | |
---|
75 | def sleep(self,secs): ###( |
---|
76 | "sleep" |
---|
77 | import time |
---|
78 | time.sleep(secs) |
---|
79 | return |
---|
80 | |
---|
81 | ###) |
---|
82 | |
---|
83 | security.declareProtected(ModifyPortalContent,'openLog') ###( |
---|
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 | ###) |
---|
94 | |
---|
95 | security.declareProtected(ModifyPortalContent,'bypassQueueCatalog') ###( |
---|
96 | def bypassQueueCatalog(self,enable=True): |
---|
97 | """bypass the QueueCatalog by setting all indexes to process imediate, |
---|
98 | if enable is True (default) the old settings are restored |
---|
99 | """ |
---|
100 | |
---|
101 | ###) |
---|
102 | |
---|
103 | security.declareProtected(ModifyPortalContent,'measureOaT') ###( |
---|
104 | def measureOaT(self,method="a",probe="1000",nr_pts="1"): |
---|
105 | """measure Object access Time""" |
---|
106 | import random |
---|
107 | if hasattr(self,'portal_catalog_real'): |
---|
108 | aq_portal = self.portal_catalog_real.evalAdvancedQuery |
---|
109 | else: |
---|
110 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
111 | nr_pts = int(nr_pts) |
---|
112 | probe = int(probe) |
---|
113 | intervall = probe/10 |
---|
114 | objects = ("application","clearance","personal") |
---|
115 | portal_types = ("StudentApplication","StudentClearance","StudentPersonal") |
---|
116 | #i = random.randrange(num_objects) |
---|
117 | count = 0 |
---|
118 | found = 0 |
---|
119 | not_found = 0 |
---|
120 | t_found = 0 |
---|
121 | t_not_found = 0 |
---|
122 | time_found = time_not_found = 0.0 |
---|
123 | t_time_found = t_time_not_found = 0.0 |
---|
124 | accessed = [] |
---|
125 | t_min = 1000 |
---|
126 | t_max = 0 |
---|
127 | #import pdb;pdb.set_trace() |
---|
128 | students = self.portal_catalog(portal_type="Student") |
---|
129 | num_students = len(students) |
---|
130 | if method == "d": |
---|
131 | query = Eq('path','/uniben/campus/students') & In('portal_type',portal_types[:nr_pts]) |
---|
132 | res = aq_portal(query) |
---|
133 | brains = {} |
---|
134 | for r in res: |
---|
135 | sid = r.relative_path.split('/')[-2] |
---|
136 | if brains.has_key(sid): |
---|
137 | brains[sid][r.portal_type] = r |
---|
138 | else: |
---|
139 | brains[sid] = {r.portal_type : r} |
---|
140 | brains_list = brains.keys() |
---|
141 | num_objects = len(brains_list) |
---|
142 | else: |
---|
143 | num_objects = num_students |
---|
144 | print "="*40 |
---|
145 | print "method: %s probes: %d nr_pts: %d num_objects: %d" % (method, |
---|
146 | probe, |
---|
147 | nr_pts, |
---|
148 | num_objects) |
---|
149 | print "nr found/not time found/not min/max" |
---|
150 | elapse = time.time() |
---|
151 | i_elapse = time.time() |
---|
152 | c_elapse = time.clock() |
---|
153 | for c in range(1,probe + 1): |
---|
154 | i = random.randrange(num_objects) |
---|
155 | if method in ('a','b','c'): |
---|
156 | student_brain = students[i] |
---|
157 | elif method == "d": |
---|
158 | #import pdb;pdb.set_trace() |
---|
159 | student_brain = brains[brains_list[i]] |
---|
160 | if method == "c": |
---|
161 | query = Eq('path',student_brain.getPath()) & In('portal_type',portal_types[:nr_pts]) |
---|
162 | res = aq_portal(query) |
---|
163 | this_portal_types = [r.portal_type for r in res] |
---|
164 | for i in range(nr_pts): |
---|
165 | oid = objects[i] |
---|
166 | if method == "a": |
---|
167 | try: |
---|
168 | student_path = student_brain.getPath() |
---|
169 | path = "%s/%s" % (student_path,oid) |
---|
170 | doc = self.unrestrictedTraverse(path).getContent() |
---|
171 | found += 1 |
---|
172 | i_time = time.time() - i_elapse |
---|
173 | time_found += i_time |
---|
174 | except: |
---|
175 | not_found += 1 |
---|
176 | i_time = time.time() - i_elapse |
---|
177 | time_not_found += i_time |
---|
178 | pass |
---|
179 | elif method == "b": |
---|
180 | try: |
---|
181 | student_object = student_brain.getObject() |
---|
182 | doc = getattr(student_object,oid).getContent() |
---|
183 | found += 1 |
---|
184 | i_time = time.time() - i_elapse |
---|
185 | time_found += i_time |
---|
186 | except: |
---|
187 | i_time = time.time() - i_elapse |
---|
188 | time_not_found += i_time |
---|
189 | not_found += 1 |
---|
190 | pass |
---|
191 | elif method == "c": |
---|
192 | if portal_types[i] in this_portal_types: |
---|
193 | found += 1 |
---|
194 | doc = res[this_portal_types.index(portal_types[i])].getObject().getContent() |
---|
195 | i_time = time.time() - i_elapse |
---|
196 | time_found += i_time |
---|
197 | else: |
---|
198 | not_found += 1 |
---|
199 | i_time = time.time() - i_elapse |
---|
200 | time_not_found += i_time |
---|
201 | elif method == "d": |
---|
202 | if student_brain.has_key(portal_types[i]): |
---|
203 | found += 1 |
---|
204 | doc = student_brain[portal_types[i]].getObject().getContent() |
---|
205 | i_time = time.time() - i_elapse |
---|
206 | time_found += i_time |
---|
207 | else: |
---|
208 | not_found += 1 |
---|
209 | i_time = time.time() - i_elapse |
---|
210 | time_not_found += i_time |
---|
211 | i_elapse = time.time() |
---|
212 | if c and (c % intervall == 0): |
---|
213 | #i_time = time.time() - i_elapse |
---|
214 | t_per = 0.0 |
---|
215 | if found: |
---|
216 | t_per = time_found/found |
---|
217 | if t_per > t_max: |
---|
218 | t_max = t_per |
---|
219 | if t_per > 0.0 and t_per < t_min: |
---|
220 | t_min = t_per |
---|
221 | itf = 0.0 |
---|
222 | if found: |
---|
223 | itf = time_found/found |
---|
224 | itnf = 0.0 |
---|
225 | if not_found : |
---|
226 | itnf = time_not_found / not_found |
---|
227 | interval_time = time_found + time_not_found |
---|
228 | s = "%(c)d: %(found)d/%(not_found)d " % vars() |
---|
229 | s += "%(interval_time)6.2f %(itf)6.4f/%(itnf)6.4f " % vars() |
---|
230 | s += "%(t_min)6.4f/%(t_max)6.4f" % vars() |
---|
231 | print s |
---|
232 | t_found += found |
---|
233 | t_not_found += not_found |
---|
234 | t_time_found += time_found |
---|
235 | t_time_not_found += time_not_found |
---|
236 | time_found = time_not_found = 0.0 |
---|
237 | found = not_found = 0 |
---|
238 | # t_found += found |
---|
239 | # t_not_found += not_found |
---|
240 | elapse = time.time() - elapse |
---|
241 | itf = 0.0 |
---|
242 | if t_found: |
---|
243 | itf = t_time_found/t_found |
---|
244 | itnf = 0.0 |
---|
245 | if t_not_found: |
---|
246 | itnf = t_time_not_found / t_not_found |
---|
247 | #c_elapse = time.clock() - c_elapse |
---|
248 | s = "%(probe)d: %(t_found)d/%(t_not_found)d " % vars() |
---|
249 | s += "%(elapse)6.2f %(itf)6.4f/%(itnf)6.4f " % vars() |
---|
250 | s += "%(t_min)6.4f/%(t_max)6.4f" % vars() |
---|
251 | print "-"*40 |
---|
252 | print s |
---|
253 | rel_found = float(t_found)/probe |
---|
254 | rel_not_found = float(t_not_found)/probe |
---|
255 | estimated_total_time = num_objects*(rel_found*itf + rel_not_found*itnf) |
---|
256 | print estimated_total_time |
---|
257 | ###) |
---|
258 | |
---|
259 | security.declareProtected(ModifyPortalContent,'writeLog') ###( |
---|
260 | def writeLog(self,logfile,s): |
---|
261 | """write to the log file""" |
---|
262 | logfile.write(s) |
---|
263 | |
---|
264 | ###) |
---|
265 | |
---|
266 | def generateStudentId(self,letter): ###( |
---|
267 | import random |
---|
268 | r = random |
---|
269 | ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
270 | if letter == '?': |
---|
271 | letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY') |
---|
272 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
273 | students = self.portal_url.getPortalObject().campus.students |
---|
274 | ## while hasattr(students, sid): |
---|
275 | ## sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
276 | while self.students_catalog(id = sid): |
---|
277 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
278 | return sid |
---|
279 | ###) |
---|
280 | |
---|
281 | def generatePassword(self,s=None): ###( |
---|
282 | import random |
---|
283 | r = random |
---|
284 | ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
285 | if s is None: |
---|
286 | s = 'abcdefghklmnpqrstuvwxy23456789' |
---|
287 | pw = '' |
---|
288 | while len(pw) < 6: |
---|
289 | pw += r.choice(s) |
---|
290 | return pw |
---|
291 | ###) |
---|
292 | |
---|
293 | security.declareProtected(ManagePortal, 'removeDeletedDocIds') ###( |
---|
294 | def removeDeletedDocIds(self, max=1000): |
---|
295 | """ |
---|
296 | remove deleted docids from repository commit after max |
---|
297 | """ |
---|
298 | logger = logging.getLogger('WAeUPTool.removeDeletedDocIds') |
---|
299 | repository = getToolByName(self, 'portal_repository') |
---|
300 | pxtool = getToolByName(self, 'portal_proxies') |
---|
301 | pxtool_infos = pxtool.getRevisionsUsed() |
---|
302 | |
---|
303 | nb_revs = 0 |
---|
304 | docids_d = {} # all docids |
---|
305 | unused_docids_d = {} # all docids that are unused |
---|
306 | ids_unused_revs_docids = [] # ids for revs of unused docids |
---|
307 | ids_unused_revs = [] # ids for unused revs |
---|
308 | total = 0 |
---|
309 | idlist = repository.objectIds() |
---|
310 | for id in idlist: |
---|
311 | docid, rev = repository._splitId(id) |
---|
312 | if docid is None: |
---|
313 | logger.info("invalid doc_id %s" % docid) |
---|
314 | continue |
---|
315 | nb_revs += 1 |
---|
316 | docids_d[docid] = None |
---|
317 | if not pxtool_infos.has_key(docid): |
---|
318 | unused_docids_d[docid] = None |
---|
319 | ids_unused_revs_docids.append(id) |
---|
320 | ids_unused_revs.append(id) |
---|
321 | elif not pxtool_infos[docid].has_key(rev): |
---|
322 | ids_unused_revs.append(id) |
---|
323 | if len(ids_unused_revs) >= max: |
---|
324 | repository.manage_delObjects(ids_unused_revs) |
---|
325 | #import pdb;pdb.set_trace() |
---|
326 | transaction.commit() |
---|
327 | total += max |
---|
328 | logger.info('removed %d total %d unused docids ' % (max,total)) |
---|
329 | anz = len(ids_unused_revs) |
---|
330 | if anz > 0: |
---|
331 | repository.manage_delObjects(ids_unused_revs) |
---|
332 | transaction.commit() |
---|
333 | total += anz |
---|
334 | logger.info('finished removing %d unused docids ' % (total)) |
---|
335 | |
---|
336 | |
---|
337 | ###) |
---|
338 | |
---|
339 | security.declareProtected(ModifyPortalContent,'getCredential') ###( |
---|
340 | def getCredential(self,student_id): |
---|
341 | "return a student password" |
---|
342 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
343 | if student_entry is None: |
---|
344 | return None |
---|
345 | return getattr(student_entry,"password","not set") |
---|
346 | ###) |
---|
347 | |
---|
348 | security.declarePublic('checkPassword') ###( |
---|
349 | def checkPassword(self,student_id,password): |
---|
350 | "return a student password" |
---|
351 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
352 | if student_entry is None: |
---|
353 | return False |
---|
354 | return getattr(student_entry,"password","not set") == password |
---|
355 | ###) |
---|
356 | |
---|
357 | security.declarePublic('editPassword') ###( |
---|
358 | def editPassword(self,student_id,password): |
---|
359 | "edit a student password" |
---|
360 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
361 | if student_entry is None: |
---|
362 | return |
---|
363 | setattr(student_entry,'password',password) |
---|
364 | ###) |
---|
365 | |
---|
366 | security.declareProtected(View,'doCommit') ###( |
---|
367 | def doCommit(self,logger=None): |
---|
368 | "commit some transactions" |
---|
369 | transaction.commit() |
---|
370 | ###) |
---|
371 | |
---|
372 | security.declarePublic('loadStudentFoto') ###( |
---|
373 | def loadStudentFoto(self,student,filename,folder): |
---|
374 | "return a student passport picture" |
---|
375 | app = student.application |
---|
376 | app_doc = app.getContent() |
---|
377 | #clear = student.clearance |
---|
378 | #clear_doc = clear.getContent() |
---|
379 | #matric_no = clear_doc.matric_no.upper() |
---|
380 | picture1 ="%s/import/%s/%s.jpg" % (i_home,folder,filename) |
---|
381 | picture2 ="%s/import/%s/%s.JPG" % (i_home,folder,filename) |
---|
382 | #import pdb;pdb.set_trace() |
---|
383 | if os.path.exists(picture1): |
---|
384 | file = open(picture1) |
---|
385 | elif os.path.exists(picture2): |
---|
386 | file = open(picture2) |
---|
387 | else: |
---|
388 | return "passport picture not found %s" % picture1 |
---|
389 | reopened = False |
---|
390 | if self.portal_workflow.getInfoFor(app,'review_state',None) !='opened': |
---|
391 | self.portal_workflow.doActionFor(app,'open') |
---|
392 | reopened = True |
---|
393 | outfile = file.read() |
---|
394 | app_doc.manage_addFile('passport', |
---|
395 | file=outfile, |
---|
396 | title="%s.jpg" % filename) |
---|
397 | if reopened: |
---|
398 | self.portal_workflow.doActionFor(app,'close') |
---|
399 | return "successfully loaded passport picture" |
---|
400 | ###) |
---|
401 | |
---|
402 | security.declareProtected(ModifyPortalContent,'createOne') ###( |
---|
403 | def createOne(self,students_folder,student_brain,letter,commit=False): |
---|
404 | sid = self.waeup_tool.generateStudentId(letter) |
---|
405 | students_folder.invokeFactory('Student', sid) |
---|
406 | student = getattr(students_folder,sid) |
---|
407 | self.portal_workflow.doActionFor(student,'return') |
---|
408 | student.manage_setLocalRoles(sid, ['Owner',]) |
---|
409 | matric_no = student_brain.matric_no |
---|
410 | jamb_reg_no = student_brain.Entryregno |
---|
411 | self.students_catalog.addRecord(id = sid, |
---|
412 | matric_no = matric_no, |
---|
413 | jamb_reg_no = jamb_reg_no, |
---|
414 | sex = student_brain.Sex == "F", |
---|
415 | name = "%s %s %s" % (student_brain.Firstname, |
---|
416 | student_brain.Middlename, |
---|
417 | student_brain.Lastname) |
---|
418 | ) |
---|
419 | if commit: |
---|
420 | transaction.commit() |
---|
421 | return sid,jamb_reg_no |
---|
422 | ###) |
---|
423 | |
---|
424 | security.declareProtected(ModifyPortalContent,'addStudent') ###( |
---|
425 | def addStudent(self,dict): |
---|
426 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
427 | sid = self.waeup_tool.generateStudentId('?') |
---|
428 | students_folder.invokeFactory('Student', sid) |
---|
429 | student_obj = getattr(students_folder,sid) |
---|
430 | f2t = self.student_field2types |
---|
431 | #from pdb import set_trace; set_trace() |
---|
432 | d = {} |
---|
433 | d['jamb_sex'] = 'M' |
---|
434 | if dict.get('sex'): |
---|
435 | d['jamb_sex'] = 'F' |
---|
436 | |
---|
437 | entry_session = dict.get('entry_session') |
---|
438 | if entry_session == self.getSessionId()[-2:]: |
---|
439 | wfaction = 'admit' |
---|
440 | wft = 'wf_transition_admit' |
---|
441 | password = None |
---|
442 | else: |
---|
443 | wfaction = 'return' |
---|
444 | wft = 'wf_transition_return' |
---|
445 | password = self.generatePassword() |
---|
446 | self.makeStudentMember(sid,password) |
---|
447 | |
---|
448 | for pt in f2t.keys(): |
---|
449 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
450 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
451 | sub_doc = sub_obj.getContent() |
---|
452 | #self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj) |
---|
453 | d['Title'] = f2t[pt]['title'] |
---|
454 | for field in f2t[pt]['fields']: |
---|
455 | d[field] = dict.get(field,'') |
---|
456 | sub_doc.edit(mapping = d) |
---|
457 | new_state = f2t[pt][wft] |
---|
458 | if new_state != "remain": |
---|
459 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
460 | self.portal_workflow.doActionFor(student_obj,wfaction) |
---|
461 | student_obj.manage_setLocalRoles(sid, ['Owner',]) |
---|
462 | return sid,password |
---|
463 | ###) |
---|
464 | |
---|
465 | security.declarePublic('getCertificateBrain') ###( |
---|
466 | def getCertificateBrain(self,cert_id): |
---|
467 | "do it" |
---|
468 | res = ZCatalog.searchResults(self.portal_catalog_real, |
---|
469 | {'portal_type':"Certificate", |
---|
470 | 'id': cert_id}) |
---|
471 | if res: |
---|
472 | return res[0] |
---|
473 | return None |
---|
474 | ###) |
---|
475 | |
---|
476 | security.declareProtected(ModifyPortalContent,'get_csv_filenames') ###( |
---|
477 | def get_csv_filenames(self): |
---|
478 | "do it" |
---|
479 | files = [file for file in os.listdir("%s/import/" % (i_home)) |
---|
480 | if file.endswith('.csv') and file.find('imported') == -1] |
---|
481 | return files |
---|
482 | ###) |
---|
483 | |
---|
484 | security.declarePublic('findStudentByMatricelNo') ###( |
---|
485 | def findStudentByMatricelNo(self,matric_no): |
---|
486 | "do it" |
---|
487 | res = ZCatalog.searchResults(self.portal_catalog_real, |
---|
488 | {'portal_type':"StudentClearance", |
---|
489 | 'SearchableText': matric_no}) |
---|
490 | if res: |
---|
491 | return res[0] |
---|
492 | return None |
---|
493 | ###) |
---|
494 | |
---|
495 | security.declarePublic('makeStudentMember') ###( |
---|
496 | def makeStudentMember(self,sid,password='uNsEt'): |
---|
497 | """make the student a member""" |
---|
498 | membership = self.portal_membership |
---|
499 | membership.addMember(sid, |
---|
500 | password , |
---|
501 | roles=('Member', |
---|
502 | 'Student', |
---|
503 | ), |
---|
504 | domains='', |
---|
505 | properties = {'memberareaCreationFlag': False, |
---|
506 | 'homeless': True},) |
---|
507 | member = membership.getMemberById(sid) |
---|
508 | self.portal_registration.afterAdd(member, sid, password, None) |
---|
509 | #self.manage_setLocalRoles(sid, ['Owner',]) |
---|
510 | ###) |
---|
511 | |
---|
512 | security.declarePublic('makeStudentData') ###( |
---|
513 | def makeStudentData(self,student_id,email=None,phone_nr=None): |
---|
514 | "create Datastructure for a returning Student" |
---|
515 | #import pdb;pdb.set_trace() |
---|
516 | logger = logging.getLogger('WAeUPTool.makeStudentData') |
---|
517 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
518 | #res = self.students_catalog(id=student_id) |
---|
519 | #if res: |
---|
520 | # st = res[0] |
---|
521 | #res = self.returning_import(matric_no = st.matric_no) |
---|
522 | res = self.returning_import(id = student_id) |
---|
523 | if res: |
---|
524 | student = res[0] |
---|
525 | else: |
---|
526 | logger.info('Id %s not found in returning_import' % student_id) |
---|
527 | return |
---|
528 | logger.info('%s creates data structure' % student_id) |
---|
529 | s_results = self.results_import(matric_no = student.matric_no) |
---|
530 | if s_results: |
---|
531 | lnr = self.getLevelFromResultsCosCode(s_results) |
---|
532 | level = "%d00" % lnr |
---|
533 | verdict,eligible = self.getVerdict(s_results[0].Verdict) |
---|
534 | if eligible: |
---|
535 | level = "%d00" % (lnr + 1) |
---|
536 | else: |
---|
537 | logger.info('matric_no %s not found in results_import' % student.matric_no) |
---|
538 | level = 0 |
---|
539 | verdict = 'N/A' |
---|
540 | #student should not be allowed to perform this transition |
---|
541 | #wftool = self.portal_workflow |
---|
542 | #wftool.doActionFor(student,'return') |
---|
543 | certcode_org = student.Coursemajorcode |
---|
544 | certcode = makeCertificateCode(certcode_org) |
---|
545 | certificate_brain = self.getCertificateBrain(certcode) |
---|
546 | if not certificate_brain: |
---|
547 | em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org) |
---|
548 | logger.info(em) |
---|
549 | matric_no = student.matric_no |
---|
550 | sid = student_id |
---|
551 | student_obj = getattr(students_folder,sid) |
---|
552 | student_obj.invokeFactory('StudentApplication','application') |
---|
553 | application = student_obj.application |
---|
554 | self.portal_workflow.doActionFor(application,'open',dest_container=application) |
---|
555 | da = {'Title': 'Application Data'} |
---|
556 | student_obj.invokeFactory('StudentPersonal','personal') |
---|
557 | da['jamb_reg_no'] = student.Entryregno |
---|
558 | em = self.getEntryMode(student.Entryregno) |
---|
559 | ## em = student.Mode_of_Entry |
---|
560 | ## if em in ('DIRECT', 'DIRECT ENTRY',): |
---|
561 | ## em = 'DE' |
---|
562 | ## elif em in ('U.M.E', 'UNE',): |
---|
563 | ## em = 'UME' |
---|
564 | ## elif not em: |
---|
565 | ## em = "unknown" |
---|
566 | da['entry_mode'] = em |
---|
567 | personal = student_obj.personal |
---|
568 | self.portal_workflow.doActionFor(personal,'open',dest_container=personal) |
---|
569 | dp = {'Title': 'Personal Data'} |
---|
570 | student_obj.invokeFactory('StudentClearance','clearance') |
---|
571 | clearance = student_obj.clearance |
---|
572 | self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance) |
---|
573 | dc = {'Title': 'Clearance/Eligibility Record'} |
---|
574 | dc['matric_no'] = matric_no |
---|
575 | state = student.State |
---|
576 | lga = student.LGA |
---|
577 | if state and lga: |
---|
578 | lga = state + ' / ' + lga |
---|
579 | else: |
---|
580 | lga = "None" |
---|
581 | da['jamb_lga'] = dc['lga'] = lga |
---|
582 | da['app_email'] = dp['email'] = email |
---|
583 | da['app_mobile'] = dp['phone'] = phone_nr |
---|
584 | dp['firstname'] = student.Firstname |
---|
585 | dp['middlename'] = student.Middlename |
---|
586 | dp['lastname'] = student.Lastname |
---|
587 | da['jamb_lastname'] = "%s %s %s" % (student.Firstname,student.Middlename,student.Lastname) |
---|
588 | da['jamb_sex'] = student.Sex |
---|
589 | dp['sex'] = student.Sex == 'F' |
---|
590 | dp['perm_address'] = student.Permanent_Address |
---|
591 | application.getContent().edit(mapping=da) |
---|
592 | self.portal_workflow.doActionFor(application,'close',dest_container=application) |
---|
593 | personal.getContent().edit(mapping=dp) |
---|
594 | clearance.getContent().edit(mapping=dc) |
---|
595 | self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance) |
---|
596 | ## catd = {} |
---|
597 | ## catd['id'] = sid |
---|
598 | ## catd['entry_mode']= da['entry_mode'] |
---|
599 | ## catd['matric_no'] = matric_no |
---|
600 | ## catd['jamb_reg_no'] = da['jamb_reg_no'] |
---|
601 | ## catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp |
---|
602 | ## catd['sex'] = dp['sex'] |
---|
603 | ## catd['level'] = level |
---|
604 | ## catd['verdict'] = verdict |
---|
605 | ## if certificate_brain: |
---|
606 | ## cpath = certificate_brain.getPath().split('/') |
---|
607 | ## catd['faculty'] = cpath[-4] |
---|
608 | ## catd['department'] = cpath[-3] |
---|
609 | ## catd['course'] = certcode |
---|
610 | ## self.students_catalog.modifyRecord(**catd) |
---|
611 | # |
---|
612 | # Study Course |
---|
613 | # |
---|
614 | student_obj.invokeFactory('StudentStudyCourse','study_course') |
---|
615 | studycourse = student_obj.study_course |
---|
616 | self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse) |
---|
617 | dsc = {} |
---|
618 | dsc['study_course'] = certcode |
---|
619 | dsc['current_level'] = level |
---|
620 | dsc['current_verdict'] = verdict |
---|
621 | dsc['current_mode'] = em |
---|
622 | dsc['current_session'] = '06' |
---|
623 | studycourse.getContent().edit(mapping=dsc) |
---|
624 | # |
---|
625 | # Level |
---|
626 | # |
---|
627 | ## l = getattr(studycourse,level,None) |
---|
628 | ## if l is None: |
---|
629 | ## studycourse.invokeFactory('StudentStudyLevel', level) |
---|
630 | ## l = getattr(studycourse, level) |
---|
631 | ## self.portal_workflow.doActionFor(l,'open',dest_container=l) |
---|
632 | ## l.getContent().edit(mapping={'Title': "Level %s" % level}) |
---|
633 | ###) |
---|
634 | |
---|
635 | security.declarePublic('makeStudentLevel') ###( |
---|
636 | def makeStudentLevel(self,student_id): |
---|
637 | "create the StudyLevel for a returning Student" |
---|
638 | #import pdb;pdb.set_trace() |
---|
639 | logger = logging.getLogger('WAeUPTool.makeStudentLevel') |
---|
640 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
641 | res = self.students_catalog(id=student_id) |
---|
642 | if res: |
---|
643 | st = res[0] |
---|
644 | course = st.course |
---|
645 | matric_no = st.matric_no |
---|
646 | level = st.level |
---|
647 | res = self.results_import(matric_no = matric_no) |
---|
648 | if res: |
---|
649 | results = res |
---|
650 | logger.info('%s creating Level %s' % (student_id,level)) |
---|
651 | # |
---|
652 | # Level |
---|
653 | # |
---|
654 | student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id) |
---|
655 | studycourse = getattr(student_obj,"study_course",None) |
---|
656 | self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse) |
---|
657 | l = getattr(studycourse,level,None) |
---|
658 | if l is None: |
---|
659 | studycourse.invokeFactory('StudentStudyLevel', level) |
---|
660 | l = getattr(studycourse, level) |
---|
661 | self.portal_workflow.doActionFor(l,'open',dest_container=l) |
---|
662 | l.getContent().edit(mapping={'Title': "Level %s" % level}) |
---|
663 | ###) |
---|
664 | |
---|
665 | security.declarePublic('getHallInfo') ###( |
---|
666 | def getHallInfo(self,bed): |
---|
667 | """return Hall Info""" |
---|
668 | info = {} |
---|
669 | hall,block,room,letter = bed.split('_') |
---|
670 | res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="AccoHall",id=hall) |
---|
671 | if res and len(res) == 1: |
---|
672 | hall_brain = res[0] |
---|
673 | hall_doc = hall_brain.getObject().getContent() |
---|
674 | else: |
---|
675 | return info |
---|
676 | info['hall_title'] = hall_brain.Title |
---|
677 | info['maintenance_code'] = hall_doc.maintenance_code |
---|
678 | res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="ScratchCardBatch") |
---|
679 | batch_doc = None |
---|
680 | for brain in res: |
---|
681 | if brain.id.startswith(info['maintenance_code']): |
---|
682 | batch_doc = brain.getObject().getContent() |
---|
683 | break |
---|
684 | if batch_doc is None: |
---|
685 | info['maintenance_fee'] = None |
---|
686 | else: |
---|
687 | info['maintenance_fee'] = batch_doc.cost |
---|
688 | return info |
---|
689 | ###) |
---|
690 | |
---|
691 | security.declarePublic('showFsPicture') ###( |
---|
692 | def showFsPicture(self,path): |
---|
693 | """return a picture from the filesystem""" |
---|
694 | picture_path = "%s/import/%s" % (i_home,path) |
---|
695 | if os.path.exists(picture_path): |
---|
696 | return open(picture_path).read() |
---|
697 | |
---|
698 | ###) |
---|
699 | |
---|
700 | security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###( |
---|
701 | def deleteAllCourses(self,department="All"): |
---|
702 | ''' delete the courses''' |
---|
703 | pm = self.portal_membership |
---|
704 | member = pm.getAuthenticatedMember() |
---|
705 | |
---|
706 | if str(member) not in ("henrik","joachim"): |
---|
707 | return "not possible" |
---|
708 | if department == "All": |
---|
709 | res = self.portal_catalog({'meta_type': 'Department'}) |
---|
710 | if len(res) < 1: |
---|
711 | return "No Departments found" |
---|
712 | |
---|
713 | deleted = [] |
---|
714 | for dep in res: |
---|
715 | cf = dep.getObject().courses |
---|
716 | if cf: |
---|
717 | cf.manage_delObjects(ids=cf.objectIds()) |
---|
718 | deleted.append("deleted Courses in %s" % dep.getId) |
---|
719 | return "\r".join(deleted) |
---|
720 | ###) |
---|
721 | |
---|
722 | security.declareProtected(ModifyPortalContent,'getLogfileLines') ###( |
---|
723 | def getLogfileLines(self,filename="event.log",numlines=20): |
---|
724 | """Get last NUMLINES lines of logfile FILENAME. |
---|
725 | |
---|
726 | Return last lines' of a file in the instances logfile directory as |
---|
727 | a list. The number of returned lines equals `numlines' or less. If |
---|
728 | less than `numlines' lines are available, the whole file ist |
---|
729 | returned. If the file can not be opened or some other error |
---|
730 | occurs, empty list is returend. |
---|
731 | """ |
---|
732 | result = [] |
---|
733 | lines_hit = 0 |
---|
734 | |
---|
735 | # We only handle files in instances' log directory... |
---|
736 | logpath = os.path.join(i_home, "log") |
---|
737 | filename = str(os.path.abspath( os.path.join( logpath, filename ))) |
---|
738 | if not filename.startswith( logpath ): |
---|
739 | # Attempt to access file outside log-dir... |
---|
740 | return [] |
---|
741 | |
---|
742 | try: |
---|
743 | fd = file( filename, "rb" ) |
---|
744 | except IOError: |
---|
745 | return [] |
---|
746 | if not fd: |
---|
747 | return [] |
---|
748 | |
---|
749 | if os.linesep == None: |
---|
750 | linesep = '\n' |
---|
751 | else: |
---|
752 | linesep = os.linesep |
---|
753 | |
---|
754 | # Try to find 'numlines' times a lineseparator, searching from end |
---|
755 | # and moving to the beginning of file... |
---|
756 | fd.seek( 0, 2) # Move to end of file... |
---|
757 | while lines_hit < numlines: |
---|
758 | if fd.read(1) == linesep[-1]: # This moves filedescriptor |
---|
759 | # one step forward... |
---|
760 | lines_hit += 1 |
---|
761 | try: |
---|
762 | fd.seek( -2, 1) # Go two bytes back from current pos... |
---|
763 | except IOError: |
---|
764 | # We cannot go back two bytes. Maybe the file is too small... |
---|
765 | break |
---|
766 | fd.seek(2,1) |
---|
767 | |
---|
768 | # Read all lines from current position... |
---|
769 | result = fd.readlines() |
---|
770 | # Remove line endings... |
---|
771 | result = [x.strip() for x in result] |
---|
772 | fd.close() |
---|
773 | return result |
---|
774 | ###) |
---|
775 | |
---|
776 | security.declareProtected(ModifyPortalContent,"getCallbacksFromLog")###( |
---|
777 | def getCallbacksFromLog(self,filename): |
---|
778 | """fix Online Payment Transactions from Z2.log entries""" |
---|
779 | import transaction |
---|
780 | import random |
---|
781 | from cgi import parse_qs |
---|
782 | from urlparse import urlparse |
---|
783 | #from pdb import set_trace |
---|
784 | wftool = self.portal_workflow |
---|
785 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
786 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
787 | s = r'(?P<client_ip>\S+) - (?P<member_id>\S+) \[' |
---|
788 | s += r'(?P<date>.*)\] "(?P<get>.*)" (?P<codes>\d+ \d+) "' |
---|
789 | s += r'(?P<intersw>.*)" "(?P<agent>.*)"' |
---|
790 | data = re.compile(s) |
---|
791 | start = True |
---|
792 | tr_count = 1 |
---|
793 | total = 0 |
---|
794 | #name = 'pume_results' |
---|
795 | #name = 'epaymentsuccessful_z2log2' |
---|
796 | name = filename |
---|
797 | no_import = [] |
---|
798 | imported = [] |
---|
799 | logger = logging.getLogger('WAeUPTool.getFailedTransactions') |
---|
800 | try: |
---|
801 | transactions = open("%s/import/%s" % (i_home,name),"rb").readlines() |
---|
802 | except: |
---|
803 | logger.error('Error reading %s' % name) |
---|
804 | return |
---|
805 | tas = [] |
---|
806 | for line in transactions: |
---|
807 | dict = {} |
---|
808 | items = data.search(line) |
---|
809 | dict['idict'] = idict = items.groupdict() |
---|
810 | #print idict |
---|
811 | #from pdb import set_trace;set_trace() |
---|
812 | urlparsed = urlparse(idict['get'][4:]) |
---|
813 | #print urlparsed |
---|
814 | path = urlparsed[2].split('/') |
---|
815 | dict['student_id'] = student_id = path[8] |
---|
816 | dict['payment_id'] = payment_id = path[10] |
---|
817 | dict['qs_dict'] = qs_dict = parse_qs(urlparsed[4]) |
---|
818 | tas.append(dict) |
---|
819 | tr_count += 1 |
---|
820 | return tas |
---|
821 | ###) |
---|
822 | |
---|
823 | security.declareProtected(ModifyPortalContent,"importOnlinePaymentTransactions")###( |
---|
824 | def importOnlinePaymentTransactions(self): |
---|
825 | """load Online Payment Transactions from CSV values""" |
---|
826 | import transaction |
---|
827 | import random |
---|
828 | #from pdb import set_trace |
---|
829 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
830 | opt = self.online_payments_import |
---|
831 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
832 | start = True |
---|
833 | tr_count = 1 |
---|
834 | total = 0 |
---|
835 | #name = 'pume_results' |
---|
836 | name = 'OnlineTransactions' |
---|
837 | no_import = [] |
---|
838 | imported = [] |
---|
839 | logger = logging.getLogger('WAeUPTool.importOnlinePaymentTransactions') |
---|
840 | try: |
---|
841 | transactions = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
842 | except: |
---|
843 | logger.error('Error reading %s.csv' % name) |
---|
844 | return |
---|
845 | for pay_transaction in transactions: |
---|
846 | if start: |
---|
847 | start = False |
---|
848 | logger.info('Start loading from %s.csv' % name) |
---|
849 | s = ','.join(['"%s"' % fn for fn in pay_transaction.keys()]) |
---|
850 | no_import.append('%s,"Error"' % s) |
---|
851 | format = ','.join(['"%%(%s)s"' % fn for fn in pay_transaction.keys()]) |
---|
852 | format_error = format + ',"%(Error)s"' |
---|
853 | data = {} |
---|
854 | |
---|
855 | # format of the first file sent by Tayo |
---|
856 | #data['datetime'] = date = DateTime.DateTime(pay_transaction['Date']) |
---|
857 | #data['student_id'] = student_id = pay_transaction['Payer ID'] |
---|
858 | #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)'] |
---|
859 | #data['response_code'] = response_code = pay_transaction['Resp Code'] |
---|
860 | #data['amount'] = amount = pay_transaction['Amount'] |
---|
861 | |
---|
862 | # format of the second file sent by Tayo |
---|
863 | #data['datetime'] = date = 0 |
---|
864 | #data['student_id'] = student_id = pay_transaction['Payer ID'] |
---|
865 | #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)'] |
---|
866 | #data['response_code'] = response_code = '00' |
---|
867 | #data['amount'] = amount = pay_transaction['Amount'] |
---|
868 | |
---|
869 | # format of the third file sent by Kehinde |
---|
870 | data['datetime'] = date = 0 |
---|
871 | data['student_id'] = student_id = pay_transaction['customer_id'] |
---|
872 | data['order_id'] = order_id = pay_transaction['merchant_reference'] |
---|
873 | data['response_code'] = response_code = '00' |
---|
874 | data['amount'] = amount = pay_transaction['Amount'] |
---|
875 | |
---|
876 | dup = False |
---|
877 | if response_code == "12": |
---|
878 | continue |
---|
879 | try: |
---|
880 | opt.addRecord(**data) |
---|
881 | except ValueError: |
---|
882 | dup = True |
---|
883 | #from pdb import set_trace;set_trace() |
---|
884 | if dup: |
---|
885 | if response_code == "00": |
---|
886 | try: |
---|
887 | opt.modifyRecord(**data) |
---|
888 | except: |
---|
889 | logger.info("duplicate uid, order_id %(order_id)s, student_id %(student_id)s, response_code %(response_code)s" % data) |
---|
890 | continue |
---|
891 | else: |
---|
892 | pay_transaction['Error'] = "Duplicate order_id" |
---|
893 | no_import.append( format_error % pay_transaction) |
---|
894 | logger.info("duplicate order_id %(order_id)s for %(student_id)s %(response_code)s" % data) |
---|
895 | continue |
---|
896 | tr_count += 1 |
---|
897 | if tr_count > 1000: |
---|
898 | if len(no_import) > 0: |
---|
899 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
900 | '\n'.join(no_import) + '\n') |
---|
901 | no_import = [] |
---|
902 | em = '%d transactions committed\n' % (tr_count) |
---|
903 | transaction.commit() |
---|
904 | regs = [] |
---|
905 | logger.info(em) |
---|
906 | total += tr_count |
---|
907 | tr_count = 0 |
---|
908 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
909 | '\n'.join(no_import)) |
---|
910 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
911 | ###) |
---|
912 | |
---|
913 | security.declareProtected(ModifyPortalContent,'mass_create_faculty') ###( |
---|
914 | def mass_create_faculty(self,mapping): |
---|
915 | "create a faculty" |
---|
916 | logger = logging.getLogger('WAeUPTool.mass_create_faculty') |
---|
917 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
918 | fid = mapping['code'] |
---|
919 | if getattr(academics_folder,fid,None) is not None: |
---|
920 | return '', "Faculty with ID: %s exists" % fid |
---|
921 | logger.info('Creating Faculty %(code)s = %(title)s' % mapping) |
---|
922 | academics_folder.invokeFactory('Faculty', fid) |
---|
923 | f = getattr(academics_folder,fid,None) |
---|
924 | f.getContent().edit(mapping=mapping) |
---|
925 | return fid,'' |
---|
926 | ###) |
---|
927 | |
---|
928 | security.declareProtected(ModifyPortalContent,'mass_edit_Faculty') ###( |
---|
929 | def mass_edit_faculty(self,mapping): |
---|
930 | "edit a faculty" |
---|
931 | logger = logging.getLogger('WAeUPTool.mass_edit_faculty') |
---|
932 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
933 | fid = mapping['code'] |
---|
934 | f = getattr(academics_folder,fid,None) |
---|
935 | if f is None: |
---|
936 | return '', "Faculty with ID: %s does not exist" % fid |
---|
937 | logger.info('Editing Faculty %(code)s = %(title)s' % mapping) |
---|
938 | f.getContent().edit(mapping=mapping) |
---|
939 | return fid,'' |
---|
940 | ###) |
---|
941 | |
---|
942 | security.declareProtected(ModifyPortalContent,'mass_create_department') ###( |
---|
943 | def mass_create_department(self,mapping): |
---|
944 | "create a department in the correct faculty" |
---|
945 | logger = logging.getLogger('WAeUPTool.mass_create_department') |
---|
946 | fid = mapping['faculty_code'] |
---|
947 | if getattr(self,'_v_faculties',None) is None: |
---|
948 | res = self.portal_catalog(portal_type = "Faculty") |
---|
949 | self._v_faculties = {} |
---|
950 | for f in res: |
---|
951 | self._v_faculties[f.getId] = f.getObject() |
---|
952 | f = self._v_faculties.get(fid,None) |
---|
953 | if f is None: |
---|
954 | return '', "No Faculty with ID: %s" % fid |
---|
955 | else: |
---|
956 | did = mapping.get('code') |
---|
957 | d = getattr(f,did,None) |
---|
958 | if d is None or d.portal_type == "Faculty": |
---|
959 | logger.info('Creating Department %(code)s = %(title)s' % mapping) |
---|
960 | f.invokeFactory('Department', did) |
---|
961 | d = getattr(f,did) |
---|
962 | d.invokeFactory('CoursesFolder','courses') |
---|
963 | courses = getattr(d,'courses') |
---|
964 | dict = {'Title': 'Courses'} |
---|
965 | courses.getContent().edit(mapping=dict) |
---|
966 | d.invokeFactory('CertificatesFolder','certificates') |
---|
967 | certificates = getattr(d,'certificates') |
---|
968 | dict = {'Title': 'Certificates'} |
---|
969 | certificates.getContent().edit(mapping=dict) |
---|
970 | d.getContent().edit(mapping=mapping) |
---|
971 | return did,'' |
---|
972 | ###) |
---|
973 | |
---|
974 | security.declareProtected(ModifyPortalContent,'mass_edit_department') ###( |
---|
975 | def mass_edit_department(self,mapping): |
---|
976 | "create a department in the correct faculty" |
---|
977 | logger = logging.getLogger('WAeUPTool.mass_create_department') |
---|
978 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
979 | fid = mapping['faculty_code'] |
---|
980 | did = mapping.get('code') |
---|
981 | try: |
---|
982 | d = getattr(getattr(academics_folder,fid),did,None) |
---|
983 | except KeyError: |
---|
984 | return '', "Department %s or Faculty %s wrong" % (did,fid) |
---|
985 | else: |
---|
986 | if d is None or d.portal_type == "Faculty": |
---|
987 | logger.info('Editing Department %(code)s = %(title)s' % mapping) |
---|
988 | d.getContent().edit(mapping=mapping) |
---|
989 | return did,'' |
---|
990 | ###) |
---|
991 | |
---|
992 | field2types_student = { ###( |
---|
993 | 'StudentApplication': |
---|
994 | {'id': 'application', |
---|
995 | 'title': 'Application Data', |
---|
996 | 'wf_transition_return': 'close', |
---|
997 | 'wf_transition_admit': 'remain', |
---|
998 | 'fields': |
---|
999 | ('jamb_reg_no', |
---|
1000 | 'entry_mode', |
---|
1001 | 'entry_session', |
---|
1002 | 'jamb_score', |
---|
1003 | 'app_email', |
---|
1004 | 'jamb_age', |
---|
1005 | 'jamb_state', |
---|
1006 | 'jamb_lga', |
---|
1007 | ) |
---|
1008 | }, |
---|
1009 | #'StudentPume': |
---|
1010 | # {'id': 'pume', |
---|
1011 | # 'title': 'Pume Data', |
---|
1012 | # 'wf_transition_return': 'close', |
---|
1013 | # 'wf_transition_admit': 'close', |
---|
1014 | # 'fields': |
---|
1015 | # ('pume_score', |
---|
1016 | # ) |
---|
1017 | # }, |
---|
1018 | 'StudentClearance': |
---|
1019 | {'id': 'clearance', |
---|
1020 | 'title': 'Clearance/Eligibility Record', |
---|
1021 | 'wf_transition_return': 'close', |
---|
1022 | 'wf_transition_admit': 'remain', |
---|
1023 | 'fields': |
---|
1024 | ('matric_no', |
---|
1025 | 'nationality', |
---|
1026 | 'lga', |
---|
1027 | 'birthday', |
---|
1028 | ) |
---|
1029 | }, |
---|
1030 | 'StudentPersonal': |
---|
1031 | {'id': 'personal', |
---|
1032 | 'title': 'Personal Data', |
---|
1033 | 'wf_transition_return': 'open', |
---|
1034 | 'wf_transition_admit': 'remain', |
---|
1035 | 'fields': |
---|
1036 | ('firstname', |
---|
1037 | 'middlename', |
---|
1038 | 'lastname', |
---|
1039 | 'sex', |
---|
1040 | 'email', |
---|
1041 | 'phone', |
---|
1042 | 'perm_address', |
---|
1043 | ) |
---|
1044 | }, |
---|
1045 | 'StudentStudyCourse': |
---|
1046 | {'id': 'study_course', |
---|
1047 | 'title': 'Study Course', |
---|
1048 | 'wf_transition_return': 'open', |
---|
1049 | 'wf_transition_admit': 'remain', |
---|
1050 | 'fields': |
---|
1051 | ('study_course', |
---|
1052 | 'current_level', |
---|
1053 | 'current_session', |
---|
1054 | 'current_mode', |
---|
1055 | 'current_verdict', |
---|
1056 | ) |
---|
1057 | }, |
---|
1058 | |
---|
1059 | 'PaymentsFolder': |
---|
1060 | {'id': 'payments', |
---|
1061 | 'title': 'Payments', |
---|
1062 | 'wf_transition_return': 'open', |
---|
1063 | 'wf_transition_admit': 'open', |
---|
1064 | 'fields': |
---|
1065 | () |
---|
1066 | }, |
---|
1067 | } |
---|
1068 | ###) |
---|
1069 | |
---|
1070 | security.declareProtected(ModifyPortalContent,'mass_create_student') ###( |
---|
1071 | def mass_create_student(self,mapping): |
---|
1072 | "create a students record due import" |
---|
1073 | logger = logging.getLogger('WAeUPTool.mass_create_student') |
---|
1074 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1075 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
1076 | if jamb_reg_no: |
---|
1077 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
1078 | if res: |
---|
1079 | return '',"jamb_reg_no exists" |
---|
1080 | matric_no = mapping.get('matric_no',None) |
---|
1081 | if matric_no: |
---|
1082 | res = self.students_catalog(matric_no = matric_no) |
---|
1083 | if res: |
---|
1084 | return '',"matric_no exists" |
---|
1085 | sid = self.waeup_tool.generateStudentId('?') |
---|
1086 | students_folder.invokeFactory('Student', sid) |
---|
1087 | student_obj = getattr(students_folder,sid) |
---|
1088 | f2t = self.field2types_student |
---|
1089 | d = {} |
---|
1090 | d['jamb_sex'] = 'M' |
---|
1091 | if mapping.get('sex'): |
---|
1092 | d['jamb_sex'] = 'F' |
---|
1093 | transition = mapping.get('reg_transition','admit') |
---|
1094 | if transition not in ('admit','return'): |
---|
1095 | return '',"no valid transition provided" |
---|
1096 | for pt in f2t.keys(): |
---|
1097 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
1098 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
1099 | sub_doc = sub_obj.getContent() |
---|
1100 | d['Title'] = f2t[pt]['title'] |
---|
1101 | for field in f2t[pt]['fields']: |
---|
1102 | d[field] = mapping.get(field,'') |
---|
1103 | if pt == "StudyCourse": |
---|
1104 | for von,zu in (('entry_mode','current_mode'), |
---|
1105 | ('entry_session','current_session')): |
---|
1106 | if mapping.get(zu,None) is None and mapping.get(von,None) is not None: |
---|
1107 | d[zu] = mapping[von] |
---|
1108 | sub_doc.edit(mapping = d) |
---|
1109 | |
---|
1110 | #import pdb;pdb.set_trace() |
---|
1111 | new_state = f2t[pt]['wf_transition_%(transition)s' % vars()] |
---|
1112 | if new_state != "remain": |
---|
1113 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
1114 | self.portal_workflow.doActionFor(student_obj,transition) |
---|
1115 | student_obj.manage_setLocalRoles(sid, ['Owner',]) |
---|
1116 | return sid,'' |
---|
1117 | ###) |
---|
1118 | |
---|
1119 | security.declareProtected(ModifyPortalContent,'mass_edit_student') ###( |
---|
1120 | def mass_edit_student(self,mapping): |
---|
1121 | "edit a students record due import" |
---|
1122 | logger = logging.getLogger('WAeUPTool.mass_edit_student') |
---|
1123 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1124 | sid = mapping.get('id',None) |
---|
1125 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
1126 | matric_no = mapping.get('matric_no',None) |
---|
1127 | editable_keys = mapping.keys() |
---|
1128 | if sid: |
---|
1129 | res = self.students_catalog(id = sid) |
---|
1130 | if not res: |
---|
1131 | return '',"no student with id %s" % sid |
---|
1132 | elif matric_no and res[0].matric_no and\ |
---|
1133 | matric_no != res[0].matric_no: |
---|
1134 | return '%s' % res[0].id ,"student has no matric_no %s" % matric_no |
---|
1135 | elif jamb_reg_no and res[0].jamb_reg_no and\ |
---|
1136 | jamb_reg_no != res[0].jamb_reg_no: |
---|
1137 | return '%s' % res[0].id ,"student has no jamb_reg_no %s" % jamb_reg_no |
---|
1138 | elif jamb_reg_no: |
---|
1139 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
1140 | if not res: |
---|
1141 | return '',"no student with jamb_reg_no %s" % jamb_reg_no |
---|
1142 | editable_keys.remove('jamb_reg_no') |
---|
1143 | elif matric_no: |
---|
1144 | res = self.students_catalog(matric_no = matric_no) |
---|
1145 | if not res: |
---|
1146 | return '',"no student with matric_no %s" % matric_no |
---|
1147 | editable_keys.remove('matric_no') |
---|
1148 | |
---|
1149 | ## included only to change wf state from admitted to returning |
---|
1150 | #if res[0].review_state not in ('admitted','objection_raised'): |
---|
1151 | # return '%s' % res[0].id ,"student is not in state admitted or objection_raised" |
---|
1152 | ## end inclusion |
---|
1153 | |
---|
1154 | sid = res[0].id |
---|
1155 | student_obj = getattr(students_folder,sid) |
---|
1156 | f2t = self.field2types_student |
---|
1157 | d = {} |
---|
1158 | any_change = False |
---|
1159 | for pt in f2t.keys(): |
---|
1160 | if pt == "student_application": |
---|
1161 | d['jamb_sex'] = 'M' |
---|
1162 | if mapping.get('sex'): |
---|
1163 | d['jamb_sex'] = 'F' |
---|
1164 | intersect = set(f2t[pt]['fields']).intersection(set(editable_keys)) |
---|
1165 | if intersect: |
---|
1166 | sub_obj = getattr(student_obj,f2t[pt]['id'],None) |
---|
1167 | if sub_obj is None: |
---|
1168 | try: |
---|
1169 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
1170 | except: |
---|
1171 | continue |
---|
1172 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
1173 | d['Title'] = f2t[pt]['title'] |
---|
1174 | sub_doc = sub_obj.getContent() |
---|
1175 | for field in intersect: |
---|
1176 | changed = False |
---|
1177 | if getattr(sub_doc,field) != mapping.get(field,''): |
---|
1178 | any_change = True |
---|
1179 | changed = True |
---|
1180 | d[field] = mapping.get(field,'') |
---|
1181 | if changed: |
---|
1182 | sub_doc.edit(mapping = d) |
---|
1183 | |
---|
1184 | |
---|
1185 | ## included only to change wf state from admitted to returning |
---|
1186 | # if res[0].review_state in ('admitted','objection_raised'): |
---|
1187 | # new_state = f2t[pt]['wf_transition_return'] |
---|
1188 | # sub_obj = getattr(student_obj,f2t[pt]['id'],None) |
---|
1189 | # if sub_obj and new_state != "remain": |
---|
1190 | # try: |
---|
1191 | # self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
1192 | # except: |
---|
1193 | # #logger.info('%s, wf transition %s of %s failed' % (sid,new_state,sub_obj.id)) |
---|
1194 | # pass |
---|
1195 | #if res[0].review_state in ('admitted','objection_raised'): |
---|
1196 | # wfaction = 'return' |
---|
1197 | # try: |
---|
1198 | # self.portal_workflow.doActionFor(student_obj,wfaction) |
---|
1199 | # logger.info('%s, wf state changed' % sid) |
---|
1200 | # any_change = True |
---|
1201 | # except: |
---|
1202 | # logger.info('%s, wf transition failed, old state = %s' % (sid,res[0].review_state)) |
---|
1203 | # pass |
---|
1204 | ## end inclusion |
---|
1205 | |
---|
1206 | |
---|
1207 | if any_change: |
---|
1208 | return sid,'' |
---|
1209 | else: |
---|
1210 | return sid,'not modified' |
---|
1211 | ###) |
---|
1212 | |
---|
1213 | security.declareProtected(ModifyPortalContent,"importData")###( |
---|
1214 | def importData(self,filename,name,edit=False,bypass_queue_catalog=False): |
---|
1215 | """load data from CSV values""" |
---|
1216 | import transaction |
---|
1217 | import random |
---|
1218 | |
---|
1219 | pm = self.portal_membership |
---|
1220 | member = pm.getAuthenticatedMember() |
---|
1221 | |
---|
1222 | logger = logging.getLogger('WAeUPTool.importData') |
---|
1223 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
1224 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1225 | start = True |
---|
1226 | tr_count = 1 |
---|
1227 | total_imported = 0 |
---|
1228 | total_not_imported = 0 |
---|
1229 | total = 0 |
---|
1230 | iname = "import_%s" % name |
---|
1231 | stool = getToolByName(self, 'portal_schemas') |
---|
1232 | ltool = getToolByName(self, 'portal_layouts') |
---|
1233 | schema = stool._getOb(iname) |
---|
1234 | if schema is None: |
---|
1235 | em = 'No such schema %s' % iname |
---|
1236 | logger.error('No such schema %s' % iname) |
---|
1237 | return em |
---|
1238 | layout = ltool._getOb(iname) |
---|
1239 | if layout is None: |
---|
1240 | em = 'No such layout %s' % iname |
---|
1241 | logger.error(em) |
---|
1242 | return em |
---|
1243 | validators = {} |
---|
1244 | for widget in layout.keys(): |
---|
1245 | validators[widget] = layout[widget].validate |
---|
1246 | #import pdb;pdb.set_trace() |
---|
1247 | if edit: |
---|
1248 | importer_name = "mass_edit_%s" % name |
---|
1249 | else: |
---|
1250 | importer_name = "mass_create_%s" % name |
---|
1251 | importer = getattr(self, '%s' % importer_name,None) |
---|
1252 | if importer is None: |
---|
1253 | em = 'No importer function %s' % importer_name |
---|
1254 | logger.error(em) |
---|
1255 | return em |
---|
1256 | not_imported = [] |
---|
1257 | imported = [] |
---|
1258 | try: |
---|
1259 | items = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb")) |
---|
1260 | except: |
---|
1261 | em = 'Error reading %s.csv' % filename |
---|
1262 | logger.error(em) |
---|
1263 | return em |
---|
1264 | for item in items: |
---|
1265 | if start: |
---|
1266 | start = False |
---|
1267 | adapters = [MappingStorageAdapter(schema, item)] |
---|
1268 | logger.info('%s starts import from %s.csv' % (member,filename)) |
---|
1269 | #import_keys = [k for k in item.keys() if not k.startswith('ignore')] |
---|
1270 | attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb")).next() |
---|
1271 | import_keys = [k for k in attrs if not k.startswith('ignore')] |
---|
1272 | diff2schema = set(import_keys).difference(set(schema.keys())) |
---|
1273 | diff2layout = set(import_keys).difference(set(layout.keys())) |
---|
1274 | if diff2schema: |
---|
1275 | em = "not ignorable key(s) %s found in heading" % diff2schema |
---|
1276 | return em |
---|
1277 | s = ','.join(['"%s"' % fn for fn in import_keys]) |
---|
1278 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(s + ',"Error"'+ '\n') |
---|
1279 | s = '"id",' + s |
---|
1280 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(s + '\n') |
---|
1281 | format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) |
---|
1282 | format_error = format + ',"%(Error)s"' |
---|
1283 | format = '"%(id)s",'+ format |
---|
1284 | |
---|
1285 | dm = DataModel(item, adapters,context=self) |
---|
1286 | ds = DataStructure(data=item,datamodel=dm) |
---|
1287 | error_string = "" |
---|
1288 | for k in import_keys: |
---|
1289 | #import pdb;pdb.set_trace() |
---|
1290 | if not validators[k](ds): |
---|
1291 | error_string += " %s : %s" % (k, |
---|
1292 | self.translation_service(ds.getError(k), |
---|
1293 | ds.getErrorMapping(k))) |
---|
1294 | if not error_string: |
---|
1295 | item.update(dm) |
---|
1296 | item['id'],error = importer(item) |
---|
1297 | if error: |
---|
1298 | error_string += error |
---|
1299 | if error_string: |
---|
1300 | item['Error'] = error_string |
---|
1301 | not_imported.append(format_error % item) |
---|
1302 | total_not_imported += 1 |
---|
1303 | else: |
---|
1304 | em = format % item |
---|
1305 | imported.append(em) |
---|
1306 | logger.info("%(total_imported)d of %(total)d %(em)s" % vars()) |
---|
1307 | tr_count += 1 |
---|
1308 | total_imported += 1 |
---|
1309 | total += 1 |
---|
1310 | if total and not total % 100: |
---|
1311 | transaction.commit() |
---|
1312 | if len(not_imported) > 0: |
---|
1313 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
1314 | '\n'.join(not_imported) + '\n') |
---|
1315 | not_imported = [] |
---|
1316 | if len(imported) > 0: |
---|
1317 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
1318 | '\n'.join(imported) + '\n') |
---|
1319 | imported = [] |
---|
1320 | em = '%d transactions committed\n' % (tr_count) |
---|
1321 | regs = [] |
---|
1322 | logger.info(em) |
---|
1323 | tr_count = 0 |
---|
1324 | if len(imported) > 0: |
---|
1325 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
1326 | '\n'.join(imported)) |
---|
1327 | if len(not_imported) > 0: |
---|
1328 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
1329 | '\n'.join(not_imported)) |
---|
1330 | em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) |
---|
1331 | return em |
---|
1332 | ###) |
---|
1333 | |
---|
1334 | InitializeClass(WAeUPTool) |
---|