1 | #-*- mode: python; mode: fold -*- |
---|
2 | # $Id: Students.py 1887 2007-06-13 03:46:47Z henrik $ |
---|
3 | from string import Template |
---|
4 | from Globals import InitializeClass |
---|
5 | from AccessControl import ClassSecurityInfo |
---|
6 | from AccessControl.SecurityManagement import newSecurityManager |
---|
7 | from zExceptions import BadRequest |
---|
8 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
9 | from Products.CMFCore.utils import UniqueObject, getToolByName |
---|
10 | from Products.CMFCore.permissions import View |
---|
11 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
12 | from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder |
---|
13 | #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument |
---|
14 | from Products.CPSDocument.CPSDocument import CPSDocument |
---|
15 | from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder |
---|
16 | from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser |
---|
17 | from Products.WAeUP_SRP.Academics import makeCertificateCode |
---|
18 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
19 | import DateTime |
---|
20 | import logging |
---|
21 | import csv,re,os |
---|
22 | import Globals |
---|
23 | p_home = Globals.package_home(globals()) |
---|
24 | i_home = Globals.INSTANCE_HOME |
---|
25 | MAX_TRANS = 1000 |
---|
26 | from urllib import urlencode |
---|
27 | |
---|
28 | import DateTime |
---|
29 | #import PIL.Image |
---|
30 | from StringIO import StringIO |
---|
31 | |
---|
32 | def makeCertificateCode(code): ###( |
---|
33 | code = code.replace('.','') |
---|
34 | code = code.replace('(','') |
---|
35 | code = code.replace(')','') |
---|
36 | code = code.replace('/','') |
---|
37 | code = code.replace(' ','') |
---|
38 | code = code.replace('_','') |
---|
39 | return code |
---|
40 | |
---|
41 | ###) |
---|
42 | |
---|
43 | def response_write(response,s): |
---|
44 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
45 | while s.find('<') > -1: |
---|
46 | s = s.replace('<','<') |
---|
47 | while s.find('>') > -1: |
---|
48 | #from pdb import set_trace;set_trace() |
---|
49 | s = s.replace('>','>') |
---|
50 | response.write("%s<br>\n" % s) |
---|
51 | |
---|
52 | def getInt(s): ###( |
---|
53 | try: |
---|
54 | return int(s) |
---|
55 | except: |
---|
56 | return 0 |
---|
57 | |
---|
58 | def getFloat(s): |
---|
59 | try: |
---|
60 | return float(s) |
---|
61 | except: |
---|
62 | return 0.0 |
---|
63 | |
---|
64 | ###) |
---|
65 | |
---|
66 | def getStudentByRegNo(self,reg_no): ###( |
---|
67 | """search student by JAMB Reg No and return StudentFolder""" |
---|
68 | search = ZCatalog.searchResults(self.portal_catalog_real,{'meta_type': 'StudentApplication', |
---|
69 | 'SearchableText': reg_no, |
---|
70 | }) |
---|
71 | if len(search) < 1: |
---|
72 | return None |
---|
73 | return search[0].getObject().aq_parent |
---|
74 | |
---|
75 | ###) |
---|
76 | |
---|
77 | def checkJambNo(jnr): |
---|
78 | try: |
---|
79 | if len(jnr) != 10: |
---|
80 | return False |
---|
81 | except: |
---|
82 | return False |
---|
83 | try: |
---|
84 | int(jnr[:8]) |
---|
85 | return True |
---|
86 | except: |
---|
87 | return False |
---|
88 | |
---|
89 | class StudentsFolder(CPSDocument): ###( |
---|
90 | """ |
---|
91 | WAeUP container for the various WAeUP containers data |
---|
92 | """ |
---|
93 | meta_type = 'StudentsFolder' |
---|
94 | portal_type = meta_type |
---|
95 | security = ClassSecurityInfo() |
---|
96 | |
---|
97 | security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###( |
---|
98 | def loadPumeResultsFromCSV(self): |
---|
99 | """load Fulltime Studentdata from CSV values into pumeresults catalog""" |
---|
100 | import transaction |
---|
101 | import random |
---|
102 | ## csv_d = {'jamb_reg_no': "RegNumber", ###( |
---|
103 | ## 'status': "Admission Status", |
---|
104 | ## 'name': "Name", |
---|
105 | ## 'score': "Score", |
---|
106 | ## 'sex': "Sex", |
---|
107 | ## 'faculty': "Faculty", |
---|
108 | ## 'department': "Dept", |
---|
109 | ## 'course': "Course", |
---|
110 | ## 'course_code_org': "Course Code", |
---|
111 | ## } |
---|
112 | ###) |
---|
113 | ## csv_d = {'jamb_reg_no': "JAMBRegno", |
---|
114 | ## 'name': "Name", |
---|
115 | ## 'score': "Score", |
---|
116 | ## 'sex': "Sex", |
---|
117 | ## 'course': "Course", |
---|
118 | ## 'faculty': "Faculty", |
---|
119 | ## 'department': "Dept", |
---|
120 | ## 'course_code_org': "Course Code", |
---|
121 | ## 'status': "Admission Status", |
---|
122 | ## 'result_type': None, |
---|
123 | ## } |
---|
124 | |
---|
125 | csv_d = {'jamb_reg_no': "reg_no", |
---|
126 | 'name': "fullname", |
---|
127 | 'score': "pume_score", |
---|
128 | 'sex': "sex", |
---|
129 | 'course': "study_course", |
---|
130 | 'course_code_org': "study_course", |
---|
131 | 'status': "admission_status", |
---|
132 | 'result_type': "entry_mode", |
---|
133 | } |
---|
134 | |
---|
135 | csv_fields = [f[1] for f in csv_d.items() if f[1]] |
---|
136 | tr_count = 0 |
---|
137 | total = 0 |
---|
138 | #name = 'pup_new' |
---|
139 | #name = 'pup_update' |
---|
140 | name = 'Admitted' |
---|
141 | update = name.endswith('update') |
---|
142 | no_import = [] |
---|
143 | ok_import = [] |
---|
144 | ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()])) |
---|
145 | no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields])) |
---|
146 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
147 | ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current) |
---|
148 | #open(ok_import_name,"w").write('\n'.join(no_import)) |
---|
149 | no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current) |
---|
150 | #open(no_import_name,"w").write('\n'.join(no_import)) |
---|
151 | logger = logging.getLogger('Students.loadPumeResultsFromCSV' % name) |
---|
152 | starttime = DateTime.now() |
---|
153 | logger.info('Start loading from %s.csv' % name) |
---|
154 | try: |
---|
155 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
156 | except: |
---|
157 | logger.error('Error reading %s.csv' % name) |
---|
158 | return |
---|
159 | pume = self.portal_pumeresults |
---|
160 | format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields]) |
---|
161 | import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()]) |
---|
162 | eduplicate = '%s,"duplicate"' % format |
---|
163 | eoriginal = '%s,"original"' % format |
---|
164 | einvalidjamb = '%s,"invalid JambRegNo"' % format |
---|
165 | added = 'added ,%s' % format |
---|
166 | #from pdb import set_trace;set_trace() |
---|
167 | for jamb in result: |
---|
168 | dict = {} |
---|
169 | for f,fn in csv_d.items(): |
---|
170 | dict[f] = jamb.get(csv_d[f]) |
---|
171 | dict['result_type'] = 'CEST' |
---|
172 | jnr = jamb.get(csv_d['jamb_reg_no']) |
---|
173 | #if not checkJambNo(jnr): |
---|
174 | # logger.info(einvalidjamb % jamb) |
---|
175 | # dd = {} |
---|
176 | # for f,fn in csv_d.items(): |
---|
177 | # dd[fn] = getattr(data,f) |
---|
178 | # no_import.append(eduplicate % dd) |
---|
179 | # no_import.append(eduplicate % jamb) |
---|
180 | # continue |
---|
181 | res = pume(jamb_reg_no=jnr) |
---|
182 | if len(res) > 0: |
---|
183 | if update: |
---|
184 | try: |
---|
185 | pume.modifyRecord(**dict) |
---|
186 | except ValueError: |
---|
187 | logger.info(eduplicate % jamb) |
---|
188 | continue |
---|
189 | except KeyError: |
---|
190 | pume.addRecord(**dict) |
---|
191 | logger.info(added % jamb) |
---|
192 | continue |
---|
193 | else: |
---|
194 | data = res[0] |
---|
195 | if data.name != jamb.get(csv_d['name']): |
---|
196 | #set_trace() |
---|
197 | logger.info(eduplicate % jamb) |
---|
198 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict |
---|
199 | #logger.info(em) |
---|
200 | dd = {} |
---|
201 | for f,fn in csv_d.items(): |
---|
202 | dd[fn] = getattr(data,f) |
---|
203 | no_import.append(eoriginal % dd) |
---|
204 | no_import.append(eduplicate % jamb) |
---|
205 | continue |
---|
206 | try: |
---|
207 | pume.addRecord(**dict) |
---|
208 | ok_import.append(import_format % dict) |
---|
209 | except ValueError: |
---|
210 | logger.info(eduplicate % jamb) |
---|
211 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict |
---|
212 | #logger.info(em) |
---|
213 | no_import.append(eduplicate % jamb) |
---|
214 | logger.info('End loading from %s.csv' % name) |
---|
215 | if len(no_import) > 1: |
---|
216 | open(no_import_name,"w+").write('\n'.join(no_import)) |
---|
217 | open(ok_import_name,"w+").write('\n'.join(ok_import)) |
---|
218 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
219 | ###) |
---|
220 | |
---|
221 | security.declareProtected(ModifyPortalContent,"createStudents")###( |
---|
222 | def createStudents(self): |
---|
223 | """ |
---|
224 | load addmitted Studentdata from CSV values and create Studentobjects. |
---|
225 | This is the current method to create new addmitted Students. |
---|
226 | Before running the eventservice for the students_catalog must be disabled. |
---|
227 | """ |
---|
228 | import transaction |
---|
229 | import random |
---|
230 | #from pdb import set_trace |
---|
231 | wftool = self.portal_workflow |
---|
232 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
233 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
234 | levels = {'ume_ft':'100', |
---|
235 | 'de_ft': '200', |
---|
236 | 'ug_pt': '100', |
---|
237 | 'de_pt': '200', |
---|
238 | 'pg_ft': '700', |
---|
239 | 'pg_pt': '700', |
---|
240 | 'dp_pt': '100', |
---|
241 | 'dp_ft': '100', |
---|
242 | } |
---|
243 | csv_d = {'jamb_reg_no': "reg_no", |
---|
244 | 'entry_mode': 'entry_mode', |
---|
245 | 'jamb_firstname': "firstname", |
---|
246 | 'jamb_middlename': "middlename", |
---|
247 | 'jamb_lastname': "lastname", |
---|
248 | 'jamb_sex': "sex", |
---|
249 | 'jamb_state': "state", |
---|
250 | 'birthday': "date_of_birth", |
---|
251 | 'app_email': "email", |
---|
252 | 'study_course': "study_course", |
---|
253 | 'perm_address': "address", |
---|
254 | 'admission_status': "admission_status", |
---|
255 | } |
---|
256 | csv_fields = [f[1] for f in csv_d.items()] |
---|
257 | tr_count = 0 |
---|
258 | total = 0 |
---|
259 | #name = 'pume_results' |
---|
260 | name = 'Admitted' |
---|
261 | no_import = [] |
---|
262 | s = ','.join(['"%s"' % fn for fn in csv_fields]) |
---|
263 | no_import.append('"Error",%s' % s) |
---|
264 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields]) |
---|
265 | no_certificate = "no certificate %s" % format |
---|
266 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import)) |
---|
267 | logger = logging.getLogger('Students.StudentsFolder.createStudents') |
---|
268 | logger.info('Start loading from %s.csv' % name) |
---|
269 | certs = {} |
---|
270 | try: |
---|
271 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
272 | except: |
---|
273 | logger.error('Error reading %s.csv' % name) |
---|
274 | return |
---|
275 | for result in results: |
---|
276 | if not result.get(csv_d['admission_status']).startswith('Admitted'): |
---|
277 | continue |
---|
278 | #result['Error'] = "Processing " |
---|
279 | #logger.info(format % result) |
---|
280 | jamb_reg_no = result.get(csv_d['jamb_reg_no']) |
---|
281 | res = self.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
282 | if res: |
---|
283 | em = 'Student with RegNo %s already exists\n' % jamb_reg_no |
---|
284 | logger.info(em) |
---|
285 | result['Error'] = "Student exists" |
---|
286 | no_import.append(format % result) |
---|
287 | continue |
---|
288 | cert_id = makeCertificateCode(result.get(csv_d['study_course'])) |
---|
289 | if cert_id not in certs.keys(): |
---|
290 | res = self.portal_catalog(meta_type = "Certificate",id = cert_id) |
---|
291 | if not res: |
---|
292 | em = 'No Certificate with ID %s \n' % cert_id |
---|
293 | logger.info(em) |
---|
294 | result['Error'] = "No Certificate %s" % cert_id |
---|
295 | no_import.append( format % result) |
---|
296 | continue |
---|
297 | cert = res[0] |
---|
298 | cert_path = cert.getPath().split('/') |
---|
299 | certificate = certs[cert_id] = {'faculty': cert_path[-4], |
---|
300 | 'department': cert_path[-3]} |
---|
301 | cert_doc = certs[cert_id] |
---|
302 | catalog_entry = {} |
---|
303 | catalog_entry['jamb_reg_no'] = jamb_reg_no |
---|
304 | firstname = result.get(csv_d['jamb_firstname']) |
---|
305 | middlename = result.get(csv_d['jamb_middlename']) |
---|
306 | lastname = result.get(csv_d['jamb_lastname']) |
---|
307 | perm_address = result.get(csv_d['perm_address']) |
---|
308 | sid = self.generateStudentId('x') |
---|
309 | students_folder.invokeFactory('Student', sid) |
---|
310 | catalog_entry['id'] = sid |
---|
311 | tr_count += 1 |
---|
312 | logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s reg_no %(jamb_reg_no)s ' % vars()) |
---|
313 | student = getattr(self,sid) |
---|
314 | student.manage_setLocalRoles(sid, ['Owner',]) |
---|
315 | student.invokeFactory('StudentApplication','application') |
---|
316 | da = {'Title': 'Application Data'} |
---|
317 | da["jamb_firstname"] = firstname |
---|
318 | da["jamb_middlename"] = middlename |
---|
319 | da["jamb_lastname"] = lastname |
---|
320 | catalog_entry['entry_session'] = da["entry_session"] = self.getSessionId()[-2:] |
---|
321 | catalog_entry['sex'] = sex = result.get(csv_d['jamb_sex']).startswith('F') |
---|
322 | da_fields = ('jamb_reg_no', |
---|
323 | 'jamb_sex', |
---|
324 | 'jamb_state', |
---|
325 | 'entry_mode', |
---|
326 | 'app_email', |
---|
327 | ) |
---|
328 | for f in da_fields: |
---|
329 | da[f] = result.get(csv_d[f]) |
---|
330 | catalog_entry['email'] = da['app_email'] |
---|
331 | catalog_entry['entry_mode'] = da['entry_mode'] |
---|
332 | app = student.application |
---|
333 | app_doc = app.getContent() |
---|
334 | app.getContent().edit(mapping=da) |
---|
335 | picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no) |
---|
336 | app.manage_setLocalRoles(sid, ['Owner',]) |
---|
337 | |
---|
338 | picture_id = da['jamb_reg_no'].replace('/','_') |
---|
339 | file = None |
---|
340 | for ext in ('jpg','JPG'): |
---|
341 | picture ="%s/import/pictures_admitted_latest/%s.%s" % (i_home,picture_id,ext) |
---|
342 | if os.path.exists(picture): |
---|
343 | file = open(picture) |
---|
344 | break |
---|
345 | if file is not None: |
---|
346 | |
---|
347 | ## file conversion does not work |
---|
348 | #img = PIL.Image.open(file) |
---|
349 | #img.thumbnail((150,200), |
---|
350 | # resample=PIL.Image.ANTIALIAS) |
---|
351 | #outfile = StringIO() |
---|
352 | #img.save(outfile, format=img.format) |
---|
353 | |
---|
354 | outfile = file.read() |
---|
355 | app_doc.manage_addFile('passport', |
---|
356 | file=outfile, |
---|
357 | title="%s.jpg" % jamb_reg_no) |
---|
358 | #wftool.doActionFor(app,'close') |
---|
359 | dp = {} |
---|
360 | dp['firstname'] = firstname |
---|
361 | dp['middlename'] = middlename |
---|
362 | dp['lastname'] = lastname |
---|
363 | dp['email'] = da['app_email'] |
---|
364 | dp['sex'] = sex |
---|
365 | dp['perm_address'] = perm_address |
---|
366 | catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp |
---|
367 | student.invokeFactory('StudentPersonal','personal') |
---|
368 | per = student.personal |
---|
369 | per_doc = per.getContent() |
---|
370 | per_doc.edit(mapping = dp) |
---|
371 | per.manage_setLocalRoles(sid, ['Owner',]) |
---|
372 | wftool.doActionFor(student,'pume_pass') |
---|
373 | wftool.doActionFor(student,'admit') |
---|
374 | # |
---|
375 | # Clearance |
---|
376 | # |
---|
377 | student.invokeFactory('StudentClearance','clearance') |
---|
378 | #wftool.doActionFor(student.clearance,'open') |
---|
379 | clearance = student.clearance |
---|
380 | dc = {'Title': 'Clearance/Eligibility Record'} |
---|
381 | clearance = student.clearance |
---|
382 | date_str = result.get(csv_d['birthday']) |
---|
383 | try: |
---|
384 | date = DateTime.DateTime(date_str) |
---|
385 | except: |
---|
386 | #import pdb;pdb.set_trace() |
---|
387 | date = None |
---|
388 | dc['birthday'] = date |
---|
389 | clearance.getContent().edit(mapping=dc) |
---|
390 | clearance.manage_setLocalRoles(sid, ['Owner',]) |
---|
391 | # |
---|
392 | # Study Course |
---|
393 | # |
---|
394 | student.invokeFactory('StudentStudyCourse','study_course') |
---|
395 | study_course = student.study_course |
---|
396 | dsc = {} |
---|
397 | catalog_entry['level'] = dsc['current_level'] = levels.get(da['entry_mode'],'100') |
---|
398 | #catalog_entry['level'] = dsc['current_level'] = '100' # Attention: not for DE students |
---|
399 | catalog_entry['session'] = dsc['current_session'] = da['entry_session'] |
---|
400 | catalog_entry['mode'] = dsc['current_mode'] = da['entry_mode'] |
---|
401 | catalog_entry['course'] = dsc['study_course'] = cert_id |
---|
402 | catalog_entry['faculty'] = certificate['faculty'] |
---|
403 | catalog_entry['department'] = certificate['department'] |
---|
404 | catalog_entry['verdict'] = dsc['current_verdict'] = 'N/A' |
---|
405 | catalog_entry['review_state'] = self.portal_workflow.getInfoFor(student,'review_state',None) |
---|
406 | study_course.getContent().edit(mapping=dsc) |
---|
407 | #import pdb;pdb.set_trace() |
---|
408 | self.students_catalog.addRecord(**catalog_entry) |
---|
409 | if tr_count > 1000: |
---|
410 | if len(no_import) > 0: |
---|
411 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write( |
---|
412 | '\n'.join(no_import) + "\n") |
---|
413 | no_import = [] |
---|
414 | em = '%d transactions commited\n' % tr_count |
---|
415 | transaction.commit() |
---|
416 | logger.info(em) |
---|
417 | total += tr_count |
---|
418 | tr_count = 0 |
---|
419 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write( |
---|
420 | '\n'.join(no_import)) |
---|
421 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
422 | ###) |
---|
423 | |
---|
424 | security.declareProtected(ModifyPortalContent,"transferStudents")###( |
---|
425 | def transferStudents(self,filename): |
---|
426 | """ |
---|
427 | load Interfaculty transferStudents Studentdata from CSV values. |
---|
428 | """ |
---|
429 | import transaction |
---|
430 | import random |
---|
431 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
432 | pm = self.portal_membership |
---|
433 | member = pm.getAuthenticatedMember() |
---|
434 | logger = logging.getLogger('Students.StudentsFolder.transferStudents') |
---|
435 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
436 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
437 | csv_fields = ('old_matric_no', |
---|
438 | 'matric_no', |
---|
439 | 'study_course', |
---|
440 | 'current_mode', |
---|
441 | 'current_level', |
---|
442 | ) |
---|
443 | tr_count = 0 |
---|
444 | total = 0 |
---|
445 | total_imported = 0 |
---|
446 | total_not_imported = 0 |
---|
447 | imported = [] |
---|
448 | not_imported = [] |
---|
449 | certs = {} |
---|
450 | try: |
---|
451 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb")) |
---|
452 | except: |
---|
453 | logger.error('Error reading %s.csv' % filename) |
---|
454 | return |
---|
455 | start = True |
---|
456 | for result in results: |
---|
457 | total += 1 |
---|
458 | if start: |
---|
459 | start = False |
---|
460 | logger.info('%s starts import from %s.csv' % (member,filename)) |
---|
461 | import_keys = [k for k in result.keys() if not k.startswith('ignore')] |
---|
462 | diff2schema = set(import_keys).difference(set(csv_fields)) |
---|
463 | if diff2schema: |
---|
464 | em = "not ignorable key(s) %s found in heading" % diff2schema |
---|
465 | return em |
---|
466 | s = ','.join(['"%s"' % fn for fn in import_keys]) |
---|
467 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(s + ',"Error"'+ '\n') |
---|
468 | s = '"id",' + s |
---|
469 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(s + '\n') |
---|
470 | format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) |
---|
471 | format_error = format + ',"%(Error)s"' |
---|
472 | format = '"%(id)s",'+ format |
---|
473 | old_matric_no = result.get('old_matric_no') |
---|
474 | res = self.students_catalog(matric_no = old_matric_no) |
---|
475 | result['id'] = "None" |
---|
476 | if not res: |
---|
477 | em = 'Student with matric_no %s not found' % old_matric_no |
---|
478 | logger.info(em) |
---|
479 | result['Error'] = "Student does not exist" |
---|
480 | not_imported.append(format_error % result) |
---|
481 | total_not_imported += 1 |
---|
482 | continue |
---|
483 | student_brain = res[0] |
---|
484 | student_object = getattr(students_folder,student_brain.id) |
---|
485 | result['id'] = student_brain.id |
---|
486 | cert_id = makeCertificateCode(result.get('study_course')) |
---|
487 | if cert_id not in certs.keys(): |
---|
488 | res = self.portal_catalog(meta_type = "Certificate",id = cert_id) |
---|
489 | if not res: |
---|
490 | em = 'No certificate with ID %s \n' % cert_id |
---|
491 | logger.info(em) |
---|
492 | result['Error'] = "No Certificate %s" % cert_id |
---|
493 | not_imported.append( format_error % result) |
---|
494 | total_not_imported += 1 |
---|
495 | continue |
---|
496 | cert = res[0] |
---|
497 | cert_path = cert.getPath().split('/') |
---|
498 | certificate = certs[cert_id] = {'faculty': cert_path[-4], |
---|
499 | 'department': cert_path[-3]} |
---|
500 | cert_doc = certs[cert_id] |
---|
501 | clearance = getattr(student_object,'clearance',None) |
---|
502 | if clearance is None: |
---|
503 | em = 'Student has no clearance object' |
---|
504 | logger.info(em) |
---|
505 | result['Error'] = em |
---|
506 | not_imported.append( format_error % result) |
---|
507 | total_not_imported += 1 |
---|
508 | continue |
---|
509 | clearance_doc = clearance.getContent() |
---|
510 | study_course = student_object.study_course |
---|
511 | study_course_doc = study_course.getContent() |
---|
512 | old_study_course = study_course_doc.study_course |
---|
513 | old_current_level = study_course_doc.current_level |
---|
514 | current_level = result.get('current_level',None) |
---|
515 | new_study_course = result.get('study_course',None) |
---|
516 | try: |
---|
517 | icl = int(current_level) |
---|
518 | except: |
---|
519 | em = 'Invalid new level %s' % current_level |
---|
520 | logger.info(em) |
---|
521 | result['Error'] = em |
---|
522 | not_imported.append( format_error % result) |
---|
523 | total_not_imported += 1 |
---|
524 | continue |
---|
525 | if icl == int(old_current_level) and old_study_course == new_study_course: |
---|
526 | em = 'Already transferred' |
---|
527 | logger.info(em) |
---|
528 | result['Error'] = em |
---|
529 | not_imported.append( format_error % result) |
---|
530 | total_not_imported += 1 |
---|
531 | continue |
---|
532 | if study_course.objectIds(): |
---|
533 | em = 'Already registered level %s for %s, but is supposed to study %s at level %s' % (old_current_level,old_study_course,new_study_course,current_level) |
---|
534 | logger.info(em) |
---|
535 | result['Error'] = em |
---|
536 | not_imported.append( format_error % result) |
---|
537 | total_not_imported += 1 |
---|
538 | continue |
---|
539 | #from pdb import set_trace; set_trace() |
---|
540 | cd = {} |
---|
541 | matric_no_history = getattr(clearance_doc,'matric_no_history',[]) |
---|
542 | if not matric_no_history: |
---|
543 | matric_no_history = [] |
---|
544 | matric_no_history.append(old_matric_no) |
---|
545 | cd['matric_no_history'] = matric_no_history |
---|
546 | cd['matric_no'] = result.get('matric_no') |
---|
547 | clearance_doc.edit(mapping = cd) |
---|
548 | dsc = {} |
---|
549 | study_course_history = getattr(study_course_doc,'study_course_history',[]) |
---|
550 | if not study_course_history: |
---|
551 | study_course_history = [] |
---|
552 | study_course_history.append(old_study_course) |
---|
553 | dsc['study_course_history'] = study_course_history |
---|
554 | dsc['study_course'] = new_study_course |
---|
555 | dsc['current_level'] = current_level |
---|
556 | dsc['current_mode'] = result.get('current_mode') |
---|
557 | study_course_doc.edit(mapping=dsc) |
---|
558 | imported.append( format % result) |
---|
559 | tr_count += 1 |
---|
560 | total_imported += 1 |
---|
561 | if tr_count > 1000: |
---|
562 | if len(not_imported) > 0: |
---|
563 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
564 | '\n'.join(not_imported) + '\n') |
---|
565 | not_imported = [] |
---|
566 | if len(imported) > 0: |
---|
567 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
568 | '\n'.join(imported) + '\n') |
---|
569 | imported = [] |
---|
570 | em = '%d transactions committed\n' % (tr_count) |
---|
571 | transaction.commit() |
---|
572 | regs = [] |
---|
573 | logger.info(em) |
---|
574 | tr_count = 0 |
---|
575 | if len(imported) > 0: |
---|
576 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
577 | '\n'.join(imported)) |
---|
578 | if len(not_imported) > 0: |
---|
579 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
580 | '\n'.join(not_imported)) |
---|
581 | em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) |
---|
582 | logger.info(em) |
---|
583 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
584 | ###) |
---|
585 | |
---|
586 | security.declareProtected(ModifyPortalContent,"importReturningStudents")###( |
---|
587 | def importReturningStudents(self): |
---|
588 | """load Returning Studentdata from CSV values""" |
---|
589 | import transaction |
---|
590 | import random |
---|
591 | #from pdb import set_trace |
---|
592 | wftool = self.portal_workflow |
---|
593 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
594 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
595 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
596 | tr_count = 1 |
---|
597 | total = 0 |
---|
598 | #name = 'pume_results' |
---|
599 | name = 'Returning' |
---|
600 | table = self.returning_import |
---|
601 | no_import = [] |
---|
602 | imported = [] |
---|
603 | logger = logging.getLogger('Students.StudentsFolder.importReturningStudents') |
---|
604 | try: |
---|
605 | returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
606 | except: |
---|
607 | logger.error('Error reading %s.csv' % name) |
---|
608 | return |
---|
609 | l = self.portal_catalog({'meta_type': "Certificate"}) |
---|
610 | certs = {} |
---|
611 | cert_docs = {} |
---|
612 | for f in l: |
---|
613 | certs[f.getId] = f.getObject().getContent() |
---|
614 | start = True |
---|
615 | res = table() |
---|
616 | regs = [] |
---|
617 | if len(res) > 0: |
---|
618 | regs = [s.matric_no for s in res] |
---|
619 | #import pdb;pdb.set_trace() |
---|
620 | for student in returning: |
---|
621 | if start: |
---|
622 | start = False |
---|
623 | logger.info('Start loading from %s.csv' % name) |
---|
624 | s = ','.join(['"%s"' % fn for fn in student.keys()]) |
---|
625 | imported.append(s) |
---|
626 | no_import.append('%s,"Error"' % s) |
---|
627 | format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()]) |
---|
628 | format_error = format + ',"%(Error)s"' |
---|
629 | no_certificate = "no certificate %s" % format |
---|
630 | student['matric_no'] = matric_no = student.get('matric_no').upper() |
---|
631 | student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper() |
---|
632 | student['Permanent_Address'] = perm_address = student.get('Permanent Address') |
---|
633 | if matric_no == '': |
---|
634 | student['Error'] = "Empty matric_no" |
---|
635 | no_import.append( format_error % student) |
---|
636 | continue |
---|
637 | if matric_no in regs or self.returning_import(matric_no = matric_no): |
---|
638 | student['Error'] = "Duplicate" |
---|
639 | no_import.append( format_error % student) |
---|
640 | continue |
---|
641 | cert_id = makeCertificateCode(student.get('Coursemajorcode')) |
---|
642 | if cert_id not in certs.keys(): |
---|
643 | student['Error'] = "No Certificate %s" % cert_id |
---|
644 | no_import.append( format_error % student) |
---|
645 | continue |
---|
646 | try: |
---|
647 | table.addRecord(**student) |
---|
648 | except ValueError: |
---|
649 | student['Error'] = "Duplicate" |
---|
650 | no_import.append( format_error % student) |
---|
651 | continue |
---|
652 | regs.append(student.get('matric_no')) |
---|
653 | imported.append(format % student) |
---|
654 | tr_count += 1 |
---|
655 | if tr_count > 1000: |
---|
656 | if len(no_import) > 0: |
---|
657 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
658 | '\n'.join(no_import) + '\n') |
---|
659 | no_import = [] |
---|
660 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
661 | '\n'.join(no_import) + "\n") |
---|
662 | imported = [] |
---|
663 | em = '%d transactions commited total %s\n' % (tr_count,total) |
---|
664 | transaction.commit() |
---|
665 | regs = [] |
---|
666 | logger.info(em) |
---|
667 | total += tr_count |
---|
668 | tr_count = 0 |
---|
669 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
670 | '\n'.join(imported)) |
---|
671 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
672 | '\n'.join(no_import)) |
---|
673 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
674 | ###) |
---|
675 | |
---|
676 | security.declareProtected(ModifyPortalContent,"fixAllNames")###( |
---|
677 | def fixAllNames(self): |
---|
678 | "fix all students names" |
---|
679 | import transaction |
---|
680 | response = self.REQUEST.RESPONSE |
---|
681 | logger = logging.getLogger('fixAllNames') |
---|
682 | logger.info('Start') |
---|
683 | students = self.portal_catalog(portal_type='Student') |
---|
684 | count = 0 |
---|
685 | total = 0 |
---|
686 | for student in students: |
---|
687 | scat_res = self.students_catalog(id = student.getId) |
---|
688 | if not scat_res: |
---|
689 | self.students_catalog.addRecord(id = student.getId) |
---|
690 | scat_res = self.students_catalog(id = student.getId) |
---|
691 | student_entry = scat_res[0] |
---|
692 | old_new = self.fixName(student,student_entry) |
---|
693 | count += 1 |
---|
694 | response_write(response,'"%d","%s",%s' % (count + total,student_entry.id,old_new)) |
---|
695 | if count > 2000: |
---|
696 | transaction.commit() |
---|
697 | logger.info("%d transactions commited" % count) |
---|
698 | total += count |
---|
699 | count = 0 |
---|
700 | ###) |
---|
701 | |
---|
702 | security.declareProtected(ModifyPortalContent,"fixName")###( |
---|
703 | def fixName(self,student_brain, student_entry): |
---|
704 | "fix the name of a student" |
---|
705 | fix = "first" |
---|
706 | if student_entry.get('name_fixed',None) == fix: |
---|
707 | return "Name already fixed" |
---|
708 | student_id = student_entry.id |
---|
709 | new_student = student_entry.jamb_reg_no.startswith('6') |
---|
710 | student_obj = student_brain.getObject() |
---|
711 | personal = getattr(student_obj,'personal',None) |
---|
712 | invalid = '' |
---|
713 | if personal is None: |
---|
714 | return '"%s","Returning","%s","%s"' % (invalid,student_entry.name,"not logged in") |
---|
715 | per_doc = personal.getContent() |
---|
716 | old_first = per_doc.firstname |
---|
717 | old_middle = per_doc.middlename |
---|
718 | old_last = per_doc.lastname |
---|
719 | new_first = '' |
---|
720 | new_middle = '' |
---|
721 | new_last = '' |
---|
722 | if new_student: |
---|
723 | if not old_first and not old_middle and old_last: |
---|
724 | new_names = [n.capitalize() for n in old_last.split()] |
---|
725 | if len(new_names) > 1: |
---|
726 | old_first = new_names[0] |
---|
727 | old_last = new_names[-1] |
---|
728 | old_middle = ' '.join(new_names[1:-1]) |
---|
729 | else: |
---|
730 | old_last = new_names[0] |
---|
731 | old_first = '' |
---|
732 | old_middle = '' |
---|
733 | if old_first: |
---|
734 | new_first = old_first |
---|
735 | if old_middle: |
---|
736 | new_middle = old_middle |
---|
737 | if old_last: |
---|
738 | new_last = old_last |
---|
739 | if old_first.find('<') != -1 or\ |
---|
740 | old_first.find('>') != -1 or\ |
---|
741 | old_middle.find('<') != -1 or\ |
---|
742 | old_middle.find('>') != -1 or\ |
---|
743 | old_last.find('<') != -1 or\ |
---|
744 | old_last.find('>') != -1: |
---|
745 | invalid = "invalid characters" |
---|
746 | else: |
---|
747 | new_first = old_first |
---|
748 | if new_first.strip() == '-': |
---|
749 | new_first = '' |
---|
750 | new_middle = old_middle |
---|
751 | if new_middle.strip() == '-': |
---|
752 | new_middle = '' |
---|
753 | new_last = old_last |
---|
754 | if new_last.strip() == '-': |
---|
755 | new_last = '' |
---|
756 | name = "%(new_first)s %(new_middle)s %(new_last)s" % vars() |
---|
757 | if new_student: |
---|
758 | text = "New" |
---|
759 | else: |
---|
760 | text = "Returning" |
---|
761 | old_new = '"%s","%s","%s","%s"' % (invalid,text, |
---|
762 | student_entry.name, |
---|
763 | name) |
---|
764 | if not invalid: |
---|
765 | self.students_catalog.modifyRecord(id = student_id, |
---|
766 | name_fixed = fix, |
---|
767 | name = name) |
---|
768 | per_doc.edit(mapping = {'firstname' : new_first, |
---|
769 | 'middlename' : new_middle, |
---|
770 | 'lastname' : new_last, |
---|
771 | }) |
---|
772 | return old_new |
---|
773 | ###) |
---|
774 | |
---|
775 | security.declareProtected(ModifyPortalContent,"updateReturningStudents")###( |
---|
776 | def updateReturningStudents(self): |
---|
777 | """load and overwrite Returning Student Data from CSV values""" |
---|
778 | import transaction |
---|
779 | import random |
---|
780 | #from pdb import set_trace |
---|
781 | wftool = self.portal_workflow |
---|
782 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
783 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
784 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
785 | tr_count = 1 |
---|
786 | total = 0 |
---|
787 | #name = 'pume_results' |
---|
788 | name = 'Returning_update' |
---|
789 | table = self.returning_import |
---|
790 | no_import = [] |
---|
791 | imported = [] |
---|
792 | logger = logging.getLogger('Students.StudentsFolder.updateReturningStudents') |
---|
793 | try: |
---|
794 | returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
795 | except: |
---|
796 | logger.error('Error reading %s.csv' % name) |
---|
797 | return |
---|
798 | l = self.portal_catalog({'meta_type': "Certificate"}) |
---|
799 | certs = {} |
---|
800 | cert_docs = {} |
---|
801 | for f in l: |
---|
802 | certs[f.getId] = f.getObject().getContent() |
---|
803 | start = True |
---|
804 | res = table() |
---|
805 | regs = [] |
---|
806 | if len(res) > 0: |
---|
807 | regs = [s.matric_no for s in res] |
---|
808 | for student in returning: |
---|
809 | if start: |
---|
810 | start = False |
---|
811 | logger.info('Start loading from %s.csv' % name) |
---|
812 | s = ','.join(['"%s"' % fn for fn in student.keys()]) |
---|
813 | imported.append(s) |
---|
814 | no_import.append('%s,"Error"' % s) |
---|
815 | format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()]) |
---|
816 | format_error = format + ',"%(Error)s"' |
---|
817 | no_certificate = "no certificate %s" % format |
---|
818 | matric_no = student.get('matric_no').upper() |
---|
819 | student['matric_no'] = matric_no |
---|
820 | if matric_no == '': |
---|
821 | student['Error'] = "Empty matric_no" |
---|
822 | no_import.append( format_error % student) |
---|
823 | continue |
---|
824 | # if matric_no in regs or self.returning_import(matric_no = matric_no): |
---|
825 | # student['Error'] = "Duplicate" |
---|
826 | # no_import.append( format_error % student) |
---|
827 | # continue |
---|
828 | # cert_id = makeCertificateCode(student.get('Coursemajorcode')) |
---|
829 | # if cert_id not in certs.keys(): |
---|
830 | # student['Error'] = "No Certificate %s" % cert_id |
---|
831 | # no_import.append( format_error % student) |
---|
832 | # continue |
---|
833 | try: |
---|
834 | table.modifyRecord(**student) |
---|
835 | except KeyError: |
---|
836 | #import pdb;pdb.set_trace() |
---|
837 | student['Error'] = "no Student found to update" |
---|
838 | no_import.append( format_error % student) |
---|
839 | continue |
---|
840 | #s = self.students_catalog(matric_no=matric_no) |
---|
841 | #if s: |
---|
842 | # level = "%s" % (int(student.get('Level')) + 100) |
---|
843 | # self.students_catalog.modifyRecord(id = s[0].id, |
---|
844 | # level=level) |
---|
845 | |
---|
846 | regs.append(student.get('matric_no')) |
---|
847 | imported.append(format % student) |
---|
848 | tr_count += 1 |
---|
849 | if tr_count > 1000: |
---|
850 | if len(no_import) > 0: |
---|
851 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
852 | '\n'.join(no_import) + '\n') |
---|
853 | no_import = [] |
---|
854 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
855 | '\n'.join(no_import) + "\n") |
---|
856 | imported = [] |
---|
857 | em = '%d transactions commited total %s\n' % (tr_count,total) |
---|
858 | transaction.commit() |
---|
859 | regs = [] |
---|
860 | logger.info(em) |
---|
861 | total += tr_count |
---|
862 | tr_count = 0 |
---|
863 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
864 | '\n'.join(imported)) |
---|
865 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
866 | '\n'.join(no_import)) |
---|
867 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
868 | ###) |
---|
869 | |
---|
870 | security.declareProtected(ModifyPortalContent,"importResults")###( |
---|
871 | def importResults(self): |
---|
872 | """load Returning Students Results from CSV""" |
---|
873 | import transaction |
---|
874 | import random |
---|
875 | #from pdb import set_trace |
---|
876 | wftool = self.portal_workflow |
---|
877 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
878 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
879 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
880 | tr_count = 1 |
---|
881 | total = 0 |
---|
882 | #name = 'pume_results' |
---|
883 | name = 'Results' |
---|
884 | table = self.results_import |
---|
885 | no_import = [] |
---|
886 | imported = [] |
---|
887 | logger = logging.getLogger('Students.StudentsFolder.importResults') |
---|
888 | try: |
---|
889 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
890 | except: |
---|
891 | logger.error('Error reading %s.csv' % name) |
---|
892 | return |
---|
893 | l = self.portal_catalog({'meta_type': "Course"}) |
---|
894 | courses = [f.getId for f in l] |
---|
895 | start = True |
---|
896 | res = table() |
---|
897 | regs = [] |
---|
898 | if len(res) > 0: |
---|
899 | regs = [s.key for s in res] |
---|
900 | no_course = [] |
---|
901 | no_course_list = [] |
---|
902 | course_count = 0 |
---|
903 | for result in results: |
---|
904 | if start: |
---|
905 | start = False |
---|
906 | logger.info('Start loading from %s.csv' % name) |
---|
907 | s = ','.join(['"%s"' % fn for fn in result.keys()]) |
---|
908 | imported.append(s) |
---|
909 | no_import.append('%s,"Error"' % s) |
---|
910 | format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()]) |
---|
911 | format_error = format + ',"%(Error)s"' |
---|
912 | no_certificate = "no certificate %s" % format |
---|
913 | course_id = result.get('CosCode') |
---|
914 | if not course_id: |
---|
915 | course_id = 'N/A' |
---|
916 | result['CosCode'] = course_id |
---|
917 | matric_no = result.get('matric_no').upper() |
---|
918 | result['matric_no'] = matric_no |
---|
919 | key = matric_no+course_id |
---|
920 | if matric_no == '': |
---|
921 | result['Error'] = "Empty matric_no" |
---|
922 | no_import.append( format_error % result) |
---|
923 | continue |
---|
924 | if key in regs or self.results_import(key = key): |
---|
925 | result['Error'] = "Duplicate" |
---|
926 | no_import.append( format_error % result) |
---|
927 | continue |
---|
928 | if course_id not in courses: |
---|
929 | if course_id not in no_course: |
---|
930 | course_count +=1 |
---|
931 | no_course.append(course_id) |
---|
932 | no_course_list.append('"%s"' % course_id) |
---|
933 | #result['Error'] = "No Course" |
---|
934 | #logger.info(format_error % result) |
---|
935 | |
---|
936 | result['key'] = key |
---|
937 | try: |
---|
938 | table.addRecord(**result) |
---|
939 | except ValueError: |
---|
940 | #import pdb;pdb.set_trace() |
---|
941 | result['Error'] = "Duplicate" |
---|
942 | no_import.append( format_error % result) |
---|
943 | continue |
---|
944 | |
---|
945 | regs.append(key) |
---|
946 | imported.append(format % result) |
---|
947 | tr_count += 1 |
---|
948 | if tr_count > 1000: |
---|
949 | if len(no_import) > 0: |
---|
950 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
951 | '\n'.join(no_import)+'\n') |
---|
952 | no_import = [] |
---|
953 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
954 | '\n'.join(imported) + '\n') |
---|
955 | imported = [] |
---|
956 | if no_course_list: |
---|
957 | open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write( |
---|
958 | '\n'.join(no_course_list) + '\n') |
---|
959 | no_course_list = [] |
---|
960 | em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count) |
---|
961 | transaction.commit() |
---|
962 | logger.info(em) |
---|
963 | regs = [] |
---|
964 | total += tr_count |
---|
965 | tr_count = 0 |
---|
966 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
967 | '\n'.join(imported)) |
---|
968 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
969 | '\n'.join(no_import)) |
---|
970 | if no_course_list: |
---|
971 | open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write( |
---|
972 | '\n'.join(no_course_list)) |
---|
973 | em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count) |
---|
974 | logger.info(em) |
---|
975 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
976 | ###) |
---|
977 | |
---|
978 | security.declareProtected(ModifyPortalContent,"updateStudyCourse")###( |
---|
979 | def updateStudyCourse(self): |
---|
980 | """update StudyCourse from CSV values""" |
---|
981 | import transaction |
---|
982 | import random |
---|
983 | from pdb import set_trace |
---|
984 | wftool = self.portal_workflow |
---|
985 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
986 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
987 | csv_d = {'jamb_reg_no': "RegNumber", |
---|
988 | 'jamb_lastname': "Name", |
---|
989 | 'session': "Session", |
---|
990 | 'pume_tot_score': "PUME SCORE", |
---|
991 | 'jamb_score': "JambScore", |
---|
992 | 'jamb_sex': "Sex", |
---|
993 | 'jamb_state': "State", |
---|
994 | ## 'jamb_first_cos': "AdminCourse", |
---|
995 | 'faculty': "AdminFaculty", |
---|
996 | 'course_code': "AdmitCoscode", |
---|
997 | 'stud_status':"AdmitStatus", |
---|
998 | 'department': "AdmitDept", |
---|
999 | 'jamb_lga': "LGA", |
---|
1000 | 'app_email': "email", |
---|
1001 | 'app_mobile': "PhoneNumbers", |
---|
1002 | } |
---|
1003 | csv_fields = [f[1] for f in csv_d.items()] |
---|
1004 | tr_count = 0 |
---|
1005 | total = 0 |
---|
1006 | #name = 'pume_results' |
---|
1007 | name = 'StudyCourseChange' |
---|
1008 | no_import = [] |
---|
1009 | s = ','.join(['"%s"' % fn for fn in csv_fields]) |
---|
1010 | no_import.append('"Error",%s' % s) |
---|
1011 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields]) |
---|
1012 | no_certificate = "no certificate %s" % format |
---|
1013 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write( |
---|
1014 | '\n'.join(no_import)) |
---|
1015 | logger = logging.getLogger('Students.StudentsFolder.updateStudyCourse') |
---|
1016 | logger.info('Start loading from %s.csv' % name) |
---|
1017 | l = self.portal_catalog({'meta_type': "Certificate"}) |
---|
1018 | try: |
---|
1019 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
1020 | except: |
---|
1021 | logger.error('Error reading %s.csv' % name) |
---|
1022 | return |
---|
1023 | for jamb in result: |
---|
1024 | jamb['Error'] = "Processing " |
---|
1025 | logger.info(format % jamb) |
---|
1026 | jamb_reg_no = jamb.get(csv_d['jamb_reg_no']) |
---|
1027 | res = self.portal_catalog({'portal_type': "StudentApplication", |
---|
1028 | 'SearchableText': jamb_reg_no }) |
---|
1029 | if not res: |
---|
1030 | em = 'Student with jamb_reg_no %s does not exists\n' % jamb_reg_no |
---|
1031 | logger.info(em) |
---|
1032 | jamb['Error'] = "Student does not exist" |
---|
1033 | no_import.append(format % jamb) |
---|
1034 | continue |
---|
1035 | sid = res[0].getPath().split('/')[-2] |
---|
1036 | cert_id = makeCertificateCode(jamb.get(csv_d['course_code'])) |
---|
1037 | res = self.portal_catalog(portal_type = "Certificate", id = cert_id) |
---|
1038 | if not res: |
---|
1039 | em = 'No Certificate with ID %s \n' % cert_id |
---|
1040 | logger.info(em) |
---|
1041 | jamb['Error'] = "No Certificate %s" % cert_id |
---|
1042 | no_import.append( format % jamb) |
---|
1043 | continue |
---|
1044 | cert_brain = res[0] |
---|
1045 | catalog_entry = {} |
---|
1046 | student = getattr(self,sid) |
---|
1047 | # |
---|
1048 | # Study Course |
---|
1049 | # |
---|
1050 | study_course = student.study_course |
---|
1051 | dsc = {} |
---|
1052 | cert_pl = cert_brain.getPath().split('/') |
---|
1053 | catalog_entry['id'] = sid |
---|
1054 | catalog_entry['faculty'] = cert_pl[-4] |
---|
1055 | catalog_entry['department'] = cert_pl[-3] |
---|
1056 | catalog_entry['course'] = cert_id |
---|
1057 | dsc['study_course'] = cert_id |
---|
1058 | study_course.getContent().edit(mapping=dsc) |
---|
1059 | self.students_catalog.modifyRecord(**catalog_entry) |
---|
1060 | if tr_count > 10: |
---|
1061 | if len(no_import) > 1: |
---|
1062 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write( |
---|
1063 | '\n'.join(no_import)) |
---|
1064 | no_import = [] |
---|
1065 | em = '%d transactions commited\n' % tr_count |
---|
1066 | transaction.commit() |
---|
1067 | logger.info(em) |
---|
1068 | total += tr_count |
---|
1069 | tr_count = 0 |
---|
1070 | tr_count += 1 |
---|
1071 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
1072 | ###) |
---|
1073 | |
---|
1074 | security.declareProtected(View,"fixOwnership") ###( |
---|
1075 | def fixOwnership(self): |
---|
1076 | """fix Ownership""" |
---|
1077 | for s in self.portal_catalog(meta_type = 'Student'): |
---|
1078 | student = s.getObject() |
---|
1079 | sid = s.getId |
---|
1080 | import pdb;pdb.set_trace() |
---|
1081 | student.application.manage_setLocalRoles(sid, ['Owner',]) |
---|
1082 | student.personal.manage_setLocalRoles(sid, ['Owner',]) |
---|
1083 | ###) |
---|
1084 | |
---|
1085 | security.declareProtected(View,"Title") ###( |
---|
1086 | def Title(self): |
---|
1087 | """compose title""" |
---|
1088 | return "Student Section" |
---|
1089 | ###) |
---|
1090 | |
---|
1091 | def generateStudentId(self,letter,students = None): ###( |
---|
1092 | import random |
---|
1093 | r = random |
---|
1094 | if students is None: |
---|
1095 | students = self.portal_url.getPortalObject().campus.students |
---|
1096 | if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
1097 | letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY') |
---|
1098 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
1099 | while hasattr(students, sid): |
---|
1100 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
1101 | return sid |
---|
1102 | #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000)) |
---|
1103 | ###) |
---|
1104 | |
---|
1105 | InitializeClass(StudentsFolder) |
---|
1106 | |
---|
1107 | def addStudentsFolder(container, id, REQUEST=None, **kw): ###( |
---|
1108 | """Add a Student.""" |
---|
1109 | ob = StudentsFolder(id, **kw) |
---|
1110 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1111 | ###) |
---|
1112 | |
---|
1113 | ###) |
---|
1114 | |
---|
1115 | class Student(CPSDocument): ###( |
---|
1116 | """ |
---|
1117 | WAeUP Student container for the various student data |
---|
1118 | """ |
---|
1119 | meta_type = 'Student' |
---|
1120 | portal_type = meta_type |
---|
1121 | security = ClassSecurityInfo() |
---|
1122 | |
---|
1123 | security.declareProtected(View,"Title") |
---|
1124 | def Title(self): |
---|
1125 | """compose title""" |
---|
1126 | reg_nr = self.getId()[1:] |
---|
1127 | data = getattr(self,'personal',None) |
---|
1128 | if data: |
---|
1129 | content = data.getContent() |
---|
1130 | return "%s %s %s" % (content.firstname,content.middlename,content.lastname) |
---|
1131 | data = getattr(self,'application',None) |
---|
1132 | if data: |
---|
1133 | content = data.getContent() |
---|
1134 | return "%s" % (content.jamb_lastname) |
---|
1135 | return self.title |
---|
1136 | |
---|
1137 | security.declarePrivate('makeStudentMember') ###( |
---|
1138 | def makeStudentMember(self,sid,password='uNsEt'): |
---|
1139 | """make the student a member""" |
---|
1140 | membership = self.portal_membership |
---|
1141 | membership.addMember(sid, |
---|
1142 | password , |
---|
1143 | roles=('Member', |
---|
1144 | 'Student', |
---|
1145 | ), |
---|
1146 | domains='', |
---|
1147 | properties = {'memberareaCreationFlag': False, |
---|
1148 | 'homeless': True},) |
---|
1149 | member = membership.getMemberById(sid) |
---|
1150 | self.portal_registration.afterAdd(member, sid, password, None) |
---|
1151 | self.manage_setLocalRoles(sid, ['Owner',]) |
---|
1152 | |
---|
1153 | ###) |
---|
1154 | |
---|
1155 | security.declareProtected(View,'createSubObjects') ###( |
---|
1156 | def createSubObjects(self): |
---|
1157 | """make the student a member""" |
---|
1158 | dp = {'Title': 'Personal Data'} |
---|
1159 | app_doc = self.application.getContent() |
---|
1160 | names = app_doc.jamb_lastname.split() |
---|
1161 | if len(names) == 3: |
---|
1162 | dp['firstname'] = names[0].capitalize() |
---|
1163 | dp['middlename'] = names[1].capitalize() |
---|
1164 | dp['lastname'] = names[2].capitalize() |
---|
1165 | elif len(names) == 2: |
---|
1166 | dp['firstname'] = names[0].capitalize() |
---|
1167 | dp['lastname'] = names[1].capitalize() |
---|
1168 | else: |
---|
1169 | dp['lastname'] = app_doc.jamb_lastname |
---|
1170 | dp['sex'] = app_doc.jamb_sex == 'F' |
---|
1171 | dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga ) |
---|
1172 | proxy = self.aq_parent |
---|
1173 | proxy.invokeFactory('StudentPersonal','personal') |
---|
1174 | per = proxy.personal |
---|
1175 | per_doc = per.getContent() |
---|
1176 | per_doc.edit(mapping = dp) |
---|
1177 | per.manage_setLocalRoles(proxy.getId(), ['Owner',]) |
---|
1178 | #self.portal_workflow.doActionFor(per,'open',dest_container=per) |
---|
1179 | |
---|
1180 | ###) |
---|
1181 | |
---|
1182 | InitializeClass(Student) |
---|
1183 | |
---|
1184 | def addStudent(container, id, REQUEST=None, **kw): |
---|
1185 | """Add a Student.""" |
---|
1186 | ob = Student(id, **kw) |
---|
1187 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1188 | |
---|
1189 | ###) |
---|
1190 | |
---|
1191 | class StudentAccommodation(CPSDocument): ###( |
---|
1192 | """ |
---|
1193 | WAeUP Student container for the various student data |
---|
1194 | """ |
---|
1195 | meta_type = 'StudentAccommodation' |
---|
1196 | portal_type = meta_type |
---|
1197 | security = ClassSecurityInfo() |
---|
1198 | |
---|
1199 | security.declareProtected(View,"Title") |
---|
1200 | def Title(self): |
---|
1201 | """compose title""" |
---|
1202 | content = self.getContent() |
---|
1203 | #return "Accommodation Data for %s %s" % (content.firstname,content.lastname) |
---|
1204 | return "Accommodation Data for Session %s" % content.session |
---|
1205 | |
---|
1206 | |
---|
1207 | InitializeClass(StudentAccommodation) |
---|
1208 | |
---|
1209 | def addStudentAccommodation(container, id, REQUEST=None, **kw): |
---|
1210 | """Add a Students personal data.""" |
---|
1211 | ob = StudentAccommodation(id, **kw) |
---|
1212 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1213 | |
---|
1214 | ###) |
---|
1215 | |
---|
1216 | class StudentPersonal(CPSDocument): ###( |
---|
1217 | """ |
---|
1218 | WAeUP Student container for the various student data |
---|
1219 | """ |
---|
1220 | meta_type = 'StudentPersonal' |
---|
1221 | portal_type = meta_type |
---|
1222 | security = ClassSecurityInfo() |
---|
1223 | |
---|
1224 | security.declareProtected(View,"Title") |
---|
1225 | def Title(self): |
---|
1226 | """compose title""" |
---|
1227 | content = self.getContent() |
---|
1228 | #return "Personal Data for %s %s" % (content.firstname,content.lastname) |
---|
1229 | return "Personal Data" |
---|
1230 | |
---|
1231 | |
---|
1232 | InitializeClass(StudentPersonal) |
---|
1233 | |
---|
1234 | def addStudentPersonal(container, id, REQUEST=None, **kw): |
---|
1235 | """Add a Students personal data.""" |
---|
1236 | ob = StudentPersonal(id, **kw) |
---|
1237 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1238 | |
---|
1239 | ###) |
---|
1240 | |
---|
1241 | class StudentClearance(CPSDocument): ###( |
---|
1242 | """ |
---|
1243 | WAeUP Student container for the various student data |
---|
1244 | """ |
---|
1245 | meta_type = 'StudentClearance' |
---|
1246 | portal_type = meta_type |
---|
1247 | security = ClassSecurityInfo() |
---|
1248 | |
---|
1249 | security.declareProtected(View,"Title") |
---|
1250 | def Title(self): |
---|
1251 | """compose title""" |
---|
1252 | content = self.getContent() |
---|
1253 | #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname) |
---|
1254 | return "Clearance/Eligibility Record" |
---|
1255 | |
---|
1256 | |
---|
1257 | InitializeClass(StudentClearance) |
---|
1258 | |
---|
1259 | def addStudentClearance(container, id, REQUEST=None, **kw): |
---|
1260 | """Add a Students personal data.""" |
---|
1261 | ob = StudentClearance(id, **kw) |
---|
1262 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1263 | |
---|
1264 | ###) |
---|
1265 | |
---|
1266 | class StudentStudyLevel(CPSDocument): ###( |
---|
1267 | """ |
---|
1268 | WAeUP Student container for the various student data |
---|
1269 | """ |
---|
1270 | meta_type = 'StudentStudyLevel' |
---|
1271 | portal_type = meta_type |
---|
1272 | security = ClassSecurityInfo() |
---|
1273 | |
---|
1274 | security.declareProtected(View,"Title") |
---|
1275 | def Title(self): |
---|
1276 | """compose title""" |
---|
1277 | return "Level %s" % self.aq_parent.getId() |
---|
1278 | |
---|
1279 | def create_course_results(self,cert_id,current_level): ###( |
---|
1280 | "create all courses in a level" |
---|
1281 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
1282 | res = self.portal_catalog(portal_type="Certificate", id = cert_id) |
---|
1283 | l = [] |
---|
1284 | import transaction |
---|
1285 | if res: |
---|
1286 | cert = res[0] |
---|
1287 | path = cert.getPath() |
---|
1288 | query = Eq("path","%s/%s" % (path,current_level)) &\ |
---|
1289 | Eq('portal_type','CertificateCourse') |
---|
1290 | courses = aq_portal(query) |
---|
1291 | #from pdb import set_trace;set_trace() |
---|
1292 | self_proxy = self.aq_parent |
---|
1293 | for c in courses: |
---|
1294 | d = self.getCourseInfo(c.getId) |
---|
1295 | cr_id = self_proxy.invokeFactory('StudentCourseResult',c.getId) |
---|
1296 | course_result = getattr(self_proxy,cr_id) |
---|
1297 | self.portal_workflow.doActionFor(course_result,'open') |
---|
1298 | d['core_or_elective'] = getattr(c.getObject().getContent(),'core_or_elective') |
---|
1299 | course_result.getContent().edit(mapping=d) |
---|
1300 | #transaction.commit() |
---|
1301 | ###) |
---|
1302 | |
---|
1303 | InitializeClass(StudentStudyLevel) |
---|
1304 | |
---|
1305 | def addStudentStudyLevel(container, id, REQUEST=None, **kw): |
---|
1306 | """Add a Students personal data.""" |
---|
1307 | ob = StudentStudyLevel(id, **kw) |
---|
1308 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1309 | |
---|
1310 | ###) |
---|
1311 | |
---|
1312 | class StudentStudyCourse(CPSDocument): ###( |
---|
1313 | """ |
---|
1314 | WAeUP Student container for the various student data |
---|
1315 | """ |
---|
1316 | meta_type = 'StudentStudyCourse' |
---|
1317 | portal_type = meta_type |
---|
1318 | security = ClassSecurityInfo() |
---|
1319 | |
---|
1320 | security.declareProtected(View,"Title") |
---|
1321 | def Title(self): |
---|
1322 | """compose title""" |
---|
1323 | content = self.getContent() |
---|
1324 | return "Study Course" |
---|
1325 | |
---|
1326 | |
---|
1327 | InitializeClass(StudentStudyCourse) |
---|
1328 | |
---|
1329 | def addStudentStudyCourse(container, id, REQUEST=None, **kw): |
---|
1330 | """Add a Students personal data.""" |
---|
1331 | ob = StudentStudyCourse(id, **kw) |
---|
1332 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1333 | |
---|
1334 | ###) |
---|
1335 | |
---|
1336 | class StudentApplication(CPSDocument): ###( |
---|
1337 | """ |
---|
1338 | WAeUP Student container for the various student data |
---|
1339 | """ |
---|
1340 | meta_type = 'StudentApplication' |
---|
1341 | portal_type = meta_type |
---|
1342 | security = ClassSecurityInfo() |
---|
1343 | |
---|
1344 | security.declareProtected(View,"Title") |
---|
1345 | def Title(self): |
---|
1346 | """compose title""" |
---|
1347 | return "Application Data" |
---|
1348 | |
---|
1349 | |
---|
1350 | InitializeClass(StudentApplication) |
---|
1351 | |
---|
1352 | def addStudentApplication(container, id, REQUEST=None, **kw): |
---|
1353 | """Add a Students eligibility data.""" |
---|
1354 | ob = StudentApplication(id, **kw) |
---|
1355 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1356 | ###) |
---|
1357 | |
---|
1358 | class StudentPume(CPSDocument): ###( |
---|
1359 | """ |
---|
1360 | WAeUP Student container for the various student data |
---|
1361 | """ |
---|
1362 | meta_type = 'StudentPume' |
---|
1363 | portal_type = meta_type |
---|
1364 | security = ClassSecurityInfo() |
---|
1365 | |
---|
1366 | security.declareProtected(View,"Title") |
---|
1367 | def Title(self): |
---|
1368 | """compose title""" |
---|
1369 | return "PUME Results" |
---|
1370 | |
---|
1371 | |
---|
1372 | InitializeClass(StudentPume) |
---|
1373 | |
---|
1374 | def addStudentPume(container, id, REQUEST=None, **kw): |
---|
1375 | """Add a Students PUME data.""" |
---|
1376 | ob = StudentPume(id, **kw) |
---|
1377 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1378 | ###) |
---|
1379 | |
---|
1380 | ##class StudentSemester(CPSDocument): ###( |
---|
1381 | ## """ |
---|
1382 | ## WAeUP StudentSemester containing the courses and students |
---|
1383 | ## """ |
---|
1384 | ## meta_type = 'StudentSemester' |
---|
1385 | ## portal_type = meta_type |
---|
1386 | ## security = ClassSecurityInfo() |
---|
1387 | ## |
---|
1388 | ##InitializeClass(StudentSemester) |
---|
1389 | ## |
---|
1390 | ##def addStudentSemester(container, id, REQUEST=None, **kw): |
---|
1391 | ## """Add a StudentSemester.""" |
---|
1392 | ## ob = StudentSemester(id, **kw) |
---|
1393 | ## return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1394 | ## |
---|
1395 | #####) |
---|
1396 | |
---|
1397 | ##class Semester(CPSDocument): ###( |
---|
1398 | ## """ |
---|
1399 | ## WAeUP Semester containing the courses and students |
---|
1400 | ## """ |
---|
1401 | ## meta_type = 'Semester' |
---|
1402 | ## portal_type = meta_type |
---|
1403 | ## security = ClassSecurityInfo() |
---|
1404 | ## |
---|
1405 | ##InitializeClass(Semester) |
---|
1406 | ## |
---|
1407 | ##def addSemester(container, id, REQUEST=None, **kw): |
---|
1408 | ## """Add a Semester.""" |
---|
1409 | ## ob = Semester(id, **kw) |
---|
1410 | ## return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1411 | ## |
---|
1412 | #####) |
---|
1413 | |
---|
1414 | class StudentCourseResult(CPSDocument): ###( |
---|
1415 | """ |
---|
1416 | WAeUP StudentCourseResult |
---|
1417 | """ |
---|
1418 | meta_type = 'StudentCourseResult' |
---|
1419 | portal_type = meta_type |
---|
1420 | security = ClassSecurityInfo() |
---|
1421 | |
---|
1422 | def getCourseEntry(self,cid): |
---|
1423 | res = self.portal_catalog({'meta_type': "Course", |
---|
1424 | 'id': cid}) |
---|
1425 | if res: |
---|
1426 | return res[-1] |
---|
1427 | else: |
---|
1428 | return None |
---|
1429 | |
---|
1430 | security.declareProtected(View,"Title") |
---|
1431 | def Title(self): |
---|
1432 | """compose title""" |
---|
1433 | cid = self.aq_parent.getId() |
---|
1434 | ce = self.getCourseEntry(cid) |
---|
1435 | if ce: |
---|
1436 | return "%s" % ce.Title |
---|
1437 | return "No course with id %s" % cid |
---|
1438 | |
---|
1439 | InitializeClass(StudentCourseResult) |
---|
1440 | |
---|
1441 | def addStudentCourseResult(container, id, REQUEST=None, **kw): |
---|
1442 | """Add a StudentCourseResult.""" |
---|
1443 | ob = StudentCourseResult(id, **kw) |
---|
1444 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
1445 | ###) |
---|
1446 | |
---|
1447 | # Backward Compatibility StudyLevel |
---|
1448 | |
---|
1449 | from Products.WAeUP_SRP.Academics import StudyLevel |
---|
1450 | |
---|
1451 | from Products.WAeUP_SRP.Academics import addStudyLevel |
---|
1452 | |
---|