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: WAeUPImport.py 3726 2008-10-14 10:11:54Z 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 Globals import package_home,INSTANCE_HOME |
---|
44 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
45 | import csv,re,os,sys |
---|
46 | from shutil import copy2 |
---|
47 | import DateTime,time |
---|
48 | import logging |
---|
49 | p_home = package_home(globals()) |
---|
50 | i_home = INSTANCE_HOME |
---|
51 | from utils import makeDigest |
---|
52 | |
---|
53 | NO_KEY = '----' |
---|
54 | IGNORE = 'ignore' |
---|
55 | class WAeUPImport(UniqueObject, SimpleItem, ActionProviderBase): ###( |
---|
56 | """ WAeUPImport """ |
---|
57 | required_modes = ('create',) |
---|
58 | |
---|
59 | def __init__(self,waeup_tool): ###( |
---|
60 | self.students_folder = waeup_tool.portal_url.getPortalObject().campus.students |
---|
61 | self.member = member = waeup_tool.portal_membership.getAuthenticatedMember() |
---|
62 | self.import_date = DateTime.DateTime() |
---|
63 | self.imported_by = str(member) |
---|
64 | #self.current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
65 | self.waeup_tool = waeup_tool |
---|
66 | self.academics_folder = waeup_tool.portal_url.getPortalObject().campus.academics |
---|
67 | self.schema_tool = getToolByName(waeup_tool, 'portal_schemas') |
---|
68 | self.layout_tool = getToolByName(waeup_tool, 'portal_layouts') |
---|
69 | self.portal_workflow = getToolByName(waeup_tool, 'portal_workflow') |
---|
70 | self.portal_url = getToolByName(waeup_tool, 'portal_url') |
---|
71 | self.portal_catalog = waeup_tool.portal_catalog |
---|
72 | self.students_catalog = waeup_tool.students_catalog |
---|
73 | self.courses_catalog = waeup_tool.courses_catalog |
---|
74 | self.course_results = waeup_tool.course_results |
---|
75 | self.applicants_catalog = waeup_tool.applicants_catalog |
---|
76 | #self.mode = mode |
---|
77 | # self.import_method = getattr(self, '%s' % mode,None) |
---|
78 | errors = [] |
---|
79 | # if self.import_method is None: |
---|
80 | # errors.append('No importer method %s' % mode) |
---|
81 | self.pending_path = "%s/import/%s.pending" % (i_home,self.plural_name) |
---|
82 | self.pending_tmp = "%s/import/%s.pending.tmp" % (i_home,self.plural_name) |
---|
83 | self.pending_backup = "%s/import/%s.pending.old" % (i_home,self.plural_name) |
---|
84 | self.pending_fn = os.path.split(self.pending_path)[1] |
---|
85 | self.imported_path = "%s/import/%s.imported" % (i_home,self.plural_name) |
---|
86 | self.imported_fn = os.path.split(self.imported_path)[1] |
---|
87 | iname = "import_%s" % self.name |
---|
88 | self.logger = logging.getLogger('WAeUPImport.%sImport' % self.plural_name.capitalize()) |
---|
89 | self.schema = self.schema_tool._getOb(iname,None) |
---|
90 | #self.pending_schema = self.schema_tool._getOb("%s_pending" % iname,None) |
---|
91 | self.layout = self.layout_tool._getOb(iname,None) |
---|
92 | while True: |
---|
93 | if self.schema is None: |
---|
94 | errors.append('no schema %s' % iname) |
---|
95 | # if self.pending_schema is None: |
---|
96 | # self.pending_schema = self.schema |
---|
97 | if self.layout is None: |
---|
98 | errors.append('no such layout %s' % iname) |
---|
99 | if errors: |
---|
100 | break |
---|
101 | self.data_keys = self.schema.keys() |
---|
102 | self.csv_keys = self.schema.keys() |
---|
103 | info = {} |
---|
104 | info['imported_from'] = '' |
---|
105 | info['import_record_no'] = 0 |
---|
106 | info['imported_by'] = self.imported_by |
---|
107 | info['import_date'] = self.import_date.strftime("%d/%m/%y %H:%M:%S") |
---|
108 | info['error'] = '' |
---|
109 | self.info = info |
---|
110 | self.csv_keys.extend(self.info) |
---|
111 | self.validators = {} |
---|
112 | for widget in self.layout.keys(): |
---|
113 | self.validators[widget] = self.layout[widget].validate |
---|
114 | self.required_keys = {} |
---|
115 | for mode in self.required_modes: |
---|
116 | self.required_keys[mode] = [self.layout.getIdUnprefixed(id) |
---|
117 | for id,widget in self.layout.objectItems() |
---|
118 | if widget.is_required] |
---|
119 | break |
---|
120 | self.init_errors = ','.join(errors) |
---|
121 | ###) |
---|
122 | |
---|
123 | def findStudent(self,mode,student_id=None, matric_no=None, jamb_reg_no=None): ###( |
---|
124 | student_record = None |
---|
125 | msg = '' |
---|
126 | key_used = '' |
---|
127 | while True: |
---|
128 | if student_id: |
---|
129 | key_used = 'student_id' |
---|
130 | res = self.students_catalog(id = student_id) |
---|
131 | if not res: |
---|
132 | msg = "no student with id %s" % student_id |
---|
133 | break |
---|
134 | student_record = res[0] |
---|
135 | if matric_no and student_record.matric_no: |
---|
136 | if matric_no != student_record.matric_no: |
---|
137 | msg = "old matric_no %s overwritten with %s" % (student_record.matric_no,matric_no) |
---|
138 | #logger.info("%s, old matric_no %s overwritten with %s" % (student_record.id,student_record.matric_no,matric_no)) |
---|
139 | if jamb_reg_no and student_record.jamb_reg_no: |
---|
140 | if jamb_reg_no != student_record.jamb_reg_no: |
---|
141 | msg = "old reg_no %s overwritten with %s" % (student_record.jamb_reg_no,jamb_reg_no) |
---|
142 | #logger.info("%s, old reg_no %s overwritten with %s" % (student_record.id,student_record.jamb_reg_no,jamb_reg_no)) |
---|
143 | elif jamb_reg_no: |
---|
144 | key_used = 'jamb_reg_no' |
---|
145 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
146 | if res: |
---|
147 | if mode == 'create': |
---|
148 | msg = "jamb_reg_no exists" |
---|
149 | break |
---|
150 | else: |
---|
151 | if mode == 'edit': |
---|
152 | msg = "no student with jamb_reg_no %s" % jamb_reg_no |
---|
153 | break |
---|
154 | student_record = res[0] |
---|
155 | elif matric_no: |
---|
156 | key_used = 'matric_no' |
---|
157 | res = self.students_catalog(matric_no = matric_no) |
---|
158 | if not res: |
---|
159 | msg = "no student with matric_no %s" % matric_no |
---|
160 | break |
---|
161 | student_record = res[0] |
---|
162 | else: |
---|
163 | msg = "neither id, matric_no nor reg_no specified" |
---|
164 | break |
---|
165 | d = {} |
---|
166 | d['student_record'] = student_record |
---|
167 | d['key_used'] = key_used |
---|
168 | d['msg'] = msg |
---|
169 | return d |
---|
170 | ###) |
---|
171 | |
---|
172 | def makeIdLists(self): ###( |
---|
173 | pending_digests = [] |
---|
174 | pending = [] |
---|
175 | # pending_student_ids = [] |
---|
176 | # pending_matric_nos = [] |
---|
177 | # pending_reg_ns = [] |
---|
178 | if os.path.exists(self.pending_path): |
---|
179 | datafile = open(self.pending_path,"r") |
---|
180 | reader = csv.reader(datafile) |
---|
181 | old_headline = reader.next() |
---|
182 | #datafile.seek(0) |
---|
183 | pending_csv_reader = csv.DictReader(datafile,old_headline,) |
---|
184 | #pending_csv_reader.next() # skip headline |
---|
185 | for item in pending_csv_reader: |
---|
186 | digest = makeDigest(item,self.data_keys) |
---|
187 | if digest not in pending_digests: |
---|
188 | pending_digests += digest, |
---|
189 | pending += item, |
---|
190 | datafile.close() |
---|
191 | #copy2(self.pending_path,self.pending_backup) |
---|
192 | return pending, pending_digests |
---|
193 | ###) |
---|
194 | |
---|
195 | def checkHeadline(self,headline): ###( |
---|
196 | """ check the headline of a csv file """ |
---|
197 | import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore') |
---|
198 | or k.strip() in self.info.keys())] |
---|
199 | # import_keys = [k.strip() for k in headline if not (k.strip() in self.info.keys())] |
---|
200 | diff2schema = set(import_keys).difference(set(self.schema.keys())) |
---|
201 | diff2layout = set(import_keys).difference(set(self.layout.keys())) |
---|
202 | #if diff2schema and diff2schema != set(['id',]): |
---|
203 | if diff2schema: |
---|
204 | return list(diff2schema) |
---|
205 | return [] |
---|
206 | ###) |
---|
207 | |
---|
208 | def getHeadlineFields(self,headline,values): ###( |
---|
209 | """ check the headline of a csv file """ |
---|
210 | # import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore') |
---|
211 | # or k.strip() in self.info.keys())] |
---|
212 | import_keys = [k.strip() for k in headline if not (k.strip() in self.info.keys())] |
---|
213 | info_keys = [k.strip() for k in headline if k.strip() in self.info.keys()] |
---|
214 | si = set(import_keys) |
---|
215 | ss = set(self.schema.keys()) |
---|
216 | |
---|
217 | invalid_keys = si - ss |
---|
218 | keys = [] |
---|
219 | i = 0 |
---|
220 | duplicates = False |
---|
221 | singels = [] |
---|
222 | msg = '' |
---|
223 | while True: |
---|
224 | if len(values) != len(import_keys): |
---|
225 | if len(values) == len(import_keys) + len(info_keys): |
---|
226 | msg += "fields from pending file in headline" |
---|
227 | else: |
---|
228 | msg += "%d fields in headline but %d values" % (len(import_keys),len(values)) |
---|
229 | break |
---|
230 | for k in import_keys: |
---|
231 | if k in singels: |
---|
232 | keys += (k,'%s' % k,values[i],'(duplicate)'), |
---|
233 | msg += ("duplicate %s," % k) |
---|
234 | keys[singels.index(k)] = (k,'%s' % k,values[singels.index(k)],'(duplicate)') |
---|
235 | elif k in invalid_keys and not k.startswith(IGNORE): |
---|
236 | keys += (k,NO_KEY,values[i],'(invalid)'), |
---|
237 | else: |
---|
238 | keys += (k,k,values[i],''), |
---|
239 | i += 1 |
---|
240 | singels += k, |
---|
241 | break |
---|
242 | return msg,keys |
---|
243 | ###) |
---|
244 | ###) |
---|
245 | |
---|
246 | class ApplicationImport(WAeUPImport):###( |
---|
247 | name = "application" |
---|
248 | plural_name = "%ss" % name |
---|
249 | commit_after = 1000 |
---|
250 | |
---|
251 | def create(self,mapping):###( |
---|
252 | reg_no = mapping.get('reg_no') |
---|
253 | msg = '' |
---|
254 | while True: |
---|
255 | try: |
---|
256 | self.applicants_catalog.addRecord(**mapping) |
---|
257 | except ValueError: |
---|
258 | msg = "applicant record with reg_no %s already exists" % reg_no |
---|
259 | break |
---|
260 | return reg_no,msg,mapping |
---|
261 | ###) |
---|
262 | |
---|
263 | def edit(self,mapping):###( |
---|
264 | reg_no = mapping.get('reg_no') |
---|
265 | status = mapping.get('status') |
---|
266 | msg = '' |
---|
267 | while True: |
---|
268 | res = self.applicants_catalog(reg_no = reg_no) |
---|
269 | if len(res): |
---|
270 | if res[0].status == 'created' and status != 'created': |
---|
271 | msg = "student object with id %s for %s already created, status cannot be changed" % (res[0].student_id, reg_no) |
---|
272 | elif status == 'created' and res[0].status != 'created': |
---|
273 | msg = "student object for %s has not yet been created, status cannot be set to 'created'" % (reg_no) |
---|
274 | else: |
---|
275 | self.applicants_catalog.modifyRecord(**mapping) |
---|
276 | else: |
---|
277 | msg = "applicant record with reg_no %s does not exist" % reg_no |
---|
278 | break |
---|
279 | return reg_no,msg,mapping |
---|
280 | ###) |
---|
281 | |
---|
282 | ###) |
---|
283 | |
---|
284 | class CertificateImport(WAeUPImport):###( |
---|
285 | name = "certificate" |
---|
286 | plural_name = "%ss" % name |
---|
287 | commit_after = 100000 |
---|
288 | |
---|
289 | def create(self,mapping):###( |
---|
290 | if getattr(self,'_v_certificate_list',None) is None: |
---|
291 | self._v_certificate_list = [] |
---|
292 | if getattr(self,'_v_department_certificates',None) is None: |
---|
293 | departments = self.portal_catalog(portal_type = "Department") |
---|
294 | self._v_department_certificates = {} |
---|
295 | for department in departments: |
---|
296 | certificates_container = getattr(department.getObject(),"certificates",None) |
---|
297 | self._v_department_certificates[department.getId] = {'container': certificates_container, |
---|
298 | 'certificates': certificates_container.objectIds(), |
---|
299 | } |
---|
300 | department_id = mapping['department_code'] |
---|
301 | msg = '' |
---|
302 | certificate_id = mapping.get('code') |
---|
303 | while True: |
---|
304 | department_certificates = self._v_department_certificates.get(department_id,None) |
---|
305 | if department_certificates is None: |
---|
306 | msg = "No Department with ID: %s" % department_id |
---|
307 | break |
---|
308 | certificates_container = department_certificates['container'] |
---|
309 | certificates = department_certificates['certificates'] |
---|
310 | if certificate_id in self._v_certificate_list: |
---|
311 | msg = "Duplicate Certificate ID: %s" % department_id |
---|
312 | break |
---|
313 | if certificate_id in certificates: |
---|
314 | msg = "Duplicate Certificate ID: %s" % department_id |
---|
315 | break |
---|
316 | try: |
---|
317 | certificates_container.invokeFactory('Certificate', certificate_id) |
---|
318 | except BadRequest,E: |
---|
319 | msg = "%s" % E |
---|
320 | break |
---|
321 | self._v_certificate_list.append(certificate_id) |
---|
322 | certificate = getattr(certificates_container,certificate_id) |
---|
323 | certificate.getContent().edit(mapping=mapping) |
---|
324 | break |
---|
325 | return certificate_id,msg,mapping |
---|
326 | ###) |
---|
327 | |
---|
328 | def edit(self,mapping):###( |
---|
329 | certificate_id = mapping.get('code') |
---|
330 | res = self.portal_catalog(id=certificate_id) |
---|
331 | msg = '' |
---|
332 | while True: |
---|
333 | if not res: |
---|
334 | msg = "no certificate with id: %s" % certificate_id |
---|
335 | break |
---|
336 | c = res[0].getObject() |
---|
337 | c.getContent().edit(mapping=mapping) |
---|
338 | break |
---|
339 | return certificate_id,msg,mapping |
---|
340 | ###) |
---|
341 | ###) |
---|
342 | |
---|
343 | class CourseImport(WAeUPImport):###( |
---|
344 | name = "course" |
---|
345 | plural_name = "%ss" % name |
---|
346 | commit_after = 1000 |
---|
347 | |
---|
348 | def create(self,mapping):###( |
---|
349 | if getattr(self,'_v_course_list',None) is None: |
---|
350 | self._v_course_list = [] |
---|
351 | if getattr(self,'_v_department_courses',None) is None: |
---|
352 | departments = self.portal_catalog(portal_type = "Department") |
---|
353 | self._v_department_courses = {} |
---|
354 | for department in departments: |
---|
355 | courses_container = getattr(department.getObject(),"courses",None) |
---|
356 | if courses_container is not None: |
---|
357 | self._v_department_courses[department.getId] = {'container': courses_container, |
---|
358 | 'courses': courses_container.objectIds(), |
---|
359 | } |
---|
360 | department_id = mapping['department_code'] |
---|
361 | course_id = mapping.get('code','') |
---|
362 | msg = '' |
---|
363 | while True: |
---|
364 | department_courses = self._v_department_courses.get(department_id,None) |
---|
365 | if department_courses is None: |
---|
366 | msg = "no department with id: %(department_id)s" % vars() |
---|
367 | break |
---|
368 | courses_container = department_courses['container'] |
---|
369 | courses = department_courses['courses'] |
---|
370 | if course_id in self._v_course_list: |
---|
371 | msg = "duplicate course id: %(course_id)s" % vars() |
---|
372 | break |
---|
373 | if course_id in courses: |
---|
374 | msg = "course %(course_id)s already exists in department %(department_id)s" % vars() |
---|
375 | break |
---|
376 | try: |
---|
377 | courses_container.invokeFactory('Course', course_id) |
---|
378 | except BadRequest,E: |
---|
379 | msg = "%s" % E |
---|
380 | break |
---|
381 | self._v_course_list.append(course_id) |
---|
382 | course = getattr(courses_container,course_id) |
---|
383 | course.getContent().edit(mapping=mapping) |
---|
384 | break |
---|
385 | return course_id,msg,mapping |
---|
386 | ###) |
---|
387 | |
---|
388 | def edit(self,mapping): ###( |
---|
389 | course_id = mapping.get('code','') |
---|
390 | course = self.courses_catalog.getRecordByKey(course_id) |
---|
391 | msg = '' |
---|
392 | while True: |
---|
393 | if course is None: |
---|
394 | msg = "no course with id: %s" % course_id |
---|
395 | break |
---|
396 | course_object = getattr(getattr(getattr(getattr(self.academics_folder,course.faculty), |
---|
397 | course.department), |
---|
398 | 'courses'), |
---|
399 | course_id) |
---|
400 | course_object.getContent().edit(mapping=mapping) |
---|
401 | break |
---|
402 | return course_id,msg,mapping |
---|
403 | ###) |
---|
404 | ###) |
---|
405 | |
---|
406 | class CourseResultImport(WAeUPImport):###( |
---|
407 | """ CourseresultImport """ |
---|
408 | name = "course_result" |
---|
409 | plural_name = "%ss" % name |
---|
410 | commit_after = 1000000 |
---|
411 | required_modes = ('create','edit','remove') |
---|
412 | |
---|
413 | def create(self,mapping):###( |
---|
414 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
415 | if getattr(self,'_v_courses',None) is None: |
---|
416 | res = self.courses_catalog() |
---|
417 | self._v_courses = {} |
---|
418 | for brain in res: |
---|
419 | self._v_courses[brain.code] = brain |
---|
420 | if getattr(self,'_v_level_created',None) is None: |
---|
421 | self._v_level_created = [] |
---|
422 | if getattr(self,'_v_student_study_course',None) is None: |
---|
423 | self._v_student_study_course = {} |
---|
424 | msg = '' |
---|
425 | key = '' |
---|
426 | matric_no = mapping.get('matric_no','') |
---|
427 | id = mapping.get('id','') |
---|
428 | while True: |
---|
429 | course_id = mapping.get('code') |
---|
430 | if course_id not in self._v_courses.keys(): |
---|
431 | msg = "no course with id: %s" % course_id |
---|
432 | break |
---|
433 | result = self.findStudent('create',student_id=id,matric_no=matric_no) |
---|
434 | msg = result['msg'] |
---|
435 | if msg: |
---|
436 | break |
---|
437 | student_record = result['student_record'] |
---|
438 | student_id = mapping['student_id'] = student_record.id |
---|
439 | level_id = mapping.get('level_id','') |
---|
440 | code = mapping.get('code','') |
---|
441 | level_ident = "%(student_id)s_%(level_id)s" % vars() |
---|
442 | if level_ident not in self._v_level_created: |
---|
443 | context = self._v_student_study_course.get(student_id,None) |
---|
444 | if context is None: |
---|
445 | try: |
---|
446 | context = getattr(getattr(students_folder, |
---|
447 | "%(student_id)s" % vars()), |
---|
448 | 'study_course') |
---|
449 | self._v_student_study_course[student_id] = context |
---|
450 | except: |
---|
451 | msg = "could not create level %(level_id)s for %(student_id)s" % vars() |
---|
452 | break |
---|
453 | if level_id not in context.objectIds(): |
---|
454 | context.invokeFactory('StudentStudyLevel',"%(level_id)s" % vars()) |
---|
455 | level = getattr(context,"%(level_id)s" % vars()) |
---|
456 | self.portal_workflow.doActionFor(level,'open') |
---|
457 | session_id = mapping.get('session_id','') |
---|
458 | if session_id: |
---|
459 | level.getContent().edit(mapping={'session': "%s" % session_id,}) |
---|
460 | self.portal_workflow.doActionFor(level,'close') |
---|
461 | self._v_level_created += level_ident, |
---|
462 | mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
463 | #overwrite semester and credits in create mode |
---|
464 | for k in ('semester','credits',): |
---|
465 | mapping[k] = getattr(self._v_courses[course_id],k) |
---|
466 | try: |
---|
467 | self.course_results.addRecord(**mapping) |
---|
468 | except ValueError: |
---|
469 | msg = "course result already exists: %s" % key |
---|
470 | break |
---|
471 | return key,msg,mapping |
---|
472 | ###) |
---|
473 | |
---|
474 | def edit(self,mapping): ###( |
---|
475 | msg = '' |
---|
476 | key = '' |
---|
477 | matric_no = mapping.get('matric_no','') |
---|
478 | id = mapping.get('id','') |
---|
479 | while True: |
---|
480 | result = self.findStudent('edit',student_id=id,matric_no=matric_no) |
---|
481 | msg = result['msg'] |
---|
482 | if msg: |
---|
483 | break |
---|
484 | student_record = result['student_record'] |
---|
485 | student_id = student_record.id |
---|
486 | level_id = mapping.get('level_id','') |
---|
487 | code = mapping.get('code','') |
---|
488 | #code = mapping['code'] |
---|
489 | mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
490 | if self.course_results.getRecordByKey(key) is None: |
---|
491 | msg = "no course result with key %(key)s" % vars() |
---|
492 | break |
---|
493 | self.course_results.modifyRecord(**mapping) |
---|
494 | break |
---|
495 | return key,msg,mapping |
---|
496 | ###) |
---|
497 | |
---|
498 | def remove(self,mapping):###( |
---|
499 | key = '' |
---|
500 | msg = '' |
---|
501 | matric_no = mapping.get('matric_no','') |
---|
502 | id = mapping.get('id','') |
---|
503 | while True: |
---|
504 | result = self.findStudent('edit',student_id=id,matric_no=matric_no) |
---|
505 | msg = result['msg'] |
---|
506 | if msg: |
---|
507 | break |
---|
508 | student_record = result['student_record'] |
---|
509 | student_id = student_record.id |
---|
510 | level_id = mapping.get('level_id','') |
---|
511 | code = mapping.get('code','') |
---|
512 | key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
513 | if self.course_results.getRecordByKey(key) is None: |
---|
514 | msg = "no course result with key %(key)s" % vars() |
---|
515 | break |
---|
516 | self.course_results.deleteRecord(key) |
---|
517 | break |
---|
518 | return key,msg,mapping |
---|
519 | ###) |
---|
520 | ###) |
---|
521 | |
---|
522 | class CertificateCourseImport(WAeUPImport):###( |
---|
523 | name = "certificate_course" |
---|
524 | plural_name = "%ss" % name |
---|
525 | commit_after = 100000 |
---|
526 | required_modes = ('create','edit') |
---|
527 | |
---|
528 | def create(self,mapping): |
---|
529 | if getattr(self,'_v_courses',None) is None: |
---|
530 | res = self.courses_catalog() |
---|
531 | self._v_courses= [course.code for course in res] |
---|
532 | if getattr(self,'_v_ceritficates',None) is None: |
---|
533 | res = self.portal_catalog(portal_type = "Certificate") |
---|
534 | self._v_certificates = {} |
---|
535 | for cert in res: |
---|
536 | self._v_certificates[cert.getId] = cert.getObject() |
---|
537 | msg = '' |
---|
538 | while True: |
---|
539 | certificate_course_id = mapping.get('code') |
---|
540 | if certificate_course_id not in self._v_courses: |
---|
541 | msg = "no course with id: %s" % certificate_course_id |
---|
542 | break |
---|
543 | cert_id = mapping['certificate_code'] |
---|
544 | cert = self._v_certificates.get(cert_id,None) |
---|
545 | if cert is None: |
---|
546 | msg = "no certificate with id: %s" % cert_id |
---|
547 | break |
---|
548 | level_id = mapping.get('level') |
---|
549 | level = getattr(cert,level_id,None) |
---|
550 | if level is None: |
---|
551 | cert.invokeFactory('StudyLevel', level_id) |
---|
552 | level = getattr(cert,level_id,None) |
---|
553 | elif hasattr(level,certificate_course_id): |
---|
554 | msg = "duplicate certificate course id: %(certificate_course_id)s " % vars() |
---|
555 | msg += "in %(cert_id)s/ %(level_id)s" % vars() |
---|
556 | break |
---|
557 | level.invokeFactory('CertificateCourse', certificate_course_id) |
---|
558 | c = getattr(level,certificate_course_id) |
---|
559 | c.getContent().edit(mapping=mapping) |
---|
560 | break |
---|
561 | return certificate_course_id,msg,mapping |
---|
562 | ###) |
---|
563 | |
---|
564 | class DepartmentImport(WAeUPImport):###( |
---|
565 | name = "department" |
---|
566 | plural_name = "%ss" % name |
---|
567 | commit_after = 1000 |
---|
568 | |
---|
569 | def create(self,mapping):###( |
---|
570 | "create a department in the correct faculty" |
---|
571 | faculty_id = mapping['faculty_code'] |
---|
572 | msg = '' |
---|
573 | if getattr(self,'_v_faculties',None) is None: |
---|
574 | res = self.portal_catalog(portal_type = "Faculty") |
---|
575 | self._v_faculties = {} |
---|
576 | for f in res: |
---|
577 | self._v_faculties[f.getId] = f.getObject() |
---|
578 | department_id = mapping.get('code','') |
---|
579 | while True: |
---|
580 | faculty = self._v_faculties.get(faculty_id,None) |
---|
581 | if faculty is None: |
---|
582 | msg = "no faculty with id: %s" % faculty_id |
---|
583 | break |
---|
584 | else: |
---|
585 | d = getattr(faculty,department_id,None) |
---|
586 | if d is None or d.portal_type == "Faculty": |
---|
587 | try: |
---|
588 | faculty.invokeFactory('Department', department_id) |
---|
589 | except BadRequest,E: |
---|
590 | msg = "%s" % E |
---|
591 | break |
---|
592 | d = getattr(faculty,department_id) |
---|
593 | d.invokeFactory('CoursesFolder','courses') |
---|
594 | courses = getattr(d,'courses') |
---|
595 | dict = {'Title': 'Courses'} |
---|
596 | courses.getContent().edit(mapping=dict) |
---|
597 | d.invokeFactory('CertificatesFolder','certificates') |
---|
598 | certificates = getattr(d,'certificates') |
---|
599 | dict = {'Title': 'Certificates'} |
---|
600 | certificates.getContent().edit(mapping=dict) |
---|
601 | d.getContent().edit(mapping=mapping) |
---|
602 | break |
---|
603 | return department_id,msg,mapping |
---|
604 | ###) |
---|
605 | |
---|
606 | def edit(self,mapping): ###( |
---|
607 | "edit a department in the correct faculty" |
---|
608 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
609 | faculty_id = mapping['faculty_code'] |
---|
610 | department_id = mapping.get('code','') |
---|
611 | msg = '' |
---|
612 | while True: |
---|
613 | try: |
---|
614 | d = getattr(getattr(academics_folder,faculty_id),department_id,None) |
---|
615 | except KeyError: |
---|
616 | msg = "department %s or faculty %s wrong" % (department_id,faculty_id) |
---|
617 | break |
---|
618 | if d is None or d.portal_type == "Faculty": |
---|
619 | msg = "department %s not found" % (department_id) |
---|
620 | break |
---|
621 | d.getContent().edit(mapping=mapping) |
---|
622 | break |
---|
623 | return department_id,msg,mapping |
---|
624 | ###) |
---|
625 | ###) |
---|
626 | |
---|
627 | class FacultyImport(WAeUPImport):###( |
---|
628 | name = "faculty" |
---|
629 | plural_name = "faculties" |
---|
630 | commit_after = 100 |
---|
631 | |
---|
632 | |
---|
633 | def create(self,mapping): ###( |
---|
634 | "create a faculty" |
---|
635 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
636 | faculty_id = mapping.get('code','') |
---|
637 | msg = '' |
---|
638 | while True: |
---|
639 | if faculty_id in academics_folder.objectIds(): |
---|
640 | msg = "faculty with id: %s exists" % faculty_id |
---|
641 | break |
---|
642 | try: |
---|
643 | academics_folder.invokeFactory('Faculty', faculty_id) |
---|
644 | except BadRequest,E: |
---|
645 | msg = "%s" % E |
---|
646 | break |
---|
647 | f = getattr(academics_folder,faculty_id,None) |
---|
648 | f.getContent().edit(mapping=mapping) |
---|
649 | break |
---|
650 | return faculty_id,msg,mapping |
---|
651 | ###) |
---|
652 | |
---|
653 | def edit(self,mapping): ###( |
---|
654 | "edit a faculty" |
---|
655 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
656 | faculty_id = mapping['code'] |
---|
657 | msg = '' |
---|
658 | while True: |
---|
659 | f = getattr(academics_folder,faculty_id,None) |
---|
660 | if f is None: |
---|
661 | msg = "faculty with id: %s does not exist" % faculty_id |
---|
662 | f.getContent().edit(mapping=mapping) |
---|
663 | break |
---|
664 | return faculty_id,msg,mapping |
---|
665 | ###) |
---|
666 | ###) |
---|
667 | |
---|
668 | class StudentImport(WAeUPImport):###( |
---|
669 | name = "student" |
---|
670 | plural_name = "%ss" % name |
---|
671 | commit_after = 100 |
---|
672 | |
---|
673 | field2types_student = { ###( |
---|
674 | 'StudentApplication': |
---|
675 | {'id': 'application', |
---|
676 | #'title': 'Application Data', |
---|
677 | 'wf_transition_return': 'close', |
---|
678 | 'wf_transition_admit': 'remain', |
---|
679 | 'wf_transition_graduate': 'close', |
---|
680 | 'fields': |
---|
681 | ('jamb_reg_no', |
---|
682 | 'entry_mode', |
---|
683 | 'entry_session', |
---|
684 | 'jamb_score', |
---|
685 | 'app_email', |
---|
686 | 'jamb_age', |
---|
687 | 'jamb_state', |
---|
688 | 'jamb_lga', |
---|
689 | 'jamb_sex', |
---|
690 | ) |
---|
691 | }, |
---|
692 | #'StudentPume': |
---|
693 | # {'id': 'pume', |
---|
694 | # 'title': 'Pume Data', |
---|
695 | # 'wf_transition_return': 'close', |
---|
696 | # 'wf_transition_admit': 'close', |
---|
697 | # 'fields': |
---|
698 | # ('pume_score', |
---|
699 | # ) |
---|
700 | # }, |
---|
701 | 'StudentClearance': |
---|
702 | {'id': 'clearance', |
---|
703 | #'title': 'Clearance/Eligibility Record', |
---|
704 | 'wf_transition_return': 'close', |
---|
705 | 'wf_transition_admit': 'remain', |
---|
706 | 'wf_transition_graduate': 'close', |
---|
707 | 'fields': |
---|
708 | ('matric_no', |
---|
709 | 'nationality', |
---|
710 | 'lga', |
---|
711 | 'birthday', |
---|
712 | ) |
---|
713 | }, |
---|
714 | 'StudentPersonal': |
---|
715 | {'id': 'personal', |
---|
716 | #'title': 'Personal Data', |
---|
717 | 'wf_transition_return': 'open', |
---|
718 | 'wf_transition_admit': 'remain', |
---|
719 | 'wf_transition_graduate': 'close', |
---|
720 | 'fields': |
---|
721 | ('firstname', |
---|
722 | 'middlename', |
---|
723 | 'lastname', |
---|
724 | 'sex', |
---|
725 | 'email', |
---|
726 | 'phone', |
---|
727 | 'perm_address', |
---|
728 | ) |
---|
729 | }, |
---|
730 | 'StudentStudyCourse': |
---|
731 | {'id': 'study_course', |
---|
732 | #'title': 'Study Course', |
---|
733 | 'wf_transition_return': 'open', |
---|
734 | 'wf_transition_admit': 'remain', |
---|
735 | 'wf_transition_graduate': 'close', |
---|
736 | 'fields': |
---|
737 | ('study_course', |
---|
738 | 'current_level', |
---|
739 | 'current_session', |
---|
740 | 'current_mode', |
---|
741 | 'current_verdict', |
---|
742 | 'previous_verdict', |
---|
743 | ) |
---|
744 | }, |
---|
745 | # 'StudentStudyLevel': |
---|
746 | # {'id': 'current_level', |
---|
747 | # 'title': '', |
---|
748 | # 'wf_transition_return': 'open', |
---|
749 | # 'wf_transition_admit': 'remain', |
---|
750 | # 'fields': |
---|
751 | # ('verdict', |
---|
752 | # 'session', |
---|
753 | # ) |
---|
754 | # }, |
---|
755 | 'PaymentsFolder': |
---|
756 | {'id': 'payments', |
---|
757 | #'title': 'Payments', |
---|
758 | 'wf_transition_return': 'open', |
---|
759 | 'wf_transition_admit': 'open', |
---|
760 | 'wf_transition_graduate': 'close', |
---|
761 | 'fields': |
---|
762 | () |
---|
763 | }, |
---|
764 | } |
---|
765 | ###) |
---|
766 | |
---|
767 | def create(self,mapping): ###( |
---|
768 | "create student records due import" |
---|
769 | logger = logging.getLogger('WAeUPImport.StudentImport.create') |
---|
770 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
771 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
772 | matric_no = mapping.get('matric_no',None) |
---|
773 | entry_mode = mapping.get('entry_mode',None) |
---|
774 | msg = '' |
---|
775 | student_id = mapping.get('id',None) |
---|
776 | while True: |
---|
777 | if student_id: |
---|
778 | msg = "student_id must not be specified in create mode" |
---|
779 | break |
---|
780 | if jamb_reg_no and not entry_mode == 'transfer': |
---|
781 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
782 | if res: |
---|
783 | msg = "jamb_reg_no already assigned to student %s" % res[0].id |
---|
784 | break |
---|
785 | if matric_no: |
---|
786 | res = self.students_catalog(matric_no = matric_no) |
---|
787 | if res: |
---|
788 | msg = "matric_no already assigned to student %s" % res[0].id |
---|
789 | break |
---|
790 | if not matric_no and not jamb_reg_no: |
---|
791 | msg = "jamb_reg_no or matric_no must be specified" |
---|
792 | break |
---|
793 | student_id = self.waeup_tool.generateStudentId('?') |
---|
794 | students_folder.invokeFactory('Student', student_id) |
---|
795 | student_obj = getattr(students_folder,student_id) |
---|
796 | f2t = self.field2types_student |
---|
797 | d = {} |
---|
798 | transition = mapping.get('reg_transition','admit') |
---|
799 | if transition not in ('admit','return'): |
---|
800 | msg = "no valid transition provided" |
---|
801 | break |
---|
802 | for pt in f2t.keys(): |
---|
803 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
804 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
805 | sub_doc = sub_obj.getContent() |
---|
806 | #d['Title'] = f2t[pt]['title'] |
---|
807 | for field in f2t[pt]['fields']: |
---|
808 | d[field] = mapping.get(field,'') |
---|
809 | |
---|
810 | if pt == "StudentApplication": |
---|
811 | #d['jamb_sex'] = 'M' |
---|
812 | #if mapping.get('sex'): |
---|
813 | # d['jamb_sex'] = 'F' |
---|
814 | d['jamb_firstname'] = mapping.get('firstname',None) |
---|
815 | d['jamb_middlename'] = mapping.get('middlename',None) |
---|
816 | d['jamb_lastname'] = mapping.get('lastname',None) |
---|
817 | |
---|
818 | # if pt == "StudyCourse": |
---|
819 | # for von,zu in (('entry_mode','current_mode'), |
---|
820 | # ('entry_session','current_session')): |
---|
821 | # if mapping.get(zu,None) is None and mapping.get(von,None) is not None: |
---|
822 | # d[zu] = mapping[von] |
---|
823 | sub_doc.edit(mapping = d) |
---|
824 | |
---|
825 | #import pdb;pdb.set_trace() |
---|
826 | new_state = f2t[pt]['wf_transition_%(transition)s' % vars()] |
---|
827 | if new_state != "remain": |
---|
828 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
829 | self.portal_workflow.doActionFor(student_obj,transition) |
---|
830 | student_obj.manage_setLocalRoles(student_id, ['Owner',]) |
---|
831 | mapping['id'] = student_id |
---|
832 | break |
---|
833 | return student_id,msg,mapping |
---|
834 | ###) |
---|
835 | |
---|
836 | def edit(self,mapping): ###( |
---|
837 | "edit student records due import" |
---|
838 | wftool = self.portal_workflow |
---|
839 | logger = logging.getLogger('WAeUPImport.StudentImport.edit') |
---|
840 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
841 | student_id = mapping.get('id',None) |
---|
842 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
843 | matric_no = mapping.get('matric_no',None) |
---|
844 | editable_keys = mapping.keys() |
---|
845 | msg = '' |
---|
846 | while True: |
---|
847 | if student_id: |
---|
848 | res = self.students_catalog(id = student_id) |
---|
849 | if not res: |
---|
850 | msg = "no student with id %s" % student_id |
---|
851 | break |
---|
852 | student_record = res[0] |
---|
853 | if matric_no and student_record.matric_no: |
---|
854 | if matric_no != student_record.matric_no and not matric_no == 'transferred': |
---|
855 | res = self.students_catalog(matric_no = matric_no) |
---|
856 | if res: |
---|
857 | msg = "matric_no already assigned to student %s" % res[0].id |
---|
858 | break |
---|
859 | msg = "old matric_no %s overwritten with %s" % (student_record.matric_no,matric_no) |
---|
860 | #logger.info("%s, old matric_no %s overwritten with %s" % (student_record.id,student_record.matric_no,matric_no)) |
---|
861 | if jamb_reg_no and student_record.jamb_reg_no: |
---|
862 | if jamb_reg_no != student_record.jamb_reg_no: |
---|
863 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
864 | if res: |
---|
865 | msg = "jamb_reg_no already assigned to student %s" % res[0].id |
---|
866 | break |
---|
867 | msg = "old jamb_reg_no %s overwritten with %s" % (student_record.jamb_reg_no,jamb_reg_no) |
---|
868 | #logger.info("%s, old reg_no %s overwritten with %s" % (student_record.id,student_record.jamb_reg_no,jamb_reg_no)) |
---|
869 | elif jamb_reg_no: |
---|
870 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
871 | if not res: |
---|
872 | msg = "no student with jamb_reg_no %s" % jamb_reg_no |
---|
873 | break |
---|
874 | student_record = res[0] |
---|
875 | editable_keys.remove('jamb_reg_no') |
---|
876 | elif matric_no: |
---|
877 | res = self.students_catalog(matric_no = matric_no) |
---|
878 | if not res: |
---|
879 | msg = "no student with matric_no %s" % matric_no |
---|
880 | break |
---|
881 | student_record = res[0] |
---|
882 | editable_keys.remove('matric_no') |
---|
883 | student_id = student_record.id |
---|
884 | student_obj = getattr(students_folder,student_id) |
---|
885 | f2t = self.field2types_student |
---|
886 | d = {} |
---|
887 | any_change = False |
---|
888 | #special treatment for StudentStudyLevel |
---|
889 | d['verdict'] = mapping.get('current_verdict','') |
---|
890 | d['session'] = mapping.get('current_session','') |
---|
891 | current_level = mapping.get('current_level','') |
---|
892 | transition = mapping.get('reg_transition',None) |
---|
893 | if transition and transition not in ('admit','return','graduate'): |
---|
894 | msg = "no valid transition provided" |
---|
895 | break |
---|
896 | while d['session'] and d['verdict'] and current_level: |
---|
897 | sub_obj = getattr(student_obj,'study_course',None) |
---|
898 | if sub_obj is None: |
---|
899 | break |
---|
900 | level_obj = getattr(sub_obj,current_level,None) |
---|
901 | if level_obj is None: |
---|
902 | break |
---|
903 | any_change = True |
---|
904 | level_obj.getContent().edit(mapping = d) |
---|
905 | try: |
---|
906 | wftool.doActionFor(level_obj,'close') |
---|
907 | except: |
---|
908 | pass |
---|
909 | break |
---|
910 | |
---|
911 | for pt in f2t.keys(): |
---|
912 | #if pt == "StudentApplication": |
---|
913 | # d['jamb_sex'] = 'M' |
---|
914 | # if mapping.get('sex'): |
---|
915 | # d['jamb_sex'] = 'F' |
---|
916 | intersect = set(f2t[pt]['fields']).intersection(set(editable_keys)) |
---|
917 | #import pdb;pdb.set_trace() |
---|
918 | object_id = f2t[pt]['id'] |
---|
919 | sub_obj = getattr(student_obj,object_id,None) |
---|
920 | if intersect and pt not in ('StudentStudyLevel',): |
---|
921 | if sub_obj is None: |
---|
922 | try: |
---|
923 | student_obj.invokeFactory(pt,object_id) |
---|
924 | except: |
---|
925 | continue |
---|
926 | sub_obj = getattr(student_obj,object_id) |
---|
927 | #if f2t[pt]['title'] != '': |
---|
928 | # d['Title'] = f2t[pt]['title'] |
---|
929 | sub_doc = sub_obj.getContent() |
---|
930 | for field in intersect: |
---|
931 | changed = False |
---|
932 | if getattr(sub_doc,field,None) != mapping.get(field,''): |
---|
933 | any_change = True |
---|
934 | changed = True |
---|
935 | d[field] = mapping.get(field,'') |
---|
936 | if changed: |
---|
937 | sub_doc.edit(mapping = d) |
---|
938 | if transition: |
---|
939 | new_state = f2t[pt]['wf_transition_%(transition)s' % vars()] |
---|
940 | if new_state != "remain": |
---|
941 | try: |
---|
942 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
943 | except: |
---|
944 | pass |
---|
945 | if transition: |
---|
946 | try: |
---|
947 | self.portal_workflow.doActionFor(student_obj,transition) |
---|
948 | except: |
---|
949 | msg = "reg_transition not allowed" |
---|
950 | break |
---|
951 | return student_id,msg,mapping |
---|
952 | ###) |
---|
953 | ###) |
---|
954 | |
---|
955 | class StudentStudyLevelImport(WAeUPImport):###( |
---|
956 | """ StudentStudyLevelImport """ |
---|
957 | name = "student_study_level" |
---|
958 | plural_name = "%ss" % name |
---|
959 | commit_after = 1000000 |
---|
960 | required_modes = ('create',) |
---|
961 | |
---|
962 | def create(self,mapping): ###( |
---|
963 | "edit student levels and create StudentStudyLevel object if not existent" |
---|
964 | wftool = self.portal_workflow |
---|
965 | logger = logging.getLogger('WAeUPTool.mass_create_level') |
---|
966 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
967 | student_id = mapping.get('id',None) |
---|
968 | matric_no = mapping.get('matric_no',None) |
---|
969 | editable_keys = mapping.keys() |
---|
970 | key = '' |
---|
971 | msg = '' |
---|
972 | while True: |
---|
973 | result = self.findStudent('create',student_id=student_id,matric_no=matric_no) |
---|
974 | msg = result['msg'] |
---|
975 | if msg: |
---|
976 | break |
---|
977 | student_record = result['student_record'] |
---|
978 | student_id = student_record.id |
---|
979 | mapping['id'] = student_id |
---|
980 | session = mapping.get('session','') |
---|
981 | level = key = mapping.get('code','') |
---|
982 | if not level.isdigit(): |
---|
983 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
984 | break |
---|
985 | study_course_obj = getattr(getattr(students_folder,student_id),'study_course',None) |
---|
986 | if study_course_obj is None: |
---|
987 | msg = 'student %s: no study_course object' % student_id |
---|
988 | break |
---|
989 | level_obj = getattr(study_course_obj,level,None) |
---|
990 | if level_obj is None: |
---|
991 | # The only difference to the edit method is that we create a StudentStudyLevel object |
---|
992 | try: |
---|
993 | study_course_obj.invokeFactory('StudentStudyLevel',"%s" % level) |
---|
994 | level_obj = getattr(context,"%s" % level) |
---|
995 | except: |
---|
996 | continue |
---|
997 | level_obj.getContent().edit(mapping = mapping) |
---|
998 | level_review_state = wftool.getInfoFor(level_obj,'review_state',None) |
---|
999 | if level_review_state != "closed": |
---|
1000 | wftool.doActionFor(level_obj,'close') |
---|
1001 | break |
---|
1002 | return key,msg,mapping |
---|
1003 | ###) |
---|
1004 | |
---|
1005 | def edit(self,mapping): ###( |
---|
1006 | "edit student levels" |
---|
1007 | wftool = self.portal_workflow |
---|
1008 | logger = logging.getLogger('WAeUPTool.mass_edit_level') |
---|
1009 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1010 | student_id = mapping.get('id',None) |
---|
1011 | matric_no = mapping.get('matric_no',None) |
---|
1012 | key = '' |
---|
1013 | msg = '' |
---|
1014 | while True: |
---|
1015 | result = self.findStudent('create',student_id=student_id,matric_no=matric_no) |
---|
1016 | msg = result['msg'] |
---|
1017 | if msg: |
---|
1018 | break |
---|
1019 | student_record = result['student_record'] |
---|
1020 | student_id = student_record.id |
---|
1021 | mapping['id'] = student_id |
---|
1022 | session = mapping.get('session','') |
---|
1023 | level = key = mapping.get('code','') |
---|
1024 | #import pdb;pdb.set_trace() |
---|
1025 | if not level.isdigit(): |
---|
1026 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
1027 | break |
---|
1028 | study_course_obj = getattr(getattr(students_folder,student_id),'study_course',None) |
---|
1029 | if study_course_obj is None: |
---|
1030 | msg = 'student %s: no study_course object' % student_id |
---|
1031 | break |
---|
1032 | level_obj = getattr(study_course_obj,level,None) |
---|
1033 | if level_obj is None: |
---|
1034 | msg = 'student %s: no study_level object for level %s' % (student_id,level) |
---|
1035 | break |
---|
1036 | level_obj.getContent().edit(mapping = mapping) |
---|
1037 | break |
---|
1038 | return key,msg,mapping |
---|
1039 | ###) |
---|
1040 | ###) |
---|
1041 | |
---|
1042 | class VerdictImport(WAeUPImport):###( |
---|
1043 | """ VerdictImport """ |
---|
1044 | name = "verdict" |
---|
1045 | plural_name = "%ss" % name |
---|
1046 | commit_after = 100000 |
---|
1047 | required_modes = ('create','edit') |
---|
1048 | |
---|
1049 | def create(self,mapping): ###( |
---|
1050 | "edit student verdicts and create StudentStudyLevel object if not existent" |
---|
1051 | wftool = self.portal_workflow |
---|
1052 | logger = logging.getLogger('WAeUPTool.mass_edit_verdict') |
---|
1053 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1054 | student_id = mapping.get('id',None) |
---|
1055 | matric_no = mapping.get('matric_no',None) |
---|
1056 | editable_keys = mapping.keys() |
---|
1057 | key = '' |
---|
1058 | while True: |
---|
1059 | result = self.findStudent('create',student_id=student_id,matric_no=matric_no) |
---|
1060 | #student_record,msg = self.getStudentRecord(mapping) |
---|
1061 | msg = result['msg'] |
---|
1062 | if msg: |
---|
1063 | break |
---|
1064 | student_record = result['student_record'] |
---|
1065 | student_id = student_record.id |
---|
1066 | mapping['id'] = student_id |
---|
1067 | d = {} |
---|
1068 | #import pdb;pdb.set_trace() |
---|
1069 | any_change = False |
---|
1070 | #special treatment for StudentStudyLevel |
---|
1071 | current_session = d['session'] = mapping.get('current_session','') |
---|
1072 | if current_session and student_record.session != current_session: |
---|
1073 | msg = 'student %s: imported session %s does not match current_session %s' % (student_id, |
---|
1074 | current_session, |
---|
1075 | student_record.session) |
---|
1076 | break |
---|
1077 | current_level = mapping.get('current_level','') |
---|
1078 | if not current_level.isdigit(): |
---|
1079 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
1080 | break |
---|
1081 | if current_level and student_record.level != current_level: |
---|
1082 | msg = 'student %s: imported level %s does not match current_level %s' % (student_id, |
---|
1083 | current_level, |
---|
1084 | student_record.level) |
---|
1085 | break |
---|
1086 | student_review_state = student_record.review_state |
---|
1087 | if student_review_state == 'deactivated': |
---|
1088 | msg = "student %s in review_state %s" % (student_id, student_review_state) |
---|
1089 | break |
---|
1090 | if student_review_state not in ('courses_validated','returning'): |
---|
1091 | msg = "student %s in wrong review_state %s" % (student_id, student_review_state) |
---|
1092 | break |
---|
1093 | student_obj = getattr(students_folder,student_id) |
---|
1094 | # f2t = self.field2types_student |
---|
1095 | study_course_obj = getattr(student_obj,'study_course',None) |
---|
1096 | if study_course_obj is None: |
---|
1097 | msg = 'student %s: no study_course object' % student_id |
---|
1098 | break |
---|
1099 | level_obj = getattr(study_course_obj,current_level,None) |
---|
1100 | |
---|
1101 | if level_obj is None: |
---|
1102 | # The only difference to the edit method is that we create a StudentStudyLevel object |
---|
1103 | try: |
---|
1104 | study_course_obj.invokeFactory('StudentStudyLevel',"%s" % current_level) |
---|
1105 | level_obj = getattr(context,"%s" % current_level) |
---|
1106 | level_obj.portal_workflow.doActionFor(level,'open') |
---|
1107 | except: |
---|
1108 | continue |
---|
1109 | #msg = 'student %s: no study_level object for level %s' % (student_id, |
---|
1110 | # current_level) |
---|
1111 | #break |
---|
1112 | |
---|
1113 | verdict = d['verdict'] = d['current_verdict'] = mapping.get('current_verdict','') |
---|
1114 | |
---|
1115 | #if verdict == student_record.verdict: |
---|
1116 | # msg = 'student %s: verdict already set to %s' % (student_id, |
---|
1117 | # verdict) |
---|
1118 | |
---|
1119 | level_review_state = wftool.getInfoFor(level_obj,'review_state',None) |
---|
1120 | if level_review_state != "closed": |
---|
1121 | wftool.doActionFor(level_obj,'close') |
---|
1122 | # msg = 'student %s: level %s is not closed' % (student_id, |
---|
1123 | # current_level) |
---|
1124 | |
---|
1125 | study_course_obj.getContent().edit(mapping = d) |
---|
1126 | level_obj.getContent().edit(mapping = d) |
---|
1127 | if student_review_state != "returning": |
---|
1128 | wftool.doActionFor(student_obj,'return') |
---|
1129 | # try: |
---|
1130 | # wftool.doActionFor(level_obj,'close') |
---|
1131 | # except: |
---|
1132 | # pass |
---|
1133 | break |
---|
1134 | return student_id,msg,mapping |
---|
1135 | ###) |
---|
1136 | |
---|
1137 | |
---|
1138 | def edit(self,mapping): ###( |
---|
1139 | "edit student verdicts" |
---|
1140 | wftool = self.portal_workflow |
---|
1141 | logger = logging.getLogger('WAeUPTool.mass_edit_verdict') |
---|
1142 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1143 | student_id = mapping.get('id',None) |
---|
1144 | matric_no = mapping.get('matric_no',None) |
---|
1145 | editable_keys = mapping.keys() |
---|
1146 | key = '' |
---|
1147 | while True: |
---|
1148 | result = self.findStudent('edit',student_id=student_id,matric_no=matric_no) |
---|
1149 | #student_record,msg = self.getStudentRecord(mapping) |
---|
1150 | msg = result['msg'] |
---|
1151 | if msg: |
---|
1152 | break |
---|
1153 | student_record = result['student_record'] |
---|
1154 | key_used = result['key_used'] |
---|
1155 | if key_used in editable_keys: |
---|
1156 | editable_keys.remove(key_used) |
---|
1157 | student_id = student_record.id |
---|
1158 | mapping['id'] = student_id |
---|
1159 | d = {} |
---|
1160 | #import pdb;pdb.set_trace() |
---|
1161 | any_change = False |
---|
1162 | #special treatment for StudentStudyLevel |
---|
1163 | current_session = d['session'] = mapping.get('current_session','') |
---|
1164 | if current_session and student_record.session != current_session: |
---|
1165 | msg = 'student %s: imported session %s does not match current_session %s' % (student_id, |
---|
1166 | current_session, |
---|
1167 | student_record.session) |
---|
1168 | break |
---|
1169 | current_level = mapping.get('current_level','') |
---|
1170 | if not current_level.isdigit(): |
---|
1171 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
1172 | break |
---|
1173 | if current_level and student_record.level != current_level: |
---|
1174 | msg = 'student %s: imported level %s does not match current_level %s' % (student_id, |
---|
1175 | current_level, |
---|
1176 | student_record.level) |
---|
1177 | break |
---|
1178 | student_review_state = student_record.review_state |
---|
1179 | if student_review_state == 'deactivated': |
---|
1180 | msg = "student %s in review_state %s" % (student_id, student_review_state) |
---|
1181 | break |
---|
1182 | if student_review_state not in ('courses_validated','returning'): |
---|
1183 | msg = "student %s in wrong review_state %s" % (student_id, student_review_state) |
---|
1184 | break |
---|
1185 | student_obj = getattr(students_folder,student_id) |
---|
1186 | # f2t = self.field2types_student |
---|
1187 | study_course_obj = getattr(student_obj,'study_course',None) |
---|
1188 | if study_course_obj is None: |
---|
1189 | msg = 'student %s: no study_course object' % student_id |
---|
1190 | break |
---|
1191 | level_obj = getattr(study_course_obj,current_level,None) |
---|
1192 | if level_obj is None: |
---|
1193 | msg = 'student %s: no study_level object for level %s' % (student_id, |
---|
1194 | current_level) |
---|
1195 | break |
---|
1196 | verdict = d['verdict'] = d['current_verdict'] = mapping.get('current_verdict','') |
---|
1197 | |
---|
1198 | #if verdict == student_record.verdict: |
---|
1199 | # msg = 'student %s: verdict already set to %s' % (student_id, |
---|
1200 | # verdict) |
---|
1201 | |
---|
1202 | level_review_state = wftool.getInfoFor(level_obj,'review_state',None) |
---|
1203 | if level_review_state != "closed": |
---|
1204 | wftool.doActionFor(level_obj,'close') |
---|
1205 | # msg = 'student %s: level %s is not closed' % (student_id, |
---|
1206 | # current_level) |
---|
1207 | |
---|
1208 | study_course_obj.getContent().edit(mapping = d) |
---|
1209 | level_obj.getContent().edit(mapping = d) |
---|
1210 | if student_review_state != "returning": |
---|
1211 | wftool.doActionFor(student_obj,'return') |
---|
1212 | # try: |
---|
1213 | # wftool.doActionFor(level_obj,'close') |
---|
1214 | # except: |
---|
1215 | # pass |
---|
1216 | break |
---|
1217 | return student_id,msg,mapping |
---|
1218 | ###) |
---|
1219 | |
---|
1220 | def edit_old(self,mapping): ###( |
---|
1221 | "edit student verdicts" |
---|
1222 | wftool = self.portal_workflow |
---|
1223 | logger = logging.getLogger('WAeUPTool.mass_edit_verdict') |
---|
1224 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
1225 | student_id = mapping.get('id',None) |
---|
1226 | matric_no = mapping.get('matric_no',None) |
---|
1227 | editable_keys = mapping.keys() |
---|
1228 | key = '' |
---|
1229 | while True: |
---|
1230 | msg = '' |
---|
1231 | if student_id: |
---|
1232 | student_record = self.students_catalog.getRecordByKey(student_id) |
---|
1233 | if student_record is None: |
---|
1234 | #return '',"no student with id %s" % student_id |
---|
1235 | msg = "no student with id %s" % student_id |
---|
1236 | break |
---|
1237 | if matric_no and student_record.matric_no and matric_no != student_record.matric_no: |
---|
1238 | msg = 'student %s: matric_no %s does not match %s' % (student_record.id, |
---|
1239 | student_record.matric_no, |
---|
1240 | matric_no) |
---|
1241 | break |
---|
1242 | mapping['matric_no'] = student_record.matric_no |
---|
1243 | elif matric_no: |
---|
1244 | res = self.students_catalog(matric_no = matric_no) |
---|
1245 | if not res: |
---|
1246 | msg = "no student with matric_no %s" % matric_no |
---|
1247 | break |
---|
1248 | student_record = res[0] |
---|
1249 | editable_keys.remove('matric_no') |
---|
1250 | else: |
---|
1251 | msg = "no id or matric_no specified" |
---|
1252 | break |
---|
1253 | student_id = student_record.id |
---|
1254 | mapping['id'] = student_id |
---|
1255 | d = {} |
---|
1256 | #import pdb;pdb.set_trace() |
---|
1257 | any_change = False |
---|
1258 | #special treatment for StudentStudyLevel |
---|
1259 | current_session = d['session'] = mapping.get('current_session','') |
---|
1260 | if current_session and student_record.session != current_session: |
---|
1261 | msg = 'student %s: imported session %s does not match current_session %s' % (student_id, |
---|
1262 | current_session, |
---|
1263 | student_record.session) |
---|
1264 | break |
---|
1265 | current_level = mapping.get('current_level','') |
---|
1266 | if not current_level.isdigit(): |
---|
1267 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
1268 | break |
---|
1269 | if current_level and student_record.level != current_level: |
---|
1270 | msg = 'student %s: imported level %s does not match current_level %s' % (student_id, |
---|
1271 | current_level, |
---|
1272 | student_record.level) |
---|
1273 | break |
---|
1274 | student_review_state = student_record.review_state |
---|
1275 | if student_review_state == 'deactivated': |
---|
1276 | msg = "student %s in review_state %s" % (student_id, student_review_state) |
---|
1277 | break |
---|
1278 | if student_review_state not in ('courses_validated','returning'): |
---|
1279 | msg = "student %s in wrong review_state %s" % (student_id, student_review_state) |
---|
1280 | break |
---|
1281 | student_obj = getattr(students_folder,student_id) |
---|
1282 | # f2t = self.field2types_student |
---|
1283 | study_course_obj = getattr(student_obj,'study_course',None) |
---|
1284 | if study_course_obj is None: |
---|
1285 | msg = 'student %s: no study_course object' % student_id |
---|
1286 | break |
---|
1287 | level_obj = getattr(study_course_obj,current_level,None) |
---|
1288 | if level_obj is None: |
---|
1289 | msg = 'student %s: no study_level object for level %s' % (student_id, |
---|
1290 | current_level) |
---|
1291 | break |
---|
1292 | verdict = d['verdict'] = d['current_verdict'] = mapping.get('current_verdict','') |
---|
1293 | |
---|
1294 | #if verdict == student_record.verdict: |
---|
1295 | # msg = 'student %s: verdict already set to %s' % (student_id, |
---|
1296 | # verdict) |
---|
1297 | |
---|
1298 | level_review_state = wftool.getInfoFor(level_obj,'review_state',None) |
---|
1299 | if level_review_state != "closed": |
---|
1300 | wftool.doActionFor(level_obj,'close') |
---|
1301 | # msg = 'student %s: level %s is not closed' % (student_id, |
---|
1302 | # current_level) |
---|
1303 | |
---|
1304 | study_course_obj.getContent().edit(mapping = d) |
---|
1305 | level_obj.getContent().edit(mapping = d) |
---|
1306 | if student_review_state != "returning": |
---|
1307 | wftool.doActionFor(student_obj,'return') |
---|
1308 | # try: |
---|
1309 | # wftool.doActionFor(level_obj,'close') |
---|
1310 | # except: |
---|
1311 | # pass |
---|
1312 | break |
---|
1313 | return student_id,msg,mapping |
---|
1314 | ###) |
---|
1315 | ###) |
---|