[1981] | 1 | ## Script (Python) "findStudentsWithCourses" |
---|
| 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_294_resolve.py 1983 2007-07-04 07:32:36Z henrik $ |
---|
| 11 | """ |
---|
| 12 | """ |
---|
| 13 | try: |
---|
| 14 | from Products.zdb import set_trace |
---|
| 15 | except: |
---|
| 16 | def set_trace(): |
---|
| 17 | pass |
---|
| 18 | import logging |
---|
| 19 | logger = logging.getLogger('Skins.ti_294_resolve') |
---|
| 20 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 21 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
| 22 | |
---|
| 23 | request = context.REQUEST |
---|
| 24 | session = request.SESSION |
---|
| 25 | response = request.RESPONSE |
---|
| 26 | setheader = request.RESPONSE.setHeader |
---|
| 27 | students = context.portal_url.getPortalObject().campus.students |
---|
| 28 | #from string import Template |
---|
| 29 | def rwrite(s): |
---|
| 30 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
| 31 | response.write("%s<br>\n\r" % s) |
---|
| 32 | |
---|
| 33 | #logger.info('') |
---|
| 34 | |
---|
| 35 | query = In('registered_courses',('',)) |
---|
| 36 | brains = aq_students(query) |
---|
| 37 | total = len(brains) |
---|
| 38 | logger.info('found %d students with empty course_result id' % total) |
---|
| 39 | count = 0 |
---|
| 40 | commit_after = 5000 |
---|
| 41 | todelete = [] |
---|
| 42 | for brain in brains: |
---|
| 43 | level_object = None |
---|
| 44 | # |
---|
| 45 | #set_trace() |
---|
| 46 | try: |
---|
| 47 | level_object = getattr(getattr(getattr(students,brain.id),'study_course'),brain.level) |
---|
| 48 | except: |
---|
| 49 | continue |
---|
| 50 | ids = level_object.objectIds() |
---|
| 51 | logger.info('ids of %s: %s#%s' % (brain.id,list(ids),brain.registered_courses)) |
---|
| 52 | if '' in ids: |
---|
| 53 | todelete.append((brain.id,level_object)) |
---|
| 54 | rwrite("appended %s" % (brain.id)) |
---|
[1983] | 55 | else: |
---|
| 56 | registered_courses = brain.registered_courses |
---|
| 57 | registered_courses.remove('') |
---|
| 58 | logger.info('object not found, %s' % registered_courses) |
---|
| 59 | # the following line is critical (modifying the catalog in a for brain in brains loop) |
---|
| 60 | context.students_catalog.modifyRecord(id=brain.id,registered_courses=registered_courses) |
---|
[1981] | 61 | logger.info('todelete %d' % (len(todelete))) |
---|
| 62 | for brain_id,level_object in todelete: |
---|
| 63 | count += 1 |
---|
| 64 | #set_trace() |
---|
| 65 | try: |
---|
| 66 | level_object.manage_delObjects(['',]) |
---|
| 67 | logger.info('deleted course with empty id of %s' % (brain_id)) |
---|
| 68 | rwrite("%d: deleted empty id in %s" % (count, level_object.absolute_url())) |
---|
| 69 | except: |
---|
| 70 | logger.info('could not delete course with empty in of %s' % (level_object.absolute_url())) |
---|
| 71 | rwrite("%d: could not delete empty id in %s" % (count, level_object.absolute_url())) |
---|
| 72 | if count and not count % commit_after: |
---|
| 73 | context.waeup_tool.doCommit() |
---|
| 74 | logger.info("committed %d transactions" % commit_after) |
---|
| 75 | logger.info('finished for %d of %d' % (count,total)) |
---|
| 76 | return |
---|
[1983] | 77 | |
---|