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 3098 2008-02-04 08:58:20Z 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 |
---|
30 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
31 | course_results = context.course_results |
---|
32 | aq_course_results = course_results.evalAdvancedQuery |
---|
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) |
---|
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))) |
---|
52 | students = {} |
---|
53 | for level in levels: |
---|
54 | student_id = level.getPath().split('/')[-3] |
---|
55 | students[student_id] = level.getId |
---|
56 | course_results_to_change = [] |
---|
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)) |
---|
63 | for level in ('110','210'): |
---|
64 | if hasattr(study_course,level): |
---|
65 | new_level = "%d" % (int(level) + 90) |
---|
66 | study_course.manage_renameObjects((level,),(new_level,)) |
---|
67 | logger.info('%s renamed level from %s to %s' % (student_id,level,new_level)) |
---|
68 | query = Eq('student_id',student_id) & Eq('level_id',level) |
---|
69 | results = aq_course_results(query) |
---|
70 | if results: |
---|
71 | for result in results: |
---|
72 | d = {} |
---|
73 | for field in course_results.schema(): |
---|
74 | d[field] = getattr(result,field) |
---|
75 | d['level_id'] = new_level |
---|
76 | d['old_key'] = d['key'] |
---|
77 | d['key'] = d['key'].replace(level,new_level) |
---|
78 | course_results_to_change += d, |
---|
79 | else: |
---|
80 | logger.info('%s no course_results for %s' % (student_id,level)) |
---|
81 | else: |
---|
82 | logger.info('%s no level-object for %s' % (student_id,level)) |
---|
83 | #set_trace() |
---|
84 | for result in course_results_to_change: |
---|
85 | try: |
---|
86 | course_results.addRecord(**result) |
---|
87 | logger.info('%(student_id)s adding %(key)s' % result) |
---|
88 | except ValueError: |
---|
89 | set_trace() |
---|
90 | logger.info('%(student_id)s %(key)s already there' % result) |
---|
91 | logger.info('%(student_id)s deleting %(old_key)s' % result) |
---|
92 | course_results.deleteRecord(result['old_key']) |
---|
93 | |
---|
94 | logger.info('finished') |
---|