[5211] | 1 | ## Script (Python) "fix_session" |
---|
| 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 | # $Id: fix_session_in_course_results.py 5695 2011-02-01 07:12:14Z henrik $ |
---|
| 11 | """ |
---|
| 12 | """ |
---|
| 13 | try: |
---|
| 14 | from Products.zdb import set_trace |
---|
| 15 | except: |
---|
| 16 | def set_trace(): |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | mtool = context.portal_membership |
---|
| 20 | member = mtool.getAuthenticatedMember() |
---|
| 21 | if str(member) not in ('admin',): |
---|
| 22 | return |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | import logging |
---|
| 26 | import DateTime |
---|
| 27 | logger = logging.getLogger('fix_session_in_course_results') |
---|
| 28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 29 | |
---|
| 30 | course_results = context.course_results |
---|
| 31 | students = context.portal_url.getPortalObject().campus.students |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | brains = course_results() |
---|
| 35 | total = len(brains) |
---|
| 36 | edited = 0 |
---|
| 37 | for brain in brains: |
---|
| 38 | if not students.hasObject(brain.student_id): |
---|
| 39 | logger.info("no student %s" % student_id) |
---|
| 40 | continue |
---|
| 41 | student = getattr(students, brain.student_id) |
---|
| 42 | if not student.hasObject("study_course"): |
---|
| 43 | logger.info("%s: no study_course" % brain.student_id) |
---|
| 44 | continue |
---|
| 45 | study_course = student.study_course |
---|
| 46 | if not study_course.hasObject(brain.level_id): |
---|
| 47 | logger.info("%s: no level %s" % (brain.student_id,brain.level_id)) |
---|
| 48 | continue |
---|
| 49 | level_obj = getattr(study_course, brain.level_id) |
---|
| 50 | level_doc = level_obj.getContent() |
---|
| 51 | session_id = level_doc.session |
---|
| 52 | if len(session_id) == 9: |
---|
| 53 | session_id = session_id[2:4] |
---|
| 54 | elif len(session_id) != 2: |
---|
[5695] | 55 | #logger.info("invalid session %s in level %s for student %s" % |
---|
| 56 | # (session_id,brain.level_id,brain.student_id)) |
---|
| 57 | continue |
---|
[5211] | 58 | if brain.session_id != session_id: |
---|
| 59 | data = {} |
---|
[5695] | 60 | old_session_id = brain.session_id |
---|
[5211] | 61 | data['key'] = brain.key |
---|
| 62 | data['session_id'] = session_id |
---|
| 63 | course_results.modifyRecord(**data) |
---|
| 64 | logger.info("%s: %s overwritten with %s" % (brain.key,old_session_id,session_id)) |
---|
| 65 | edited += 1 |
---|
| 66 | |
---|
| 67 | logger.info("edited %d course results of %s total" % (edited,total)) |
---|