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 3179 2008-02-18 18:09:15Z joachim $ |
---|
20 | """The WAeUP Tool Box. |
---|
21 | """ |
---|
22 | |
---|
23 | #from AccessControl import ClassSecurityInfo |
---|
24 | #from Acquisition import aq_inner |
---|
25 | #from Acquisition import aq_parent |
---|
26 | #from Globals import DTMLFile |
---|
27 | #from Globals import InitializeClass |
---|
28 | #from OFS.SimpleItem import SimpleItem |
---|
29 | 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 | class WAeUPImport: ###( |
---|
54 | """ WAeUPImport """ |
---|
55 | required_modes = ('create',) |
---|
56 | |
---|
57 | def __init__(self,mode,waeup_tool): |
---|
58 | self.member = member = waeup_tool.portal_membership.getAuthenticatedMember() |
---|
59 | self.import_date = DateTime.DateTime().strftime("%d/%m/%y %H:%M:%S") |
---|
60 | self.current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
61 | self.waeup_tool = waeup_tool |
---|
62 | self.schema_tool = getToolByName(waeup_tool, 'portal_schemas') |
---|
63 | self.layout_tool = getToolByName(waeup_tool, 'portal_layouts') |
---|
64 | self.portal_workflow = getToolByName(waeup_tool, 'portal_workflow') |
---|
65 | self.portal_url = getToolByName(waeup_tool, 'portal_url') |
---|
66 | self.portal_catalog = waeup_tool.portal_catalog |
---|
67 | self.students_catalog = waeup_tool.students_catalog |
---|
68 | self.courses_catalog = waeup_tool.courses_catalog |
---|
69 | self.course_results = waeup_tool.course_results |
---|
70 | self.mode = mode |
---|
71 | self.import_method = getattr(self, '%s' % mode,None) |
---|
72 | errors = [] |
---|
73 | if self.import_method is None: |
---|
74 | errors.append('No importer method %s' % mode) |
---|
75 | self.pending_path = "%s/import/%s.pending" % (i_home,self.plural_name) |
---|
76 | self.pending_tmp = "%s/import/%s.pending.tmp" % (i_home,self.plural_name) |
---|
77 | self.pending_backup = "%s/import/%s.pending.old" % (i_home,self.plural_name) |
---|
78 | self.pending_fn = os.path.split(self.pending_path)[1] |
---|
79 | self.imported_path = "%s/import/%s.imported" % (i_home,self.plural_name) |
---|
80 | self.imported_fn = os.path.split(self.imported_path)[1] |
---|
81 | iname = "import_%s" % self.name |
---|
82 | self.logger = logging.getLogger('WAeUPTool.import%s' % self.plural_name.capitalize()) |
---|
83 | self.schema = self.schema_tool._getOb(iname,None) |
---|
84 | self.pending_schema = self.schema_tool._getOb("%s_pending" % iname,None) |
---|
85 | self.layout = self.layout_tool._getOb(iname,None) |
---|
86 | if self.schema is None: |
---|
87 | errors.append('No schema %s' % iname) |
---|
88 | if self.pending_schema is None: |
---|
89 | self.pending_schema = self.schema |
---|
90 | if self.layout is None: |
---|
91 | errors.append('No such layout %s' % iname) |
---|
92 | self.data_keys = self.pending_schema.keys() |
---|
93 | self.csv_keys = self.pending_schema.keys() |
---|
94 | info = {} |
---|
95 | info['imported_from'] = '' |
---|
96 | info['imported_by'] = str(member) |
---|
97 | info['import_date'] = self.import_date |
---|
98 | info['import_mode'] = mode |
---|
99 | info['error'] = '' |
---|
100 | info['digest'] = '' |
---|
101 | self.info = info |
---|
102 | self.csv_keys.extend(self.info) |
---|
103 | self.validators = {} |
---|
104 | for widget in self.layout.keys(): |
---|
105 | self.validators[widget] = self.layout[widget].validate |
---|
106 | self.init_errors = ','.join(errors) |
---|
107 | |
---|
108 | def makeIdLists(self): |
---|
109 | pending_digests = [] |
---|
110 | pending = [] |
---|
111 | # pending_student_ids = [] |
---|
112 | # pending_matric_nos = [] |
---|
113 | # pending_reg_ns = [] |
---|
114 | if os.path.exists(self.pending_path): |
---|
115 | datafile = open(self.pending_path,"r") |
---|
116 | pending_csv_reader = csv.DictReader(datafile,self.csv_keys,) |
---|
117 | pending_csv_reader.next() # skip headline |
---|
118 | for item in pending_csv_reader: |
---|
119 | digest = makeDigest(item,self.data_keys) |
---|
120 | if digest not in pending_digests: |
---|
121 | pending_digests += digest, |
---|
122 | pending += item, |
---|
123 | datafile.close() |
---|
124 | copy2(self.pending_path,self.pending_backup) |
---|
125 | return pending, pending_digests |
---|
126 | ###) |
---|
127 | |
---|
128 | class ApplicationImport(WAeUPImport):###( |
---|
129 | name = "applications" |
---|
130 | plural_name = "%ss" % name |
---|
131 | commit_after = 1000 |
---|
132 | |
---|
133 | def create(self,mapping):###( |
---|
134 | reg_no = mapping.get('reg_no') |
---|
135 | msg = '' |
---|
136 | while True: |
---|
137 | try: |
---|
138 | self.applicants_catalog.addRecord(**mapping) |
---|
139 | except ValueError: |
---|
140 | msg = "applicant record with reg_no %s already exists" % reg_no |
---|
141 | break |
---|
142 | return reg_no,msg,mapping |
---|
143 | ###) |
---|
144 | |
---|
145 | def edit(self,mapping):###( |
---|
146 | reg_no = mapping.get('reg_no') |
---|
147 | status = mapping.get('status') |
---|
148 | msg = '' |
---|
149 | while True: |
---|
150 | res = self.applicants_catalog(reg_no = reg_no) |
---|
151 | if len(res): |
---|
152 | if res[0].status == 'created' and status != 'created': |
---|
153 | msg = "student object with id %s for %s already created, status cannot be changed" % (res[0].student_id, reg_no) |
---|
154 | elif status == 'created' and res[0].status != 'created': |
---|
155 | msg = "student object for %s has not yet been created, status cannot be set to 'created'" % (reg_no) |
---|
156 | else: |
---|
157 | self.applicants_catalog.modifyRecord(**mapping) |
---|
158 | else: |
---|
159 | msg = "applicant record with reg_no %s does not exist" % reg_no |
---|
160 | break |
---|
161 | return reg_no,msg,mapping |
---|
162 | ###) |
---|
163 | |
---|
164 | ###) |
---|
165 | |
---|
166 | class CertificateImport(WAeUPImport):###( |
---|
167 | name = "certificate" |
---|
168 | plural_name = "%ss" % name |
---|
169 | commit_after = 100000 |
---|
170 | |
---|
171 | def create(self,mapping):###( |
---|
172 | if getattr(self,'_v_certificate_list',None) is None: |
---|
173 | self._v_certificate_list = [] |
---|
174 | if getattr(self,'_v_department_certificates',None) is None: |
---|
175 | res = self.portal_catalog(portal_type = "Department") |
---|
176 | self._v_department_certificates = {} |
---|
177 | for d in res: |
---|
178 | self._v_department_certificates[d.getId] = getattr(d.getObject(),"certificates",None) |
---|
179 | did = mapping['department_code'] |
---|
180 | msg = '' |
---|
181 | while True: |
---|
182 | d = self._v_department_certificates.get(did,None) |
---|
183 | if d is None: |
---|
184 | msg = "No Department with ID: %s" % did |
---|
185 | break |
---|
186 | certificate_id = mapping.get('code') |
---|
187 | if certificate_id in self._v_certificate_list: |
---|
188 | msg = "Duplicate Certificate ID: %s" % did |
---|
189 | break |
---|
190 | c = getattr(d,certificate_id,None) |
---|
191 | if c is not None: |
---|
192 | msg = "Duplicate Certificate ID: %s" % did |
---|
193 | break |
---|
194 | try: |
---|
195 | d.invokeFactory('Certificate', certificate_id) |
---|
196 | except BadRequest,E: |
---|
197 | msg = "%s" % E |
---|
198 | break |
---|
199 | self._v_certificate_list.append(certificate_id) |
---|
200 | c = getattr(d,certificate_id) |
---|
201 | c.getContent().edit(mapping=mapping) |
---|
202 | break |
---|
203 | return certificate_id,msg,mapping |
---|
204 | ###) |
---|
205 | |
---|
206 | def edit(self,mapping):###( |
---|
207 | certificate_id = mapping.get('code') |
---|
208 | res = self.portal_catalog(id=certificate_id) |
---|
209 | msg = '' |
---|
210 | while True: |
---|
211 | if not res: |
---|
212 | msg = "No Certificate with ID: %s" % certificate_id |
---|
213 | break |
---|
214 | c = res[0].getObject() |
---|
215 | c.getContent().edit(mapping=mapping) |
---|
216 | break |
---|
217 | return certificate_id,msg,mapping |
---|
218 | ###) |
---|
219 | ###) |
---|
220 | |
---|
221 | class CourseImport(WAeUPImport):###( |
---|
222 | name = "course" |
---|
223 | plural_name = "%ss" % name |
---|
224 | commit_after = 1000 |
---|
225 | |
---|
226 | def create(self,mapping):###( |
---|
227 | if getattr(self,'_v_course_list',None) is None: |
---|
228 | self._v_course_list = [] |
---|
229 | if getattr(self,'_v_department_courses',None) is None: |
---|
230 | departments = self.portal_catalog(portal_type = "Department") |
---|
231 | self._v_department_courses = {} |
---|
232 | for department in departments: |
---|
233 | courses_folder = getattr(department.getObject(),"courses",None) |
---|
234 | if courses_folder is not None: |
---|
235 | self._v_department_courses[department.getId] = courses_folder.objectIds() |
---|
236 | department_id = mapping['department_code'] |
---|
237 | course_id = mapping.get('code','') |
---|
238 | msg = '' |
---|
239 | while True: |
---|
240 | department_courses = self._v_department_courses.get(department_id,None) |
---|
241 | if department_courses is None: |
---|
242 | msg = "No Department with ID: %(department_id)s" % vars() |
---|
243 | break |
---|
244 | if course_id in self._v_course_list: |
---|
245 | msg = "Duplicate Course ID: %(course_id)s" % vars() |
---|
246 | break |
---|
247 | if course_id in department_courses: |
---|
248 | msg = "Course %(course_id)s already exists in department %(department_id)s" % vars() |
---|
249 | break |
---|
250 | try: |
---|
251 | department.invokeFactory('Course', course_id) |
---|
252 | except BadRequest,E: |
---|
253 | msg = "%s" % E |
---|
254 | break |
---|
255 | self._v_course_list.append(course_id) |
---|
256 | course = getattr(department,course_id) |
---|
257 | course.getContent().edit(mapping=mapping) |
---|
258 | break |
---|
259 | return course_id,msg,mapping |
---|
260 | ###) |
---|
261 | |
---|
262 | def edit(self,mapping): ###( |
---|
263 | course_id = mapping.get('code','') |
---|
264 | res = self.portal_catalog(id=course_id) |
---|
265 | while True: |
---|
266 | if not res: |
---|
267 | msg = "No Course with ID: %s" % course_id |
---|
268 | break |
---|
269 | c = res[0].getObject() |
---|
270 | c.getContent().edit(mapping=mapping) |
---|
271 | break |
---|
272 | return course_id,msg,mapping |
---|
273 | ###) |
---|
274 | ###) |
---|
275 | |
---|
276 | class CourseResultImport(WAeUPImport):###( |
---|
277 | """ CourseresultImport """ |
---|
278 | name = "course_result" |
---|
279 | plural_name = "%ss" % name |
---|
280 | commit_after = 1000000 |
---|
281 | |
---|
282 | def getStudentRecord(self,mapping): |
---|
283 | for id_key in ('student_id','matric_no'): |
---|
284 | id_field = mapping.get(id_key,'') |
---|
285 | if id_field: |
---|
286 | search_key = id_key |
---|
287 | search_field = id_field |
---|
288 | break |
---|
289 | if search_key == "student_id": |
---|
290 | search_key = 'id' |
---|
291 | query = Eq(search_key,search_field) |
---|
292 | res = self.students_catalog.evalAdvancedQuery(query) |
---|
293 | student_record = None |
---|
294 | msg = '' |
---|
295 | if res: |
---|
296 | student_record = res[0] |
---|
297 | if search_key == "matric_no": |
---|
298 | mapping['student_id'] = student_record.id |
---|
299 | elif search_key == "student_id": |
---|
300 | mapping['matric_no'] = student_record.matric_no |
---|
301 | else: |
---|
302 | msg = "No student with %(search_key)s %(search_field)s" % vars() |
---|
303 | return student_record,msg |
---|
304 | |
---|
305 | def create(self,mapping):###( |
---|
306 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
307 | if getattr(self,'_v_courses',None) is None: |
---|
308 | res = self.courses_catalog() |
---|
309 | self._v_courses = {} |
---|
310 | for brain in res: |
---|
311 | self._v_courses[brain.code] = brain |
---|
312 | if getattr(self,'_v_level_created',None) is None: |
---|
313 | self._v_level_created = [] |
---|
314 | msg = '' |
---|
315 | key = '' |
---|
316 | while True: |
---|
317 | course_id = mapping.get('code') |
---|
318 | if course_id not in self._v_courses.keys(): |
---|
319 | msg = "No course with ID: %s" % course_id |
---|
320 | break |
---|
321 | student_record,msg = self.getStudentRecord(mapping) |
---|
322 | if msg: |
---|
323 | break |
---|
324 | student_id = student_record.id |
---|
325 | level_id = mapping['level_id'] |
---|
326 | code = mapping['code'] |
---|
327 | if student_id not in self._v_level_created: |
---|
328 | try: |
---|
329 | context = getattr(getattr(students_folder, |
---|
330 | "%(student_id)s" % vars()), |
---|
331 | 'study_course') |
---|
332 | except: |
---|
333 | msg = "could not create level %(level_id)s for %(student_id)s" % vars() |
---|
334 | break |
---|
335 | if level_id not in context.objectIds(): |
---|
336 | context.invokeFactory('StudentStudyLevel',"%(level_id)s" % vars()) |
---|
337 | level = getattr(context,"%(level_id)s" % vars()) |
---|
338 | self.portal_workflow.doActionFor(level,'open') |
---|
339 | # the session string must not be copied into the level object |
---|
340 | current_verdict = getattr(student_record,'current_verdict','') |
---|
341 | current_session = getattr(student_record,'current_session','') |
---|
342 | if current_verdict and student_record.current_level == level_id: |
---|
343 | level.getContent().edit(mapping={'verdict': "%s" % |
---|
344 | current_verdict, |
---|
345 | 'session': "%s" % |
---|
346 | current_session, |
---|
347 | }) |
---|
348 | self.portal_workflow.doActionFor(level,'close') |
---|
349 | self._v_level_created += student_id, |
---|
350 | mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
351 | for k in ('semester','credits',): |
---|
352 | mapping[k] = getattr(self._v_courses[course_id],k) |
---|
353 | try: |
---|
354 | self.course_results.addRecord(**mapping) |
---|
355 | except ValueError: |
---|
356 | msg = "course result already exists: %s" % key |
---|
357 | break |
---|
358 | return key,msg,mapping |
---|
359 | ###) |
---|
360 | |
---|
361 | def edit(self,mapping): ###( |
---|
362 | #import pdb;pdb.set_trace() |
---|
363 | msg = '' |
---|
364 | key = '' |
---|
365 | while True: |
---|
366 | student_record,msg = self.getStudentRecord(mapping) |
---|
367 | if msg: |
---|
368 | break |
---|
369 | student_id = student_record.id |
---|
370 | level_id = mapping['level_id'] |
---|
371 | code = mapping['code'] |
---|
372 | mapping['key'] = key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
373 | break |
---|
374 | try: |
---|
375 | self.course_results.modifyRecord(**mapping) |
---|
376 | except KeyError: |
---|
377 | msg = "No course result to edit: %s" % key |
---|
378 | return key,msg,mapping |
---|
379 | ###) |
---|
380 | |
---|
381 | def delete(self,mapping):###( |
---|
382 | id_key = '' |
---|
383 | msg = '' |
---|
384 | while True: |
---|
385 | student_record,msg = self.getStudentRecord(mapping) |
---|
386 | if msg: |
---|
387 | break |
---|
388 | student_id = student_record.id |
---|
389 | level_id = mapping['level_id'] |
---|
390 | code = mapping['code'] |
---|
391 | key = "%(student_id)s|%(level_id)s|%(code)s" % vars() |
---|
392 | if self.course_results.getRecordByKey(key) is None: |
---|
393 | msg = "no course-result with %(key)s" % vars() |
---|
394 | break |
---|
395 | self.course_results.deleteRecord(key) |
---|
396 | break |
---|
397 | return key,msg,mapping |
---|
398 | ###) |
---|
399 | |
---|
400 | ###) |
---|
401 | |
---|
402 | class CertificateCourseImport(WAeUPImport):###( |
---|
403 | name = "certificate_course" |
---|
404 | plural_name = "%ss" % name |
---|
405 | commit_after = 100000 |
---|
406 | |
---|
407 | def create(self,mapping): |
---|
408 | if getattr(self,'_v_courses',None) is None: |
---|
409 | res = self.courses_catalog() |
---|
410 | self._v_courses= [course.code for course in res] |
---|
411 | if getattr(self,'_v_ceritficates',None) is None: |
---|
412 | res = self.portal_catalog(portal_type = "Certificate") |
---|
413 | self._v_certificates = {} |
---|
414 | for cert in res: |
---|
415 | self._v_certificates[cert.getId] = cert.getObject() |
---|
416 | msg = '' |
---|
417 | while True: |
---|
418 | certificate_course_id = mapping.get('code') |
---|
419 | if certificate_course_id not in self._v_courses: |
---|
420 | msg = "No Course with ID: %s" % certificate_course_id |
---|
421 | break |
---|
422 | cert_id = mapping['certificate_code'] |
---|
423 | cert = self._v_certificates.get(cert_id,None) |
---|
424 | if cert is None: |
---|
425 | msg = "No Certificate with ID: %s" % cert_id |
---|
426 | break |
---|
427 | level_id = mapping.get('level') |
---|
428 | level = getattr(cert,level_id,None) |
---|
429 | if level is None: |
---|
430 | cert.invokeFactory('StudyLevel', level_id) |
---|
431 | level = getattr(cert,level_id,None) |
---|
432 | elif hasattr(level,certificate_course_id): |
---|
433 | msg = "Duplicate CertificateCourse ID: %(certificate_course_id)s " % vars() |
---|
434 | msg += "in %(cert_id)s/ %(level_id)s" % vars() |
---|
435 | break |
---|
436 | level.invokeFactory('CertificateCourse', certificate_course_id) |
---|
437 | c = getattr(level,certificate_course_id) |
---|
438 | c.getContent().edit(mapping=mapping) |
---|
439 | break |
---|
440 | return certificate_course_id,msg,mapping |
---|
441 | ###) |
---|
442 | |
---|
443 | class DepartmentImport(WAeUPImport):###( |
---|
444 | name = "department" |
---|
445 | plural_name = "%ss" % name |
---|
446 | commit_after = 1000 |
---|
447 | |
---|
448 | def create(self,mapping):###( |
---|
449 | "create a department in the correct faculty" |
---|
450 | faculty_id = mapping['faculty_code'] |
---|
451 | msg = '' |
---|
452 | if getattr(self,'_v_faculties',None) is None: |
---|
453 | res = self.portal_catalog(portal_type = "Department") |
---|
454 | self._v_faculties = {} |
---|
455 | for f in res: |
---|
456 | self._v_faculties[f.getId] = f.getObject() |
---|
457 | department_id = mapping.get('code','') |
---|
458 | while True: |
---|
459 | faculty = self._v_faculties.get(faculty_id,None) |
---|
460 | if faculty is None: |
---|
461 | msg = "No Faculty with ID: %s" % faculty_id |
---|
462 | break |
---|
463 | else: |
---|
464 | d = getattr(faculty,department_id,None) |
---|
465 | if d is None or d.portal_type == "Faculty": |
---|
466 | try: |
---|
467 | faculty.invokeFactory('Department', department_id) |
---|
468 | except BadRequest,E: |
---|
469 | msg = "%s" % E |
---|
470 | break |
---|
471 | d = getattr(faculty,department_id) |
---|
472 | d.invokeFactory('CoursesFolder','courses') |
---|
473 | courses = getattr(d,'courses') |
---|
474 | dict = {'Title': 'Courses'} |
---|
475 | courses.getContent().edit(mapping=dict) |
---|
476 | d.invokeFactory('CertificatesFolder','certificates') |
---|
477 | certificates = getattr(d,'certificates') |
---|
478 | dict = {'Title': 'Certificates'} |
---|
479 | certificates.getContent().edit(mapping=dict) |
---|
480 | d.getContent().edit(mapping=mapping) |
---|
481 | break |
---|
482 | return department_id,msg,mapping |
---|
483 | ###) |
---|
484 | |
---|
485 | def edit(self,mapping): ###( |
---|
486 | "edit a department in the correct faculty" |
---|
487 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
488 | faculty_id = mapping['faculty_code'] |
---|
489 | department_id = mapping.get('code','') |
---|
490 | msg = '' |
---|
491 | while True: |
---|
492 | try: |
---|
493 | d = getattr(getattr(academics_folder,faculty_id),department_id,None) |
---|
494 | except KeyError: |
---|
495 | msg = "Department %s or Faculty %s wrong" % (department_id,faculty_id) |
---|
496 | break |
---|
497 | if d is None or d.portal_type == "Faculty": |
---|
498 | msg = "Department %s not found" % (department_id) |
---|
499 | break |
---|
500 | d.getContent().edit(mapping=mapping) |
---|
501 | break |
---|
502 | return department_id,msg,mapping |
---|
503 | ###) |
---|
504 | ###) |
---|
505 | |
---|
506 | class FacultyImport(WAeUPImport):###( |
---|
507 | name = "faculty" |
---|
508 | plural_name = "faculties" |
---|
509 | commit_after = 100 |
---|
510 | |
---|
511 | def create(self,mapping): ###( |
---|
512 | "create a faculty" |
---|
513 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
514 | faculty_id = mapping.get('code','') |
---|
515 | msg = '' |
---|
516 | while True: |
---|
517 | if faculty_id in academics_folder.objectIds(): |
---|
518 | msg = "Faculty with ID: %s exists" % faculty_id |
---|
519 | break |
---|
520 | logger.info('Creating Faculty %(code)s, %(title)s' % mapping) |
---|
521 | try: |
---|
522 | academics_folder.invokeFactory('Faculty', faculty_id) |
---|
523 | except BadRequest,E: |
---|
524 | msg = "%s" % E |
---|
525 | break |
---|
526 | f = getattr(academics_folder,faculty_id,None) |
---|
527 | f.getContent().edit(mapping=mapping) |
---|
528 | break |
---|
529 | return faculty_id,msg,mapping |
---|
530 | ###) |
---|
531 | |
---|
532 | def edit(self,mapping): ###( |
---|
533 | "edit a faculty" |
---|
534 | academics_folder = self.portal_url.getPortalObject().campus.academics |
---|
535 | faculty_id = mapping['code'] |
---|
536 | msg = '' |
---|
537 | while True: |
---|
538 | f = getattr(academics_folder,faculty_id,None) |
---|
539 | if f is None: |
---|
540 | msg = "Faculty with ID: %s does not exist" % faculty_id |
---|
541 | f.getContent().edit(mapping=mapping) |
---|
542 | break |
---|
543 | return faculty_id,msg,mapping |
---|
544 | ###) |
---|
545 | ###) |
---|
546 | |
---|
547 | class StudentImport(WAeUPImport):###( |
---|
548 | name = "student" |
---|
549 | plural_name = "%ss" % name |
---|
550 | commit_after = 100 |
---|
551 | |
---|
552 | field2types_student = { ###( |
---|
553 | 'StudentApplication': |
---|
554 | {'id': 'application', |
---|
555 | 'title': 'Application Data', |
---|
556 | 'wf_transition_return': 'close', |
---|
557 | 'wf_transition_admit': 'remain', |
---|
558 | 'fields': |
---|
559 | ('jamb_reg_no', |
---|
560 | 'entry_mode', |
---|
561 | 'entry_session', |
---|
562 | 'jamb_score', |
---|
563 | 'app_email', |
---|
564 | 'jamb_age', |
---|
565 | 'jamb_state', |
---|
566 | 'jamb_lga', |
---|
567 | 'jamb_sex', |
---|
568 | ) |
---|
569 | }, |
---|
570 | #'StudentPume': |
---|
571 | # {'id': 'pume', |
---|
572 | # 'title': 'Pume Data', |
---|
573 | # 'wf_transition_return': 'close', |
---|
574 | # 'wf_transition_admit': 'close', |
---|
575 | # 'fields': |
---|
576 | # ('pume_score', |
---|
577 | # ) |
---|
578 | # }, |
---|
579 | 'StudentClearance': |
---|
580 | {'id': 'clearance', |
---|
581 | 'title': 'Clearance/Eligibility Record', |
---|
582 | 'wf_transition_return': 'close', |
---|
583 | 'wf_transition_admit': 'remain', |
---|
584 | 'fields': |
---|
585 | ('matric_no', |
---|
586 | 'nationality', |
---|
587 | 'lga', |
---|
588 | 'birthday', |
---|
589 | ) |
---|
590 | }, |
---|
591 | 'StudentPersonal': |
---|
592 | {'id': 'personal', |
---|
593 | 'title': 'Personal Data', |
---|
594 | 'wf_transition_return': 'open', |
---|
595 | 'wf_transition_admit': 'remain', |
---|
596 | 'fields': |
---|
597 | ('firstname', |
---|
598 | 'middlename', |
---|
599 | 'lastname', |
---|
600 | 'sex', |
---|
601 | 'email', |
---|
602 | 'phone', |
---|
603 | 'perm_address', |
---|
604 | ) |
---|
605 | }, |
---|
606 | 'StudentStudyCourse': |
---|
607 | {'id': 'study_course', |
---|
608 | 'title': 'Study Course', |
---|
609 | 'wf_transition_return': 'open', |
---|
610 | 'wf_transition_admit': 'remain', |
---|
611 | 'fields': |
---|
612 | ('study_course', |
---|
613 | 'current_level', |
---|
614 | 'current_session', |
---|
615 | 'current_mode', |
---|
616 | 'current_verdict', |
---|
617 | 'previous_verdict', |
---|
618 | ) |
---|
619 | }, |
---|
620 | # 'StudentStudyLevel': |
---|
621 | # {'id': 'current_level', |
---|
622 | # 'title': '', |
---|
623 | # 'wf_transition_return': 'open', |
---|
624 | # 'wf_transition_admit': 'remain', |
---|
625 | # 'fields': |
---|
626 | # ('verdict', |
---|
627 | # 'session', |
---|
628 | # ) |
---|
629 | # }, |
---|
630 | 'PaymentsFolder': |
---|
631 | {'id': 'payments', |
---|
632 | 'title': 'Payments', |
---|
633 | 'wf_transition_return': 'open', |
---|
634 | 'wf_transition_admit': 'open', |
---|
635 | 'fields': |
---|
636 | () |
---|
637 | }, |
---|
638 | } |
---|
639 | ###) |
---|
640 | |
---|
641 | def create(self,mapping): ###( |
---|
642 | "create student records due import" |
---|
643 | logger = logging.getLogger('WAeUPTool.mass_create_student') |
---|
644 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
645 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
646 | msg = '' |
---|
647 | student_id = '' |
---|
648 | while True: |
---|
649 | if jamb_reg_no: |
---|
650 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
651 | if res: |
---|
652 | msg = "jamb_reg_no exists" |
---|
653 | break |
---|
654 | matric_no = mapping.get('matric_no',None) |
---|
655 | if matric_no: |
---|
656 | res = self.students_catalog(matric_no = matric_no) |
---|
657 | if res: |
---|
658 | msg = "matric_no exists" |
---|
659 | break |
---|
660 | student_id = self.waeup_tool.generateStudentId('?') |
---|
661 | students_folder.invokeFactory('Student', student_id) |
---|
662 | student_obj = getattr(students_folder,student_id) |
---|
663 | f2t = self.field2types_student |
---|
664 | d = {} |
---|
665 | transition = mapping.get('reg_transition','admit') |
---|
666 | if transition not in ('admit','return'): |
---|
667 | msg = "no valid transition provided" |
---|
668 | break |
---|
669 | for pt in f2t.keys(): |
---|
670 | student_obj.invokeFactory(pt,f2t[pt]['id']) |
---|
671 | sub_obj = getattr(student_obj,f2t[pt]['id']) |
---|
672 | sub_doc = sub_obj.getContent() |
---|
673 | d['Title'] = f2t[pt]['title'] |
---|
674 | for field in f2t[pt]['fields']: |
---|
675 | d[field] = mapping.get(field,'') |
---|
676 | |
---|
677 | if pt == "StudentApplication": |
---|
678 | #d['jamb_sex'] = 'M' |
---|
679 | #if mapping.get('sex'): |
---|
680 | # d['jamb_sex'] = 'F' |
---|
681 | d['jamb_firstname'] = mapping.get('firstname',None) |
---|
682 | d['jamb_middlename'] = mapping.get('middlename',None) |
---|
683 | d['jamb_lastname'] = mapping.get('lastname',None) |
---|
684 | |
---|
685 | # if pt == "StudyCourse": |
---|
686 | # for von,zu in (('entry_mode','current_mode'), |
---|
687 | # ('entry_session','current_session')): |
---|
688 | # if mapping.get(zu,None) is None and mapping.get(von,None) is not None: |
---|
689 | # d[zu] = mapping[von] |
---|
690 | sub_doc.edit(mapping = d) |
---|
691 | |
---|
692 | #import pdb;pdb.set_trace() |
---|
693 | new_state = f2t[pt]['wf_transition_%(transition)s' % vars()] |
---|
694 | if new_state != "remain": |
---|
695 | self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
696 | self.portal_workflow.doActionFor(student_obj,transition) |
---|
697 | student_obj.manage_setLocalRoles(student_id, ['Owner',]) |
---|
698 | mapping['id'] = student_id |
---|
699 | break |
---|
700 | return student_id,msg,mapping |
---|
701 | ###) |
---|
702 | |
---|
703 | def edit(self,mapping): ###( |
---|
704 | wftool = self.portal_workflow |
---|
705 | "edit student records due import" |
---|
706 | logger = logging.getLogger('WAeUPTool.mass_edit_student') |
---|
707 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
708 | student_id = mapping.get('id',None) |
---|
709 | jamb_reg_no = mapping.get('jamb_reg_no',None) |
---|
710 | matric_no = mapping.get('matric_no',None) |
---|
711 | editable_keys = mapping.keys() |
---|
712 | msg = '' |
---|
713 | while True: |
---|
714 | if student_id: |
---|
715 | res = self.students_catalog(id = student_id) |
---|
716 | if not res: |
---|
717 | msg = "no student with id %s" % student_id |
---|
718 | break |
---|
719 | student_record = res[0] |
---|
720 | if matric_no and student_record.matric_no: |
---|
721 | if matric_no != student_record.matric_no: |
---|
722 | msg = "old matric_no %s overwritten with %s" % (student_record.matric_no,matric_no) |
---|
723 | #logger.info("%s, old matric_no %s overwritten with %s" % (student_record.id,student_record.matric_no,matric_no)) |
---|
724 | if jamb_reg_no and student_record.jamb_reg_no: |
---|
725 | if jamb_reg_no != student_record.jamb_reg_no: |
---|
726 | msg = "old reg_no %s overwritten with %s" % (student_record.jamb_reg_no,jamb_reg_no) |
---|
727 | #logger.info("%s, old reg_no %s overwritten with %s" % (student_record.id,student_record.jamb_reg_no,jamb_reg_no)) |
---|
728 | elif jamb_reg_no: |
---|
729 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
730 | if not res: |
---|
731 | msg = "no student with jamb_reg_no %s" % jamb_reg_no |
---|
732 | break |
---|
733 | student_record = res[0] |
---|
734 | editable_keys.remove('jamb_reg_no') |
---|
735 | elif matric_no: |
---|
736 | res = self.students_catalog(matric_no = matric_no) |
---|
737 | if not res: |
---|
738 | msg = "no student with matric_no %s" % matric_no |
---|
739 | break |
---|
740 | student_record = res[0] |
---|
741 | editable_keys.remove('matric_no') |
---|
742 | ## included only to change wf state from admitted to returning |
---|
743 | #if student_record.review_state not in ('admitted','objection_raised'): |
---|
744 | # return '%s' % student_record.id ,"student is not in state admitted or objection_raised" |
---|
745 | ## end inclusion |
---|
746 | student_id = student_record.id |
---|
747 | student_obj = getattr(students_folder,student_id) |
---|
748 | f2t = self.field2types_student |
---|
749 | d = {} |
---|
750 | any_change = False |
---|
751 | #special treatment for StudentStudyLevel |
---|
752 | d['verdict'] = mapping.get('current_verdict','') |
---|
753 | d['session'] = mapping.get('current_session','') |
---|
754 | current_level = mapping.get('current_level','') |
---|
755 | while d['session'] and d['verdict'] and current_level: |
---|
756 | sub_obj = getattr(student_obj,'study_course',None) |
---|
757 | if sub_obj is None: |
---|
758 | break |
---|
759 | level_obj = getattr(sub_obj,current_level,None) |
---|
760 | if level_obj is None: |
---|
761 | break |
---|
762 | any_change = True |
---|
763 | level_obj.getContent().edit(mapping = d) |
---|
764 | try: |
---|
765 | wftool.doActionFor(level_obj,'close') |
---|
766 | except: |
---|
767 | pass |
---|
768 | break |
---|
769 | for pt in f2t.keys(): |
---|
770 | #if pt == "StudentApplication": |
---|
771 | # d['jamb_sex'] = 'M' |
---|
772 | # if mapping.get('sex'): |
---|
773 | # d['jamb_sex'] = 'F' |
---|
774 | intersect = set(f2t[pt]['fields']).intersection(set(editable_keys)) |
---|
775 | if intersect and pt not in ('StudentStudyLevel',): |
---|
776 | object_id = f2t[pt]['id'] |
---|
777 | sub_obj = getattr(student_obj,object_id,None) |
---|
778 | if sub_obj is None: |
---|
779 | try: |
---|
780 | student_obj.invokeFactory(pt,object_id) |
---|
781 | except: |
---|
782 | continue |
---|
783 | sub_obj = getattr(student_obj,object_id) |
---|
784 | if f2t[pt]['title'] != '': |
---|
785 | d['Title'] = f2t[pt]['title'] |
---|
786 | sub_doc = sub_obj.getContent() |
---|
787 | for field in intersect: |
---|
788 | changed = False |
---|
789 | if getattr(sub_doc,field,None) != mapping.get(field,''): |
---|
790 | any_change = True |
---|
791 | changed = True |
---|
792 | d[field] = mapping.get(field,'') |
---|
793 | if changed: |
---|
794 | sub_doc.edit(mapping = d) |
---|
795 | ## included only to change wf state from admitted to returning |
---|
796 | # if student_record.review_state in ('admitted','objection_raised'): |
---|
797 | # new_state = f2t[pt]['wf_transition_return'] |
---|
798 | # sub_obj = getattr(student_obj,f2t[pt]['id'],None) |
---|
799 | # if sub_obj and new_state != "remain": |
---|
800 | # try: |
---|
801 | # self.portal_workflow.doActionFor(sub_obj,new_state,dest_container=sub_obj) |
---|
802 | # except: |
---|
803 | # #logger.info('%s, wf transition %s of %s failed' % (student_id,new_state,sub_obj.id)) |
---|
804 | # pass |
---|
805 | #if student_record.review_state in ('admitted','objection_raised'): |
---|
806 | # wfaction = 'return' |
---|
807 | # try: |
---|
808 | # self.portal_workflow.doActionFor(student_obj,wfaction) |
---|
809 | # logger.info('%s, wf state changed' % student_id) |
---|
810 | # any_change = True |
---|
811 | # except: |
---|
812 | # logger.info('%s, wf transition failed, old state = %s' % (student_id,student_record.review_state)) |
---|
813 | # pass |
---|
814 | ## end inclusion |
---|
815 | break |
---|
816 | # if not any_change: |
---|
817 | # msg = 'not modified' |
---|
818 | return student_id,msg,mapping |
---|
819 | ###) |
---|
820 | ###) |
---|
821 | |
---|
822 | class VerdictImport(WAeUPImport):###( |
---|
823 | """ VerdictImport """ |
---|
824 | name = "verdict" |
---|
825 | plural_name = "%ss" % name |
---|
826 | commit_after = 100000 |
---|
827 | required_modes = ('create','edit') |
---|
828 | |
---|
829 | def edit(self,mapping): |
---|
830 | "edit student verdicts" |
---|
831 | wftool = self.portal_workflow |
---|
832 | logger = logging.getLogger('WAeUPTool.mass_edit_verdict') |
---|
833 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
834 | student_id = mapping.get('id',None) |
---|
835 | matric_no = mapping.get('matric_no',None) |
---|
836 | editable_keys = mapping.keys() |
---|
837 | while True: |
---|
838 | key = '' |
---|
839 | msg = '' |
---|
840 | if student_id: |
---|
841 | student_record = self.students_catalog.getRecordByKey(student_id) |
---|
842 | if student_record is None: |
---|
843 | #return '',"no student with id %s" % student_id |
---|
844 | msg = "no student with id %s" % student_id |
---|
845 | break |
---|
846 | if matric_no and student_record.matric_no and matric_no != student_record.matric_no: |
---|
847 | msg = 'student %s: matric_no %s does not match %s' % (student_record.id, |
---|
848 | student_record.matric_no, |
---|
849 | matric_no) |
---|
850 | break |
---|
851 | mapping['matric_no'] = student_record.matric_no |
---|
852 | elif matric_no: |
---|
853 | res = self.students_catalog(matric_no = matric_no) |
---|
854 | if not res: |
---|
855 | msg = "no student with matric_no %s" % matric_no |
---|
856 | break |
---|
857 | student_record = res[0] |
---|
858 | editable_keys.remove('matric_no') |
---|
859 | else: |
---|
860 | msg = "no id or matric_no specified" |
---|
861 | break |
---|
862 | student_id = student_record.id |
---|
863 | mapping['id'] = student_id |
---|
864 | d = {} |
---|
865 | #import pdb;pdb.set_trace() |
---|
866 | any_change = False |
---|
867 | #special treatment for StudentStudyLevel |
---|
868 | current_session = d['session'] = mapping.get('current_session','') |
---|
869 | if current_session and student_record.session != current_session: |
---|
870 | msg = 'student %s: imported session %s does not match current_session %s' % (student_id, |
---|
871 | current_session, |
---|
872 | student_record.session) |
---|
873 | break |
---|
874 | current_level = mapping.get('current_level','') |
---|
875 | if not current_level.isdigit(): |
---|
876 | msg = 'student %s: imported level is empty' % (student_id,) |
---|
877 | break |
---|
878 | if current_level and student_record.level != current_level: |
---|
879 | msg = 'student %s: imported level %s does not match current_level %s' % (student_id, |
---|
880 | current_level, |
---|
881 | student_record.level) |
---|
882 | break |
---|
883 | student_review_state = student_record.review_state |
---|
884 | if student_review_state == 'deactivated': |
---|
885 | msg = "student %s in review_state %s" % (student_id, student_review_state) |
---|
886 | break |
---|
887 | if student_review_state not in ('courses_validated','returning'): |
---|
888 | msg = "student %s in wrong review_state %s" % (student_id, student_review_state) |
---|
889 | break |
---|
890 | student_obj = getattr(students_folder,student_id) |
---|
891 | # f2t = self.field2types_student |
---|
892 | study_course_obj = getattr(student_obj,'study_course',None) |
---|
893 | if study_course_obj is None: |
---|
894 | msg = 'student %s: no study_course object' % student_id |
---|
895 | break |
---|
896 | level_obj = getattr(study_course_obj,current_level,None) |
---|
897 | if level_obj is None: |
---|
898 | msg = 'student %s: no study_level object for level %s' % (student_id, |
---|
899 | current_level) |
---|
900 | break |
---|
901 | verdict = d['verdict'] = d['current_verdict'] = mapping.get('current_verdict','') |
---|
902 | |
---|
903 | #if verdict == student_record.verdict: |
---|
904 | # msg = 'student %s: verdict already set to %s' % (student_id, |
---|
905 | # verdict) |
---|
906 | |
---|
907 | level_review_state = wftool.getInfoFor(level_obj,'review_state',None) |
---|
908 | if level_review_state != "closed": |
---|
909 | wftool.doActionFor(level_obj,'close') |
---|
910 | # msg = 'student %s: level %s is not closed' % (student_id, |
---|
911 | # current_level) |
---|
912 | |
---|
913 | study_course_obj.getContent().edit(mapping = d) |
---|
914 | level_obj.getContent().edit(mapping = d) |
---|
915 | if student_review_state != "returning": |
---|
916 | wftool.doActionFor(student_obj,'return') |
---|
917 | # try: |
---|
918 | # wftool.doActionFor(level_obj,'close') |
---|
919 | # except: |
---|
920 | # pass |
---|
921 | break |
---|
922 | return student_id,msg,mapping |
---|
923 | ###) |
---|
924 | |
---|