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 3765 2008-11-15 10:44:40Z 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 | from zExceptions import BadRequest |
---|
30 | |
---|
31 | from Products.CMFCore.utils import getToolByName |
---|
32 | from Products.CPSSchemas.DataStructure import DataStructure |
---|
33 | from Products.CPSSchemas.DataModel import DataModel |
---|
34 | from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter |
---|
35 | from Products.CMFCore.ActionProviderBase import ActionProviderBase |
---|
36 | from Products.CMFCore.permissions import View |
---|
37 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
38 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
39 | from Products.CMFCore.permissions import ManagePortal |
---|
40 | from Products.CMFCore.utils import UniqueObject |
---|
41 | from Products.CMFCore.URLTool import URLTool |
---|
42 | from Products.CMFCore.utils import getToolByName |
---|
43 | from Students import makeCertificateCode |
---|
44 | from Globals import package_home,INSTANCE_HOME |
---|
45 | from WAeUPImport import ApplicationImport,CertificateImport,CertificateCourseImport |
---|
46 | from WAeUPImport import CourseImport,CourseResultImport,StudentStudyLevelImport |
---|
47 | from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport |
---|
48 | from utils import makeDigest |
---|
49 | import DateTime,time |
---|
50 | import logging |
---|
51 | import transaction |
---|
52 | import csv,re,os,sys |
---|
53 | import md5 |
---|
54 | from shutil import copy2,copy |
---|
55 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
56 | |
---|
57 | p_home = package_home(globals()) |
---|
58 | i_home = INSTANCE_HOME |
---|
59 | images_base = os.path.join(i_home,"images") |
---|
60 | EMPTY = 'XXX' |
---|
61 | |
---|
62 | def getImagesDir(student_id): |
---|
63 | return os.path.join("%s" % images_base,student_id[0],student_id) |
---|
64 | |
---|
65 | def getObject(object,name): |
---|
66 | if object.hasObject(name): |
---|
67 | return getattr(object,name) |
---|
68 | return None |
---|
69 | |
---|
70 | class WAeUPTool(UniqueObject, SimpleItem, ActionProviderBase): |
---|
71 | """WAeUP tool""" |
---|
72 | |
---|
73 | id = 'waeup_tool' |
---|
74 | meta_type = 'WAeUP Tool' |
---|
75 | _actions = () |
---|
76 | security = ClassSecurityInfo() |
---|
77 | security.declareObjectProtected(View) |
---|
78 | manage_options = ( ActionProviderBase.manage_options |
---|
79 | + SimpleItem.manage_options |
---|
80 | ) |
---|
81 | |
---|
82 | security.declareProtected(View,'re_split') ###( |
---|
83 | def re_split(self,split_string,string): |
---|
84 | return re.split(split_string,string) |
---|
85 | ###) |
---|
86 | |
---|
87 | security.declareProtected(View,'difference') ###( |
---|
88 | def difference(self,l1,l2): |
---|
89 | return set(l1).difference(set(l2)) |
---|
90 | ###) |
---|
91 | |
---|
92 | def rwrite(self,s): ###( |
---|
93 | response = self.REQUEST.RESPONSE |
---|
94 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
95 | response.write("%s<br />\r\n" % s) |
---|
96 | ###) |
---|
97 | |
---|
98 | def addtodict(self,d,key,item): ###( |
---|
99 | d[key].append(item) |
---|
100 | return d[key] |
---|
101 | ###) |
---|
102 | |
---|
103 | def sleep(self,secs): ###( |
---|
104 | "sleep" |
---|
105 | import time |
---|
106 | time.sleep(secs) |
---|
107 | return |
---|
108 | ###) |
---|
109 | |
---|
110 | security.declareProtected(View,'updateRoleMappingsFor') ###( |
---|
111 | def updateRoleMappingsFor(self,wf_definition,ob): |
---|
112 | "do so for public" |
---|
113 | wf_def = getattr(self.portal_workflow,wf_definition) |
---|
114 | wf_def.updateRoleMappingsFor(ob) |
---|
115 | ###) |
---|
116 | |
---|
117 | security.declareProtected(View,'getStatesLgas') ###( |
---|
118 | def getStatesLgas(self): |
---|
119 | """return lga info""" |
---|
120 | voc = getattr(self.portal_vocabularies,'local_gov_areas') |
---|
121 | states = [] |
---|
122 | lgas = [] |
---|
123 | d = {} |
---|
124 | wd = {} |
---|
125 | for k,v in voc.items(): |
---|
126 | parts = v.split(' / ') |
---|
127 | if len(parts) == 1: |
---|
128 | state = parts[0].lower() |
---|
129 | lga = "" |
---|
130 | elif len(parts) == 2: |
---|
131 | state = "_".join(re.split('[^a-zA-Z0-9/]',parts[0].lower())) |
---|
132 | lga = "-".join(re.split('[^a-zA-Z0-9/]',parts[1].lower())) |
---|
133 | else: |
---|
134 | continue |
---|
135 | if state not in states: |
---|
136 | states.append(state) |
---|
137 | if lga not in lgas: |
---|
138 | lgas.append(lga) |
---|
139 | words = re.split('[^a-zA-Z0-9/]',k) |
---|
140 | words.sort() |
---|
141 | wd[k] = words |
---|
142 | d[k] = v |
---|
143 | mapping = {} |
---|
144 | mapping['word_dict'] = wd |
---|
145 | mapping['lga_dict'] = d |
---|
146 | mapping['states'] = states |
---|
147 | mapping['lgas'] = lgas |
---|
148 | return mapping |
---|
149 | ###) |
---|
150 | |
---|
151 | security.declareProtected(View,'findLga') ###( |
---|
152 | def findLga(self,words,words_dict): |
---|
153 | words = re.split('[^a-zA-Z0-9/]',words) |
---|
154 | lga_words = [] |
---|
155 | for word in words: |
---|
156 | if word: |
---|
157 | lga_words += word.strip().lower(), |
---|
158 | lga_words.sort() |
---|
159 | state_lga = '' |
---|
160 | while not state_lga: |
---|
161 | for k,l in words_dict.items(): |
---|
162 | if lga_words == l: |
---|
163 | state_lga = k |
---|
164 | break |
---|
165 | break |
---|
166 | return state_lga |
---|
167 | ###) |
---|
168 | security.declareProtected(View,'getAccessInfo') ###( |
---|
169 | def getAccessInfo(self,context): |
---|
170 | "return a dict with access_info" |
---|
171 | logger = logging.getLogger('WAeUPTool.getAccessInfo') |
---|
172 | mtool = self.portal_membership |
---|
173 | member = mtool.getAuthenticatedMember() |
---|
174 | member_id = str(member) |
---|
175 | info = {} |
---|
176 | is_anonymous = info['is_anonymous'] = mtool.isAnonymousUser() |
---|
177 | is_student = info['is_student'] = ord(member_id[1]) > 48 and ord(member_id[1]) <= 57 |
---|
178 | is_staff = info['is_staff'] = not is_anonymous and not is_student |
---|
179 | roles = member.getRolesInContext(context) |
---|
180 | info['is_sectionofficer'] = not is_student and ("SectionOfficer" in roles or |
---|
181 | "SectionManager" in roles or |
---|
182 | "Manager" in roles) |
---|
183 | info['is_clearanceofficer'] = not is_student and ("ClearanceOfficer" in roles) |
---|
184 | is_allowed = info['is_allowed'] = not is_anonymous |
---|
185 | requested_id = context.getStudentId() |
---|
186 | student_id = None |
---|
187 | if is_allowed: |
---|
188 | if not is_student and requested_id: |
---|
189 | student_id = requested_id |
---|
190 | elif not is_allowed and (not is_staff or member_id != requested_id): |
---|
191 | logger.info('%s tried to access %s of %s' % (member_id,context.portal_type,requested_id)) |
---|
192 | else: |
---|
193 | student_id = member_id |
---|
194 | info['student_id'] = student_id |
---|
195 | return info |
---|
196 | ###) |
---|
197 | |
---|
198 | security.declareProtected(ModifyPortalContent,'openLog') ###( |
---|
199 | def openLog(self,name): |
---|
200 | """open a log file""" |
---|
201 | version = 1 |
---|
202 | path = "%s/log/%s_%d.log" % (i_home,name,version) |
---|
203 | while os.path.exists(path): |
---|
204 | version += 1 |
---|
205 | path = "%s/log/%s_%d.log" % (i_home,name,version) |
---|
206 | log = open(path,"w") |
---|
207 | return log |
---|
208 | ###) |
---|
209 | |
---|
210 | security.declareProtected(ModifyPortalContent,'bypassQueueCatalog') ###( |
---|
211 | def bypassQueueCatalog(self,enable=True): |
---|
212 | """bypass the QueueCatalog by setting all indexes to process imediate, |
---|
213 | if enable is True (default) the old settings are restored |
---|
214 | """ |
---|
215 | |
---|
216 | ###) |
---|
217 | |
---|
218 | security.declareProtected(ModifyPortalContent,'measureOaT') ###( |
---|
219 | def measureOaT(self,method="a",probe="1000",nr_pts="1"): |
---|
220 | """measure Object access Time""" |
---|
221 | import random |
---|
222 | if hasattr(self,'portal_catalog_real'): |
---|
223 | aq_portal = self.portal_catalog_real.evalAdvancedQuery |
---|
224 | else: |
---|
225 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
226 | nr_pts = int(nr_pts) |
---|
227 | probe = int(probe) |
---|
228 | intervall = probe/10 |
---|
229 | objects = ("application","clearance","personal") |
---|
230 | portal_types = ("StudentApplication","StudentClearance","StudentPersonal") |
---|
231 | #i = random.randrange(num_objects) |
---|
232 | count = 0 |
---|
233 | found = 0 |
---|
234 | not_found = 0 |
---|
235 | t_found = 0 |
---|
236 | t_not_found = 0 |
---|
237 | time_found = time_not_found = 0.0 |
---|
238 | t_time_found = t_time_not_found = 0.0 |
---|
239 | accessed = [] |
---|
240 | t_min = 1000 |
---|
241 | t_max = 0 |
---|
242 | #import pdb;pdb.set_trace() |
---|
243 | students = self.portal_catalog(portal_type="Student") |
---|
244 | num_students = len(students) |
---|
245 | if method == "d": |
---|
246 | query = Eq('path','/uniben/campus/students') & In('portal_type',portal_types[:nr_pts]) |
---|
247 | res = aq_portal(query) |
---|
248 | brains = {} |
---|
249 | for r in res: |
---|
250 | sid = r.relative_path.split('/')[-2] |
---|
251 | if brains.has_key(sid): |
---|
252 | brains[sid][r.portal_type] = r |
---|
253 | else: |
---|
254 | brains[sid] = {r.portal_type : r} |
---|
255 | brains_list = brains.keys() |
---|
256 | num_objects = len(brains_list) |
---|
257 | else: |
---|
258 | num_objects = num_students |
---|
259 | print "="*40 |
---|
260 | print "method: %s probes: %d nr_pts: %d num_objects: %d" % (method, |
---|
261 | probe, |
---|
262 | nr_pts, |
---|
263 | num_objects) |
---|
264 | print "nr found/not time found/not min/max" |
---|
265 | elapse = time.time() |
---|
266 | i_elapse = time.time() |
---|
267 | c_elapse = time.clock() |
---|
268 | for c in range(1,probe + 1): |
---|
269 | i = random.randrange(num_objects) |
---|
270 | if method in ('a','b','c'): |
---|
271 | student_brain = students[i] |
---|
272 | elif method == "d": |
---|
273 | #import pdb;pdb.set_trace() |
---|
274 | student_brain = brains[brains_list[i]] |
---|
275 | if method == "c": |
---|
276 | query = Eq('path',student_brain.getPath()) & In('portal_type',portal_types[:nr_pts]) |
---|
277 | res = aq_portal(query) |
---|
278 | this_portal_types = [r.portal_type for r in res] |
---|
279 | for i in range(nr_pts): |
---|
280 | oid = objects[i] |
---|
281 | if method == "a": |
---|
282 | try: |
---|
283 | student_path = student_brain.getPath() |
---|
284 | path = "%s/%s" % (student_path,oid) |
---|
285 | doc = self.unrestrictedTraverse(path).getContent() |
---|
286 | found += 1 |
---|
287 | i_time = time.time() - i_elapse |
---|
288 | time_found += i_time |
---|
289 | except: |
---|
290 | not_found += 1 |
---|
291 | i_time = time.time() - i_elapse |
---|
292 | time_not_found += i_time |
---|
293 | pass |
---|
294 | elif method == "b": |
---|
295 | try: |
---|
296 | student_object = student_brain.getObject() |
---|
297 | doc = getattr(student_object,oid).getContent() |
---|
298 | found += 1 |
---|
299 | i_time = time.time() - i_elapse |
---|
300 | time_found += i_time |
---|
301 | except: |
---|
302 | i_time = time.time() - i_elapse |
---|
303 | time_not_found += i_time |
---|
304 | not_found += 1 |
---|
305 | pass |
---|
306 | elif method == "c": |
---|
307 | if portal_types[i] in this_portal_types: |
---|
308 | found += 1 |
---|
309 | doc = res[this_portal_types.index(portal_types[i])].getObject().getContent() |
---|
310 | i_time = time.time() - i_elapse |
---|
311 | time_found += i_time |
---|
312 | else: |
---|
313 | not_found += 1 |
---|
314 | i_time = time.time() - i_elapse |
---|
315 | time_not_found += i_time |
---|
316 | elif method == "d": |
---|
317 | if student_brain.has_key(portal_types[i]): |
---|
318 | found += 1 |
---|
319 | doc = student_brain[portal_types[i]].getObject().getContent() |
---|
320 | i_time = time.time() - i_elapse |
---|
321 | time_found += i_time |
---|
322 | else: |
---|
323 | not_found += 1 |
---|
324 | i_time = time.time() - i_elapse |
---|
325 | time_not_found += i_time |
---|
326 | i_elapse = time.time() |
---|
327 | if c and (c % intervall == 0): |
---|
328 | #i_time = time.time() - i_elapse |
---|
329 | t_per = 0.0 |
---|
330 | if found: |
---|
331 | t_per = time_found/found |
---|
332 | if t_per > t_max: |
---|
333 | t_max = t_per |
---|
334 | if t_per > 0.0 and t_per < t_min: |
---|
335 | t_min = t_per |
---|
336 | itf = 0.0 |
---|
337 | if found: |
---|
338 | itf = time_found/found |
---|
339 | itnf = 0.0 |
---|
340 | if not_found : |
---|
341 | itnf = time_not_found / not_found |
---|
342 | interval_time = time_found + time_not_found |
---|
343 | s = "%(c)d: %(found)d/%(not_found)d " % vars() |
---|
344 | s += "%(interval_time)6.2f %(itf)6.4f/%(itnf)6.4f " % vars() |
---|
345 | s += "%(t_min)6.4f/%(t_max)6.4f" % vars() |
---|
346 | print s |
---|
347 | t_found += found |
---|
348 | t_not_found += not_found |
---|
349 | t_time_found += time_found |
---|
350 | t_time_not_found += time_not_found |
---|
351 | time_found = time_not_found = 0.0 |
---|
352 | found = not_found = 0 |
---|
353 | # t_found += found |
---|
354 | # t_not_found += not_found |
---|
355 | elapse = time.time() - elapse |
---|
356 | itf = 0.0 |
---|
357 | if t_found: |
---|
358 | itf = t_time_found/t_found |
---|
359 | itnf = 0.0 |
---|
360 | if t_not_found: |
---|
361 | itnf = t_time_not_found / t_not_found |
---|
362 | #c_elapse = time.clock() - c_elapse |
---|
363 | s = "%(probe)d: %(t_found)d/%(t_not_found)d " % vars() |
---|
364 | s += "%(elapse)6.2f %(itf)6.4f/%(itnf)6.4f " % vars() |
---|
365 | s += "%(t_min)6.4f/%(t_max)6.4f" % vars() |
---|
366 | print "-"*40 |
---|
367 | print s |
---|
368 | rel_found = float(t_found)/probe |
---|
369 | rel_not_found = float(t_not_found)/probe |
---|
370 | estimated_total_time = num_objects*(rel_found*itf + rel_not_found*itnf) |
---|
371 | print estimated_total_time |
---|
372 | ###) |
---|
373 | |
---|
374 | security.declareProtected(ModifyPortalContent,'writeLog') ###( |
---|
375 | def writeLog(self,logfile,s): |
---|
376 | """write to the log file""" |
---|
377 | logfile.write(s) |
---|
378 | ###) |
---|
379 | |
---|
380 | def generateStudentId(self,letter): ###( |
---|
381 | import random |
---|
382 | r = random |
---|
383 | ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
384 | if letter == '?': |
---|
385 | letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY') |
---|
386 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
387 | students = self.portal_url.getPortalObject().campus.students |
---|
388 | # while hasattr(students, sid): |
---|
389 | # sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
390 | while self.students_catalog(id = sid): |
---|
391 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
392 | return sid |
---|
393 | ###) |
---|
394 | |
---|
395 | def generatePassword(self,s=None): ###( |
---|
396 | import random |
---|
397 | r = random |
---|
398 | ##if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
399 | if s is None: |
---|
400 | s = 'abcdefghklmnpqrstuvwxy23456789' |
---|
401 | pw = '' |
---|
402 | while len(pw) < 6: |
---|
403 | pw += r.choice(s) |
---|
404 | return pw |
---|
405 | ###) |
---|
406 | |
---|
407 | security.declareProtected(ModifyPortalContent, 'dumpSchoolfeePayments') ###( |
---|
408 | def dumpSchoolfeePayments(self): |
---|
409 | "dump paid schoolfees" |
---|
410 | mtool = self.portal_membership |
---|
411 | member = mtool.getAuthenticatedMember() |
---|
412 | logger = logging.getLogger('WAeUPTool.dumpSchoolfees') |
---|
413 | aq_student = self.students_catalog.evalAdvancedQuery |
---|
414 | query = In('review_state',('schoolfee_paid', |
---|
415 | 'courses_registered', |
---|
416 | 'courses_validated', |
---|
417 | )) |
---|
418 | res = aq_student(query) |
---|
419 | #import pdb;pdb.set_trace() |
---|
420 | l = [] |
---|
421 | logger.info("start for %d" % len(res)) |
---|
422 | count = 1 |
---|
423 | log_after = 100 |
---|
424 | for student in res: |
---|
425 | if not count % log_after: |
---|
426 | logger.info("processed %d total %d" % (log_after,count)) |
---|
427 | count += 1 |
---|
428 | fee_dict =self.getSchoolFee(student) |
---|
429 | fulltime = student.mode.endswith('_ft') |
---|
430 | d = {} |
---|
431 | d['student_id'] = student.id |
---|
432 | d['name'] = student.name |
---|
433 | d['amount'] = fee_dict.get(new_returning) |
---|
434 | l += d, |
---|
435 | csv_name = self.dumpListToCSV(l,'payments') |
---|
436 | logger.info('%s dumped payments to %s' % (member,export_file)) |
---|
437 | ###) |
---|
438 | |
---|
439 | security.declarePublic('dumpListToCSV') ###( |
---|
440 | def dumpListToCSV(self,l,filename,fields=None): |
---|
441 | """dump a list of dicts to a CSV file""" |
---|
442 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
443 | export_file = "%s/export/%s_%s.csv" % (i_home,filename,current,) |
---|
444 | if fields is None: |
---|
445 | fields = l[0].keys() |
---|
446 | headline = ','.join(fields) |
---|
447 | out = open(export_file,"wb") |
---|
448 | out.write(headline +'\n') |
---|
449 | out.close() |
---|
450 | out = open(export_file,"a") |
---|
451 | csv_writer = csv.DictWriter(out,fields,) |
---|
452 | csv_writer.writerows(l) |
---|
453 | return export_file |
---|
454 | ###) |
---|
455 | |
---|
456 | security.declareProtected(ManagePortal, 'listMembers') ###( |
---|
457 | def listMembers(self): |
---|
458 | "list all members" |
---|
459 | mtool = self.portal_membership |
---|
460 | member = mtool.getAuthenticatedMember() |
---|
461 | logger = logging.getLogger('WAeUPTool.listMembers') |
---|
462 | if str(member) not in ('admin','joachim'): |
---|
463 | logger.info('%s tried to list members' % (member)) |
---|
464 | return None |
---|
465 | members = self.portal_directories.members |
---|
466 | all = members.listEntryIdsAndTitles() |
---|
467 | l = [] |
---|
468 | for user_id,name in all: |
---|
469 | d = {} |
---|
470 | d['user_id'] = user_id |
---|
471 | d['name'] = name |
---|
472 | d['pw'] = getattr(getattr(members,user_id),'password') |
---|
473 | d['email'] = getattr(getattr(members,user_id),'email') |
---|
474 | d['groups'] = " ".join(getattr(getattr(members,user_id),'groups')) |
---|
475 | d['roles'] = " ".join(getattr(getattr(members,user_id),'roles')) |
---|
476 | l += d, |
---|
477 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
478 | export_file = "%s/export/member_list_%s.csv" % (i_home,current,) |
---|
479 | logger.info('%s dumped member list to %s' % (member,export_file)) |
---|
480 | fields = l[0].keys() |
---|
481 | headline = ','.join(fields) |
---|
482 | out = open(export_file,"wb") |
---|
483 | out.write(headline +'\n') |
---|
484 | out.close() |
---|
485 | out = open(export_file,"a") |
---|
486 | csv_writer = csv.DictWriter(out,fields,) |
---|
487 | csv_writer.writerows(l) |
---|
488 | ###) |
---|
489 | |
---|
490 | security.declareProtected(ManagePortal, 'listStudents') ###( |
---|
491 | def listStudents(self): |
---|
492 | "list all students" |
---|
493 | mtool = self.portal_membership |
---|
494 | member = mtool.getAuthenticatedMember() |
---|
495 | logger = logging.getLogger('WAeUPTool.listStudents') |
---|
496 | if str(member) not in ('admin','joachim'): |
---|
497 | logger.info('%s tried to list students' % (member)) |
---|
498 | return None |
---|
499 | students = self.portal_directories.students |
---|
500 | all = students.listEntryIdsAndTitles() |
---|
501 | l = [] |
---|
502 | for user_id,name in all: |
---|
503 | d = {} |
---|
504 | d['user_id'] = user_id |
---|
505 | d['name'] = name |
---|
506 | d['pw'] = getattr(getattr(students,user_id),'password') |
---|
507 | d['email'] = getattr(getattr(students,user_id),'email') |
---|
508 | d['groups'] = " ".join(getattr(getattr(students,user_id),'groups')) |
---|
509 | d['roles'] = " ".join(getattr(getattr(students,user_id),'roles')) |
---|
510 | l += d, |
---|
511 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
512 | export_file = "%s/export/student_list_%s.csv" % (i_home,current,) |
---|
513 | logger.info('%s dumped student list to %s' % (member,export_file)) |
---|
514 | fields = l[0].keys() |
---|
515 | headline = ','.join(fields) |
---|
516 | out = open(export_file,"wb") |
---|
517 | out.write(headline +'\n') |
---|
518 | out.close() |
---|
519 | out = open(export_file,"a") |
---|
520 | csv_writer = csv.DictWriter(out,fields,) |
---|
521 | csv_writer.writerows(l) |
---|
522 | ###) |
---|
523 | |
---|
524 | security.declareProtected(ManagePortal, 'removeDeletedDocIds') ###( |
---|
525 | def removeDeletedDocIds(self, max=1000): |
---|
526 | """ |
---|
527 | remove deleted docids from repository commit after max |
---|
528 | """ |
---|
529 | logger = logging.getLogger('WAeUPTool.removeDeletedDocIds') |
---|
530 | repository = getToolByName(self, 'portal_repository') |
---|
531 | pxtool = getToolByName(self, 'portal_proxies') |
---|
532 | logger.info('start') |
---|
533 | pxtool_infos = pxtool.getRevisionsUsed() |
---|
534 | logger.info('found %d used revisions ' % (len(pxtool_infos))) |
---|
535 | |
---|
536 | nb_revs = 0 |
---|
537 | docids_d = {} # all docids |
---|
538 | unused_docids_d = {} # all docids that are unused |
---|
539 | ids_unused_revs_docids = [] # ids for revs of unused docids |
---|
540 | unused_ids = [] # ids for unused revs |
---|
541 | total = 0 |
---|
542 | idlist = repository.objectIds() |
---|
543 | to_delete = 0 |
---|
544 | found = False |
---|
545 | for id in idlist: |
---|
546 | docid, rev = repository._splitId(id) |
---|
547 | if docid is None: |
---|
548 | logger.info("invalid doc_id %s" % docid) |
---|
549 | continue |
---|
550 | nb_revs += 1 |
---|
551 | if not pxtool_infos.has_key(docid): |
---|
552 | found = True |
---|
553 | to_delete += 1 |
---|
554 | unused_ids.append(id) |
---|
555 | elif not pxtool_infos[docid].has_key(rev): |
---|
556 | found = True |
---|
557 | to_delete += 1 |
---|
558 | unused_ids.append(id) |
---|
559 | if found and not to_delete % max: |
---|
560 | found = False |
---|
561 | #import pdb;pdb.set_trace() |
---|
562 | repository.manage_delObjects(unused_ids) |
---|
563 | transaction.commit() |
---|
564 | logger.info('removed %d total %d unused docids ' % (max,to_delete)) |
---|
565 | else: |
---|
566 | if unused_ids: |
---|
567 | repository.manage_delObjects(unused_ids) |
---|
568 | transaction.commit() |
---|
569 | logger.info('finished removing %d unused docids ' % (to_delete)) |
---|
570 | ###) |
---|
571 | |
---|
572 | security.declareProtected(View,'getCredential') ###( |
---|
573 | def getCredential(self,student_id): |
---|
574 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
575 | if not self.isStaff(): |
---|
576 | mtool = self.portal_membership |
---|
577 | member = mtool.getAuthenticatedMember() |
---|
578 | logger = logging.getLogger('WAeUPTool.getCredential') |
---|
579 | logger.info('%s tried to access password of %s' % (member,student_id)) |
---|
580 | return None |
---|
581 | if student_entry is None: |
---|
582 | return None |
---|
583 | return getattr(student_entry,"password","not set") |
---|
584 | ###) |
---|
585 | |
---|
586 | security.declarePublic('checkPassword') ###( |
---|
587 | def checkPassword(self,student_id,password): |
---|
588 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
589 | if student_entry is None: |
---|
590 | return False |
---|
591 | return getattr(student_entry,"password","not set") == password |
---|
592 | ###) |
---|
593 | |
---|
594 | security.declarePublic('checkGenericPassword') ###( |
---|
595 | def checkGenericPassword(self,member_id): |
---|
596 | member_entry = getattr(self.portal_directories.members,member_id,None) |
---|
597 | if member_entry is None: |
---|
598 | return False |
---|
599 | ltool = getToolByName(self, 'portal_layouts') |
---|
600 | unsecure_words = ltool._getOb('members')['w__password'].check_words |
---|
601 | password = getattr(member_entry,"password","not set") |
---|
602 | is_unsecure = password in unsecure_words |
---|
603 | if is_unsecure: |
---|
604 | logger = logging.getLogger('WAeUPTool.checkGenericPassword') |
---|
605 | logger.info('Member %s tried to log in with unsecure password %s' %(member_id,password)) |
---|
606 | return is_unsecure |
---|
607 | ###) |
---|
608 | |
---|
609 | security.declareProtected(ModifyPortalContent,'editPassword') ###( |
---|
610 | def editPassword(self,student_id,password): |
---|
611 | "edit a student password" |
---|
612 | student_entry = getattr(self.portal_directories.students,student_id,None) |
---|
613 | if student_entry is None: |
---|
614 | return |
---|
615 | setattr(student_entry,'password',password) |
---|
616 | ###) |
---|
617 | |
---|
618 | security.declareProtected(ModifyPortalContent,'doCommit') ###( |
---|
619 | def doCommit(self,logger=None): |
---|
620 | "commit some transactions" |
---|
621 | transaction.commit() |
---|
622 | ###) |
---|
623 | |
---|
624 | security.declarePublic('loadStudentFoto') ###( |
---|
625 | def loadStudentFoto(self,student,filename,folder): |
---|
626 | "return a student passport picture" |
---|
627 | #import pdb;pdb.set_trace() |
---|
628 | picture ="%s/import/%s/%s" % (i_home,folder,filename) |
---|
629 | student_id = student.getId() |
---|
630 | images_dir = getImagesDir(student_id) |
---|
631 | if not os.path.exists(images_dir): |
---|
632 | os.mkdir(images_dir) |
---|
633 | image_name = os.path.join(images_dir,"passport_%(student_id)s.jpg" % vars()) |
---|
634 | for extension in ('.jpg','.JPG'): |
---|
635 | fullname = "%(picture)s%(extension)s" % vars() |
---|
636 | if os.path.exists(fullname): |
---|
637 | copy2(fullname,image_name) |
---|
638 | return "successfully copied passport picture" |
---|
639 | return "passport picture not found: %s.jpg or .JPG" % picture |
---|
640 | ###) |
---|
641 | |
---|
642 | def old____loadStudentFoto(self,student,filename,folder): ###( |
---|
643 | "return a student passport picture" |
---|
644 | app = student.application |
---|
645 | app_doc = app.getContent() |
---|
646 | #clear = student.clearance |
---|
647 | #clear_doc = clear.getContent() |
---|
648 | #matric_no = clear_doc.matric_no.upper() |
---|
649 | picture1 ="%s/import/%s/%s.jpg" % (i_home,folder,filename) |
---|
650 | picture2 ="%s/import/%s/%s.JPG" % (i_home,folder,filename) |
---|
651 | #import pdb;pdb.set_trace() |
---|
652 | if os.path.exists(picture1): |
---|
653 | file = open(picture1) |
---|
654 | elif os.path.exists(picture2): |
---|
655 | file = open(picture2) |
---|
656 | else: |
---|
657 | return "passport picture not found %s" % picture1 |
---|
658 | reopened = False |
---|
659 | if self.portal_workflow.getInfoFor(app,'review_state',None) !='opened': |
---|
660 | self.portal_workflow.doActionFor(app,'open') |
---|
661 | reopened = True |
---|
662 | outfile = file.read() |
---|
663 | app_doc.manage_addFile('passport', |
---|
664 | file=outfile, |
---|
665 | title="%s.jpg" % filename) |
---|
666 | if reopened: |
---|
667 | self.portal_workflow.doActionFor(app,'close') |
---|
668 | return "successfully loaded passport picture" |
---|
669 | ###) |
---|
670 | |
---|
671 | security.declareProtected(ModifyPortalContent,'createOne') ###( |
---|
672 | def createOne(self,students_folder,student_brain,letter,commit=False): |
---|
673 | sid = self.waeup_tool.generateStudentId(letter) |
---|
674 | students_folder.invokeFactory('Student', sid) |
---|
675 | student = getattr(students_folder,sid) |
---|
676 | self.portal_workflow.doActionFor(student,'return') |
---|
677 | student.manage_setLocalRoles(sid, ['Owner',]) |
---|
678 | matric_no = student_brain.matric_no |
---|
679 | jamb_reg_no = student_brain.Entryregno |
---|
680 | self.students_catalog.addRecord(id = sid, |
---|
681 | matric_no = matric_no, |
---|
682 | jamb_reg_no = jamb_reg_no, |
---|
683 | sex = student_brain.Sex == "F", |
---|
684 | name = "%s %s %s" % (student_brain.Firstname, |
---|
685 | student_brain.Middlename, |
---|
686 | student_brain.Lastname) |
---|
687 | ) |
---|
688 | if commit: |
---|
689 | transaction.commit() |
---|
690 | return sid,jamb_reg_no |
---|
691 | ###) |
---|
692 | |
---|
693 | security.declareProtected(ModifyPortalContent,'addStudent') ###( |
---|
694 | def addStudent(self,dict): |
---|
695 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
696 | sid = self.waeup_tool.generateStudentId('?') |
---|
697 | students_folder.invokeFactory('Student', sid) |
---|
698 | student_obj = getattr(students_folder,sid) |
---|
699 | f2t = StudentImport.field2types_student |
---|
700 | #from pdb import set_trace; set_trace() |
---|
701 | d = {} |
---|
702 | #d['jamb_sex'] = 'M' |
---|
703 | #if dict.get('sex'): |
---|
704 | # d['jamb_sex'] = 'F' |
---|
705 | |
---|
706 | entry_session = dict.get('entry_session') |
---|
707 | if entry_session == self.getSessionId()[0]: |
---|
708 | wfaction = 'admit' |
---|
709 | wft = 'wf_transition_admit' |
---|
710 | password = None |
---|
711 | else: |
---|
712 | wfaction = 'return' |
---|
713 | wft = 'wf_transition_return' |
---|
714 | password = self.generatePassword() |
---|
715 | self.makeStudentMember(sid,password) |
---|
716 | |
---|
717 | for pt in f2t.keys(): |
---|
718 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
719 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
720 | sub_doc = sub_obj.getContent() |
---|
721 | #self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj) |
---|
722 | #d['Title'] = f2t[pt]['title'] |
---|
723 | for field in f2t[pt]['fields']: |
---|
724 | d[field] = dict.get(field,'') |
---|
725 | sub_doc.edit(mapping = d) |
---|
726 | new_state = f2t[pt][wft] |
---|
727 | if new_state != "remain": |
---|
728 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
729 | self.portal_workflow.doActionFor(student_obj,wfaction) |
---|
730 | student_obj.manage_setLocalRoles(sid, ['Owner',]) |
---|
731 | return sid,password |
---|
732 | ###) |
---|
733 | |
---|
734 | security.declarePublic('getCertificateBrain') ###( |
---|
735 | def getCertificateBrain(self,cert_id): |
---|
736 | "do it" |
---|
737 | res = ZCatalog.searchResults(self.portal_catalog_real, |
---|
738 | {'portal_type':"Certificate", |
---|
739 | 'id': cert_id}) |
---|
740 | if res: |
---|
741 | return res[0] |
---|
742 | return None |
---|
743 | ###) |
---|
744 | |
---|
745 | security.declareProtected(ModifyPortalContent,'get_csv_filenames') ###( |
---|
746 | def get_csv_filenames(self): |
---|
747 | "do it" |
---|
748 | files = [file for file in os.listdir("%s/import/" % (i_home)) |
---|
749 | if file.endswith('.csv') and (file.find('imported') == -1 and |
---|
750 | file.find('pending') == -1)] |
---|
751 | return files |
---|
752 | ###) |
---|
753 | |
---|
754 | security.declarePublic('findStudentByMatricelNo') ###( |
---|
755 | def findStudentByMatricelNo(self,matric_no): |
---|
756 | "do it" |
---|
757 | res = ZCatalog.searchResults(self.portal_catalog_real, |
---|
758 | {'portal_type':"StudentClearance", |
---|
759 | 'SearchableText': matric_no}) |
---|
760 | if res: |
---|
761 | return res[0] |
---|
762 | return None |
---|
763 | ###) |
---|
764 | |
---|
765 | security.declarePublic('makeStudentMember') ###( |
---|
766 | def makeStudentMember(self,sid,password='uNsEt'): |
---|
767 | """make the student a member""" |
---|
768 | membership = self.portal_membership |
---|
769 | membership.addMember(sid, |
---|
770 | password , |
---|
771 | roles=('Member', |
---|
772 | 'Student', |
---|
773 | ), |
---|
774 | domains='', |
---|
775 | properties = {'memberareaCreationFlag': False, |
---|
776 | 'homeless': True},) |
---|
777 | member = membership.getMemberById(sid) |
---|
778 | self.portal_registration.afterAdd(member, sid, password, None) |
---|
779 | #self.manage_setLocalRoles(sid, ['Owner',]) |
---|
780 | ###) |
---|
781 | |
---|
782 | security.declareProtected(View,'makeStudentData') ###( |
---|
783 | def makeStudentData(self,student_id,email=None,phone_nr=None): |
---|
784 | "create Datastructure for a returning Student" |
---|
785 | #import pdb;pdb.set_trace() |
---|
786 | logger = logging.getLogger('WAeUPTool.makeStudentData') |
---|
787 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
788 | #res = self.students_catalog(id=student_id) |
---|
789 | #if res: |
---|
790 | # st = res[0] |
---|
791 | #res = self.returning_import(matric_no = st.matric_no) |
---|
792 | res = self.returning_import(id = student_id) |
---|
793 | if res: |
---|
794 | student = res[0] |
---|
795 | else: |
---|
796 | logger.info('Id %s not found in returning_import' % student_id) |
---|
797 | return |
---|
798 | logger.info('%s creates data structure' % student_id) |
---|
799 | s_results = self.results_import(matric_no = student.matric_no) |
---|
800 | if s_results: |
---|
801 | lnr = self.getLevelFromResultsCosCode(s_results) |
---|
802 | level = "%d00" % lnr |
---|
803 | verdict,eligible = self.getVerdict(s_results[0].Verdict) |
---|
804 | #if eligible: |
---|
805 | # level = "%d00" % (lnr + 1) |
---|
806 | else: |
---|
807 | logger.info('matric_no %s not found in results_import' % student.matric_no) |
---|
808 | level = '' |
---|
809 | verdict = '' |
---|
810 | #student should not be allowed to perform this transition |
---|
811 | #wftool = self.portal_workflow |
---|
812 | #wftool.doActionFor(student,'return') |
---|
813 | certcode_org = student.Coursemajorcode |
---|
814 | certcode = makeCertificateCode(certcode_org) |
---|
815 | certificate_brain = self.getCertificateBrain(certcode) |
---|
816 | if not certificate_brain: |
---|
817 | em = 'Certificate %s org-code %s not found\n' % (certcode, certcode_org) |
---|
818 | logger.info(em) |
---|
819 | matric_no = student.matric_no |
---|
820 | sid = student_id |
---|
821 | student_obj = getattr(students_folder,sid) |
---|
822 | if not getattr(student_obj,'application'): |
---|
823 | student_obj.invokeFactory('StudentApplication','application') |
---|
824 | application = student_obj.application |
---|
825 | self.portal_workflow.doActionFor(application,'open',dest_container=application) |
---|
826 | da = {'Title': 'Application Data'} |
---|
827 | student_obj.invokeFactory('StudentPersonal','personal') |
---|
828 | da['jamb_reg_no'] = student.Entryregno |
---|
829 | em = self.getEntryMode(student.Entryregno) |
---|
830 | da['entry_mode'] = em |
---|
831 | personal = student_obj.personal |
---|
832 | self.portal_workflow.doActionFor(personal,'open',dest_container=personal) |
---|
833 | dp = {'Title': 'Personal Data'} |
---|
834 | student_obj.invokeFactory('StudentClearance','clearance') |
---|
835 | clearance = student_obj.clearance |
---|
836 | self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance) |
---|
837 | dc = {'Title': 'Clearance/Eligibility Record'} |
---|
838 | dc['matric_no'] = matric_no |
---|
839 | state = student.State |
---|
840 | lga = student.LGA |
---|
841 | if state and lga: |
---|
842 | lga = state + ' / ' + lga |
---|
843 | else: |
---|
844 | lga = "None" |
---|
845 | da['jamb_lga'] = dc['lga'] = lga |
---|
846 | da['app_email'] = dp['email'] = email |
---|
847 | da['app_mobile'] = dp['phone'] = phone_nr |
---|
848 | dp['firstname'] = student.Firstname |
---|
849 | dp['middlename'] = student.Middlename |
---|
850 | dp['lastname'] = student.Lastname |
---|
851 | da['jamb_lastname'] = "%s %s %s" % (student.Firstname,student.Middlename,student.Lastname) |
---|
852 | da['jamb_sex'] = student.Sex |
---|
853 | dp['sex'] = student.Sex == 'F' |
---|
854 | dp['perm_address'] = student.Permanent_Address |
---|
855 | application.getContent().edit(mapping=da) |
---|
856 | self.portal_workflow.doActionFor(application,'close',dest_container=application) |
---|
857 | personal.getContent().edit(mapping=dp) |
---|
858 | clearance.getContent().edit(mapping=dc) |
---|
859 | self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance) |
---|
860 | # |
---|
861 | # Study Course |
---|
862 | # |
---|
863 | student_obj.invokeFactory('StudentStudyCourse','study_course') |
---|
864 | studycourse = student_obj.study_course |
---|
865 | self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse) |
---|
866 | dsc = {} |
---|
867 | dsc['study_course'] = certcode |
---|
868 | dsc['current_level'] = level |
---|
869 | dsc['current_verdict'] = verdict |
---|
870 | dsc['current_mode'] = em |
---|
871 | dsc['current_session'] = '05' |
---|
872 | studycourse.getContent().edit(mapping=dsc) |
---|
873 | # |
---|
874 | # Level |
---|
875 | # |
---|
876 | # l = getattr(studycourse,level,None) |
---|
877 | # if l is None: |
---|
878 | # studycourse.invokeFactory('StudentStudyLevel', level) |
---|
879 | # l = getattr(studycourse, level) |
---|
880 | # self.portal_workflow.doActionFor(l,'open',dest_container=l) |
---|
881 | # l.getContent().edit(mapping={'Title': "Level %s" % level}) |
---|
882 | ###) |
---|
883 | |
---|
884 | def init_timing(self): ###( |
---|
885 | if self.with_timing: |
---|
886 | if not hasattr(self,'_v_step_times'): |
---|
887 | self._v_timer_count = 0 |
---|
888 | self._v_total = 0 |
---|
889 | self._v_step_times = {} |
---|
890 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
891 | self._v_timer_file = "%s/export/timing_%s.csv" % (i_home,current,) |
---|
892 | self.timer_step = 0 |
---|
893 | self.total_time = 0 |
---|
894 | self.elapse = time.time() |
---|
895 | self.i_elapse = time.time() |
---|
896 | self.c_elapse = time.clock() |
---|
897 | ###) |
---|
898 | |
---|
899 | def do_timing(self): ###( |
---|
900 | if self.with_timing: |
---|
901 | try: |
---|
902 | raise 'dummy' |
---|
903 | except: |
---|
904 | frame = sys.exc_traceback.tb_frame.f_back |
---|
905 | locals = frame.f_locals |
---|
906 | globals = frame.f_globals |
---|
907 | functionname = frame.f_code.co_name |
---|
908 | filename = os.path.basename(frame.f_code.co_filename) |
---|
909 | lineno = frame.f_lineno |
---|
910 | mod_line = "%(functionname)s:%(lineno)s" % vars() |
---|
911 | i_time = time.time() - self.i_elapse |
---|
912 | td = {} |
---|
913 | if self._v_step_times.has_key(mod_line): |
---|
914 | a_time = self._v_step_times[mod_line]['a_time'] + i_time |
---|
915 | td['a_time'] = a_time |
---|
916 | else: |
---|
917 | td['a_time'] = i_time |
---|
918 | td['i_time'] = i_time |
---|
919 | self._v_step_times[mod_line] = td |
---|
920 | self.i_time = i_time |
---|
921 | self.total_time += i_time |
---|
922 | self.timer_step +=1 |
---|
923 | self.i_elapse = time.time() |
---|
924 | ###) |
---|
925 | |
---|
926 | security.declareProtected(ModifyPortalContent,'print_timing') ###( ###( |
---|
927 | def print_timing(self): |
---|
928 | if self.with_timing: |
---|
929 | l = [] |
---|
930 | timer_count = self._v_timer_count + 1 |
---|
931 | mod_lines = self._v_step_times.keys() |
---|
932 | mod_lines.sort(cmp,reverse=0) |
---|
933 | for mod_line in mod_lines: |
---|
934 | td = self._v_step_times[mod_line] |
---|
935 | i_time = td['i_time'] |
---|
936 | a_time = td['a_time']/(self._v_timer_count + 1) |
---|
937 | l += ("%(mod_line)s,%(i_time)6.2f,%(a_time)6.2f,%(timer_count)d" % vars()), |
---|
938 | total_time = self.total_time |
---|
939 | total_avarage = self._v_total / timer_count |
---|
940 | l += ("total,%(total_time)6.4f,%(total_avarage)6.4f,%(timer_count)d" % vars()), |
---|
941 | print "\r\n".join(l) |
---|
942 | out = open(self._v_timer_file,'a') |
---|
943 | out.write("\r\n".join(l)) |
---|
944 | out.close() |
---|
945 | ###) |
---|
946 | |
---|
947 | security.declareProtected(ModifyPortalContent,'get_timing_data') ###( ###( |
---|
948 | def get_timing_data(self): |
---|
949 | if self.with_timing: |
---|
950 | timer_count = self._v_timer_count + 1 |
---|
951 | results = {} |
---|
952 | for k,d in self._v_step_times.items(): |
---|
953 | dd = {} |
---|
954 | dd['a_time'] = d['a_time']/timer_count |
---|
955 | dd['i_time'] = d['i_time'] |
---|
956 | dd['count'] = timer_count |
---|
957 | results[k] = dd |
---|
958 | dd = {} |
---|
959 | dd['a_time'] = self._v_total / timer_count |
---|
960 | dd['i_time'] = self.total_time |
---|
961 | dd['count'] = timer_count |
---|
962 | results["total"] = dd |
---|
963 | return results |
---|
964 | ###) |
---|
965 | |
---|
966 | security.declareProtected(ModifyPortalContent,'admitOneStudent') ###( |
---|
967 | def admitOneStudent(self,brain,entry_session,pin_password,with_timing=False): |
---|
968 | "create Datastructure for an admitted Student" |
---|
969 | #import pdb;pdb.set_trace() |
---|
970 | logger = logging.getLogger('WAeUPTool.admitOneStudent') |
---|
971 | self.with_timing = with_timing |
---|
972 | |
---|
973 | if brain.screening_type in ('cest','sandwich',): |
---|
974 | reg_no = "%s%s/%s" % (brain.course1[:3],brain.serial,brain.entry_session) |
---|
975 | else: |
---|
976 | reg_no = brain.reg_no |
---|
977 | |
---|
978 | #ignore argument entry_session |
---|
979 | if not brain.entry_session: |
---|
980 | logger.info('no entry_session for %s provided' % (reg_no)) |
---|
981 | return |
---|
982 | |
---|
983 | if not hasattr(self,"_v_certificates"): |
---|
984 | self._v_certificates = self.getCertificatesDict() |
---|
985 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
986 | |
---|
987 | res = self.students_catalog(jamb_reg_no = reg_no) |
---|
988 | if res: |
---|
989 | logger.info('student with this reg_no already exists %s with id %s' % (reg_no,res[0].id)) |
---|
990 | return |
---|
991 | if brain.status != "admitted": |
---|
992 | logger.info('status of %s is %s' % (reg_no,brain.status)) |
---|
993 | return |
---|
994 | pin_parts = brain.pin.split('-') |
---|
995 | if pin_parts and len(pin_parts) != 3: |
---|
996 | logger.info('invalid pin %s for %s' % (brain.pin,reg_no)) |
---|
997 | return |
---|
998 | if not brain.course_admitted: |
---|
999 | logger.info('no course_admitted provided for %s' % (reg_no)) |
---|
1000 | return |
---|
1001 | if brain.course_admitted not in self._v_certificates: |
---|
1002 | logger.info('certificate %s not found for %s' % (brain.course_admitted,reg_no)) |
---|
1003 | return |
---|
1004 | if brain.sex not in (True,False): |
---|
1005 | logger.info('sex of %s not available' % (reg_no)) |
---|
1006 | return |
---|
1007 | self.init_timing() |
---|
1008 | student_id = self.generateStudentId('?') |
---|
1009 | students_folder.invokeFactory('Student', student_id) |
---|
1010 | student_object = getattr(students_folder,student_id) |
---|
1011 | self.do_timing() |
---|
1012 | if pin_password: |
---|
1013 | password = pin_parts[2] |
---|
1014 | self.makeStudentMember(student_id,password = password) |
---|
1015 | student_object.manage_setLocalRoles(student_id, ['Owner',]) |
---|
1016 | self.do_timing() |
---|
1017 | #logger.info("creating %s reg_no %s" % (student_id,reg_no)) |
---|
1018 | # |
---|
1019 | # application |
---|
1020 | # |
---|
1021 | student_object.invokeFactory('StudentApplication','application') |
---|
1022 | application = student_object.application |
---|
1023 | #self.portal_workflow.doActionFor(application,'open',dest_container=application) |
---|
1024 | #self.do_timing() |
---|
1025 | da = {'Title': 'Application Data'} |
---|
1026 | da['jamb_reg_no'] = reg_no |
---|
1027 | |
---|
1028 | sex = 'M' |
---|
1029 | if brain.sex: |
---|
1030 | sex = 'F' |
---|
1031 | da['jamb_sex'] = sex |
---|
1032 | da['jamb_age'] = brain.jamb_age |
---|
1033 | da['app_reg_pin'] = brain.pin |
---|
1034 | da['jamb_lga'] = brain.jamb_lga |
---|
1035 | da['jamb_state'] = brain.jamb_state |
---|
1036 | da['jamb_score'] = brain.aggregate |
---|
1037 | da['app_email'] = brain.email |
---|
1038 | da['app_mobile'] = brain.phone |
---|
1039 | |
---|
1040 | da['entry_mode'] = self._v_certificates[brain.course_admitted]['study_mode'] |
---|
1041 | |
---|
1042 | #da['entry_session'] = entry_session |
---|
1043 | da['entry_session'] = brain.entry_session |
---|
1044 | da['jamb_lastname'] = brain.lastname |
---|
1045 | da['jamb_middlename'] = brain.middlenames # different field names! |
---|
1046 | da['jamb_firstname'] = brain.firstname |
---|
1047 | da['screening_application_date'] = brain.application_date |
---|
1048 | da['date_of_birth'] = brain.date_of_birth |
---|
1049 | da['jamb_first_cos'] = brain.course1 |
---|
1050 | da['jamb_second_cos'] = brain.course2 |
---|
1051 | da['course3'] = brain.course3 |
---|
1052 | da['screening_type'] = brain.screening_type |
---|
1053 | da['screening_score'] = brain.screening_score |
---|
1054 | da['screening_date'] = brain.screening_date |
---|
1055 | da['hq_type'] = brain.hq_type |
---|
1056 | da['hq_grade'] = brain.hq_grade |
---|
1057 | da['aos'] = brain.aos |
---|
1058 | |
---|
1059 | application.getContent().edit(mapping=da) |
---|
1060 | self.do_timing() |
---|
1061 | #self.portal_workflow.doActionFor(application,'close',dest_container=application) |
---|
1062 | # |
---|
1063 | # personal |
---|
1064 | # |
---|
1065 | student_object.invokeFactory('StudentPersonal','personal') |
---|
1066 | personal = student_object.personal |
---|
1067 | #self.portal_workflow.doActionFor(personal,'open',dest_container=personal) |
---|
1068 | #self.do_timing() |
---|
1069 | dp = {'Title': 'Personal Data'} |
---|
1070 | dp['sex'] = brain.sex |
---|
1071 | dp['email'] = brain.email |
---|
1072 | dp['phone'] = brain.phone |
---|
1073 | dp['lastname'] = brain.lastname |
---|
1074 | dp['middlename'] = brain.middlenames # different field names! |
---|
1075 | dp['firstname'] = brain.firstname |
---|
1076 | personal.getContent().edit(mapping=dp) |
---|
1077 | self.do_timing() |
---|
1078 | # |
---|
1079 | # clearance |
---|
1080 | # |
---|
1081 | student_object.invokeFactory('StudentClearance','clearance') |
---|
1082 | clearance = student_object.clearance |
---|
1083 | #self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance) |
---|
1084 | dc = {'Title': 'Clearance/Eligibility Record'} |
---|
1085 | dc['lga'] = brain.lga |
---|
1086 | dc['birthday'] = brain.date_of_birth |
---|
1087 | clearance.getContent().edit(mapping=dc) |
---|
1088 | self.do_timing() |
---|
1089 | #self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance) |
---|
1090 | # |
---|
1091 | # study Course |
---|
1092 | # |
---|
1093 | student_object.invokeFactory('StudentStudyCourse','study_course') |
---|
1094 | studycourse = student_object.study_course |
---|
1095 | #self.portal_workflow.doActionFor(studycourse,'open',dest_container=studycourse) |
---|
1096 | #self.do_timing() |
---|
1097 | dsc = {} |
---|
1098 | dsc['study_course'] = brain.course_admitted |
---|
1099 | dsc['current_verdict'] = '' |
---|
1100 | dsc['current_mode'] = da['entry_mode'] |
---|
1101 | if da['entry_mode'].startswith('de'): |
---|
1102 | dsc['current_level'] = '200' |
---|
1103 | elif da['entry_mode'].startswith('pre'): |
---|
1104 | dsc['current_level'] = '000' |
---|
1105 | else: |
---|
1106 | dsc['current_level'] = '100' |
---|
1107 | dsc['current_session'] = brain.entry_session |
---|
1108 | studycourse.getContent().edit(mapping=dsc) |
---|
1109 | self.do_timing() |
---|
1110 | # |
---|
1111 | # payments folder |
---|
1112 | student_object.invokeFactory('PaymentsFolder','payments') |
---|
1113 | payments = getattr(student_object,'payments') |
---|
1114 | #self.do_timing() |
---|
1115 | dpay = {} |
---|
1116 | dpay['Title'] = 'Payments' |
---|
1117 | payments.getContent().edit(mapping=dpay) |
---|
1118 | self.portal_workflow.doActionFor(payments,'open') |
---|
1119 | self.do_timing() |
---|
1120 | # |
---|
1121 | # passport foto |
---|
1122 | app_picture ="%s/import/images/%s/%s_passport.jpg" % (i_home, |
---|
1123 | brain.screening_type, |
---|
1124 | brain.reg_no) |
---|
1125 | images_dir = getImagesDir(student_id) |
---|
1126 | #images_dir = os.path.join("%s" % images_base,student_id) |
---|
1127 | letter_dir,student_dir = os.path.split(images_dir) |
---|
1128 | if not os.path.exists(letter_dir): |
---|
1129 | os.mkdir(letter_dir) |
---|
1130 | if not os.path.exists(images_dir): |
---|
1131 | os.mkdir(images_dir) |
---|
1132 | image_name = os.path.join(images_dir,"passport_%(student_id)s.jpg" % vars()) |
---|
1133 | if os.path.exists(app_picture): |
---|
1134 | copy2(app_picture,image_name) |
---|
1135 | else: |
---|
1136 | logger.info('passport of %s/%s not found: %s' % (student_id, |
---|
1137 | brain.reg_no, |
---|
1138 | app_picture)) |
---|
1139 | |
---|
1140 | self.do_timing() |
---|
1141 | self.print_timing() |
---|
1142 | if with_timing: |
---|
1143 | self.timer_step = 0 |
---|
1144 | self._v_timer_count += 1 |
---|
1145 | self._v_total += self.total_time |
---|
1146 | return student_id |
---|
1147 | ###) |
---|
1148 | |
---|
1149 | security.declareProtected(ModifyPortalContent,'makeStudentLevel') ###( |
---|
1150 | def makeStudentLevel(self,student_id): |
---|
1151 | "create the StudyLevel for a returning Student" |
---|
1152 | #import pdb;pdb.set_trace() |
---|
1153 | logger = logging.getLogger('WAeUPTool.makeStudentLevel') |
---|
1154 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1155 | res = self.students_catalog(id=student_id) |
---|
1156 | if res: |
---|
1157 | st = res[0] |
---|
1158 | course = st.course |
---|
1159 | matric_no = st.matric_no |
---|
1160 | level = st.level |
---|
1161 | res = self.results_import(matric_no = matric_no) |
---|
1162 | if res: |
---|
1163 | results = res |
---|
1164 | logger.info('%s creating Level %s' % (student_id,level)) |
---|
1165 | # |
---|
1166 | # Level |
---|
1167 | # |
---|
1168 | student_obj = getattr(self.portal_url.getPortalObject().campus.students,student_id) |
---|
1169 | studycourse = getattr(student_obj,"study_course",None) |
---|
1170 | self.portal_workflow.doActionFor(studycourse,'close_for_edit',dest_container=studycourse) |
---|
1171 | l = getattr(studycourse,level,None) |
---|
1172 | if l is None: |
---|
1173 | studycourse.invokeFactory('StudentStudyLevel', level) |
---|
1174 | l = getattr(studycourse, level) |
---|
1175 | self.portal_workflow.doActionFor(l,'open',dest_container=l) |
---|
1176 | l.getContent().edit(mapping={'Title': "Level %s" % level}) |
---|
1177 | ###) |
---|
1178 | |
---|
1179 | security.declarePublic('getHallInfo') ###( |
---|
1180 | def getHallInfo(self,bed): |
---|
1181 | """return Hall Info""" |
---|
1182 | info = {} |
---|
1183 | bedsplit = bed.split('_') |
---|
1184 | if len(bedsplit) == 4: |
---|
1185 | hall,block,room,letter = bed.split('_') |
---|
1186 | else: |
---|
1187 | info['maintenance_code'] = 'None' |
---|
1188 | return info |
---|
1189 | res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="AccoHall",id=hall) |
---|
1190 | if res and len(res) == 1: |
---|
1191 | hall_brain = res[0] |
---|
1192 | hall_doc = hall_brain.getObject().getContent() |
---|
1193 | else: |
---|
1194 | return info |
---|
1195 | info['hall_title'] = hall_brain.Title |
---|
1196 | info['maintenance_code'] = hall_doc.maintenance_code |
---|
1197 | res = ZCatalog.searchResults(self.portal_catalog_real,portal_type="ScratchCardBatch") |
---|
1198 | batch_doc = None |
---|
1199 | for brain in res: |
---|
1200 | if brain.id.startswith(info['maintenance_code']): |
---|
1201 | batch_doc = brain.getObject().getContent() |
---|
1202 | break |
---|
1203 | if batch_doc is None: |
---|
1204 | info['maintenance_fee'] = '' |
---|
1205 | else: |
---|
1206 | info['maintenance_fee'] = batch_doc.cost |
---|
1207 | return info |
---|
1208 | ###) |
---|
1209 | |
---|
1210 | security.declareProtected(ModifyPortalContent,'removePictureFolder') ###( |
---|
1211 | def removePictureFolder(self,student_id): |
---|
1212 | """remove picture_folder by renaming it""" |
---|
1213 | path = 'images' |
---|
1214 | picture_path = os.path.join(i_home,path,student_id) |
---|
1215 | if not os.path.exists(picture_path): |
---|
1216 | return False |
---|
1217 | os.rename(picture_path,picture_path + "_removed") |
---|
1218 | return True |
---|
1219 | ###) |
---|
1220 | |
---|
1221 | security.declareProtected(ModifyPortalContent,'restorePictureFolder') ###( |
---|
1222 | def restorePictureFolder(self,student_id): |
---|
1223 | """restore picture_folder by renaming it""" |
---|
1224 | path = 'images' |
---|
1225 | picture_path = os.path.join(i_home,path,student_id) |
---|
1226 | if not os.path.exists(picture_path + "_removed"): |
---|
1227 | return False |
---|
1228 | os.rename(picture_path + "_removed",picture_path) |
---|
1229 | return True |
---|
1230 | ###) |
---|
1231 | |
---|
1232 | security.declarePublic('picturesExist') ###( |
---|
1233 | def picturesExist(self, ids,student_id=None): |
---|
1234 | """check if pictures exist in the filesystem""" |
---|
1235 | if student_id is None: |
---|
1236 | student_id = self.getStudentId() |
---|
1237 | if student_id is None: |
---|
1238 | return False |
---|
1239 | picture_path = getImagesDir(student_id) |
---|
1240 | #picture_path = os.path.join(images_base,student_id) |
---|
1241 | if not os.path.exists(picture_path): |
---|
1242 | return False |
---|
1243 | pictures = [picture[:picture.rfind('_')] for picture in os.listdir(picture_path)] |
---|
1244 | return set(ids).issubset(set(pictures)) |
---|
1245 | ###) |
---|
1246 | |
---|
1247 | security.declarePublic('picturesList') ###( |
---|
1248 | def picturesList(self): |
---|
1249 | """check if pictures exist in the filesystem""" |
---|
1250 | path = 'images' |
---|
1251 | student_id = self.getStudentId() |
---|
1252 | #picture_path = os.path.join(i_home,path,student_id) |
---|
1253 | picture_path = getImagesDir(student_id) |
---|
1254 | if not os.path.exists(picture_path): |
---|
1255 | return [] |
---|
1256 | return [picture[:picture.rfind('_')] for picture in os.listdir(picture_path)] |
---|
1257 | ###) |
---|
1258 | |
---|
1259 | security.declarePublic('showFsPicture') ###( |
---|
1260 | def showFsPicture(self,path): |
---|
1261 | """return a picture from the filesystem""" |
---|
1262 | #picture_path = os.path.join(i_home,path) |
---|
1263 | picture_path = os.path.join(images_base,path) |
---|
1264 | response = self.REQUEST.RESPONSE |
---|
1265 | #import pdb;pdb.set_trace() |
---|
1266 | registry = getToolByName(self, 'mimetypes_registry') |
---|
1267 | mimetype = str(registry.lookupExtension(path.lower()) or |
---|
1268 | registry.lookupExtension('file.bin')) |
---|
1269 | if os.path.exists(picture_path): |
---|
1270 | response.setHeader('Content-type',mimetype) |
---|
1271 | return open(picture_path).read() |
---|
1272 | picture_path = os.path.join(i_home,'import',path) |
---|
1273 | if os.path.exists(picture_path): |
---|
1274 | return open(picture_path).read() |
---|
1275 | ###) |
---|
1276 | |
---|
1277 | security.declareProtected(ModifyPortalContent,'deleteAllCourses') ###( |
---|
1278 | def deleteAllCourses(self,department="All"): |
---|
1279 | ''' delete the courses''' |
---|
1280 | pm = self.portal_membership |
---|
1281 | member = pm.getAuthenticatedMember() |
---|
1282 | |
---|
1283 | if str(member) not in ("henrik","joachim"): |
---|
1284 | return "not possible" |
---|
1285 | if department == "All": |
---|
1286 | res = self.portal_catalog({'meta_type': 'Department'}) |
---|
1287 | if len(res) < 1: |
---|
1288 | return "No Departments found" |
---|
1289 | |
---|
1290 | deleted = [] |
---|
1291 | for dep in res: |
---|
1292 | cf = dep.getObject().courses |
---|
1293 | if cf: |
---|
1294 | cf.manage_delObjects(ids=cf.objectIds()) |
---|
1295 | deleted.append("deleted Courses in %s" % dep.getId) |
---|
1296 | return "\r".join(deleted) |
---|
1297 | ###) |
---|
1298 | |
---|
1299 | security.declareProtected(ModifyPortalContent,'getLogfileLines') ###( |
---|
1300 | def getLogfileLines(self,filename="event.log",numlines=20): |
---|
1301 | """Get last NUMLINES lines of logfile FILENAME. |
---|
1302 | |
---|
1303 | Return last lines' of a file in the instances logfile directory as |
---|
1304 | a list. The number of returned lines equals `numlines' or less. If |
---|
1305 | less than `numlines' lines are available, the whole file ist |
---|
1306 | returned. If the file can not be opened or some other error |
---|
1307 | occurs, empty list is returend. |
---|
1308 | """ |
---|
1309 | result = [] |
---|
1310 | lines_hit = 0 |
---|
1311 | |
---|
1312 | # We only handle files in instances' log directory... |
---|
1313 | logpath = os.path.join(i_home, "log") |
---|
1314 | filename = str(os.path.abspath( os.path.join( logpath, filename ))) |
---|
1315 | if not filename.startswith( logpath ): |
---|
1316 | # Attempt to access file outside log-dir... |
---|
1317 | return [] |
---|
1318 | |
---|
1319 | try: |
---|
1320 | fd = file( filename, "rb" ) |
---|
1321 | except IOError: |
---|
1322 | return [] |
---|
1323 | if not fd: |
---|
1324 | return [] |
---|
1325 | |
---|
1326 | if os.linesep == None: |
---|
1327 | linesep = '\n' |
---|
1328 | else: |
---|
1329 | linesep = os.linesep |
---|
1330 | |
---|
1331 | # Try to find 'numlines' times a lineseparator, searching from end |
---|
1332 | # and moving to the beginning of file... |
---|
1333 | fd.seek( 0, 2) # Move to end of file... |
---|
1334 | while lines_hit < numlines: |
---|
1335 | if fd.read(1) == linesep[-1]: # This moves filedescriptor |
---|
1336 | # one step forward... |
---|
1337 | lines_hit += 1 |
---|
1338 | try: |
---|
1339 | fd.seek( -2, 1) # Go two bytes back from current pos... |
---|
1340 | except IOError: |
---|
1341 | # We cannot go back two bytes. Maybe the file is too small... |
---|
1342 | break |
---|
1343 | fd.seek(2,1) |
---|
1344 | |
---|
1345 | # Read all lines from current position... |
---|
1346 | result = fd.readlines() |
---|
1347 | # Remove line endings... |
---|
1348 | result = [x.strip() for x in result] |
---|
1349 | fd.close() |
---|
1350 | return result |
---|
1351 | ###) |
---|
1352 | |
---|
1353 | security.declareProtected(ModifyPortalContent,"getCallbacksFromLog")###( |
---|
1354 | def getCallbacksFromLog(self,filename): |
---|
1355 | """fix Online Payment Transactions from Z2.log entries""" |
---|
1356 | import transaction |
---|
1357 | import random |
---|
1358 | from cgi import parse_qs |
---|
1359 | from urlparse import urlparse |
---|
1360 | #from pdb import set_trace |
---|
1361 | wftool = self.portal_workflow |
---|
1362 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
1363 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1364 | s = r'(?P<client_ip>\S+) - (?P<member_id>\S+) \[' |
---|
1365 | s += r'(?P<date>.*)\] "(?P<get>.*)" (?P<codes>\d+ \d+) "' |
---|
1366 | s += r'(?P<intersw>.*)" "(?P<agent>.*)"' |
---|
1367 | data = re.compile(s) |
---|
1368 | start = True |
---|
1369 | tr_count = 1 |
---|
1370 | total = 0 |
---|
1371 | #name = 'pume_results' |
---|
1372 | #name = 'epaymentsuccessful_z2log2' |
---|
1373 | name = filename |
---|
1374 | no_import = [] |
---|
1375 | imported = [] |
---|
1376 | logger = logging.getLogger('WAeUPTool.getFailedTransactions') |
---|
1377 | try: |
---|
1378 | transactions = open("%s/import/%s" % (i_home,name),"rb").readlines() |
---|
1379 | except: |
---|
1380 | logger.error('Error reading %s' % name) |
---|
1381 | return |
---|
1382 | tas = [] |
---|
1383 | for line in transactions: |
---|
1384 | dict = {} |
---|
1385 | items = data.search(line) |
---|
1386 | dict['idict'] = idict = items.groupdict() |
---|
1387 | #print idict |
---|
1388 | #from pdb import set_trace;set_trace() |
---|
1389 | urlparsed = urlparse(idict['get'][4:]) |
---|
1390 | #print urlparsed |
---|
1391 | path = urlparsed[2].split('/') |
---|
1392 | dict['student_id'] = student_id = path[8] |
---|
1393 | dict['payment_id'] = payment_id = path[10] |
---|
1394 | dict['qs_dict'] = qs_dict = parse_qs(urlparsed[4]) |
---|
1395 | tas.append(dict) |
---|
1396 | tr_count += 1 |
---|
1397 | return tas |
---|
1398 | ###) |
---|
1399 | |
---|
1400 | security.declareProtected(ModifyPortalContent,"importOnlinePaymentTransactions")###( |
---|
1401 | def importOnlinePaymentTransactions(self): |
---|
1402 | """load Online Payment Transactions from CSV values""" |
---|
1403 | import transaction |
---|
1404 | import random |
---|
1405 | #from pdb import set_trace |
---|
1406 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
1407 | opt = self.online_payments_import |
---|
1408 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1409 | start = True |
---|
1410 | tr_count = 1 |
---|
1411 | total = 0 |
---|
1412 | #name = 'pume_results' |
---|
1413 | name = 'OnlineTransactions' |
---|
1414 | no_import = [] |
---|
1415 | imported = [] |
---|
1416 | logger = logging.getLogger('WAeUPTool.importOnlinePaymentTransactions') |
---|
1417 | try: |
---|
1418 | transactions = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
1419 | except: |
---|
1420 | logger.error('Error reading %s.csv' % name) |
---|
1421 | return |
---|
1422 | for pay_transaction in transactions: |
---|
1423 | if start: |
---|
1424 | start = False |
---|
1425 | logger.info('Start loading from %s.csv' % name) |
---|
1426 | s = ','.join(['"%s"' % fn for fn in pay_transaction.keys()]) |
---|
1427 | no_import.append('%s,"Error"' % s) |
---|
1428 | format = ','.join(['"%%(%s)s"' % fn for fn in pay_transaction.keys()]) |
---|
1429 | format_error = format + ',"%(Error)s"' |
---|
1430 | data = {} |
---|
1431 | |
---|
1432 | # format of the first file sent by Tayo |
---|
1433 | #data['datetime'] = date = DateTime.DateTime(pay_transaction['Date']) |
---|
1434 | #data['student_id'] = student_id = pay_transaction['Payer ID'] |
---|
1435 | #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)'] |
---|
1436 | #data['response_code'] = response_code = pay_transaction['Resp Code'] |
---|
1437 | #data['amount'] = amount = pay_transaction['Amount'] |
---|
1438 | |
---|
1439 | # format of the second file sent by Tayo |
---|
1440 | #data['datetime'] = date = 0 |
---|
1441 | #data['student_id'] = student_id = pay_transaction['Payer ID'] |
---|
1442 | #data['order_id'] = order_id = pay_transaction['Order ID (Tranx Ref)'] |
---|
1443 | #data['response_code'] = response_code = '00' |
---|
1444 | #data['amount'] = amount = pay_transaction['Amount'] |
---|
1445 | |
---|
1446 | # format of the third file sent by Kehinde |
---|
1447 | data['datetime'] = date = 0 |
---|
1448 | data['student_id'] = student_id = pay_transaction['customer_id'] |
---|
1449 | data['order_id'] = order_id = pay_transaction['merchant_reference'] |
---|
1450 | data['response_code'] = response_code = '00' |
---|
1451 | data['amount'] = amount = pay_transaction['Amount'] |
---|
1452 | |
---|
1453 | dup = False |
---|
1454 | if response_code == "12": |
---|
1455 | continue |
---|
1456 | try: |
---|
1457 | opt.addRecord(**data) |
---|
1458 | except ValueError: |
---|
1459 | dup = True |
---|
1460 | #from pdb import set_trace;set_trace() |
---|
1461 | if dup: |
---|
1462 | if response_code == "00": |
---|
1463 | try: |
---|
1464 | opt.modifyRecord(**data) |
---|
1465 | except: |
---|
1466 | logger.info("duplicate uid, order_id %(order_id)s, student_id %(student_id)s, response_code %(response_code)s" % data) |
---|
1467 | continue |
---|
1468 | else: |
---|
1469 | pay_transaction['Error'] = "Duplicate order_id" |
---|
1470 | no_import.append( format_error % pay_transaction) |
---|
1471 | logger.info("duplicate order_id %(order_id)s for %(student_id)s %(response_code)s" % data) |
---|
1472 | continue |
---|
1473 | tr_count += 1 |
---|
1474 | if tr_count > 1000: |
---|
1475 | if len(no_import) > 0: |
---|
1476 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
1477 | '\n'.join(no_import) + '\n') |
---|
1478 | no_import = [] |
---|
1479 | em = '%d transactions committed\n' % (tr_count) |
---|
1480 | transaction.commit() |
---|
1481 | regs = [] |
---|
1482 | logger.info(em) |
---|
1483 | total += tr_count |
---|
1484 | tr_count = 0 |
---|
1485 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
1486 | '\n'.join(no_import)) |
---|
1487 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
1488 | ###) |
---|
1489 | |
---|
1490 | security.declareProtected(ModifyPortalContent,"importData")###( |
---|
1491 | def importData(self,filename,name,edit=False,bypass_queue_catalog=False): |
---|
1492 | """load data from CSV values""" |
---|
1493 | import transaction |
---|
1494 | import random |
---|
1495 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1496 | uploads_folder = self.portal_url.getPortalObject().campus.uploads |
---|
1497 | pending_only = False |
---|
1498 | pend_str = '--' |
---|
1499 | elapse = time.time() |
---|
1500 | # |
---|
1501 | # preparations |
---|
1502 | # |
---|
1503 | if filename == pend_str: |
---|
1504 | pending_only = True |
---|
1505 | importer_name = ''.join([part.capitalize() for part in name.split('_')]) |
---|
1506 | importer = eval("%sImport" % importer_name)(self) |
---|
1507 | logger = importer.logger |
---|
1508 | if importer.init_errors: |
---|
1509 | logger.info(importer.init_errors) |
---|
1510 | return importer.init_errors |
---|
1511 | member = importer.member |
---|
1512 | #current = importer.current |
---|
1513 | import_date = importer.import_date |
---|
1514 | # |
---|
1515 | # not_imported |
---|
1516 | # |
---|
1517 | info = importer.info |
---|
1518 | data_keys = importer.data_keys |
---|
1519 | csv_keys = importer.csv_keys |
---|
1520 | #csv_keys.extend(info.keys()) |
---|
1521 | headline_mapping = dict((k,k) for k in csv_keys) |
---|
1522 | # |
---|
1523 | # pending |
---|
1524 | # |
---|
1525 | pending_path = importer.pending_path |
---|
1526 | pending_tmp = importer.pending_tmp |
---|
1527 | pending_backup = importer.pending_backup |
---|
1528 | pending_fn = importer.pending_fn |
---|
1529 | imported_path = importer.imported_path |
---|
1530 | imported_fn = importer.imported_fn |
---|
1531 | commit_after = importer.commit_after |
---|
1532 | pending = [] |
---|
1533 | pending_digests = [] |
---|
1534 | #total_added_to_pending = 0 |
---|
1535 | if not pending_only: |
---|
1536 | pending,pending_digests = importer.makeIdLists() |
---|
1537 | pending_at_start = len(pending) |
---|
1538 | datafile = open(pending_tmp,"w") |
---|
1539 | pending_csv_writer = csv.DictWriter(datafile, |
---|
1540 | csv_keys, |
---|
1541 | extrasaction='ignore') |
---|
1542 | pending_csv_writer.writerow(headline_mapping) |
---|
1543 | datafile.close() |
---|
1544 | # |
---|
1545 | # imported |
---|
1546 | # |
---|
1547 | if not os.path.exists(imported_path): |
---|
1548 | datafile = open(imported_path,"w") |
---|
1549 | imported_csv_writer = csv.DictWriter(datafile, |
---|
1550 | csv_keys, |
---|
1551 | extrasaction='ignore') |
---|
1552 | imported_csv_writer.writerow(headline_mapping) |
---|
1553 | datafile.close() |
---|
1554 | start = True |
---|
1555 | tr_count = 0 |
---|
1556 | total = 0 |
---|
1557 | total_added_to_imported = 0 |
---|
1558 | total_pending = 0 |
---|
1559 | import_source_done = "" |
---|
1560 | if pending_only: |
---|
1561 | import_source_path = pending_path |
---|
1562 | else: |
---|
1563 | import_source_path = "%s/import/%s.csv" % (i_home,filename) |
---|
1564 | import_source_done = "%s/import/%s.done" % (i_home,filename) |
---|
1565 | if not os.path.exists(import_source_path): |
---|
1566 | fn = os.path.split(import_source_path)[1] |
---|
1567 | em = 'no import file %(fn)s' % vars() |
---|
1568 | return em |
---|
1569 | import_source_fn = os.path.split(import_source_path)[1] |
---|
1570 | if not pending_only: |
---|
1571 | info['imported_from'] = import_source_fn |
---|
1572 | headline = csv.reader(open(import_source_path,"rb")).next() |
---|
1573 | if "import_mode" not in headline: |
---|
1574 | msg = 'import_mode must be in heading' |
---|
1575 | return msg |
---|
1576 | invalid_keys = importer.checkHeadline(headline) |
---|
1577 | if invalid_keys: |
---|
1578 | return 'not ignorable key(s): "%s" found in heading' % ", ".join(invalid_keys) |
---|
1579 | |
---|
1580 | import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore') |
---|
1581 | or k.strip() in info.keys())] |
---|
1582 | # diff2schema = set(import_keys).difference(set(importer.schema.keys())) |
---|
1583 | # diff2layout = set(import_keys).difference(set(importer.layout.keys())) |
---|
1584 | # if diff2schema and diff2schema != set(['id',]): |
---|
1585 | # msg = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema) |
---|
1586 | # return msg |
---|
1587 | # |
---|
1588 | # start importing |
---|
1589 | # |
---|
1590 | try: |
---|
1591 | reader = csv.DictReader(open(import_source_path,"rb")) |
---|
1592 | except: |
---|
1593 | msg = 'Error reading %s.csv' % filename |
---|
1594 | logger.error(msg) |
---|
1595 | return msg |
---|
1596 | items = [item for item in reader] |
---|
1597 | total_to_import = len(items) |
---|
1598 | tti_float = float(total_to_import) |
---|
1599 | if pending_only: |
---|
1600 | pending_at_start = total_to_import |
---|
1601 | count = 0 |
---|
1602 | imported = [] |
---|
1603 | old_commit_count = 0 |
---|
1604 | error_count = imported_count = 0 |
---|
1605 | already_in = 0 |
---|
1606 | for record in items: |
---|
1607 | item = {} |
---|
1608 | empty_value_keys = [] |
---|
1609 | for k,v in record.items(): |
---|
1610 | if k is None: |
---|
1611 | continue |
---|
1612 | if v: |
---|
1613 | if v == EMPTY: |
---|
1614 | empty_value_keys += k, |
---|
1615 | v = '' |
---|
1616 | item[k.strip()] = v.strip() |
---|
1617 | count += 1 |
---|
1618 | if start: |
---|
1619 | start = False |
---|
1620 | adapters = [MappingStorageAdapter(importer.schema, item)] |
---|
1621 | logger.info('%(member)s starts import from %(import_source_fn)s' % vars()) |
---|
1622 | dm = DataModel(item, adapters,context=self) |
---|
1623 | ds = DataStructure(data=item,datamodel=dm) |
---|
1624 | error_string = "" |
---|
1625 | total += 1 |
---|
1626 | import_mode = item.get('import_mode','') |
---|
1627 | import_method = getattr(importer, '%(import_mode)s' % vars(),None ) |
---|
1628 | if import_method is None: |
---|
1629 | error_string += "import_mode '%(import_mode)s' is invalid" % vars() |
---|
1630 | elif (import_mode in importer.required_modes and |
---|
1631 | not set(importer.required_keys[import_mode]).issubset(set(item.keys()))): |
---|
1632 | diff2import = set(importer.required_keys[import_mode]).difference(set(item.keys())) |
---|
1633 | error_string += 'required key(s): "%s" not found in record' % ", ".join(diff2import) |
---|
1634 | else: |
---|
1635 | for k in import_keys: |
---|
1636 | if k not in item.keys() or k not in importer.validators.keys(): |
---|
1637 | continue |
---|
1638 | if not importer.validators[k](ds,mode=import_mode): |
---|
1639 | error_string += ' ++ ' |
---|
1640 | error_string += "%s: %s" % (k,self.translation_service(ds.getError(k), |
---|
1641 | ds.getErrorMapping(k))) |
---|
1642 | if error_string: |
---|
1643 | error = error_string |
---|
1644 | id = '' |
---|
1645 | mapping = item |
---|
1646 | else: |
---|
1647 | temp_item = item.copy() |
---|
1648 | temp_item.update(dm) |
---|
1649 | results = import_method(temp_item) |
---|
1650 | id = results[0] |
---|
1651 | error = results[1] |
---|
1652 | mapping = results[2] |
---|
1653 | # |
---|
1654 | # restore some values |
---|
1655 | # |
---|
1656 | for k in empty_value_keys: |
---|
1657 | mapping[k] = EMPTY |
---|
1658 | if mapping.has_key('sex'): |
---|
1659 | #import pdb;pdb.set_trace() |
---|
1660 | if mapping['sex'] == True: |
---|
1661 | mapping['sex'] = 'F' |
---|
1662 | elif mapping['sex'] == False: |
---|
1663 | mapping['sex'] = 'M' |
---|
1664 | if pending_only: |
---|
1665 | info['imported_from'] = item['imported_from'] |
---|
1666 | data_string = ", ".join("%s: %s" % (k,v) for k,v in mapping.items()) |
---|
1667 | info['error'] = error |
---|
1668 | info['import_record_no'] = count + 1 |
---|
1669 | mapping.update(info) |
---|
1670 | log_list = [] |
---|
1671 | if error: |
---|
1672 | error_count += 1 |
---|
1673 | digest = makeDigest(mapping,data_keys) |
---|
1674 | if digest not in pending_digests: |
---|
1675 | pending_digests += digest, |
---|
1676 | pending.append(mapping) |
---|
1677 | if not pending_only: |
---|
1678 | log_list += "record from %(import_source_fn)s added to %(pending_fn)s, %(data_string)s, %(error)s" % vars(), |
---|
1679 | else: |
---|
1680 | already_in += 1 |
---|
1681 | pass |
---|
1682 | else: |
---|
1683 | imported_count += 1 |
---|
1684 | imported += mapping, |
---|
1685 | log_list += "record imported and added to %(imported_fn)s from %(import_source_fn)s, %(data_string)s" % vars(), |
---|
1686 | if log_list: |
---|
1687 | time_till_now = time.time() - elapse |
---|
1688 | percent_finished = (error_count + imported_count)/tti_float*100 |
---|
1689 | log_list.insert(0,("%(percent_finished)6.3f %% done in %(time_till_now)3.2fs," % vars()),) |
---|
1690 | logger.info(' '.join(log_list)) |
---|
1691 | finished = count > total_to_import - 1 |
---|
1692 | must_commit = (imported_count != old_commit_count) and (not imported_count % commit_after) |
---|
1693 | if must_commit: |
---|
1694 | old_commit_count = imported_count |
---|
1695 | |
---|
1696 | if must_commit or finished: |
---|
1697 | if len(imported): |
---|
1698 | transaction.commit() |
---|
1699 | datafile = open(imported_path,"a") |
---|
1700 | writer = csv.DictWriter(datafile, |
---|
1701 | csv_keys, |
---|
1702 | extrasaction='ignore') |
---|
1703 | writer.writerows(imported) |
---|
1704 | datafile.close() |
---|
1705 | total_added_to_imported += len(imported) |
---|
1706 | imported = [] |
---|
1707 | if len(pending) > 0: |
---|
1708 | datafile = open(pending_tmp,"a") |
---|
1709 | writer = csv.DictWriter(datafile, |
---|
1710 | csv_keys, |
---|
1711 | extrasaction='ignore') |
---|
1712 | writer.writerows(pending) |
---|
1713 | datafile.close() |
---|
1714 | total_pending += len(pending) |
---|
1715 | #total_added_to_pending += len(pending) |
---|
1716 | pending = [] |
---|
1717 | if not finished: |
---|
1718 | msg = '%(commit_after)d records imported and committed of total %(total_added_to_imported)d\n' % vars() |
---|
1719 | logger.info(msg) |
---|
1720 | elapse = time.time() - elapse |
---|
1721 | if os.path.exists(pending_path): |
---|
1722 | copy2(pending_path,pending_backup) |
---|
1723 | copy2(pending_tmp,pending_path) |
---|
1724 | msg = "finished importing from %(import_source_fn)s in %(elapse).2f seconds, " % vars() |
---|
1725 | msg += "%(count)d records totally read, %(total_added_to_imported)d added to %(imported_fn)s, " % vars() |
---|
1726 | if pending_only: |
---|
1727 | removed_pending = pending_at_start - total_pending |
---|
1728 | msg += "%(removed_pending)d removed from %(pending_fn)s" % vars() |
---|
1729 | else: |
---|
1730 | added_pending = total_pending - pending_at_start |
---|
1731 | msg += "%(added_pending)d added to %(pending_fn)s, %(already_in)s already in %(pending_fn)s" % vars() |
---|
1732 | #msg += "%(total_pending)d totally written" % vars() # this line does not make any sense |
---|
1733 | logger.info(msg) |
---|
1734 | if import_source_done: |
---|
1735 | copy(import_source_path,import_source_done) |
---|
1736 | os.remove(import_source_path) |
---|
1737 | upload = getattr(uploads_folder,os.path.split(import_source_path)[1],None) |
---|
1738 | if upload is not None: |
---|
1739 | upload_doc = upload.getContent() |
---|
1740 | mapping = {} |
---|
1741 | #mapping['import_date'] = DateTime.DateTime() |
---|
1742 | mapping['import_date'] = import_date |
---|
1743 | mapping['imported_by'] = importer.imported_by |
---|
1744 | mapping['import_message'] = msg |
---|
1745 | upload_doc.edit(mapping = mapping) |
---|
1746 | os.remove(pending_tmp) |
---|
1747 | return msg |
---|
1748 | ###) |
---|
1749 | |
---|
1750 | security.declareProtected(ModifyPortalContent,"moveImagesToFS")###( |
---|
1751 | def moveImagesToFS(self,student_id="O738726"): |
---|
1752 | "move the images to the filesystem" |
---|
1753 | images_dir = getImagesDir(student_id) |
---|
1754 | #images_dir = os.path.join("%s" % images_base,student_id) |
---|
1755 | student_folder = getattr(self.portal_url.getPortalObject().campus.students,student_id) |
---|
1756 | stool = getToolByName(self, 'portal_schemas') |
---|
1757 | schemas = ['student_application', |
---|
1758 | 'student_clearance', |
---|
1759 | ] |
---|
1760 | created = False |
---|
1761 | for schema_id in schemas: |
---|
1762 | schema = stool._getOb(schema_id) |
---|
1763 | object = getattr(student_folder,schema_id[len('student_'):],None) |
---|
1764 | if object is None: |
---|
1765 | continue |
---|
1766 | doc = object.getContent() |
---|
1767 | for key in schema.keys(): |
---|
1768 | if schema[key].meta_type != "CPS Image Field": |
---|
1769 | continue |
---|
1770 | #import pdb;pdb.set_trace() |
---|
1771 | image = getattr(doc,key,None) |
---|
1772 | if not image or not hasattr(image,"data"): |
---|
1773 | continue |
---|
1774 | if not created: |
---|
1775 | if not os.path.exists(images_dir): |
---|
1776 | os.mkdir(images_dir) |
---|
1777 | created = True |
---|
1778 | filename = os.path.join(images_dir,"%(key)s_%(student_id)s.jpg" % vars()) |
---|
1779 | open(filename,"wb").write(str(image.data)) |
---|
1780 | ###) |
---|
1781 | |
---|
1782 | security.declareProtected(ModifyPortalContent,"movePassportToFS")###( |
---|
1783 | def movePassportToFS(self,student_id="O738726"): |
---|
1784 | "move the passports to the filesystem" |
---|
1785 | images_dir = os.path.join("%s" % i_home,'passports') |
---|
1786 | student_folder = getattr(self.portal_url.getPortalObject().campus.students,student_id) |
---|
1787 | stool = getToolByName(self, 'portal_schemas') |
---|
1788 | schemas = ['student_application', |
---|
1789 | #'student_clearance', |
---|
1790 | ] |
---|
1791 | created = False |
---|
1792 | for schema_id in schemas: |
---|
1793 | schema = stool._getOb(schema_id) |
---|
1794 | object = getattr(student_folder,schema_id[len('student_'):],None) |
---|
1795 | if object is None: |
---|
1796 | continue |
---|
1797 | doc = object.getContent() |
---|
1798 | for key in schema.keys(): |
---|
1799 | if schema[key].meta_type != "CPS Image Field": |
---|
1800 | continue |
---|
1801 | #import pdb;pdb.set_trace() |
---|
1802 | image = getattr(doc,key) |
---|
1803 | if not hasattr(image,"data"): |
---|
1804 | continue |
---|
1805 | if not created: |
---|
1806 | if not os.path.exists(images_dir): |
---|
1807 | os.mkdir(images_dir) |
---|
1808 | created = True |
---|
1809 | filename = os.path.join(images_dir,"%(student_id)s.jpg" % vars()) |
---|
1810 | open(filename,"wb").write(str(image.data)) |
---|
1811 | ###) |
---|
1812 | |
---|
1813 | InitializeClass(WAeUPTool) |
---|