[3082] | 1 | ## Script (Python) "ti_71fc_resolve" |
---|
| 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: ti_71fc_resolve.py 3107 2008-02-05 07:50:16Z joachim $ |
---|
| 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','joachim'): |
---|
| 22 | return |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | import logging |
---|
| 26 | import DateTime |
---|
| 27 | logger = logging.getLogger('Skins.ti_71fc_resolve') |
---|
| 28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 29 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
[3084] | 30 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
[3098] | 31 | course_results = context.course_results |
---|
| 32 | aq_course_results = course_results.evalAdvancedQuery |
---|
[3082] | 33 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
| 34 | |
---|
| 35 | request = context.REQUEST |
---|
| 36 | session = request.SESSION |
---|
| 37 | response = request.RESPONSE |
---|
| 38 | setheader = request.RESPONSE.setHeader |
---|
| 39 | def rwrite(s): |
---|
| 40 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
| 41 | response.write("%s<br>\n\r" % s) |
---|
[3084] | 42 | levels_to_look_for = ("110","210") |
---|
| 43 | # query = In('level',levels_to_look_for) |
---|
| 44 | # student_records = aq_students(query) |
---|
| 45 | # logger.info('found %d students with levels %s' % (len(student_records), " ".join(levels_to_look_for))) |
---|
| 46 | # students = {} |
---|
| 47 | # for student_record in student_records: |
---|
| 48 | # students[student_record.id] = student_record.level |
---|
| 49 | query = In('id',levels_to_look_for) & Eq('portal_type','StudentStudyLevel') |
---|
| 50 | levels= aq_portal(query) |
---|
| 51 | logger.info('found %d students with levels %s' % (len(levels), " ".join(levels_to_look_for))) |
---|
[3082] | 52 | students = {} |
---|
[3084] | 53 | for level in levels: |
---|
| 54 | student_id = level.getPath().split('/')[-3] |
---|
| 55 | students[student_id] = level.getId |
---|
[3098] | 56 | course_results_to_change = [] |
---|
[3082] | 57 | for student_id,level in students.items(): |
---|
| 58 | study_course = getattr(getattr(students_folder,student_id),'study_course',None) |
---|
| 59 | if study_course is None: |
---|
| 60 | logger.info('%s study_course not found' % (student_record.id)) |
---|
| 61 | continue |
---|
| 62 | #logger.info('%s renaming level' % (student_id)) |
---|
[3098] | 63 | for level in ('110','210'): |
---|
| 64 | if hasattr(study_course,level): |
---|
| 65 | new_level = "%d" % (int(level) + 90) |
---|
[3107] | 66 | if hasattr(study_course,new_level): |
---|
| 67 | logger.info('%s new level %s already exists' % (student_id,new_level)) |
---|
| 68 | continue |
---|
[3098] | 69 | study_course.manage_renameObjects((level,),(new_level,)) |
---|
| 70 | logger.info('%s renamed level from %s to %s' % (student_id,level,new_level)) |
---|
| 71 | query = Eq('student_id',student_id) & Eq('level_id',level) |
---|
| 72 | results = aq_course_results(query) |
---|
| 73 | if results: |
---|
| 74 | for result in results: |
---|
| 75 | d = {} |
---|
| 76 | for field in course_results.schema(): |
---|
| 77 | d[field] = getattr(result,field) |
---|
| 78 | d['level_id'] = new_level |
---|
| 79 | d['old_key'] = d['key'] |
---|
| 80 | d['key'] = d['key'].replace(level,new_level) |
---|
| 81 | course_results_to_change += d, |
---|
| 82 | else: |
---|
| 83 | logger.info('%s no course_results for %s' % (student_id,level)) |
---|
[3082] | 84 | else: |
---|
[3098] | 85 | logger.info('%s no level-object for %s' % (student_id,level)) |
---|
| 86 | #set_trace() |
---|
| 87 | for result in course_results_to_change: |
---|
| 88 | try: |
---|
| 89 | course_results.addRecord(**result) |
---|
| 90 | logger.info('%(student_id)s adding %(key)s' % result) |
---|
| 91 | except ValueError: |
---|
| 92 | set_trace() |
---|
| 93 | logger.info('%(student_id)s %(key)s already there' % result) |
---|
| 94 | logger.info('%(student_id)s deleting %(old_key)s' % result) |
---|
| 95 | course_results.deleteRecord(result['old_key']) |
---|
| 96 | |
---|
[3082] | 97 | logger.info('finished') |
---|