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