1 | security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
|
---|
2 | def loadFullTimeStudentsFromCSV(self):
|
---|
3 | """load Fulltime Studentdata from CSV values"""
|
---|
4 | import transaction
|
---|
5 | import random
|
---|
6 | tr_count = 0
|
---|
7 | name = 'short_full_time'
|
---|
8 | no_import = False
|
---|
9 | if not no_import:
|
---|
10 | no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
|
---|
11 | no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
|
---|
12 | logger = logging.getLogger('Import.%s' % name)
|
---|
13 | logger.info('Start loading from %s.csv' % name)
|
---|
14 | pwlist = []
|
---|
15 | pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
|
---|
16 | pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
|
---|
17 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
18 | try:
|
---|
19 | students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
20 | except:
|
---|
21 | logger.error('Error reading %s.csv' % name)
|
---|
22 | return
|
---|
23 | l = self.portal_catalog({'meta_type': "StudentClearance",})
|
---|
24 | matrics = []
|
---|
25 | for s in l:
|
---|
26 | matrics.append(s.getObject().getContent().matric_no)
|
---|
27 | print matrics
|
---|
28 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
29 | certs = {}
|
---|
30 | for c in l:
|
---|
31 | ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
|
---|
32 | cid = "%(dep_id)s_%(certcode)s" % vars()
|
---|
33 | certs[cid] = c.getObject()
|
---|
34 | for student in students:
|
---|
35 | logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
|
---|
36 | sid = student.get('MatricNo')
|
---|
37 | if sid == "":
|
---|
38 | em = 'Empty MatricNo\n'
|
---|
39 | logger.info(em)
|
---|
40 | no_import.write(em)
|
---|
41 | no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
|
---|
42 | continue
|
---|
43 | certcode = makeCertificateCode(student.get('CourseMajor'))
|
---|
44 | dep_id = student.get('Dept')
|
---|
45 | fac_id = student.get('Faculty')
|
---|
46 | cid = "%(dep_id)s_%(certcode)s" % vars()
|
---|
47 | if cid not in certs.keys():
|
---|
48 | em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
|
---|
49 | logger.info(em)
|
---|
50 | no_import.write(em)
|
---|
51 | no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
|
---|
52 | continue
|
---|
53 | certificate_doc = certs[cid].getContent()
|
---|
54 | level = student.get('StudentLevel')
|
---|
55 | try:
|
---|
56 | int(level)
|
---|
57 | except:
|
---|
58 | em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
|
---|
59 | logger.info(em)
|
---|
60 | no_import.write(em)
|
---|
61 | no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
|
---|
62 | continue
|
---|
63 | matric_no = student.get('MatricNo')
|
---|
64 | if matric_no not in matrics:
|
---|
65 | matrics.append(matric_no)
|
---|
66 | sid = self.generateStudentId(student.get('Lastname')[0])
|
---|
67 | #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
|
---|
68 | students_folder.invokeFactory('Student', sid)
|
---|
69 | logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
|
---|
70 | student_obj = getattr(self,sid)
|
---|
71 | access_code = "%d" % random.randint(1000000000,9999999999)
|
---|
72 | student_obj.getContent().makeStudentMember(sid,access_code,)
|
---|
73 | pwl_dict = {'student_id': sid,'access_code':access_code}
|
---|
74 | student_obj.invokeFactory('StudentApplication','application')
|
---|
75 | application = student_obj.application
|
---|
76 | da = {'Title': 'Application Data'}
|
---|
77 | student_obj.invokeFactory('StudentPersonal','personal')
|
---|
78 | da['jamb_reg_no'] = student.get('EntryRegNo')
|
---|
79 | personal = student_obj.personal
|
---|
80 | dp = {'Title': 'Personal Data'}
|
---|
81 | student_obj.invokeFactory('StudentClearance','clearance')
|
---|
82 | clearance = student_obj.clearance
|
---|
83 | dc = {'Title': 'Clearance/Eligibility Record'}
|
---|
84 | dc['matric_no'] = matric_no
|
---|
85 | state = student.get('State')
|
---|
86 | lga = student.get('LGA')
|
---|
87 | if state and lga:
|
---|
88 | lga = state + ' / ' + lga
|
---|
89 | else:
|
---|
90 | lga = "None"
|
---|
91 | dc['lga'] = lga
|
---|
92 | dc['nationality'] = student.get('Nationality')
|
---|
93 | dc['email'] = student.get('Emailaddress')
|
---|
94 | dp['firstname'] = student.get('FirstName')
|
---|
95 | dp['middlename'] = student.get('MiddleName')
|
---|
96 | dp['lastname'] = student.get('Lastname')
|
---|
97 | dp['former_surname'] = student.get('FormerSurname')
|
---|
98 | dp['sex'] = student.get('Sex') == 'F'
|
---|
99 | dp['perm_address'] = student.get('PermanentAddress')
|
---|
100 | dp['perm_city'] = student.get('PermanentAddressCity')
|
---|
101 | dp['campus_address'] = student.get('CampusAddress')
|
---|
102 | dp['phone'] = student.get('PhoneNumber')
|
---|
103 | application.getContent().edit(mapping=da)
|
---|
104 | personal.getContent().edit(mapping=dp)
|
---|
105 | clearance.getContent().edit(mapping=dc)
|
---|
106 | #
|
---|
107 | # Study Course
|
---|
108 | #
|
---|
109 | student_obj.invokeFactory('StudentStudyCourse','study_course')
|
---|
110 | studycourse = student_obj.study_course
|
---|
111 | dsc = {}
|
---|
112 | from_certificate = ['title',
|
---|
113 | 'max_elect',
|
---|
114 | 'max_pass',
|
---|
115 | 'n_core',
|
---|
116 | 'nr_years',
|
---|
117 | 'probation_credits',
|
---|
118 | 'promotion_credits',
|
---|
119 | 'start_level',
|
---|
120 | ]
|
---|
121 | for f in from_certificate:
|
---|
122 | dsc[f] = getattr(certificate_doc,f)
|
---|
123 | dsc['faculty'] = fac_id
|
---|
124 | dsc['department'] = dep_id
|
---|
125 | dsc['study_course'] = certcode
|
---|
126 | css = student.get('CurrentSession') or '2004-2005'
|
---|
127 | cs = int(css.split('-')[0]) - 2000
|
---|
128 | cl = int(student.get('StudentLevel') or '100')/100
|
---|
129 | dsc['entry_session'] = "200%s" % (cs - cl)
|
---|
130 | dsc['clr_ac_pin'] = access_code
|
---|
131 | studycourse.getContent().edit(mapping=dsc)
|
---|
132 | #
|
---|
133 | # Level
|
---|
134 | #
|
---|
135 | ## l = getattr(studycourse,level,None)
|
---|
136 | ## if 0 and l is None:
|
---|
137 | ## #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
|
---|
138 | ## logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
|
---|
139 | ## studycourse.invokeFactory('StudentStudyLevel', level)
|
---|
140 | ## l = getattr(studycourse, level)
|
---|
141 | ## certificate = certs[certcode]
|
---|
142 | ## cert_level = getattr(certificate,level,None)
|
---|
143 | ## if cert_level is None:
|
---|
144 | ## logger.info('Level %(level)s not in %(certcode)s' % vars())
|
---|
145 | ## l.getContent().edit(mapping={'Title': "Level %s" % level})
|
---|
146 | else:
|
---|
147 | em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
|
---|
148 | logger.info(em)
|
---|
149 | no_import.write(em)
|
---|
150 | no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
|
---|
151 | continue
|
---|
152 | if tr_count > MAX_TRANS:
|
---|
153 | transaction.commit()
|
---|
154 | em = 'Transaction commited\n' % student
|
---|
155 | logger.info(em)
|
---|
156 | tr_count = 0
|
---|
157 | tr_count += 1
|
---|
158 | pwl_dict.update(dc)
|
---|
159 | pwl_dict.update(da)
|
---|
160 | pwl_dict.update(dp)
|
---|
161 | wftool = self.portal_workflow
|
---|
162 | pwlist.append(pwl_template.substitute(pwl_dict))
|
---|
163 | wftool.doActionFor(student_obj,'clear_and_validate')
|
---|
164 | student_obj.manage_setLocalRoles(sid, ['Owner',])
|
---|
165 | wftool.doActionFor(application,'close')
|
---|
166 | application.manage_setLocalRoles(sid, ['Owner',])
|
---|
167 | wftool.doActionFor(clearance,'close')
|
---|
168 | clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
169 | wftool.doActionFor(personal,'close')
|
---|
170 | personal.manage_setLocalRoles(sid, ['Owner',])
|
---|
171 | wftool.doActionFor(studycourse,'close_for_edit')
|
---|
172 | studycourse.manage_setLocalRoles(sid, ['Owner',])
|
---|
173 | open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
|
---|
174 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
175 | ###)
|
---|
176 |
|
---|
177 | security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
|
---|
178 | def loadPumeResultsFromCSV(self):
|
---|
179 | """load Fulltime Studentdata from CSV values into pumeresults catalog"""
|
---|
180 | import transaction
|
---|
181 | import random
|
---|
182 | ## csv_d = {'jamb_reg_no': "RegNumber", ###(
|
---|
183 | ## 'status': "Admission Status",
|
---|
184 | ## 'name': "Name",
|
---|
185 | ## 'score': "Score",
|
---|
186 | ## 'sex': "Sex",
|
---|
187 | ## 'faculty': "Faculty",
|
---|
188 | ## 'department': "Dept",
|
---|
189 | ## 'course': "Course",
|
---|
190 | ## 'course_code_org': "Course Code",
|
---|
191 | ## }
|
---|
192 | ###)
|
---|
193 | csv_d = {'jamb_reg_no': "JAMBRegno",
|
---|
194 | 'name': "Name",
|
---|
195 | 'score': "Score",
|
---|
196 | 'sex': "Sex",
|
---|
197 | 'course': "Course",
|
---|
198 | 'faculty': "Faculty",
|
---|
199 | 'department': "Dept",
|
---|
200 | 'course_code_org': "Course Code",
|
---|
201 | 'status': "Admission Status",
|
---|
202 | 'result_type': None,
|
---|
203 | }
|
---|
204 | csv_fields = [f[1] for f in csv_d.items() if f[1]]
|
---|
205 | tr_count = 0
|
---|
206 | total = 0
|
---|
207 | #name = 'pup_new'
|
---|
208 | name = 'pup_update'
|
---|
209 | update = name.endswith('update')
|
---|
210 | no_import = []
|
---|
211 | ok_import = []
|
---|
212 | ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()]))
|
---|
213 | no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields]))
|
---|
214 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
215 | ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current)
|
---|
216 | #open(ok_import_name,"w").write('\n'.join(no_import))
|
---|
217 | no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
|
---|
218 | #open(no_import_name,"w").write('\n'.join(no_import))
|
---|
219 | logger = logging.getLogger('Import.%s' % name)
|
---|
220 | starttime = DateTime.now()
|
---|
221 | logger.info('Start loading from %s.csv' % name)
|
---|
222 | try:
|
---|
223 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
224 | except:
|
---|
225 | logger.error('Error reading %s.csv' % name)
|
---|
226 | return
|
---|
227 | pume = self.portal_pumeresults
|
---|
228 | format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
229 | import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
|
---|
230 | eduplicate = '%s,"duplicate"' % format
|
---|
231 | einvalidjamb = '%s,"invalid JambRegNo"' % format
|
---|
232 | added = 'added ,%s' % format
|
---|
233 | #from pdb import set_trace;set_trace()
|
---|
234 | for jamb in result:
|
---|
235 | dict = {}
|
---|
236 | for f,fn in csv_d.items():
|
---|
237 | dict[f] = jamb.get(csv_d[f])
|
---|
238 | dict['result_type'] = 'DE'
|
---|
239 | jnr = jamb.get(csv_d['jamb_reg_no'])
|
---|
240 | if not checkJambNo(jnr):
|
---|
241 | logger.info(einvalidjamb % jamb)
|
---|
242 | dd = {}
|
---|
243 | for f,fn in csv_d.items():
|
---|
244 | dd[fn] = getattr(data,f)
|
---|
245 | no_import.append(eduplicate % dd)
|
---|
246 | no_import.append(eduplicate % jamb)
|
---|
247 | continue
|
---|
248 | res = pume(jamb_reg_no=jnr)
|
---|
249 | if len(res) > 0:
|
---|
250 | if update:
|
---|
251 | try:
|
---|
252 | pume.modifyRecord(**dict)
|
---|
253 | except ValueError:
|
---|
254 | logger.info(eduplicate % jamb)
|
---|
255 | continue
|
---|
256 | except KeyError:
|
---|
257 | pume.addRecord(**dict)
|
---|
258 | logger.info(added % jamb)
|
---|
259 | continue
|
---|
260 | else:
|
---|
261 | data = res[0]
|
---|
262 | if data.name != jamb.get(csv_d['name']):
|
---|
263 | #set_trace()
|
---|
264 | logger.info(eduplicate % jamb)
|
---|
265 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
|
---|
266 | #logger.info(em)
|
---|
267 | dd = {}
|
---|
268 | for f,fn in csv_d.items():
|
---|
269 | dd[fn] = getattr(data,f)
|
---|
270 | no_import.append(eduplicate % dd)
|
---|
271 | no_import.append(eduplicate % jamb)
|
---|
272 | continue
|
---|
273 | try:
|
---|
274 | pume.addRecord(**dict)
|
---|
275 | ok_import.append(import_format % dict)
|
---|
276 | except ValueError:
|
---|
277 | logger.info(eduplicate % jamb)
|
---|
278 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
|
---|
279 | #logger.info(em)
|
---|
280 | no_import.append(eduplicate % jamb)
|
---|
281 | logger.info('End loading from %s.csv' % name)
|
---|
282 | if len(no_import) > 1:
|
---|
283 | open(no_import_name,"w+").write('\n'.join(no_import))
|
---|
284 | open(ok_import_name,"w+").write('\n'.join(ok_import))
|
---|
285 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
286 | ###)
|
---|
287 |
|
---|
288 |
|
---|
289 | security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
|
---|
290 | def OLDloadPumeResultsFromCSV(self):
|
---|
291 | """load Fulltime Studentdata from CSV values"""
|
---|
292 | import transaction
|
---|
293 | import random
|
---|
294 | wftool = self.portal_workflow
|
---|
295 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
296 | csv_d = {'jamb_reg_no': "JAMBRegno",
|
---|
297 | 'jamb_lastname': "Name",
|
---|
298 | 'pume_options': "Options",
|
---|
299 | 'session': "Session",
|
---|
300 | 'days': "Days",
|
---|
301 | 'response': "Responce",
|
---|
302 | 'wrong': "Wrong",
|
---|
303 | 'pume_eng_score': "EngScore",
|
---|
304 | 'pume_gen_score': "GenScore",
|
---|
305 | 'pume_tot_score': "Score",
|
---|
306 | 'batch': "Batch",
|
---|
307 | 'serial': "SerialNo",
|
---|
308 | 'jamb_score': "JambScore",
|
---|
309 | 'omitted':"Omitted",
|
---|
310 | 'search_key': "SearchKey",
|
---|
311 | 'jamb_sex': "Sex",
|
---|
312 | 'fac1': "Fac1",
|
---|
313 | 'fac2': "Fac2",
|
---|
314 | 'jamb_first_cos': "CourseofStudy",
|
---|
315 | 'stud_status':"StudStatus",
|
---|
316 | 'registered': "Registered",
|
---|
317 | 'jamb_state': "State",
|
---|
318 | 'eng_fail': "EngFail",
|
---|
319 | 'gen_fail': "GenFail",
|
---|
320 | 'un_ans_eng': "UnAnsEng",
|
---|
321 | 'un_ans_eng': "UnAnsGen",
|
---|
322 | 'total_ans': "TotalUnAns",
|
---|
323 | 'dept': "Dept",
|
---|
324 | 'jamb_second_cos': "Course2",
|
---|
325 | 'jamb_third_cos': "course3",
|
---|
326 | }
|
---|
327 | csv_fields = [f[1] for f in csv_d.items()]
|
---|
328 | tr_count = 0
|
---|
329 | name = 'pume_results'
|
---|
330 | no_import = []
|
---|
331 | s = ','.join(['"%s"' % fn for fn in csv_fields])
|
---|
332 | no_import.append('%s\n' % s)
|
---|
333 | logger = logging.getLogger('Import.%s' % name)
|
---|
334 | logger.info('Start loading from %s.csv' % name)
|
---|
335 | try:
|
---|
336 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
337 | except:
|
---|
338 | logger.error('Error reading %s.csv' % name)
|
---|
339 | return
|
---|
340 | for jamb in result:
|
---|
341 | format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
342 | processing = "processing %s" % format
|
---|
343 | logger.info(processing % jamb)
|
---|
344 | jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
|
---|
345 | #import pdb;pdb.set_trace()
|
---|
346 | res = self.portal_catalog({'portal_type': "StudentApplication",
|
---|
347 | 'jamb_reg_no': jamb_reg_no })
|
---|
348 | if res:
|
---|
349 | em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
|
---|
350 | logger.info(em)
|
---|
351 | no_import.append(em)
|
---|
352 | no_import.append(format % jamb)
|
---|
353 | continue
|
---|
354 | cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
|
---|
355 | res = self.portal_catalog({'portal_type': "Certificate",
|
---|
356 | 'id': cert_id })
|
---|
357 | if len(res) < 1:
|
---|
358 | em = 'No Certificate with ID %s \n' % cert_id
|
---|
359 | logger.info(em)
|
---|
360 | no_import.append(em)
|
---|
361 | no_import.append(format % jamb)
|
---|
362 | continue
|
---|
363 | cert = res[0].getObject()
|
---|
364 | cert_path = res[0].getPath()
|
---|
365 | cert_doc = cert.getContent()
|
---|
366 | jamb_name = jamb.get(csv_d['jamb_lastname'])
|
---|
367 | jamb_name.replace('>','')
|
---|
368 | names = jamb_name.split()
|
---|
369 | letter = names[-1][0].upper()
|
---|
370 | sid = self.generateStudentId(letter)
|
---|
371 | not_created = True
|
---|
372 | while not_created:
|
---|
373 | try:
|
---|
374 | students_folder.invokeFactory('Student', sid)
|
---|
375 | not_created = False
|
---|
376 | except BadRequest:
|
---|
377 | sid = self.generateStudentId(letter)
|
---|
378 | logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
|
---|
379 | student = getattr(self,sid)
|
---|
380 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
381 | student.invokeFactory('StudentClearance','clearance')
|
---|
382 | #wftool.doActionFor(student.clearance,'open')
|
---|
383 | dp = {'Title': 'Clearance/Eligibility Record'}
|
---|
384 | student.clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
385 | student.invokeFactory('StudentPume','pume')
|
---|
386 | dp = {'Title': 'Pume Data'}
|
---|
387 | student.invokeFactory('StudentApplication','application')
|
---|
388 | da = {'Title': 'Application Data'}
|
---|
389 | da["jamb_lastname"] = jamb_name
|
---|
390 | da_fields = ('jamb_reg_no',
|
---|
391 | 'jamb_sex',
|
---|
392 | 'jamb_state',
|
---|
393 | 'jamb_score',
|
---|
394 | 'jamb_first_cos',
|
---|
395 | 'jamb_sex',
|
---|
396 | 'jamb_state',
|
---|
397 | 'jamb_first_cos',
|
---|
398 | 'jamb_second_cos',
|
---|
399 | )
|
---|
400 | for f in da_fields:
|
---|
401 | da[f] = jamb.get(csv_d[f])
|
---|
402 | app = student.application
|
---|
403 | app.getContent().edit(mapping=da)
|
---|
404 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
405 | #wftool.doActionFor(app,'close')
|
---|
406 | dp_fields = (
|
---|
407 | 'pume_eng_score',
|
---|
408 | 'pume_gen_score',
|
---|
409 | 'pume_tot_score',
|
---|
410 | )
|
---|
411 | for f in dp_fields:
|
---|
412 | dp[f] = float(jamb.get(csv_d[f]))
|
---|
413 | pume = student.pume
|
---|
414 | pume.getContent().edit(mapping=dp)
|
---|
415 | #wftool.doActionFor(pume,'close')
|
---|
416 | pume.manage_setLocalRoles(sid, ['Owner',])
|
---|
417 | #
|
---|
418 | # Study Course
|
---|
419 | #
|
---|
420 | student.invokeFactory('StudentStudyCourse','study_course')
|
---|
421 | study_course = student.study_course
|
---|
422 | dsc = {}
|
---|
423 | from_certificate = ['title',
|
---|
424 | 'max_elect',
|
---|
425 | 'max_pass',
|
---|
426 | 'n_core',
|
---|
427 | 'nr_years',
|
---|
428 | 'probation_credits',
|
---|
429 | 'promotion_credits',
|
---|
430 | 'start_level',
|
---|
431 | ]
|
---|
432 | for f in from_certificate:
|
---|
433 | dsc[f] = getattr(cert_doc,f)
|
---|
434 | cpl = cert_path.split('/')
|
---|
435 | dsc['faculty'] = cpl[-4]
|
---|
436 | dsc['department'] = cpl[-3]
|
---|
437 | dsc['study_course'] = cert_id
|
---|
438 | dsc['entry_session'] = jamb.get(csv_d['session'])
|
---|
439 | study_course.getContent().edit(mapping=dsc)
|
---|
440 | student.getContent().createSubObjects()
|
---|
441 | if dp['pume_tot_score']>49:
|
---|
442 | wftool.doActionFor(student,'pume_pass')
|
---|
443 | wftool.doActionFor(student,'admit')
|
---|
444 | else:
|
---|
445 | wftool.doActionFor(student,'pume_fail')
|
---|
446 | wftool.doActionFor(student,'reject_admission')
|
---|
447 | if len(no_import) > 1:
|
---|
448 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
|
---|
449 | '\n'.join(no_import))
|
---|
450 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
451 | ###)
|
---|
452 |
|
---|
453 | security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
|
---|
454 | def loadFullTimeStudentsResultsFromCSV(self):
|
---|
455 | """load Fulltime Studentdata from CSV values"""
|
---|
456 | #return
|
---|
457 | level_wf_actions = {}
|
---|
458 | level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
|
---|
459 | level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
|
---|
460 | level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
|
---|
461 | level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
|
---|
462 | import transaction
|
---|
463 | wftool = self.portal_workflow
|
---|
464 | tr_count = 0
|
---|
465 | name = 'short_full_time_results_2004_2005'
|
---|
466 | no_import = False
|
---|
467 | if not no_import:
|
---|
468 | no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
|
---|
469 | no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
|
---|
470 | logger = logging.getLogger('import.%s' % name)
|
---|
471 | logger.info('Start loading from %s.csv' % name)
|
---|
472 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
473 | try:
|
---|
474 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
475 | except:
|
---|
476 | logger.error('Error reading %s.csv' % name)
|
---|
477 | return
|
---|
478 | l = self.portal_catalog({'meta_type': "Course"})
|
---|
479 | courses = {}
|
---|
480 | for c in l:
|
---|
481 | courses[c.id] = c.getObject()
|
---|
482 | level_changed = False
|
---|
483 | student_changed = False
|
---|
484 | sid = ''
|
---|
485 | #import pdb;pdb.set_trace()
|
---|
486 | for result in results:
|
---|
487 | temp_sid = result.get('Matnumber')
|
---|
488 | if temp_sid != sid:
|
---|
489 | student_changed = True
|
---|
490 | res = self.portal_catalog({'meta_type': "StudentClearance",
|
---|
491 | 'SearchableText': temp_sid })
|
---|
492 | if not res:
|
---|
493 | em = 'Student with ID %(Matnumber)s not found\n' % result
|
---|
494 | logger.info(em)
|
---|
495 | no_import.write(em)
|
---|
496 | no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
|
---|
497 | continue
|
---|
498 | elif len(res) > 1:
|
---|
499 | em = 'More than one Student with ID %(Matnumber)s found\n' % result
|
---|
500 | logger.info(em)
|
---|
501 | no_import.write(em)
|
---|
502 | no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
|
---|
503 | continue
|
---|
504 | sid = temp_sid
|
---|
505 | sf = res[0].getObject().aq_parent
|
---|
506 | sc = getattr(sf,'study_course')
|
---|
507 | level = ''
|
---|
508 | else:
|
---|
509 | student_changed = False
|
---|
510 | course = result.get('CosCode')
|
---|
511 | if course not in courses.keys():
|
---|
512 | em = 'Course with ID %(CosCode)s not found\n' % result
|
---|
513 | logger.info(em)
|
---|
514 | no_import.write(em)
|
---|
515 | no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
|
---|
516 | continue
|
---|
517 | course_doc = courses[course].getContent()
|
---|
518 | temp_level = result.get('Level')
|
---|
519 | student_id = sf.getId()
|
---|
520 | result['StudentId'] = student_id
|
---|
521 | if temp_level != level:
|
---|
522 | try:
|
---|
523 | int(temp_level)
|
---|
524 | except:
|
---|
525 | em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
|
---|
526 | logger.info(em)
|
---|
527 | no_import.write(em)
|
---|
528 | no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
|
---|
529 | continue
|
---|
530 | level_changed = True
|
---|
531 | if 'dlev' in vars().keys():
|
---|
532 | wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
|
---|
533 | level = temp_level
|
---|
534 | l = getattr(sc,level,None)
|
---|
535 | if l is None:
|
---|
536 | logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
|
---|
537 | sc.invokeFactory('StudentStudyLevel', level)
|
---|
538 | l = getattr(sc, level)
|
---|
539 | l.manage_setLocalRoles(student_id, ['Owner',])
|
---|
540 | else:
|
---|
541 | level_changed = False
|
---|
542 | cr = getattr(l,course,None)
|
---|
543 | if cr is None:
|
---|
544 | logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
|
---|
545 | l.invokeFactory('StudentCourseResult',course)
|
---|
546 | cr = getattr(l,course)
|
---|
547 | dcr = {}
|
---|
548 | from_course = ['title',
|
---|
549 | 'credits',
|
---|
550 | 'passmark',
|
---|
551 | ]
|
---|
552 | for f in from_course:
|
---|
553 | dcr[f] = getattr(course_doc,f)
|
---|
554 | dlev = {}
|
---|
555 | dcr['ansbook'] = result.get('Ansbook')
|
---|
556 | dcr['semester'] = getInt(result.get('Semster'))
|
---|
557 | dcr['status'] = result.get('CosStuatus')
|
---|
558 | dcr['score'] = getInt(result.get('Score'))
|
---|
559 | dlev['session'] = result.get('Session')
|
---|
560 | dcr['carry_level'] = result.get('CarryLevel')
|
---|
561 | dcr['grade'] = result.get('Grade')
|
---|
562 | dcr['weight'] = result.get('Weight')
|
---|
563 | dlev['verdict'] = result.get('Verdict')
|
---|
564 | dcr['import_id'] = result.get('id')
|
---|
565 | gpa = result.get('GPA').replace(',','.')
|
---|
566 | dlev['imported_gpa'] = getFloat(gpa)
|
---|
567 | cr.getContent().edit(mapping = dcr)
|
---|
568 | cr.manage_setLocalRoles(student_id, ['Owner',])
|
---|
569 | l.getContent().edit(mapping = dlev)
|
---|
570 | if tr_count > MAX_TRANS:
|
---|
571 | transaction.commit()
|
---|
572 | tr_count = 0
|
---|
573 | tr_count += 1
|
---|
574 | wftool.doActionFor(cr,'close')
|
---|
575 | wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
|
---|
576 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
577 |
|
---|
578 | ###)
|
---|
579 |
|
---|
580 | security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
|
---|
581 | def loadJAMBFromCSV(self):
|
---|
582 | """load JAMB data from CSV values"""
|
---|
583 | #return
|
---|
584 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
585 | import transaction
|
---|
586 | tr_count = 0
|
---|
587 | name = 'SampleJAMBDataII'
|
---|
588 | wftool = self.portal_workflow
|
---|
589 | no_import = False
|
---|
590 | if not no_import:
|
---|
591 | no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
|
---|
592 | no_import.write('REG-NO,NAME,SEX,STATE,LGA,ENG-SCORE,SUBJ1,SUBJ1-SCORE,SUBJ2,SUBJ2-SCORE,SUBJ3,SUBJ3-SCORE,AGGREGATE,UNIV1,FACULTY1,COURSE1,UNIV2,FACULTY2,COURSE2')
|
---|
593 | logger = logging.getLogger('Import.%s' % name)
|
---|
594 | logger.info('Start loading from %s.csv' % name)
|
---|
595 | try:
|
---|
596 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
597 | except:
|
---|
598 | logger.error('Error reading %s.csv' % name)
|
---|
599 | return
|
---|
600 | for jamb in result:
|
---|
601 | logger.info('processing %(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
|
---|
602 | jamb_reg_no = jamb.get('REG-NO')
|
---|
603 | res = self.portal_catalog({'meta_type': "StudentApplication",
|
---|
604 | 'jamb_reg_no': jamb_reg_no })
|
---|
605 | if res:
|
---|
606 | em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
|
---|
607 | logger.info(em)
|
---|
608 | no_import.write(em)
|
---|
609 | no_import.write('%(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
|
---|
610 | continue
|
---|
611 | jamb_name = jamb.get("NAME")
|
---|
612 | jamb_name.replace('>','')
|
---|
613 | names = jamb_name.split()
|
---|
614 | letter = names[-1][0].upper()
|
---|
615 | sid = self.generateStudentId(letter)
|
---|
616 | not_created = True
|
---|
617 | while not_created:
|
---|
618 | try:
|
---|
619 | students_folder.invokeFactory('Student', sid)
|
---|
620 | not_created = False
|
---|
621 | except BadRequest:
|
---|
622 | sid = self.generateStudentId(letter)
|
---|
623 | logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
|
---|
624 | student = getattr(self,sid)
|
---|
625 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
626 | student.invokeFactory('StudentApplication','application')
|
---|
627 | da = {'Title': 'Application Data'}
|
---|
628 | da["jamb_reg_no"] = jamb.get("REG-NO")
|
---|
629 | da["jamb_lastname"] = jamb_name
|
---|
630 | da["jamb_sex"] = jamb.get("SEX")
|
---|
631 | da["jamb_state"] = jamb.get("STATE")
|
---|
632 | da["jamb_lga"] = jamb.get("LGA")
|
---|
633 | da["jamb_score"] = jamb.get("AGGREGATE")
|
---|
634 | da["jamb_first_cos"] = jamb.get("COURSE1")
|
---|
635 | da["jamb_second_cos"] = jamb.get("COURSE2")
|
---|
636 | da["jamb_first_uni"] = jamb.get("UNIV1")
|
---|
637 | da["jamb_second_uni"] = jamb.get("UNIV2")
|
---|
638 | app = student.application
|
---|
639 | app_doc = app.getContent()
|
---|
640 | app_doc.edit(mapping=da)
|
---|
641 | #wftool.doActionFor(app,'open',dest_container=app)
|
---|
642 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
643 | student.getContent().createSubObjects()
|
---|
644 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
645 | ###)
|
---|
646 |
|
---|
647 |
|
---|
648 | security.declareProtected(ModifyPortalContent,"importPreviousSessionStudents")###(
|
---|
649 | def importPreviousSessionStudents(self):
|
---|
650 | """load and create previous session students from CSV"""
|
---|
651 | import transaction
|
---|
652 | import random
|
---|
653 | wftool = self.portal_workflow
|
---|
654 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
655 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
656 | tr_count = 1
|
---|
657 | total = 0
|
---|
658 | #name = 'pume_results'
|
---|
659 | name = 'Previous'
|
---|
660 | keys = self.portal_schemas.import_student.keys()
|
---|
661 | keys += self.portal_schemas.import_student_level_data.keys()
|
---|
662 | not_imported = []
|
---|
663 | imported = []
|
---|
664 | certificates = {}
|
---|
665 | logger = logging.getLogger('Students.StudentsFolder.importPreviousSessionStudents')
|
---|
666 | try:
|
---|
667 | records = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
668 | except:
|
---|
669 | logger.error('Error reading %s.csv' % name)
|
---|
670 | return
|
---|
671 | start = True
|
---|
672 | for record in records:
|
---|
673 | if start:
|
---|
674 | start = False
|
---|
675 | logger.info('Start loading from %s.csv' % name)
|
---|
676 | false_keys = [k for k in record.keys() if k not in keys]
|
---|
677 | right_keys = [k for k in record.keys() if k in keys]
|
---|
678 | if false_keys:
|
---|
679 | logger.info('Fields %s not in schema' % false_keys)
|
---|
680 | s = ','.join(['"%s"' % k for k in right_keys])
|
---|
681 | imported.append(s)
|
---|
682 | not_imported.append('%s,"error"' % s)
|
---|
683 | format = ','.join(['"%%(%s)s"' % k for k in right_keys])
|
---|
684 | format_error = format + ',"%(error)s"'
|
---|
685 | study_course = makeCertificateCode(record.get('study_course'))
|
---|
686 | matric_no = record.get('matric_no')
|
---|
687 | student_res = self.students_catalog(matric_no = matric_no)
|
---|
688 | if student_res:
|
---|
689 | record['error'] = "Student exists"
|
---|
690 | not_imported.append(format_error % record)
|
---|
691 | continue
|
---|
692 | if study_course not in certificates.keys():
|
---|
693 | cert_res = self.portal_catalog(portal_type='Certificate',
|
---|
694 | id = study_course)
|
---|
695 | if not cert_res:
|
---|
696 | record['error'] = "No such studycourse"
|
---|
697 | not_imported.append(format_error % record)
|
---|
698 | continue
|
---|
699 | certificates[cert_res[0].id] = cert_res[0]
|
---|
700 | sid = self.generateStudentId('x',students_folder)
|
---|
701 | students_folder.invokeFactory('Student', sid)
|
---|
702 | #from pdb import set_trace;set_trace()
|
---|
703 | record['student_id'] = sid
|
---|
704 | student = getattr(self,sid)
|
---|
705 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
706 | student.invokeFactory('StudentApplication','application')
|
---|
707 | app = student.application
|
---|
708 | app_doc = app.getContent()
|
---|
709 | dict = {'Title': 'Application Data'}
|
---|
710 | dict["jamb_lastname"] = "%(firstname)s %(lastname)s %(middlename)s" % record
|
---|
711 | r2d = [
|
---|
712 | ('entry_mode','entry_mode'),
|
---|
713 | ('sex','jamb_sex'),
|
---|
714 | ('jamb_score','jamb_score'),
|
---|
715 | ('jamb_reg_no','jamb_reg_no'),
|
---|
716 | ]
|
---|
717 | for r,d in r2d:
|
---|
718 | dict[d] = record[r]
|
---|
719 | app_doc.edit(mapping=dict)
|
---|
720 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
721 | wftool.doActionFor(app,'close')
|
---|
722 | record['sex'] = record['sex'] == 'F'
|
---|
723 | student.invokeFactory('StudentPersonal','personal')
|
---|
724 | dict = {}
|
---|
725 | r2d = [('firstname','firstname'),
|
---|
726 | ('middlename','middlename'),
|
---|
727 | ('lastname','lastname'),
|
---|
728 | ('sex','sex'),
|
---|
729 | ('email','email'),
|
---|
730 | ('phone','phone'),
|
---|
731 | ('address','perm_address'),
|
---|
732 | ]
|
---|
733 | for r,d in r2d:
|
---|
734 | dict[d] = record[r]
|
---|
735 | per = student.personal
|
---|
736 | per_doc = per.getContent()
|
---|
737 | per_doc.edit(mapping = dict)
|
---|
738 | per.manage_setLocalRoles(sid, ['Owner',])
|
---|
739 | #
|
---|
740 | # Clearance
|
---|
741 | #
|
---|
742 | student.invokeFactory('StudentClearance','clearance')
|
---|
743 | #wftool.doActionFor(student.clearance,'open')
|
---|
744 | clearance = getattr(student,'clearance')
|
---|
745 | dict = {'Title': 'Clearance/Eligibility Record'}
|
---|
746 | clearance.getContent().edit(mapping = dict)
|
---|
747 | student.clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
748 | #
|
---|
749 | # Study Course
|
---|
750 | #
|
---|
751 | student.invokeFactory('StudentStudyCourse','study_course')
|
---|
752 | study_course = student.study_course
|
---|
753 | dict = {}
|
---|
754 | r2d = [('session','current_session'),
|
---|
755 | ('level','current_level'),
|
---|
756 | ('verdict','current_verdict'),
|
---|
757 | ('study_course','study_course'),
|
---|
758 | ]
|
---|
759 | for r,d in r2d:
|
---|
760 | dict[d] = record[r]
|
---|
761 | study_course.getContent().edit(mapping=dict)
|
---|
762 | #
|
---|
763 | # Study Level
|
---|
764 | #
|
---|
765 | level = record['level']
|
---|
766 | study_course.invokeFactory('StudentStudyLevel',level)
|
---|
767 | study_level = getattr(study_course,level)
|
---|
768 | dict = {}
|
---|
769 | r2d = [('session','session'),
|
---|
770 | ('level','code'),
|
---|
771 | ('verdict','verdict'),
|
---|
772 | ]
|
---|
773 | for r,d in r2d:
|
---|
774 | dict[d] = record[r]
|
---|
775 | study_level.getContent().edit(mapping=dict)
|
---|
776 | wftool.doActionFor(student,'return')
|
---|
777 | imported.append(format % record)
|
---|
778 | tr_count += 1
|
---|
779 | record['tr_count'] = tr_count
|
---|
780 | record['total'] = total
|
---|
781 | logger.info('%(total)s+%(tr_count)s: Creating Student %(student_id)s %(matric_no)s %(jamb_reg_no)s' % record)
|
---|
782 | if tr_count > 1000:
|
---|
783 | if len(not_imported) > 0:
|
---|
784 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
785 | '\n'.join(not_imported)+'\n')
|
---|
786 | not_imported = []
|
---|
787 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
788 | '\n'.join(imported) + '\n')
|
---|
789 | imported = []
|
---|
790 | em = '%d transactions commited total %s' % (tr_count,total)
|
---|
791 | transaction.commit()
|
---|
792 | logger.info(em)
|
---|
793 | regs = []
|
---|
794 | total += tr_count
|
---|
795 | tr_count = 0
|
---|
796 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
797 | '\n'.join(imported))
|
---|
798 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
799 | '\n'.join(not_imported))
|
---|
800 | em = '%d transactions commited total %d' % (tr_count,total)
|
---|
801 | logger.info(em)
|
---|
802 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
803 | ###)
|
---|
804 |
|
---|
805 |
|
---|
806 | security.declareProtected(ModifyPortalContent,"createDEStudents")###(
|
---|
807 | def createDEStudents(self):
|
---|
808 | """load Fulltime Studentdata from CSV values"""
|
---|
809 | import transaction
|
---|
810 | import random
|
---|
811 | #from pdb import set_trace
|
---|
812 | wftool = self.portal_workflow
|
---|
813 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
814 | csv_d = {'jamb_reg_no': "RegNumber",
|
---|
815 | 'jamb_lastname': "Name",
|
---|
816 | 'session': "Session",
|
---|
817 | 'pume_tot_score': "PUDE SCORE",
|
---|
818 | ##'jamb_score': "JambScore",
|
---|
819 | 'entry_mode': "EntryMode",
|
---|
820 | 'jamb_sex': "Sex",
|
---|
821 | 'jamb_state': "State",
|
---|
822 | 'jamb_first_cos': "AdminCourse",
|
---|
823 | 'faculty': "AdminFaculty",
|
---|
824 | 'course_code': "AdmitCoscode",
|
---|
825 | 'stud_status':"AdmitStatus",
|
---|
826 | 'department': "AdmitDept",
|
---|
827 | 'jamb_lga': "LGA",
|
---|
828 | 'app_email': "email",
|
---|
829 | 'app_mobile': "PhoneNumbers",
|
---|
830 | }
|
---|
831 | csv_fields = [f[1] for f in csv_d.items()]
|
---|
832 | tr_count = 0
|
---|
833 | total = 0
|
---|
834 | #name = 'pume_results'
|
---|
835 | name = 'DE_Admitted'
|
---|
836 | no_import = []
|
---|
837 | s = ','.join(['"%s"' % fn for fn in csv_fields])
|
---|
838 | no_import.append('"Error",%s' % s)
|
---|
839 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
840 | no_certificate = "no certificate %s" % format
|
---|
841 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
|
---|
842 | logger = logging.getLogger('Students.StudentsFolder.createDEStudents')
|
---|
843 | logger.info('Start loading from %s.csv' % name)
|
---|
844 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
845 | certs = {}
|
---|
846 | cert_docs = {}
|
---|
847 | for f in l:
|
---|
848 | certs[f.getId] = f.getObject().getContent()
|
---|
849 | try:
|
---|
850 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
851 | except:
|
---|
852 | logger.error('Error reading %s.csv' % name)
|
---|
853 | return
|
---|
854 | for jamb in result:
|
---|
855 | jamb['Error'] = "Processing"
|
---|
856 | logger.info(format % jamb)
|
---|
857 | jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
|
---|
858 | res = self.portal_catalog({'portal_type': "StudentApplication",
|
---|
859 | 'SearchableText': jamb_reg_no })
|
---|
860 | if res:
|
---|
861 | em = 'Student with RegNo %s already exists\n' % jamb_reg_no
|
---|
862 | logger.info(em)
|
---|
863 | jamb['Error'] = "Student exists"
|
---|
864 | no_import.append(format % jamb)
|
---|
865 | continue
|
---|
866 | cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
|
---|
867 | if cert_id not in certs.keys():
|
---|
868 | em = 'No Certificate with ID %s \n' % cert_id
|
---|
869 | logger.info(em)
|
---|
870 | jamb['Error'] = "No Certificate %s" % cert_id
|
---|
871 | no_import.append( format % jamb)
|
---|
872 | continue
|
---|
873 | jamb_reg_no =jamb.get(csv_d['jamb_reg_no'])
|
---|
874 | cert_doc = certs[cert_id]
|
---|
875 | catalog_entry = {}
|
---|
876 | catalog_entry['jamb_reg_no'] = jamb_reg_no
|
---|
877 | jamb_name = jamb.get(csv_d['jamb_lastname'])
|
---|
878 | jamb_name.replace('>','')
|
---|
879 | jamb_name.replace('<','')
|
---|
880 | names = jamb_name.split()
|
---|
881 | letter = names[-1][0].upper()
|
---|
882 | sid = self.generateStudentId(letter)
|
---|
883 | not_created = True
|
---|
884 | while not_created:
|
---|
885 | try:
|
---|
886 | students_folder.invokeFactory('Student', sid)
|
---|
887 | not_created = False
|
---|
888 | except BadRequest:
|
---|
889 | sid = self.generateStudentId(letter)
|
---|
890 | catalog_entry['id'] = sid
|
---|
891 | tr_count += 1
|
---|
892 | logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
|
---|
893 | student = getattr(self,sid)
|
---|
894 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
895 | student.invokeFactory('StudentPume','pume')
|
---|
896 | dp = {'Title': 'Pume Data'}
|
---|
897 | student.invokeFactory('StudentApplication','application')
|
---|
898 | da = {'Title': 'Application Data'}
|
---|
899 | da["jamb_lastname"] = jamb_name
|
---|
900 | da_fields = ('jamb_reg_no',
|
---|
901 | 'jamb_sex',
|
---|
902 | 'entry_mode',
|
---|
903 | #'jamb_score',
|
---|
904 | 'jamb_first_cos',
|
---|
905 | 'jamb_sex',
|
---|
906 | 'jamb_state',
|
---|
907 | 'jamb_lga',
|
---|
908 | 'app_email',
|
---|
909 | 'app_mobile',
|
---|
910 | )
|
---|
911 | for f in da_fields:
|
---|
912 | da[f] = jamb.get(csv_d[f])
|
---|
913 | catalog_entry['email'] = jamb.get(csv_d['app_email'])
|
---|
914 | app = student.application
|
---|
915 | app_doc = app.getContent()
|
---|
916 | picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
|
---|
917 | #import pdb;pdb.set_trace()
|
---|
918 | if os.path.exists(picture):
|
---|
919 | file = open(picture)
|
---|
920 | if False:
|
---|
921 | img = PIL.Image.open(file)
|
---|
922 | img.thumbnail((150,200),
|
---|
923 | resample=PIL.Image.ANTIALIAS)
|
---|
924 | # We now need a buffer to write to. It can't be the same
|
---|
925 | # as the inbuffer as the PNG writer will write over itself.
|
---|
926 | outfile = StringIO()
|
---|
927 | img.save(outfile, format=img.format)
|
---|
928 | else:
|
---|
929 | outfile = file.read()
|
---|
930 | app_doc.manage_addFile('passport',
|
---|
931 | file=outfile,
|
---|
932 | title="%s.jpg" % jamb_reg_no)
|
---|
933 | app.getContent().edit(mapping=da)
|
---|
934 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
935 | #wftool.doActionFor(app,'close')
|
---|
936 | dp_fields = (
|
---|
937 | #'pume_eng_score',
|
---|
938 | #'pume_gen_score',
|
---|
939 | 'pume_tot_score',
|
---|
940 | )
|
---|
941 | dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
|
---|
942 | pume = student.pume
|
---|
943 | pume.getContent().edit(mapping=dp)
|
---|
944 | #wftool.doActionFor(pume,'close')
|
---|
945 | pume.manage_setLocalRoles(sid, ['Owner',])
|
---|
946 | #student.getContent().createSubObjects()
|
---|
947 | dp = {}
|
---|
948 | if len(names) == 3:
|
---|
949 | dp['firstname'] = names[0].capitalize()
|
---|
950 | dp['middlename'] = names[1].capitalize()
|
---|
951 | dp['lastname'] = names[2].capitalize()
|
---|
952 | elif len(names) == 2:
|
---|
953 | dp['firstname'] = names[0].capitalize()
|
---|
954 | dp['middlename'] = ''
|
---|
955 | dp['lastname'] = names[1].capitalize()
|
---|
956 | else:
|
---|
957 | dp['firstname'] = ''
|
---|
958 | dp['middlename'] = ''
|
---|
959 | dp['lastname'] = jamb_name
|
---|
960 | dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
|
---|
961 | catalog_entry['sex'] = dp['sex']
|
---|
962 | catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
|
---|
963 | student.invokeFactory('StudentPersonal','personal')
|
---|
964 | per = student.personal
|
---|
965 | per_doc = per.getContent()
|
---|
966 | per_doc.edit(mapping = dp)
|
---|
967 | per.manage_setLocalRoles(sid, ['Owner',])
|
---|
968 | if jamb.get(csv_d['stud_status']) == "Admitted":
|
---|
969 | wftool.doActionFor(student,'pume_pass')
|
---|
970 | wftool.doActionFor(student,'admit')
|
---|
971 | else:
|
---|
972 | wftool.doActionFor(student,'pume_fail')
|
---|
973 | wftool.doActionFor(student,'reject_admission')
|
---|
974 | continue
|
---|
975 | #
|
---|
976 | # Clearance
|
---|
977 | #
|
---|
978 | student.invokeFactory('StudentClearance','clearance')
|
---|
979 | #wftool.doActionFor(student.clearance,'open')
|
---|
980 | dp = {'Title': 'Clearance/Eligibility Record'}
|
---|
981 | student.clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
982 | #
|
---|
983 | # Study Course
|
---|
984 | #
|
---|
985 | student.invokeFactory('StudentStudyCourse','study_course')
|
---|
986 | study_course = student.study_course
|
---|
987 | dsc = {}
|
---|
988 | #from_certificate = ['title',
|
---|
989 | # 'max_elect',
|
---|
990 | # 'max_pass',
|
---|
991 | # 'n_core',
|
---|
992 | # 'nr_years',
|
---|
993 | # 'probation_credits',
|
---|
994 | # 'promotion_credits',
|
---|
995 | # 'start_level',
|
---|
996 | # ]
|
---|
997 | #for f in from_certificate:
|
---|
998 | # dsc[f] = getattr(cert_doc,f)
|
---|
999 | #dsc['faculty'] = jamb.get(csv_d['faculty'])
|
---|
1000 | #dsc['department'] = jamb.get(csv_d['department'])
|
---|
1001 | catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
|
---|
1002 | catalog_entry['department'] = jamb.get(csv_d['department'])
|
---|
1003 | catalog_entry['course'] = cert_id
|
---|
1004 | #catalog_entry['level'] = getattr(cert_doc,'start_level')
|
---|
1005 | catalog_entry['level'] = '200'
|
---|
1006 | dsc['study_course'] = cert_id
|
---|
1007 | dsc['current_level'] = '200'
|
---|
1008 | #dsc['entry_session'] = jamb.get(csv_d['session'])
|
---|
1009 | study_course.getContent().edit(mapping=dsc)
|
---|
1010 | self.students_catalog.addRecord(**catalog_entry)
|
---|
1011 | if tr_count > 10:
|
---|
1012 | if len(no_import) > 1:
|
---|
1013 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1014 | '\n'.join(no_import)+'\n')
|
---|
1015 | no_import = []
|
---|
1016 | em = '%d transactions commited\n' % tr_count
|
---|
1017 | transaction.commit()
|
---|
1018 | logger.info(em)
|
---|
1019 | total += tr_count
|
---|
1020 | tr_count = 0
|
---|
1021 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1022 | '\n'.join(no_import))
|
---|
1023 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1024 | ###)
|
---|
1025 |
|
---|
1026 | security.declareProtected(ModifyPortalContent,"createNewStudents")###(
|
---|
1027 | def createNewStudents(self):
|
---|
1028 | """load Fulltime Studentdata from CSV values"""
|
---|
1029 | import transaction
|
---|
1030 | import random
|
---|
1031 | #from pdb import set_trace
|
---|
1032 | wftool = self.portal_workflow
|
---|
1033 | students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
1034 | csv_d = {'jamb_reg_no': "RegNumber",
|
---|
1035 | 'jamb_lastname': "Name",
|
---|
1036 | 'session': "Session",
|
---|
1037 | 'pume_tot_score': "PUME SCORE",
|
---|
1038 | 'jamb_score': "JambScore",
|
---|
1039 | 'jamb_sex': "Sex",
|
---|
1040 | 'jamb_state': "State",
|
---|
1041 | ## 'jamb_first_cos': "AdminCourse",
|
---|
1042 | 'faculty': "AdminFaculty",
|
---|
1043 | 'course_code': "AdmitCoscode",
|
---|
1044 | 'stud_status':"AdmitStatus",
|
---|
1045 | 'department': "AdmitDept",
|
---|
1046 | 'jamb_lga': "LGA",
|
---|
1047 | 'app_email': "email",
|
---|
1048 | 'app_mobile': "PhoneNumbers",
|
---|
1049 | }
|
---|
1050 | csv_fields = [f[1] for f in csv_d.items()]
|
---|
1051 | tr_count = 0
|
---|
1052 | total = 0
|
---|
1053 | #name = 'pume_results'
|
---|
1054 | name = 'Admitted'
|
---|
1055 | no_import = []
|
---|
1056 | s = ','.join(['"%s"' % fn for fn in csv_fields])
|
---|
1057 | no_import.append('"Error",%s' % s)
|
---|
1058 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
1059 | no_certificate = "no certificate %s" % format
|
---|
1060 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
|
---|
1061 | logger = logging.getLogger('Students.StudentsFolder.createNewStudents')
|
---|
1062 | logger.info('Start loading from %s.csv' % name)
|
---|
1063 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
1064 | certs = {}
|
---|
1065 | cert_docs = {}
|
---|
1066 | for f in l:
|
---|
1067 | certs[f.getId] = f.getObject().getContent()
|
---|
1068 | try:
|
---|
1069 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1070 | except:
|
---|
1071 | logger.error('Error reading %s.csv' % name)
|
---|
1072 | return
|
---|
1073 | for jamb in result:
|
---|
1074 | jamb['Error'] = "Processing "
|
---|
1075 | logger.info(format % jamb)
|
---|
1076 | jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
|
---|
1077 | res = self.portal_catalog({'portal_type': "StudentApplication",
|
---|
1078 | 'SearchableText': jamb_reg_no })
|
---|
1079 | if res:
|
---|
1080 | em = 'Student with RegNo %s already exists\n' % jamb_reg_no
|
---|
1081 | logger.info(em)
|
---|
1082 | jamb['Error'] = "Student exists"
|
---|
1083 | no_import.append(format % jamb)
|
---|
1084 | continue
|
---|
1085 | cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
|
---|
1086 | if cert_id not in certs.keys():
|
---|
1087 | em = 'No Certificate with ID %s \n' % cert_id
|
---|
1088 | logger.info(em)
|
---|
1089 | jamb['Error'] = "No Certificate %s" % cert_id
|
---|
1090 | no_import.append( format % jamb)
|
---|
1091 | continue
|
---|
1092 | res = self.portal_pumeresults(jamb_reg_no = jamb_reg_no)
|
---|
1093 | if len(res) == 1:
|
---|
1094 | self.portal_pumeresults.modifyRecord(jamb_reg_no = jamb_reg_no,
|
---|
1095 | status = jamb.get(csv_d['stud_status']),
|
---|
1096 | )
|
---|
1097 | jamb_reg_no =jamb.get(csv_d['jamb_reg_no'])
|
---|
1098 | cert_doc = certs[cert_id]
|
---|
1099 | catalog_entry = {}
|
---|
1100 | catalog_entry['jamb_reg_no'] = jamb_reg_no
|
---|
1101 | jamb_name = jamb.get(csv_d['jamb_lastname'])
|
---|
1102 | jamb_name.replace('>','')
|
---|
1103 | jamb_name.replace('<','')
|
---|
1104 | names = jamb_name.split()
|
---|
1105 | letter = names[-1][0].upper()
|
---|
1106 | sid = self.generateStudentId(letter)
|
---|
1107 | not_created = True
|
---|
1108 | while not_created:
|
---|
1109 | try:
|
---|
1110 | students_folder.invokeFactory('Student', sid)
|
---|
1111 | not_created = False
|
---|
1112 | except BadRequest:
|
---|
1113 | sid = self.generateStudentId(letter)
|
---|
1114 | catalog_entry['id'] = sid
|
---|
1115 | tr_count += 1
|
---|
1116 | logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s reg_no %(jamb_reg_no)s ' % vars())
|
---|
1117 | student = getattr(self,sid)
|
---|
1118 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
1119 | student.invokeFactory('StudentPume','pume')
|
---|
1120 | dp = {'Title': 'Pume Data'}
|
---|
1121 | student.invokeFactory('StudentApplication','application')
|
---|
1122 | da = {'Title': 'Application Data'}
|
---|
1123 | da["jamb_lastname"] = jamb_name
|
---|
1124 | da_fields = ('jamb_reg_no',
|
---|
1125 | 'jamb_sex',
|
---|
1126 | #'jamb_state',
|
---|
1127 | 'jamb_score',
|
---|
1128 | ## 'jamb_first_cos',
|
---|
1129 | 'jamb_sex',
|
---|
1130 | 'jamb_state',
|
---|
1131 | 'jamb_lga',
|
---|
1132 | 'app_email',
|
---|
1133 | 'app_mobile',
|
---|
1134 | )
|
---|
1135 | for f in da_fields:
|
---|
1136 | da[f] = jamb.get(csv_d[f])
|
---|
1137 | catalog_entry['email'] = jamb.get(csv_d['app_email'])
|
---|
1138 | app = student.application
|
---|
1139 | app_doc = app.getContent()
|
---|
1140 | #import pdb;pdb.set_trace()
|
---|
1141 | picture ="%s/import/pictures/%s.jpg" % (i_home,picture_id)
|
---|
1142 | if os.path.exists(picture):
|
---|
1143 | file = open(picture)
|
---|
1144 | if False:
|
---|
1145 | img = PIL.Image.open(file)
|
---|
1146 | img.thumbnail((150,200),
|
---|
1147 | resample=PIL.Image.ANTIALIAS)
|
---|
1148 | # We now need a buffer to write to. It can't be the same
|
---|
1149 | # as the inbuffer as the PNG writer will write over itself.
|
---|
1150 | outfile = StringIO()
|
---|
1151 | img.save(outfile, format=img.format)
|
---|
1152 | else:
|
---|
1153 | outfile = file.read()
|
---|
1154 | app_doc.manage_addFile('passport',
|
---|
1155 | file=outfile,
|
---|
1156 | title="%s.jpg" % jamb_reg_no)
|
---|
1157 | app.getContent().edit(mapping=da)
|
---|
1158 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
1159 | #wftool.doActionFor(app,'close')
|
---|
1160 | dp_fields = (
|
---|
1161 | #'pume_eng_score',
|
---|
1162 | #'pume_gen_score',
|
---|
1163 | 'pume_tot_score',
|
---|
1164 | )
|
---|
1165 | dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
|
---|
1166 | pume = student.pume
|
---|
1167 | pume.getContent().edit(mapping=dp)
|
---|
1168 | #wftool.doActionFor(pume,'close')
|
---|
1169 | pume.manage_setLocalRoles(sid, ['Owner',])
|
---|
1170 | #student.getContent().createSubObjects()
|
---|
1171 | dp = {}
|
---|
1172 | if len(names) == 3:
|
---|
1173 | dp['firstname'] = names[0].capitalize()
|
---|
1174 | dp['middlename'] = names[1].capitalize()
|
---|
1175 | dp['lastname'] = names[2].capitalize()
|
---|
1176 | elif len(names) == 2:
|
---|
1177 | dp['firstname'] = names[0].capitalize()
|
---|
1178 | dp['middlename'] = ''
|
---|
1179 | dp['lastname'] = names[1].capitalize()
|
---|
1180 | else:
|
---|
1181 | dp['firstname'] = ''
|
---|
1182 | dp['middlename'] = ''
|
---|
1183 | dp['lastname'] = jamb_name
|
---|
1184 | dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
|
---|
1185 | catalog_entry['sex'] = dp['sex']
|
---|
1186 | catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
|
---|
1187 | student.invokeFactory('StudentPersonal','personal')
|
---|
1188 | per = student.personal
|
---|
1189 | per_doc = per.getContent()
|
---|
1190 | per_doc.edit(mapping = dp)
|
---|
1191 | per.manage_setLocalRoles(sid, ['Owner',])
|
---|
1192 | if jamb.get(csv_d['stud_status']) == "Admitted":
|
---|
1193 | wftool.doActionFor(student,'pume_pass')
|
---|
1194 | wftool.doActionFor(student,'admit')
|
---|
1195 | else:
|
---|
1196 | wftool.doActionFor(student,'pume_fail')
|
---|
1197 | wftool.doActionFor(student,'reject_admission')
|
---|
1198 | continue
|
---|
1199 | #
|
---|
1200 | # Clearance
|
---|
1201 | #
|
---|
1202 | student.invokeFactory('StudentClearance','clearance')
|
---|
1203 | #wftool.doActionFor(student.clearance,'open')
|
---|
1204 | dp = {'Title': 'Clearance/Eligibility Record'}
|
---|
1205 | student.clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
1206 | #
|
---|
1207 | # Study Course
|
---|
1208 | #
|
---|
1209 | student.invokeFactory('StudentStudyCourse','study_course')
|
---|
1210 | study_course = student.study_course
|
---|
1211 | dsc = {}
|
---|
1212 | #from_certificate = ['title',
|
---|
1213 | # 'max_elect',
|
---|
1214 | # 'max_pass',
|
---|
1215 | # 'n_core',
|
---|
1216 | # 'nr_years',
|
---|
1217 | # 'probation_credits',
|
---|
1218 | # 'promotion_credits',
|
---|
1219 | # 'start_level',
|
---|
1220 | # ]
|
---|
1221 | #for f in from_certificate:
|
---|
1222 | # dsc[f] = getattr(cert_doc,f)
|
---|
1223 | #dsc['faculty'] = jamb.get(csv_d['faculty'])
|
---|
1224 | #dsc['department'] = jamb.get(csv_d['department'])
|
---|
1225 | catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
|
---|
1226 | catalog_entry['department'] = jamb.get(csv_d['department'])
|
---|
1227 | catalog_entry['course'] = cert_id
|
---|
1228 | #catalog_entry['level'] = getattr(cert_doc,'start_level')
|
---|
1229 | catalog_entry['level'] = '100'
|
---|
1230 | dsc['study_course'] = cert_id
|
---|
1231 | #dsc['entry_level'] = '100'
|
---|
1232 | #dsc['entry_session'] = jamb.get(csv_d['session'])
|
---|
1233 | study_course.getContent().edit(mapping=dsc)
|
---|
1234 | self.students_catalog.addRecord(**catalog_entry)
|
---|
1235 | if tr_count > 10:
|
---|
1236 | if len(no_import) > 0:
|
---|
1237 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1238 | '\n'.join(no_import) + "\n")
|
---|
1239 | no_import = []
|
---|
1240 | em = '%d transactions commited\n' % tr_count
|
---|
1241 | transaction.commit()
|
---|
1242 | logger.info(em)
|
---|
1243 | total += tr_count
|
---|
1244 | tr_count = 0
|
---|
1245 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1246 | '\n'.join(no_import))
|
---|
1247 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1248 | ###)
|
---|
1249 |
|
---|
1250 |
|
---|
1251 | security.declareProtected(ModifyPortalContent,"fixVerdicts")###(
|
---|
1252 | def fixVerdicts(self,csv_file=None):
|
---|
1253 | """fix wrong uploaded verdicts"""
|
---|
1254 | import transaction
|
---|
1255 | import random
|
---|
1256 | wftool = self.portal_workflow
|
---|
1257 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
1258 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
1259 | tr_count = 1
|
---|
1260 | total = 0
|
---|
1261 | if csv_file is None:
|
---|
1262 | name = 'Verdicts'
|
---|
1263 | else:
|
---|
1264 | name = csv_file
|
---|
1265 | st_cat = self.students_catalog
|
---|
1266 | no_import = []
|
---|
1267 | verdicts_voc = self.portal_vocabularies.verdicts
|
---|
1268 | rverdicts = {}
|
---|
1269 | for k,v in verdicts_voc.items():
|
---|
1270 | rverdicts[v.upper()] = k
|
---|
1271 | rverdicts['STUDENT ON PROBATION'] = 'C'
|
---|
1272 | logger = logging.getLogger('Students.StudentsFolder.fixVerdicts')
|
---|
1273 | try:
|
---|
1274 | verdicts = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1275 | except:
|
---|
1276 | logger.error('Error reading %s.csv' % name)
|
---|
1277 | return
|
---|
1278 | start = True
|
---|
1279 | #import pdb;pdb.set_trace()
|
---|
1280 | for verdict in verdicts:
|
---|
1281 | if start:
|
---|
1282 | start = False
|
---|
1283 | logger.info('Start loading from %s.csv' % name)
|
---|
1284 | s = ','.join(['"%s"' % fn for fn in verdict.keys()])
|
---|
1285 | no_import.append('%s,"Error"' % s)
|
---|
1286 | format = ','.join(['"%%(%s)s"' % fn for fn in verdict.keys()])
|
---|
1287 | format_error = format + ',"%(Error)s"'
|
---|
1288 | matric_no = verdict.get('MAT NO')
|
---|
1289 | if not matric_no:
|
---|
1290 | continue
|
---|
1291 | matric_no = matric_no.upper()
|
---|
1292 | if matric_no == '':
|
---|
1293 | continue
|
---|
1294 | verdict_code = rverdicts.get(verdict.get('CATEGORY'),None)
|
---|
1295 | if verdict_code is None:
|
---|
1296 | continue
|
---|
1297 | sres = st_cat(matric_no = matric_no)
|
---|
1298 | if sres:
|
---|
1299 | student_id = sres[0].id
|
---|
1300 | student_obj = getattr(students_folder,student_id,None)
|
---|
1301 | if student_obj:
|
---|
1302 | study_course = getattr(student_obj,'study_course',None)
|
---|
1303 | if study_course is None:
|
---|
1304 | verdict['Error'] = "Student did not yet log in"
|
---|
1305 | no_import.append( format_error % verdict)
|
---|
1306 | continue
|
---|
1307 | st_cat.modifyRecord(id = student_id,
|
---|
1308 | verdict=verdict_code)
|
---|
1309 |
|
---|
1310 | dsc = {}
|
---|
1311 | dsc['current_verdict'] = verdict_code
|
---|
1312 | study_course.getContent().edit(mapping=dsc)
|
---|
1313 | else:
|
---|
1314 | verdict['Error'] = "Not found in students_catalog"
|
---|
1315 | no_import.append( format_error % verdict)
|
---|
1316 | continue
|
---|
1317 | tr_count += 1
|
---|
1318 | if tr_count > 1000:
|
---|
1319 | if len(no_import) > 0:
|
---|
1320 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1321 | '\n'.join(no_import) + '\n')
|
---|
1322 | no_import = []
|
---|
1323 | em = '%d transactions commited total %s\n' % (tr_count,total)
|
---|
1324 | transaction.commit()
|
---|
1325 | regs = []
|
---|
1326 | logger.info(em)
|
---|
1327 | total += tr_count
|
---|
1328 | tr_count = 0
|
---|
1329 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1330 | '\n'.join(no_import))
|
---|
1331 | total += tr_count
|
---|
1332 | em = '%d total transactions commited' % (total)
|
---|
1333 | logger.info(em)
|
---|
1334 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1335 | ###)
|
---|
1336 |
|
---|
1337 |
|
---|
1338 | security.declareProtected(ModifyPortalContent,"fixAllEntryModeForReturning")###(
|
---|
1339 | def fixAllEntryModeForReturning(self):
|
---|
1340 | "read all Returning*.csv"
|
---|
1341 | ipath = "%s/import/" % i_home
|
---|
1342 | names = os.listdir(ipath)
|
---|
1343 | for name in names:
|
---|
1344 | head,tail = os.path.splitext(name)
|
---|
1345 | if head.startswith('Returning')\
|
---|
1346 | and tail == '.csv'\
|
---|
1347 | and name.find('imported') < 0:
|
---|
1348 | self.fixEntryModeForReturning(csv_file=head)
|
---|
1349 | ###)
|
---|
1350 |
|
---|
1351 | security.declareProtected(ModifyPortalContent,"fixEntryModeForReturning")###(
|
---|
1352 | def fixEntryModeForReturning(self,csv_file=None):
|
---|
1353 | """load Returning Studentdata from CSV values"""
|
---|
1354 | import transaction
|
---|
1355 | import random
|
---|
1356 | wftool = self.portal_workflow
|
---|
1357 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
1358 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
1359 | tr_count = 1
|
---|
1360 | total = 0
|
---|
1361 | if csv_file is None:
|
---|
1362 | name = 'Returning'
|
---|
1363 | else:
|
---|
1364 | name = csv_file
|
---|
1365 | table = self.returning_import
|
---|
1366 | st_cat = self.students_catalog
|
---|
1367 | no_import = []
|
---|
1368 | logger = logging.getLogger('Students.StudentsFolder.fixEntryModeForReturning')
|
---|
1369 | try:
|
---|
1370 | returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1371 | except:
|
---|
1372 | logger.error('Error reading %s.csv' % name)
|
---|
1373 | return
|
---|
1374 | start = True
|
---|
1375 | for student in returning:
|
---|
1376 | if start:
|
---|
1377 | start = False
|
---|
1378 | logger.info('Start loading from %s.csv' % name)
|
---|
1379 | s = ','.join(['"%s"' % fn for fn in student.keys()])
|
---|
1380 | no_import.append('%s,"Error"' % s)
|
---|
1381 | format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
|
---|
1382 | format_error = format + ',"%(Error)s"'
|
---|
1383 | matric_no = student.get('matric_no')
|
---|
1384 | if not matric_no:
|
---|
1385 | continue
|
---|
1386 | matric_no = matric_no.upper()
|
---|
1387 | student['matric_no'] = matric_no
|
---|
1388 | if matric_no == '':
|
---|
1389 | continue
|
---|
1390 | if not table(matric_no = matric_no):
|
---|
1391 | student['Error'] = "Not imported yet"
|
---|
1392 | no_import.append( format_error % student)
|
---|
1393 | continue
|
---|
1394 | student_id = None
|
---|
1395 | app = None
|
---|
1396 | per = None
|
---|
1397 | if st_cat(matric_no = matric_no):
|
---|
1398 | student_id = st_cat(matric_no = matric_no)[0].id
|
---|
1399 | student_obj = getattr(students_folder,student_id,None)
|
---|
1400 | if student_obj:
|
---|
1401 | app = getattr(student_obj,'application',None)
|
---|
1402 | if app is not None:
|
---|
1403 | app_doc = app.getContent()
|
---|
1404 | per = getattr(student_obj,'personal',None)
|
---|
1405 | if per is not None:
|
---|
1406 | per_doc = per.getContent()
|
---|
1407 | student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
|
---|
1408 | student['Permanent_Address'] = perm_address = student.get('Permanent Address')
|
---|
1409 | #import pdb;pdb.set_trace()
|
---|
1410 | if not entry_mode:
|
---|
1411 | student['Error'] = "'Mode of Entry' empty"
|
---|
1412 | no_import.append( format_error % student)
|
---|
1413 | continue
|
---|
1414 | try:
|
---|
1415 | table.modifyRecord(matric_no = matric_no,
|
---|
1416 | Mode_of_Entry = entry_mode,
|
---|
1417 | Permanent_Address = perm_address)
|
---|
1418 | except KeyError:
|
---|
1419 | student['Error'] = "Not found in returning_import"
|
---|
1420 | no_import.append( format_error % student)
|
---|
1421 | continue
|
---|
1422 | if student_id is not None:
|
---|
1423 | try:
|
---|
1424 | st_cat.modifyRecord(id = student_id,
|
---|
1425 | entry_mode=entry_mode)
|
---|
1426 | except KeyError:
|
---|
1427 | student['Error'] = "Not found in students_catalog"
|
---|
1428 | no_import.append( format_error % student)
|
---|
1429 | continue
|
---|
1430 | if app is not None:
|
---|
1431 | da = {}
|
---|
1432 | da['entry_mode'] = entry_mode
|
---|
1433 | app_doc.edit(mapping=da)
|
---|
1434 | if per is not None:
|
---|
1435 | dp = {}
|
---|
1436 | dp['perm_address'] = perm_address
|
---|
1437 | per_doc.edit(mapping=dp)
|
---|
1438 | tr_count += 1
|
---|
1439 | if tr_count > 1000:
|
---|
1440 | if len(no_import) > 0:
|
---|
1441 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1442 | '\n'.join(no_import) + '\n')
|
---|
1443 | no_import = []
|
---|
1444 | em = '%d transactions commited total %s\n' % (tr_count,total)
|
---|
1445 | transaction.commit()
|
---|
1446 | regs = []
|
---|
1447 | logger.info(em)
|
---|
1448 | total += tr_count
|
---|
1449 | tr_count = 0
|
---|
1450 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1451 | '\n'.join(no_import))
|
---|
1452 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1453 | ###)
|
---|
1454 |
|
---|
1455 | security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
|
---|
1456 | def loadPumeResultsFromCSV(self):
|
---|
1457 | """load Fulltime Studentdata from CSV values into pumeresults catalog"""
|
---|
1458 | import transaction
|
---|
1459 | import random
|
---|
1460 | ## csv_d = {'jamb_reg_no': "RegNumber", ###(
|
---|
1461 | ## 'status': "Admission Status",
|
---|
1462 | ## 'name': "Name",
|
---|
1463 | ## 'score': "Score",
|
---|
1464 | ## 'sex': "Sex",
|
---|
1465 | ## 'faculty': "Faculty",
|
---|
1466 | ## 'department': "Dept",
|
---|
1467 | ## 'course': "Course",
|
---|
1468 | ## 'course_code_org': "Course Code",
|
---|
1469 | ## }
|
---|
1470 | ###)
|
---|
1471 | ## csv_d = {'jamb_reg_no': "JAMBRegno",
|
---|
1472 | ## 'name': "Name",
|
---|
1473 | ## 'score': "Score",
|
---|
1474 | ## 'sex': "Sex",
|
---|
1475 | ## 'course': "Course",
|
---|
1476 | ## 'faculty': "Faculty",
|
---|
1477 | ## 'department': "Dept",
|
---|
1478 | ## 'course_code_org': "Course Code",
|
---|
1479 | ## 'status': "Admission Status",
|
---|
1480 | ## 'result_type': None,
|
---|
1481 | ## }
|
---|
1482 |
|
---|
1483 | csv_d = {'jamb_reg_no': "reg_no",
|
---|
1484 | 'name': "fullname",
|
---|
1485 | 'score': "pume_score",
|
---|
1486 | 'sex': "sex",
|
---|
1487 | 'course': "study_course",
|
---|
1488 | 'course_code_org': "study_course",
|
---|
1489 | 'status': "admission_status",
|
---|
1490 | 'result_type': "entry_mode",
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | csv_fields = [f[1] for f in csv_d.items() if f[1]]
|
---|
1494 | tr_count = 0
|
---|
1495 | total = 0
|
---|
1496 | #name = 'pup_new'
|
---|
1497 | #name = 'pup_update'
|
---|
1498 | name = 'Admitted_update'
|
---|
1499 | update = name.endswith('update')
|
---|
1500 | no_import = []
|
---|
1501 | ok_import = []
|
---|
1502 | ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()]))
|
---|
1503 | no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields]))
|
---|
1504 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
1505 | ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current)
|
---|
1506 | #open(ok_import_name,"w").write('\n'.join(no_import))
|
---|
1507 | no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
|
---|
1508 | #open(no_import_name,"w").write('\n'.join(no_import))
|
---|
1509 | logger = logging.getLogger('Students.loadPumeResultsFromCSV')
|
---|
1510 | starttime = DateTime.now()
|
---|
1511 | logger.info('Start loading from %s.csv' % name)
|
---|
1512 | try:
|
---|
1513 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1514 | except:
|
---|
1515 | logger.error('Error reading %s.csv' % name)
|
---|
1516 | return
|
---|
1517 | pume = self.portal_pumeresults
|
---|
1518 | format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
1519 | import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
|
---|
1520 | eduplicate = '%s,"duplicate"' % format
|
---|
1521 | eoriginal = '%s,"original"' % format
|
---|
1522 | einvalidjamb = '%s,"invalid JambRegNo"' % format
|
---|
1523 | added = 'added ,%s' % format
|
---|
1524 | #from pdb import set_trace;set_trace()
|
---|
1525 | for jamb in result:
|
---|
1526 | if not jamb.get(csv_d['score']):
|
---|
1527 | logger.info('Student %s has no pume_score' % jamb.get(csv_d['jamb_reg_no']))
|
---|
1528 | continue
|
---|
1529 | dict = {}
|
---|
1530 | for f,fn in csv_d.items():
|
---|
1531 | dict[f] = jamb.get(csv_d[f])
|
---|
1532 | dict['result_type'] = 'CEST'
|
---|
1533 | jnr = jamb.get(csv_d['jamb_reg_no'])
|
---|
1534 | #if not checkJambNo(jnr):
|
---|
1535 | # logger.info(einvalidjamb % jamb)
|
---|
1536 | # dd = {}
|
---|
1537 | # for f,fn in csv_d.items():
|
---|
1538 | # dd[fn] = getattr(data,f)
|
---|
1539 | # no_import.append(eduplicate % dd)
|
---|
1540 | # no_import.append(eduplicate % jamb)
|
---|
1541 | # continue
|
---|
1542 | res = pume(jamb_reg_no=jnr)
|
---|
1543 | if len(res) > 0:
|
---|
1544 | if update:
|
---|
1545 | try:
|
---|
1546 | pume.modifyRecord(**dict)
|
---|
1547 | # Can not happen, but anyway...
|
---|
1548 | except ValueError:
|
---|
1549 | logger.info(eduplicate % jamb)
|
---|
1550 | continue
|
---|
1551 | # Can not happen, but anyway...
|
---|
1552 | except KeyError:
|
---|
1553 | pume.addRecord(**dict)
|
---|
1554 | logger.info(added % jamb)
|
---|
1555 | continue
|
---|
1556 | else:
|
---|
1557 | data = res[0]
|
---|
1558 | if data.name != jamb.get(csv_d['name']):
|
---|
1559 | #set_trace()
|
---|
1560 | logger.info(eduplicate % jamb)
|
---|
1561 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
|
---|
1562 | #logger.info(em)
|
---|
1563 | dd = {}
|
---|
1564 | for f,fn in csv_d.items():
|
---|
1565 | dd[fn] = getattr(data,f)
|
---|
1566 | no_import.append(eoriginal % dd)
|
---|
1567 | no_import.append(eduplicate % jamb)
|
---|
1568 | continue
|
---|
1569 | else:
|
---|
1570 | try:
|
---|
1571 | pume.addRecord(**dict)
|
---|
1572 | ok_import.append(import_format % dict)
|
---|
1573 | except ValueError:
|
---|
1574 | logger.info(eduplicate % jamb)
|
---|
1575 | #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
|
---|
1576 | #logger.info(em)
|
---|
1577 | no_import.append(eduplicate % jamb)
|
---|
1578 | logger.info('End loading from %s.csv' % name)
|
---|
1579 | if len(no_import) > 1:
|
---|
1580 | open(no_import_name,"w+").write('\n'.join(no_import))
|
---|
1581 | open(ok_import_name,"w+").write('\n'.join(ok_import))
|
---|
1582 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1583 | ###)
|
---|
1584 |
|
---|
1585 | security.declareProtected(ModifyPortalContent,"createStudents")###(
|
---|
1586 | def createStudents(self):
|
---|
1587 | """
|
---|
1588 | load addmitted Studentdata from CSV values and create Studentobjects.
|
---|
1589 | This is the current method to create new addmitted Students.
|
---|
1590 | Before running the eventservice for the students_catalog must be disabled.
|
---|
1591 | """
|
---|
1592 | import transaction
|
---|
1593 | import random
|
---|
1594 | #from pdb import set_trace
|
---|
1595 | wftool = self.portal_workflow
|
---|
1596 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
1597 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
1598 | levels = {'ume_ft':'100',
|
---|
1599 | 'de_ft': '200',
|
---|
1600 | 'ug_pt': '100',
|
---|
1601 | 'de_pt': '200',
|
---|
1602 | 'pg_ft': '700',
|
---|
1603 | 'pg_pt': '700',
|
---|
1604 | 'dp_pt': '100',
|
---|
1605 | 'dp_ft': '100',
|
---|
1606 | }
|
---|
1607 | csv_d = {'jamb_reg_no': "reg_no",
|
---|
1608 | 'entry_mode': 'entry_mode',
|
---|
1609 | 'jamb_firstname': "firstname",
|
---|
1610 | 'jamb_middlename': "middlename",
|
---|
1611 | 'jamb_lastname': "lastname",
|
---|
1612 | 'jamb_sex': "sex",
|
---|
1613 | 'jamb_state': "state",
|
---|
1614 | 'birthday': "date_of_birth",
|
---|
1615 | 'app_email': "email",
|
---|
1616 | 'study_course': "study_course",
|
---|
1617 | 'perm_address': "address",
|
---|
1618 | 'admission_status': "admission_status",
|
---|
1619 | }
|
---|
1620 | csv_fields = [f[1] for f in csv_d.items()]
|
---|
1621 | tr_count = 0
|
---|
1622 | total = 0
|
---|
1623 | #name = 'pume_results'
|
---|
1624 | name = 'Admitted_update'
|
---|
1625 | no_import = []
|
---|
1626 | s = ','.join(['"%s"' % fn for fn in csv_fields])
|
---|
1627 | no_import.append('"Error",%s' % s)
|
---|
1628 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
1629 | no_certificate = "no certificate %s" % format
|
---|
1630 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
|
---|
1631 | logger = logging.getLogger('Students.StudentsFolder.createStudents')
|
---|
1632 | logger.info('Start loading from %s.csv' % name)
|
---|
1633 | certs = {}
|
---|
1634 | try:
|
---|
1635 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1636 | except:
|
---|
1637 | logger.error('Error reading %s.csv' % name)
|
---|
1638 | return
|
---|
1639 | for result in results:
|
---|
1640 | if not result.get(csv_d['admission_status']).startswith('Admitted'):
|
---|
1641 | continue
|
---|
1642 | #result['Error'] = "Processing "
|
---|
1643 | #logger.info(format % result)
|
---|
1644 | jamb_reg_no = result.get(csv_d['jamb_reg_no'])
|
---|
1645 | res = self.students_catalog(jamb_reg_no = jamb_reg_no)
|
---|
1646 | if res:
|
---|
1647 | em = 'Student with RegNo %s already exists\n' % jamb_reg_no
|
---|
1648 | logger.info(em)
|
---|
1649 | result['Error'] = "Student exists"
|
---|
1650 | no_import.append(format % result)
|
---|
1651 | continue
|
---|
1652 | cert_id = makeCertificateCode(result.get(csv_d['study_course']))
|
---|
1653 | if cert_id not in certs.keys():
|
---|
1654 | res = self.portal_catalog(meta_type = "Certificate",id = cert_id)
|
---|
1655 | if not res:
|
---|
1656 | em = 'No Certificate with ID %s \n' % cert_id
|
---|
1657 | logger.info(em)
|
---|
1658 | result['Error'] = "No Certificate %s" % cert_id
|
---|
1659 | no_import.append( format % result)
|
---|
1660 | continue
|
---|
1661 | cert = res[0]
|
---|
1662 | cert_path = cert.getPath().split('/')
|
---|
1663 | certificate = certs[cert_id] = {'faculty': cert_path[-4],
|
---|
1664 | 'department': cert_path[-3]}
|
---|
1665 | cert_doc = certs[cert_id]
|
---|
1666 | catalog_entry = {}
|
---|
1667 | catalog_entry['jamb_reg_no'] = jamb_reg_no
|
---|
1668 | firstname = result.get(csv_d['jamb_firstname'])
|
---|
1669 | middlename = result.get(csv_d['jamb_middlename'])
|
---|
1670 | lastname = result.get(csv_d['jamb_lastname'])
|
---|
1671 | if len(firstname) < 3\
|
---|
1672 | and len(middlename) < 3\
|
---|
1673 | and len(lastname) < 3:
|
---|
1674 | em = 'Student Names to short \n'
|
---|
1675 | logger.info(em)
|
---|
1676 | result['Error'] = "Names to short"
|
---|
1677 | no_import.append( format % result)
|
---|
1678 | continue
|
---|
1679 | perm_address = result.get(csv_d['perm_address'])
|
---|
1680 | sid = self.generateStudentId('x')
|
---|
1681 | students_folder.invokeFactory('Student', sid)
|
---|
1682 | catalog_entry['id'] = sid
|
---|
1683 | tr_count += 1
|
---|
1684 | logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s reg_no %(jamb_reg_no)s ' % vars())
|
---|
1685 | student = getattr(self,sid)
|
---|
1686 | student.manage_setLocalRoles(sid, ['Owner',])
|
---|
1687 | student.invokeFactory('StudentApplication','application')
|
---|
1688 | da = {'Title': 'Application Data'}
|
---|
1689 | da["jamb_firstname"] = firstname
|
---|
1690 | da["jamb_middlename"] = middlename
|
---|
1691 | da["jamb_lastname"] = lastname
|
---|
1692 | catalog_entry['entry_session'] = da["entry_session"] = self.getSessionId()[0]
|
---|
1693 | catalog_entry['sex'] = sex = result.get(csv_d['jamb_sex']).startswith('F')
|
---|
1694 | da_fields = ('jamb_reg_no',
|
---|
1695 | 'jamb_sex',
|
---|
1696 | 'jamb_state',
|
---|
1697 | 'entry_mode',
|
---|
1698 | 'app_email',
|
---|
1699 | )
|
---|
1700 | for f in da_fields:
|
---|
1701 | da[f] = result.get(csv_d[f])
|
---|
1702 | catalog_entry['email'] = da['app_email']
|
---|
1703 | catalog_entry['entry_mode'] = da['entry_mode']
|
---|
1704 | app = student.application
|
---|
1705 | app_doc = app.getContent()
|
---|
1706 | app.getContent().edit(mapping=da)
|
---|
1707 | picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
|
---|
1708 | app.manage_setLocalRoles(sid, ['Owner',])
|
---|
1709 |
|
---|
1710 | picture_id = da['jamb_reg_no'].replace('/','_')
|
---|
1711 | file = None
|
---|
1712 | for ext in ('jpg','JPG'):
|
---|
1713 | picture ="%s/import/pictures_admitted_latest/%s.%s" % (i_home,picture_id,ext)
|
---|
1714 | if os.path.exists(picture):
|
---|
1715 | file = open(picture)
|
---|
1716 | break
|
---|
1717 | if file is not None:
|
---|
1718 |
|
---|
1719 | ## file conversion does not work
|
---|
1720 | #img = PIL.Image.open(file)
|
---|
1721 | #img.thumbnail((150,200),
|
---|
1722 | # resample=PIL.Image.ANTIALIAS)
|
---|
1723 | #outfile = StringIO()
|
---|
1724 | #img.save(outfile, format=img.format)
|
---|
1725 |
|
---|
1726 | outfile = file.read()
|
---|
1727 | app_doc.manage_addFile('passport',
|
---|
1728 | file=outfile,
|
---|
1729 | title="%s.jpg" % jamb_reg_no)
|
---|
1730 | #wftool.doActionFor(app,'close')
|
---|
1731 | dp = {}
|
---|
1732 | dp['firstname'] = firstname
|
---|
1733 | dp['middlename'] = middlename
|
---|
1734 | dp['lastname'] = lastname
|
---|
1735 | dp['email'] = da['app_email']
|
---|
1736 | dp['sex'] = sex
|
---|
1737 | dp['perm_address'] = perm_address
|
---|
1738 | catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
|
---|
1739 | student.invokeFactory('StudentPersonal','personal')
|
---|
1740 | per = student.personal
|
---|
1741 | per_doc = per.getContent()
|
---|
1742 | per_doc.edit(mapping = dp)
|
---|
1743 | per.manage_setLocalRoles(sid, ['Owner',])
|
---|
1744 | wftool.doActionFor(student,'pume_pass')
|
---|
1745 | wftool.doActionFor(student,'admit')
|
---|
1746 | #
|
---|
1747 | # Clearance
|
---|
1748 | #
|
---|
1749 | student.invokeFactory('StudentClearance','clearance')
|
---|
1750 | #wftool.doActionFor(student.clearance,'open')
|
---|
1751 | clearance = student.clearance
|
---|
1752 | dc = {'Title': 'Clearance/Eligibility Record'}
|
---|
1753 | clearance = student.clearance
|
---|
1754 | date_str = result.get(csv_d['birthday'])
|
---|
1755 | try:
|
---|
1756 | date = DateTime.DateTime(date_str)
|
---|
1757 | except:
|
---|
1758 | #import pdb;pdb.set_trace()
|
---|
1759 | date = None
|
---|
1760 | dc['birthday'] = date
|
---|
1761 | clearance.getContent().edit(mapping=dc)
|
---|
1762 | clearance.manage_setLocalRoles(sid, ['Owner',])
|
---|
1763 | #
|
---|
1764 | # Study Course
|
---|
1765 | #
|
---|
1766 | student.invokeFactory('StudentStudyCourse','study_course')
|
---|
1767 | study_course = student.study_course
|
---|
1768 | dsc = {}
|
---|
1769 | catalog_entry['level'] = dsc['current_level'] = levels.get(da['entry_mode'],'100')
|
---|
1770 | #catalog_entry['level'] = dsc['current_level'] = '100' # Attention: not for DE students
|
---|
1771 | catalog_entry['session'] = dsc['current_session'] = da['entry_session']
|
---|
1772 | catalog_entry['mode'] = dsc['current_mode'] = da['entry_mode']
|
---|
1773 | catalog_entry['course'] = dsc['study_course'] = cert_id
|
---|
1774 | catalog_entry['faculty'] = certificate['faculty']
|
---|
1775 | catalog_entry['department'] = certificate['department']
|
---|
1776 | catalog_entry['verdict'] = dsc['current_verdict'] = 'N/A'
|
---|
1777 | catalog_entry['review_state'] = self.portal_workflow.getInfoFor(student,'review_state',None)
|
---|
1778 | study_course.getContent().edit(mapping=dsc)
|
---|
1779 | #import pdb;pdb.set_trace()
|
---|
1780 | self.students_catalog.addRecord(**catalog_entry)
|
---|
1781 | if tr_count > 100:
|
---|
1782 | if len(no_import) > 0:
|
---|
1783 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1784 | '\n'.join(no_import) + "\n")
|
---|
1785 | no_import = []
|
---|
1786 | em = '%d transactions commited\n' % tr_count
|
---|
1787 | transaction.commit()
|
---|
1788 | logger.info(em)
|
---|
1789 | total += tr_count
|
---|
1790 | tr_count = 0
|
---|
1791 | open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
|
---|
1792 | '\n'.join(no_import))
|
---|
1793 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1794 | ###)
|
---|
1795 |
|
---|
1796 |
|
---|
1797 | security.declareProtected(ModifyPortalContent,"importReturningStudents")###(
|
---|
1798 | def importReturningStudents(self):
|
---|
1799 | """load Returning Studentdata from CSV values"""
|
---|
1800 | import transaction
|
---|
1801 | import random
|
---|
1802 | #from pdb import set_trace
|
---|
1803 | wftool = self.portal_workflow
|
---|
1804 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
1805 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
1806 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
1807 | tr_count = 1
|
---|
1808 | total = 0
|
---|
1809 | #name = 'pume_results'
|
---|
1810 | name = 'Returning'
|
---|
1811 | table = self.returning_import
|
---|
1812 | no_import = []
|
---|
1813 | imported = []
|
---|
1814 | logger = logging.getLogger('Students.StudentsFolder.importReturningStudents')
|
---|
1815 | try:
|
---|
1816 | returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
1817 | except:
|
---|
1818 | logger.error('Error reading %s.csv' % name)
|
---|
1819 | return
|
---|
1820 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
1821 | certs = {}
|
---|
1822 | cert_docs = {}
|
---|
1823 | for f in l:
|
---|
1824 | certs[f.getId] = f.getObject().getContent()
|
---|
1825 | start = True
|
---|
1826 | res = table()
|
---|
1827 | regs = []
|
---|
1828 | if len(res) > 0:
|
---|
1829 | regs = [s.matric_no for s in res]
|
---|
1830 | #import pdb;pdb.set_trace()
|
---|
1831 | for student in returning:
|
---|
1832 | if start:
|
---|
1833 | start = False
|
---|
1834 | logger.info('Start loading from %s.csv' % name)
|
---|
1835 | s = ','.join(['"%s"' % fn for fn in student.keys()])
|
---|
1836 | imported.append(s)
|
---|
1837 | no_import.append('%s,"Error"' % s)
|
---|
1838 | format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
|
---|
1839 | format_error = format + ',"%(Error)s"'
|
---|
1840 | no_certificate = "no certificate %s" % format
|
---|
1841 | student['matric_no'] = matric_no = student.get('matric_no').upper()
|
---|
1842 | student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
|
---|
1843 | student['Permanent_Address'] = perm_address = student.get('Permanent Address')
|
---|
1844 | if matric_no == '':
|
---|
1845 | student['Error'] = "Empty matric_no"
|
---|
1846 | no_import.append( format_error % student)
|
---|
1847 | continue
|
---|
1848 | if matric_no in regs or self.returning_import(matric_no = matric_no):
|
---|
1849 | student['Error'] = "Duplicate"
|
---|
1850 | no_import.append( format_error % student)
|
---|
1851 | continue
|
---|
1852 | cert_id = makeCertificateCode(student.get('Coursemajorcode'))
|
---|
1853 | if cert_id not in certs.keys():
|
---|
1854 | student['Error'] = "No Certificate %s" % cert_id
|
---|
1855 | no_import.append( format_error % student)
|
---|
1856 | continue
|
---|
1857 | try:
|
---|
1858 | table.addRecord(**student)
|
---|
1859 | except ValueError:
|
---|
1860 | student['Error'] = "Duplicate"
|
---|
1861 | no_import.append( format_error % student)
|
---|
1862 | continue
|
---|
1863 | regs.append(student.get('matric_no'))
|
---|
1864 | imported.append(format % student)
|
---|
1865 | tr_count += 1
|
---|
1866 | if tr_count > 1000:
|
---|
1867 | if len(no_import) > 0:
|
---|
1868 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1869 | '\n'.join(no_import) + '\n')
|
---|
1870 | no_import = []
|
---|
1871 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
1872 | '\n'.join(no_import) + "\n")
|
---|
1873 | imported = []
|
---|
1874 | em = '%d transactions commited total %s\n' % (tr_count,total)
|
---|
1875 | transaction.commit()
|
---|
1876 | regs = []
|
---|
1877 | logger.info(em)
|
---|
1878 | total += tr_count
|
---|
1879 | tr_count = 0
|
---|
1880 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
1881 | '\n'.join(imported))
|
---|
1882 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
1883 | '\n'.join(no_import))
|
---|
1884 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
1885 | ###)
|
---|
1886 |
|
---|
1887 | security.declareProtected(ModifyPortalContent,"fixAllNames")###(
|
---|
1888 | def fixAllNames(self):
|
---|
1889 | "fix all students names"
|
---|
1890 | import transaction
|
---|
1891 | response = self.REQUEST.RESPONSE
|
---|
1892 | logger = logging.getLogger('fixAllNames')
|
---|
1893 | logger.info('Start')
|
---|
1894 | students = self.portal_catalog(portal_type='Student')
|
---|
1895 | count = 0
|
---|
1896 | total = 0
|
---|
1897 | for student in students:
|
---|
1898 | scat_res = self.students_catalog(id = student.getId)
|
---|
1899 | if not scat_res:
|
---|
1900 | self.students_catalog.addRecord(id = student.getId)
|
---|
1901 | scat_res = self.students_catalog(id = student.getId)
|
---|
1902 | student_entry = scat_res[0]
|
---|
1903 | old_new = self.fixName(student,student_entry)
|
---|
1904 | count += 1
|
---|
1905 | response_write(response,'"%d","%s",%s' % (count + total,student_entry.id,old_new))
|
---|
1906 | if count > 2000:
|
---|
1907 | transaction.commit()
|
---|
1908 | logger.info("%d transactions commited" % count)
|
---|
1909 | total += count
|
---|
1910 | count = 0
|
---|
1911 | ###)
|
---|
1912 |
|
---|
1913 | security.declareProtected(ModifyPortalContent,"fixName")###(
|
---|
1914 | def fixName(self,student_brain, student_entry):
|
---|
1915 | "fix the name of a student"
|
---|
1916 | fix = "first"
|
---|
1917 | if student_entry.get('name_fixed',None) == fix:
|
---|
1918 | return "Name already fixed"
|
---|
1919 | student_id = student_entry.id
|
---|
1920 | new_student = student_entry.jamb_reg_no.startswith('6')
|
---|
1921 | student_obj = student_brain.getObject()
|
---|
1922 | personal = getattr(student_obj,'personal',None)
|
---|
1923 | invalid = ''
|
---|
1924 | if personal is None:
|
---|
1925 | return '"%s","Returning","%s","%s"' % (invalid,student_entry.name,"not logged in")
|
---|
1926 | per_doc = personal.getContent()
|
---|
1927 | old_first = per_doc.firstname
|
---|
1928 | old_middle = per_doc.middlename
|
---|
1929 | old_last = per_doc.lastname
|
---|
1930 | new_first = ''
|
---|
1931 | new_middle = ''
|
---|
1932 | new_last = ''
|
---|
1933 | if new_student:
|
---|
1934 | if not old_first and not old_middle and old_last:
|
---|
1935 | new_names = [n.capitalize() for n in old_last.split()]
|
---|
1936 | if len(new_names) > 1:
|
---|
1937 | old_first = new_names[0]
|
---|
1938 | old_last = new_names[-1]
|
---|
1939 | old_middle = ' '.join(new_names[1:-1])
|
---|
1940 | else:
|
---|
1941 | old_last = new_names[0]
|
---|
1942 | old_first = ''
|
---|
1943 | old_middle = ''
|
---|
1944 | if old_first:
|
---|
1945 | new_first = old_first
|
---|
1946 | if old_middle:
|
---|
1947 | new_middle = old_middle
|
---|
1948 | if old_last:
|
---|
1949 | new_last = old_last
|
---|
1950 | if old_first.find('<') != -1 or\
|
---|
1951 | old_first.find('>') != -1 or\
|
---|
1952 | old_middle.find('<') != -1 or\
|
---|
1953 | old_middle.find('>') != -1 or\
|
---|
1954 | old_last.find('<') != -1 or\
|
---|
1955 | old_last.find('>') != -1:
|
---|
1956 | invalid = "invalid characters"
|
---|
1957 | else:
|
---|
1958 | new_first = old_first
|
---|
1959 | if new_first.strip() == '-':
|
---|
1960 | new_first = ''
|
---|
1961 | new_middle = old_middle
|
---|
1962 | if new_middle.strip() == '-':
|
---|
1963 | new_middle = ''
|
---|
1964 | new_last = old_last
|
---|
1965 | if new_last.strip() == '-':
|
---|
1966 | new_last = ''
|
---|
1967 | name = "%(new_first)s %(new_middle)s %(new_last)s" % vars()
|
---|
1968 | if new_student:
|
---|
1969 | text = "New"
|
---|
1970 | else:
|
---|
1971 | text = "Returning"
|
---|
1972 | old_new = '"%s","%s","%s","%s"' % (invalid,text,
|
---|
1973 | student_entry.name,
|
---|
1974 | name)
|
---|
1975 | if not invalid:
|
---|
1976 | self.students_catalog.modifyRecord(id = student_id,
|
---|
1977 | name_fixed = fix,
|
---|
1978 | name = name)
|
---|
1979 | per_doc.edit(mapping = {'firstname' : new_first,
|
---|
1980 | 'middlename' : new_middle,
|
---|
1981 | 'lastname' : new_last,
|
---|
1982 | })
|
---|
1983 | return old_new
|
---|
1984 | ###)
|
---|
1985 |
|
---|
1986 | security.declareProtected(ModifyPortalContent,"updateReturningStudents")###(
|
---|
1987 | def updateReturningStudents(self):
|
---|
1988 | """load and overwrite Returning Student Data from CSV values"""
|
---|
1989 | import transaction
|
---|
1990 | import random
|
---|
1991 | #from pdb import set_trace
|
---|
1992 | wftool = self.portal_workflow
|
---|
1993 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
1994 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
1995 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
1996 | tr_count = 1
|
---|
1997 | total = 0
|
---|
1998 | #name = 'pume_results'
|
---|
1999 | name = 'Returning_update'
|
---|
2000 | table = self.returning_import
|
---|
2001 | no_import = []
|
---|
2002 | imported = []
|
---|
2003 | logger = logging.getLogger('Students.StudentsFolder.updateReturningStudents')
|
---|
2004 | try:
|
---|
2005 | returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
2006 | except:
|
---|
2007 | logger.error('Error reading %s.csv' % name)
|
---|
2008 | return
|
---|
2009 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
2010 | certs = {}
|
---|
2011 | cert_docs = {}
|
---|
2012 | for f in l:
|
---|
2013 | certs[f.getId] = f.getObject().getContent()
|
---|
2014 | start = True
|
---|
2015 | res = table()
|
---|
2016 | regs = []
|
---|
2017 | if len(res) > 0:
|
---|
2018 | regs = [s.matric_no for s in res]
|
---|
2019 | for student in returning:
|
---|
2020 | if start:
|
---|
2021 | start = False
|
---|
2022 | logger.info('Start loading from %s.csv' % name)
|
---|
2023 | s = ','.join(['"%s"' % fn for fn in student.keys()])
|
---|
2024 | imported.append(s)
|
---|
2025 | no_import.append('%s,"Error"' % s)
|
---|
2026 | format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
|
---|
2027 | format_error = format + ',"%(Error)s"'
|
---|
2028 | no_certificate = "no certificate %s" % format
|
---|
2029 | matric_no = student.get('matric_no').upper()
|
---|
2030 | student['matric_no'] = matric_no
|
---|
2031 | if matric_no == '':
|
---|
2032 | student['Error'] = "Empty matric_no"
|
---|
2033 | no_import.append( format_error % student)
|
---|
2034 | continue
|
---|
2035 | # if matric_no in regs or self.returning_import(matric_no = matric_no):
|
---|
2036 | # student['Error'] = "Duplicate"
|
---|
2037 | # no_import.append( format_error % student)
|
---|
2038 | # continue
|
---|
2039 | # cert_id = makeCertificateCode(student.get('Coursemajorcode'))
|
---|
2040 | # if cert_id not in certs.keys():
|
---|
2041 | # student['Error'] = "No Certificate %s" % cert_id
|
---|
2042 | # no_import.append( format_error % student)
|
---|
2043 | # continue
|
---|
2044 | try:
|
---|
2045 | table.modifyRecord(**student)
|
---|
2046 | except KeyError:
|
---|
2047 | #import pdb;pdb.set_trace()
|
---|
2048 | student['Error'] = "no Student found to update"
|
---|
2049 | no_import.append( format_error % student)
|
---|
2050 | continue
|
---|
2051 | #s = self.students_catalog(matric_no=matric_no)
|
---|
2052 | #if s:
|
---|
2053 | # level = "%s" % (int(student.get('Level')) + 100)
|
---|
2054 | # self.students_catalog.modifyRecord(id = s[0].id,
|
---|
2055 | # level=level)
|
---|
2056 |
|
---|
2057 | regs.append(student.get('matric_no'))
|
---|
2058 | imported.append(format % student)
|
---|
2059 | tr_count += 1
|
---|
2060 | if tr_count > 1000:
|
---|
2061 | if len(no_import) > 0:
|
---|
2062 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
2063 | '\n'.join(no_import) + '\n')
|
---|
2064 | no_import = []
|
---|
2065 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
2066 | '\n'.join(no_import) + "\n")
|
---|
2067 | imported = []
|
---|
2068 | em = '%d transactions commited total %s\n' % (tr_count,total)
|
---|
2069 | transaction.commit()
|
---|
2070 | regs = []
|
---|
2071 | logger.info(em)
|
---|
2072 | total += tr_count
|
---|
2073 | tr_count = 0
|
---|
2074 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
|
---|
2075 | '\n'.join(imported))
|
---|
2076 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
|
---|
2077 | '\n'.join(no_import))
|
---|
2078 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
2079 | ###)
|
---|
2080 |
|
---|
2081 | security.declareProtected(ModifyPortalContent,"exportStudents")###(
|
---|
2082 | def exportStudents(self):
|
---|
2083 | """export Studentsdata to a file"""
|
---|
2084 | member = self.portal_membership.getAuthenticatedMember()
|
---|
2085 | logger = logging.getLogger('Students.exportStudents')
|
---|
2086 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
|
---|
2087 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
2088 | export_file = "%s/export/students_%s.csv" % (i_home,current)
|
---|
2089 |
|
---|
2090 | from Products.AdvancedQuery import Eq, Between, Le,In,MatchRegexp
|
---|
2091 | aq_students = self.students_catalog.evalAdvancedQuery
|
---|
2092 | toexport = {'students_catalog': ("id",
|
---|
2093 | "matric_no",
|
---|
2094 | "jamb_reg_no",
|
---|
2095 | "name",
|
---|
2096 | "review_state",
|
---|
2097 | "entry_session",
|
---|
2098 | "session",
|
---|
2099 | "entry_mode",
|
---|
2100 | "mode",
|
---|
2101 | "verdict",
|
---|
2102 | "sex",
|
---|
2103 | "email",
|
---|
2104 | "phone",
|
---|
2105 | "faculty",
|
---|
2106 | "department",
|
---|
2107 | "course",
|
---|
2108 | "level",
|
---|
2109 | ),
|
---|
2110 | 'personal': ('firstname',
|
---|
2111 | 'middlename',
|
---|
2112 | 'lastname',
|
---|
2113 | 'perm_address',
|
---|
2114 | ),
|
---|
2115 | 'clearance': ('state', # is not an attribute of clearance but is needed for splitting lga
|
---|
2116 | 'lga',
|
---|
2117 | )
|
---|
2118 | }
|
---|
2119 | res_list = []
|
---|
2120 | lines = []
|
---|
2121 | fields = []
|
---|
2122 | with_lga = False
|
---|
2123 | for k in toexport.keys():
|
---|
2124 | for f in toexport[k]:
|
---|
2125 | if f == 'lga' :
|
---|
2126 | with_lga = True
|
---|
2127 | fields.append(f)
|
---|
2128 | headline = ','.join(fields).replace('level','current_level')
|
---|
2129 | open(export_file,"a").write(headline +'\n')
|
---|
2130 | format = '"%(' + ')s","%('.join(fields) + ')s"'
|
---|
2131 | query = In('review_state',('cleared_and_validated',
|
---|
2132 | 'clearance_requested',
|
---|
2133 | 'school_fee_paid',
|
---|
2134 | 'courses_registered',
|
---|
2135 | 'courses_validated'))
|
---|
2136 | #query = In('review_state',('clearance_requested'))
|
---|
2137 | students = aq_students(query)
|
---|
2138 | nr2export = len(students)
|
---|
2139 | logger.info('%s starts exportStudents, %s student records to export' % (member,nr2export))
|
---|
2140 | chunk = 1000
|
---|
2141 | total = 0
|
---|
2142 | start = DateTime.DateTime().timeTime()
|
---|
2143 | start_chunk = DateTime.DateTime().timeTime()
|
---|
2144 | ## alternative method slightly slower
|
---|
2145 | # student_recs = {}
|
---|
2146 | # for s in students:
|
---|
2147 | # student_recs[s.id] = s
|
---|
2148 | # catalog_recs = {}
|
---|
2149 | # brains = self.portal_catalog(portal_type = 'Student')
|
---|
2150 | # for cat_rec in brains:
|
---|
2151 | # sid = cat_rec.getId
|
---|
2152 | # catalog_recs[sid] = cat_rec
|
---|
2153 | # #import pdb;pdb.set_trace()
|
---|
2154 | # start = DateTime.DateTime().timeTime()
|
---|
2155 | # start_chunk = DateTime.DateTime().timeTime()
|
---|
2156 | # for student in students:
|
---|
2157 | # if student.id not in student_recs.keys():
|
---|
2158 | # continue
|
---|
2159 | # not_all = False
|
---|
2160 | # d = self.getFormattedStudentEntry(student_recs[student.id])
|
---|
2161 | # student_obj = catalog_recs[student.id].getObject()
|
---|
2162 | for student in students:
|
---|
2163 | not_all = False
|
---|
2164 | d = self.getFormattedStudentEntry(student)
|
---|
2165 | student_obj = getattr(students_folder,student.id)
|
---|
2166 | for k in toexport.keys()[1:]:
|
---|
2167 | try:
|
---|
2168 | object = getattr(student_obj,k)
|
---|
2169 | object_doc = object.getContent()
|
---|
2170 | except:
|
---|
2171 | logger.info('%s %s record not found' % (student.id,k))
|
---|
2172 | not_all = True
|
---|
2173 | continue
|
---|
2174 | for f in toexport[k]:
|
---|
2175 | d[f] = getattr(object_doc,f,'')
|
---|
2176 | if not_all:
|
---|
2177 | continue
|
---|
2178 | if with_lga:
|
---|
2179 | d['state'],d['lga'] = formatLGA(d['lga'],voc = self.portal_vocabularies.local_gov_areas)
|
---|
2180 | lines.append(format % d)
|
---|
2181 | total += 1
|
---|
2182 | if total and not total % chunk or total == len(students):
|
---|
2183 | open(export_file,"a").write('\n'.join(lines) +'\n')
|
---|
2184 | anz = len(lines)
|
---|
2185 | logger.info("wrote %(anz)d total written %(total)d" % vars())
|
---|
2186 | end_chunk = DateTime.DateTime().timeTime()
|
---|
2187 | duration = end_chunk-start_chunk
|
---|
2188 | per_record = duration/anz
|
---|
2189 | till_now = end_chunk - start
|
---|
2190 | avarage_per_record = till_now/total
|
---|
2191 | estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export)
|
---|
2192 | estimated_end = estimated_end.strftime("%H:%M:%S")
|
---|
2193 | logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars())
|
---|
2194 | start_chunk = DateTime.DateTime().timeTime()
|
---|
2195 | lines = []
|
---|
2196 | end = DateTime.DateTime().timeTime()
|
---|
2197 | logger.info('total time %6.2f m' % ((end-start)/60))
|
---|
2198 | filename, extension = os.path.splitext(export_file)
|
---|
2199 | from subprocess import call
|
---|
2200 | msg = "wrote %(total)d records to %(export_file)s" % vars()
|
---|
2201 | try:
|
---|
2202 | retcode = call('gzip %s' % (export_file),shell=True)
|
---|
2203 | if retcode == 0:
|
---|
2204 | msg = "wrote %(total)d records to %(export_file)s.gz" % vars()
|
---|
2205 | except OSError, e:
|
---|
2206 | retcode = -99
|
---|
2207 | logger.info("zip failed with %s" % e)
|
---|
2208 | logger.info(msg)
|
---|
2209 | args = {'portal_status_message': msg}
|
---|
2210 | #url = self.REQUEST.get('URL1') + '?' + urlencode(args)
|
---|
2211 | url = self.REQUEST.get('URL2')
|
---|
2212 | return self.REQUEST.RESPONSE.redirect(url)
|
---|
2213 | ###)
|
---|
2214 |
|
---|
2215 | security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
|
---|
2216 | def updateStudyCourse(self):
|
---|
2217 | """update StudyCourse from CSV values"""
|
---|
2218 | import transaction
|
---|
2219 | import random
|
---|
2220 | from pdb import set_trace
|
---|
2221 | wftool = self.portal_workflow
|
---|
2222 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
|
---|
2223 | students_folder = self.portal_url.getPortalObject().campus.students
|
---|
2224 | csv_d = {'jamb_reg_no': "RegNumber",
|
---|
2225 | 'jamb_lastname': "Name",
|
---|
2226 | 'session': "Session",
|
---|
2227 | 'pume_tot_score': "PUME SCORE",
|
---|
2228 | 'jamb_score': "JambScore",
|
---|
2229 | 'jamb_sex': "Sex",
|
---|
2230 | 'jamb_state': "State",
|
---|
2231 | ## 'jamb_first_cos': "AdminCourse",
|
---|
2232 | 'faculty': "AdminFaculty",
|
---|
2233 | 'course_code': "AdmitCoscode",
|
---|
2234 | 'stud_status':"AdmitStatus",
|
---|
2235 | 'department': "AdmitDept",
|
---|
2236 | 'jamb_lga': "LGA",
|
---|
2237 | 'app_email': "email",
|
---|
2238 | 'app_mobile': "PhoneNumbers",
|
---|
2239 | }
|
---|
2240 | csv_fields = [f[1] for f in csv_d.items()]
|
---|
2241 | tr_count = 0
|
---|
2242 | total = 0
|
---|
2243 | #name = 'pume_results'
|
---|
2244 | name = 'StudyCourseChange'
|
---|
2245 | no_import = []
|
---|
2246 | s = ','.join(['"%s"' % fn for fn in csv_fields])
|
---|
2247 | no_import.append('"Error",%s' % s)
|
---|
2248 | format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
|
---|
2249 | no_certificate = "no certificate %s" % format
|
---|
2250 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
|
---|
2251 | '\n'.join(no_import))
|
---|
2252 | logger = logging.getLogger('Students.StudentsFolder.updateStudyCourse')
|
---|
2253 | logger.info('Start loading from %s.csv' % name)
|
---|
2254 | l = self.portal_catalog({'meta_type': "Certificate"})
|
---|
2255 | try:
|
---|
2256 | result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
|
---|
2257 | except:
|
---|
2258 | logger.error('Error reading %s.csv' % name)
|
---|
2259 | return
|
---|
2260 | for jamb in result:
|
---|
2261 | jamb['Error'] = "Processing "
|
---|
2262 | logger.info(format % jamb)
|
---|
2263 | jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
|
---|
2264 | res = self.portal_catalog({'portal_type': "StudentApplication",
|
---|
2265 | 'SearchableText': jamb_reg_no })
|
---|
2266 | if not res:
|
---|
2267 | em = 'Student with jamb_reg_no %s does not exists\n' % jamb_reg_no
|
---|
2268 | logger.info(em)
|
---|
2269 | jamb['Error'] = "Student does not exist"
|
---|
2270 | no_import.append(format % jamb)
|
---|
2271 | continue
|
---|
2272 | sid = res[0].getPath().split('/')[-2]
|
---|
2273 | cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
|
---|
2274 | res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
|
---|
2275 | if not res:
|
---|
2276 | em = 'No Certificate with ID %s \n' % cert_id
|
---|
2277 | logger.info(em)
|
---|
2278 | jamb['Error'] = "No Certificate %s" % cert_id
|
---|
2279 | no_import.append( format % jamb)
|
---|
2280 | continue
|
---|
2281 | cert_brain = res[0]
|
---|
2282 | catalog_entry = {}
|
---|
2283 | student = getattr(self,sid)
|
---|
2284 | #
|
---|
2285 | # Study Course
|
---|
2286 | #
|
---|
2287 | study_course = student.study_course
|
---|
2288 | dsc = {}
|
---|
2289 | cert_pl = cert_brain.getPath().split('/')
|
---|
2290 | catalog_entry['id'] = sid
|
---|
2291 | catalog_entry['faculty'] = cert_pl[-4]
|
---|
2292 | catalog_entry['department'] = cert_pl[-3]
|
---|
2293 | catalog_entry['course'] = cert_id
|
---|
2294 | dsc['study_course'] = cert_id
|
---|
2295 | study_course.getContent().edit(mapping=dsc)
|
---|
2296 | self.students_catalog.modifyRecord(**catalog_entry)
|
---|
2297 | if tr_count > 10:
|
---|
2298 | if len(no_import) > 1:
|
---|
2299 | open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
|
---|
2300 | '\n'.join(no_import))
|
---|
2301 | no_import = []
|
---|
2302 | em = '%d transactions commited\n' % tr_count
|
---|
2303 | transaction.commit()
|
---|
2304 | logger.info(em)
|
---|
2305 | total += tr_count
|
---|
2306 | tr_count = 0
|
---|
2307 | tr_count += 1
|
---|
2308 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
|
---|
2309 | ###) |
---|