1 | ## Script (Python) "logged_in" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | ## |
---|
10 | """Prepare user login |
---|
11 | modified from cps_default/logged_in.py |
---|
12 | |
---|
13 | $Id: logged_in.py 1933 2007-06-19 05:05:29Z henrik $ |
---|
14 | """ |
---|
15 | try: |
---|
16 | from Products.zdb import set_trace |
---|
17 | except: |
---|
18 | def set_trace(): |
---|
19 | pass |
---|
20 | import DateTime |
---|
21 | import os |
---|
22 | current = DateTime.DateTime() |
---|
23 | import logging |
---|
24 | logger = logging.getLogger('Skins.logged_in') |
---|
25 | |
---|
26 | |
---|
27 | from urllib import unquote |
---|
28 | |
---|
29 | request = context.REQUEST |
---|
30 | response = request.RESPONSE |
---|
31 | utool = context.portal_url |
---|
32 | mtool = context.portal_membership |
---|
33 | wftool = context.portal_workflow |
---|
34 | portal = utool.getPortalObject() |
---|
35 | |
---|
36 | redirect_to_portal = False |
---|
37 | is_anon = mtool.isAnonymousUser() |
---|
38 | member = mtool.getAuthenticatedMember() |
---|
39 | member_id = str(member) |
---|
40 | |
---|
41 | try: |
---|
42 | cred = context.waeup_tool.getCredentialFromAuthHeader(request) |
---|
43 | except: |
---|
44 | cred = "no authentication header" |
---|
45 | |
---|
46 | if context.isStaff(): |
---|
47 | is_unsecure = context.waeup_tool.checkGenericPassword(member_id) |
---|
48 | if is_unsecure: |
---|
49 | response.expireCookie('__ac', path='/') |
---|
50 | return response.redirect("%s/user_logged_in_disabled" % context.portal_url()) |
---|
51 | |
---|
52 | #load_passport = hasattr(context.waeup_tool,'loadStudentFoto') |
---|
53 | |
---|
54 | if not is_anon: |
---|
55 | if "Student" in member.getRoles(): |
---|
56 | students = context.portal_url.getPortalObject().campus.students |
---|
57 | student = getattr(students,member_id) |
---|
58 | student_app = getattr(student,'application',None) |
---|
59 | student_per = getattr(student,'personal',None) |
---|
60 | |
---|
61 | |
---|
62 | ######################################################### |
---|
63 | |
---|
64 | # perform makeStudentData for returning students who login for the first time |
---|
65 | # the returning key comes from set_access_data |
---|
66 | |
---|
67 | if request.has_key('returning') and student_per is None: |
---|
68 | email=request.get("email") |
---|
69 | phone=request.get("phone_nr") |
---|
70 | context.waeup_tool.makeStudentData(member_id, |
---|
71 | email=email, |
---|
72 | phone_nr=phone, |
---|
73 | ) |
---|
74 | #d = {} |
---|
75 | #d['id'] = member_id |
---|
76 | #d['email'] = email |
---|
77 | #d['phone'] = phone |
---|
78 | #context.students_catalog.modifyRecord(**d) |
---|
79 | elif student_per is None: |
---|
80 | context.waeup_tool.makeStudentData(member_id) |
---|
81 | |
---|
82 | ######################################################### |
---|
83 | |
---|
84 | # add missing payments folder |
---|
85 | |
---|
86 | if 'payments' not in student.objectIds(): |
---|
87 | student.invokeFactory('PaymentsFolder','payments') |
---|
88 | payments = getattr(student,'payments') |
---|
89 | wftool.doActionFor(payments,'open') |
---|
90 | d = {} |
---|
91 | d['Title'] = 'Online Payments' |
---|
92 | payments.getContent().edit(mapping=d) |
---|
93 | |
---|
94 | ######################################################### |
---|
95 | |
---|
96 | #student_pume = getattr(student,'pume',None) |
---|
97 | s_review_state = context.getStudentReviewState(student.id) |
---|
98 | a_review_state = wftool.getInfoFor(student_app,'review_state',None) |
---|
99 | student_per = getattr(student,'personal',None) |
---|
100 | p_review_state = wftool.getInfoFor(student_per,'review_state',None) |
---|
101 | logger.info('%s logged in, review_state %s' % (member,s_review_state)) |
---|
102 | if student_app is None: |
---|
103 | logger.info('%s logged in, without application object' % (member)) |
---|
104 | else: |
---|
105 | app_doc = student_app.getContent() |
---|
106 | |
---|
107 | res = context.students_catalog(id = member_id) |
---|
108 | matric_no = res[0].matric_no |
---|
109 | jamb_reg_no = res[0].jamb_reg_no |
---|
110 | entry_mode = res[0].entry_mode |
---|
111 | verdict = res[0].verdict |
---|
112 | level = res[0].level |
---|
113 | has_level = level |
---|
114 | has_verdict = verdict and not verdict == 'N/A' |
---|
115 | |
---|
116 | ######################################################### |
---|
117 | |
---|
118 | # save email and phone of returning students after all objects have been created |
---|
119 | |
---|
120 | if request.has_key('returning') and student_app: |
---|
121 | email=request.get("email") |
---|
122 | phone=request.get("phone_nr") |
---|
123 | # should be always closed, however ... |
---|
124 | #if a_review_state != 'opened': |
---|
125 | # wftool.doActionFor(student_app,'open') |
---|
126 | # should be always opened after import, however ... |
---|
127 | if p_review_state != 'opened': |
---|
128 | wftool.doActionFor(student_per,'open') |
---|
129 | per_doc = student_per.getContent() |
---|
130 | #app_doc.edit(mapping = {'app_email' : email}) |
---|
131 | per_doc.edit(mapping = {'email' : email, 'phone' : phone}) |
---|
132 | #wftool.doActionFor(student_app,'close') |
---|
133 | |
---|
134 | ######################################################### |
---|
135 | |
---|
136 | # look for passport pictures of returning students |
---|
137 | |
---|
138 | if s_review_state == 'returning' and\ |
---|
139 | not context.waeup_tool.picturesExist(('passport',), member_id): |
---|
140 | folder = 'pictures_returning' |
---|
141 | #res = context.students_catalog(id = member_id) |
---|
142 | filename = res[0].matric_no.upper() |
---|
143 | msg = context.waeup_tool.loadStudentFoto(student,filename,folder) |
---|
144 | logger.info('%s (%s), %s' % (member,s_review_state,msg)) |
---|
145 | |
---|
146 | ######################################################### |
---|
147 | |
---|
148 | # look for passport pictures of transfer students |
---|
149 | |
---|
150 | if entry_mode == 'transfer' and\ |
---|
151 | s_review_state in ('returning','school_fee_paid',) and\ |
---|
152 | not context.waeup_tool.picturesExist(('passport',), member_id): |
---|
153 | res_both = context.students_catalog(jamb_reg_no = jamb_reg_no) |
---|
154 | #set_trace() |
---|
155 | if len(res_both) == 2: |
---|
156 | for ts in res_both: |
---|
157 | if ts.id != member_id: |
---|
158 | msg = context.waeup_tool.loadTransferStudentFoto(ts.id,member_id) |
---|
159 | logger.info('%s (%s on transfer), %s' % (member,s_review_state,msg)) |
---|
160 | |
---|
161 | ######################################################### |
---|
162 | |
---|
163 | # look for passport pictures of new students |
---|
164 | |
---|
165 | if s_review_state in ('admitted', |
---|
166 | 'student_created', |
---|
167 | 'clearance_pin_entered', |
---|
168 | 'clearance_requested' |
---|
169 | ) and not context.waeup_tool.picturesExist(('passport',), |
---|
170 | member_id): |
---|
171 | folder = 'pictures_admitted_latest' |
---|
172 | #filename = app_doc.jamb_reg_no.replace('/','_') |
---|
173 | filename = member_id |
---|
174 | msg = context.waeup_tool.loadStudentFoto(student,filename,folder) |
---|
175 | logger.info('%s (%s), %s' % (member,s_review_state,msg)) |
---|
176 | if 'passport picture not found' in msg: |
---|
177 | filename = app_doc.app_reg_pin.replace('-','') |
---|
178 | msg = context.waeup_tool.loadStudentFoto(student,filename,folder) |
---|
179 | logger.info('%s (%s), %s' % (member,s_review_state,msg)) |
---|
180 | |
---|
181 | ######################################################### |
---|
182 | |
---|
183 | # perform necessary updates for new students |
---|
184 | |
---|
185 | # 1. add pin and application date to app_doc |
---|
186 | |
---|
187 | if s_review_state == "student_created": |
---|
188 | wftool.doActionFor(student,'admit') |
---|
189 | s_review_state = 'admitted' |
---|
190 | |
---|
191 | if s_review_state == "admitted" and a_review_state == 'created': |
---|
192 | wftool.doActionFor(student_app,'open') |
---|
193 | #if student_pume is not None: |
---|
194 | # wftool.doActionFor(student_pume,'close') |
---|
195 | da = {} |
---|
196 | pin = request.get('pin') |
---|
197 | |
---|
198 | # if the student comes directly, add missing pin or pin with wrong syntax (fix) |
---|
199 | if not pin: |
---|
200 | jamb_reg_no = app_doc.jamb_reg_no |
---|
201 | for reg_no in (jamb_reg_no,jamb_reg_no.lower(),jamb_reg_no.upper()): |
---|
202 | res = context.portal_pins(student=reg_no) |
---|
203 | if len(res) > 0: |
---|
204 | break |
---|
205 | if len(res) > 0: |
---|
206 | p = res[0].pin |
---|
207 | if len(p) > 10: |
---|
208 | if p.startswith('IPTP'): |
---|
209 | pin = "%s-%s-%s" % (p[:4],p[4:5],p[5:]) |
---|
210 | else: |
---|
211 | pin = "%s-%s-%s" % (p[:3],p[3:-10],p[-10:]) |
---|
212 | else: |
---|
213 | pin = p |
---|
214 | da['app_ac_pin'] = pin |
---|
215 | da['app_ac_date'] = current |
---|
216 | app_doc.edit(mapping = da) |
---|
217 | |
---|
218 | # 2. same as 1 but without opening app_doc, assuming that the student already logged in |
---|
219 | # but the pin is missing in app_doc (fix) |
---|
220 | |
---|
221 | elif s_review_state in ("admitted") and a_review_state == 'opened' and\ |
---|
222 | (not app_doc.app_ac_pin or app_doc.app_ac_pin.startswith('IPT-P')): |
---|
223 | jamb_reg_no = app_doc.jamb_reg_no |
---|
224 | for reg_no in (jamb_reg_no,jamb_reg_no.lower(),jamb_reg_no.upper()): |
---|
225 | res = context.portal_pins(student=reg_no) |
---|
226 | if len(res) > 0: |
---|
227 | break |
---|
228 | if len(res) > 0: |
---|
229 | p = res[0].pin |
---|
230 | if len(p) > 10: |
---|
231 | if p.startswith('IPTP'): |
---|
232 | pin = "%s-%s-%s" % (p[:4],p[4:5],p[5:]) |
---|
233 | else: |
---|
234 | pin = "%s-%s-%s" % (p[:3],p[3:-10],p[-10:]) |
---|
235 | else: |
---|
236 | pin = p |
---|
237 | da = {} |
---|
238 | da['app_ac_pin'] = pin |
---|
239 | app_doc.edit(mapping = da) |
---|
240 | |
---|
241 | # 3. open clearance object and close application object in state clearance_pin_entered |
---|
242 | |
---|
243 | elif s_review_state == "clearance_pin_entered": |
---|
244 | student_clr = getattr(student,'clearance',None) |
---|
245 | clr_review_state = wftool.getInfoFor(student_clr,'review_state',None) |
---|
246 | if a_review_state != 'closed': |
---|
247 | wftool.doActionFor(student_app,'close') |
---|
248 | if clr_review_state != 'opened': |
---|
249 | wftool.doActionFor(student_clr,'open') |
---|
250 | |
---|
251 | # 4. open personal object (fix). This should be done in clearance_edit. |
---|
252 | |
---|
253 | if p_review_state == 'created': |
---|
254 | wftool.doActionFor(student_per,'open') |
---|
255 | |
---|
256 | ######################################################### |
---|
257 | |
---|
258 | #if matric_no: |
---|
259 | # has_results = context.results_import(matric_no = matric_no) |
---|
260 | #else: |
---|
261 | # has_results = '' |
---|
262 | |
---|
263 | ######################################################### |
---|
264 | |
---|
265 | # fetch current verdict via getVerdict and level via getLevelFromResultsCosCode |
---|
266 | |
---|
267 | #if s_review_state in ('returning','school_fee_paid') and not (has_verdict and has_level): |
---|
268 | # if has_results: |
---|
269 | # study_course = getattr(student,'study_course') |
---|
270 | # sc_review_state = wftool.getInfoFor(study_course,'review_state',None) |
---|
271 | # dsc = {} |
---|
272 | # if not has_verdict and s_review_state == 'returning': |
---|
273 | # dsc['current_verdict'] = context.getVerdict(has_results[0].Verdict)[0] |
---|
274 | # if not has_level and s_review_state == 'returning': |
---|
275 | # lnr = context.getLevelFromResultsCosCode(has_results) |
---|
276 | # dsc['current_level'] = "%d00" % lnr |
---|
277 | # elif not has_level and s_review_state == 'school_fee_paid': |
---|
278 | # lnr = context.getLevelFromResultsCosCode(has_results) |
---|
279 | # cv = context.getVerdict(has_results[0].Verdict)[0] |
---|
280 | # if cv in ('A','B',): |
---|
281 | # lnr += 1 |
---|
282 | # dsc['current_level'] = "%d00" % lnr |
---|
283 | # dsc['previous_verdict'] = cv |
---|
284 | # if sc_review_state != 'opened': |
---|
285 | # wftool.doActionFor(study_course,'open') |
---|
286 | # study_course_doc =study_course.getContent() |
---|
287 | # study_course_doc.edit(mapping = dsc) |
---|
288 | # wftool.doActionFor(study_course,'close_for_edit') |
---|
289 | |
---|
290 | ######################################################### |
---|
291 | |
---|
292 | |
---|
293 | # determine appropriate redirect url |
---|
294 | |
---|
295 | if s_review_state == "application_pin_entered": |
---|
296 | redirect_url = "%s/application_edit_form" % student.absolute_url() |
---|
297 | elif s_review_state in ('admitted', 'objection_raised',): |
---|
298 | redirect_url = "%s/admission_form" % student.absolute_url() |
---|
299 | elif s_review_state == "clearance_pin_entered": |
---|
300 | redirect_url = "%s/clearance_edit_form" % student.absolute_url() |
---|
301 | elif s_review_state == "cleared_and_validated": |
---|
302 | redirect_url = "%s/personal_edit_form" % student.absolute_url() |
---|
303 | #elif s_review_state == "returning" and has_results: |
---|
304 | # redirect_url = "%s/session_results_view" % student.absolute_url() |
---|
305 | elif s_review_state in ('school_fee_paid','courses_registered'): |
---|
306 | redirect_url = "%s/study_course/study_course_view" % student.absolute_url() |
---|
307 | else: |
---|
308 | redirect_url = "%s/student_index" % student.absolute_url() |
---|
309 | |
---|
310 | |
---|
311 | else: |
---|
312 | logger.info('%s logged in' % (member)) |
---|
313 | redirect_url = portal.absolute_url() |
---|
314 | #Anonymous |
---|
315 | else: |
---|
316 | logger.info('failed login with %s' % cred) |
---|
317 | response.expireCookie('__ac', path='/') |
---|
318 | return context.user_logged_in_failed() |
---|
319 | |
---|
320 | # Setup skins |
---|
321 | if (getattr(utool, 'updateSkinCookie', False) and |
---|
322 | utool.updateSkinCookie()): |
---|
323 | context.setupCurrentSkin() |
---|
324 | |
---|
325 | response.redirect(redirect_url) |
---|