source: WAeUP_SRP/trunk/skins/waeup_utilities/ti_71fc_resolve.py @ 10361

Last change on this file since 10361 was 3107, checked in by joachim, 17 years ago

should fix the impossible

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
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"""
13try:
14    from Products.zdb import set_trace
15except:
16    def set_trace():
17        pass
18
19mtool = context.portal_membership
20member = mtool.getAuthenticatedMember()
21if str(member) not in ('admin','joachim'):
22    return
23
24
25import logging
26import DateTime
27logger = logging.getLogger('Skins.ti_71fc_resolve')
28from Products.AdvancedQuery import Eq, Between, Le,In
29aq_students = context.students_catalog.evalAdvancedQuery
30aq_portal = context.portal_catalog_real.evalAdvancedQuery
31course_results = context.course_results
32aq_course_results = course_results.evalAdvancedQuery
33students_folder = context.portal_url.getPortalObject().campus.students
34
35request = context.REQUEST
36session = request.SESSION
37response = request.RESPONSE
38setheader = request.RESPONSE.setHeader
39def rwrite(s):
40    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
41    response.write("%s<br>\n\r" % s)
42levels_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
49query = In('id',levels_to_look_for) & Eq('portal_type','StudentStudyLevel')
50levels= aq_portal(query)
51logger.info('found %d students with levels %s' % (len(levels), " ".join(levels_to_look_for)))
52students = {}
53for level in levels:
54    student_id = level.getPath().split('/')[-3]
55    students[student_id] = level.getId
56course_results_to_change = []
57for 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            if hasattr(study_course,new_level):
67                logger.info('%s new level %s already exists' % (student_id,new_level))
68                continue
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))
84        else:
85            logger.info('%s no level-object for %s' % (student_id,level))
86#set_trace()
87for 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   
97logger.info('finished')
Note: See TracBrowser for help on using the repository browser.