| 1 | ## Script (Python) "purgeCourseResultsObjects" |
|---|
| 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: purgeCourseResultsObjects.py 3206 2008-02-23 22:59:10Z 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','joachim'): |
|---|
| 22 | return |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | import logging |
|---|
| 26 | import DateTime |
|---|
| 27 | logger = logging.getLogger('Skins.purgeCourseResultsObjects') |
|---|
| 28 | from Products.AdvancedQuery import Eq, Between, Le,In |
|---|
| 29 | aq_results = context.course_results.evalAdvancedQuery |
|---|
| 30 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
|---|
| 31 | #aq_students = context.students_catalog.evalAdvancedQuery |
|---|
| 32 | students = context.portal_url.getPortalObject().campus.students |
|---|
| 33 | request = context.REQUEST |
|---|
| 34 | session = request.SESSION |
|---|
| 35 | response = request.RESPONSE |
|---|
| 36 | setheader = request.RESPONSE.setHeader |
|---|
| 37 | |
|---|
| 38 | results = aq_portal(Eq('portal_type','StudentCourseResult')) |
|---|
| 39 | total = len(results) |
|---|
| 40 | logger.info("found %d course result objects" % (total)) |
|---|
| 41 | count = 0 |
|---|
| 42 | moved = 0 |
|---|
| 43 | cocount = 0 |
|---|
| 44 | commit_after = 20 |
|---|
| 45 | current_student_id = '' |
|---|
| 46 | current_level_id = '' |
|---|
| 47 | to_move = [] |
|---|
| 48 | for result in results: |
|---|
| 49 | count += 1 |
|---|
| 50 | pl = result.getPath().split('/') |
|---|
| 51 | student_id = pl[-4] |
|---|
| 52 | level_id = pl[-2] |
|---|
| 53 | if student_id != current_student_id: |
|---|
| 54 | if current_student_id: |
|---|
| 55 | to_move += (current_student_id,current_level_id), |
|---|
| 56 | current_student_id = student_id |
|---|
| 57 | current_level_id = level_id |
|---|
| 58 | elif level_id != current_level_id: |
|---|
| 59 | to_move += (current_student_id,current_level_id), |
|---|
| 60 | current_level_id = level_id |
|---|
| 61 | for student_id,level_id in to_move: |
|---|
| 62 | try: |
|---|
| 63 | study_level = getattr(getattr(getattr(students,student_id), |
|---|
| 64 | "study_course") |
|---|
| 65 | ,level_id) |
|---|
| 66 | except: |
|---|
| 67 | logger.info('%s level %s not found' % (student_id,level_id)) |
|---|
| 68 | continue |
|---|
| 69 | context.course_results.moveResultsHere(study_level,student_id) |
|---|
| 70 | moved += 1 |
|---|
| 71 | logger.info("moved results of student %s" % (student_id)) |
|---|
| 72 | if moved and not moved % commit_after: |
|---|
| 73 | context.waeup_tool.doCommit() |
|---|
| 74 | logger.info("Committing %s transactions, total %s" % (commit_after,len(to_move))) |
|---|
| 75 | # cocount += 1 |
|---|
| 76 | # if cocount > 2: |
|---|
| 77 | # break |
|---|
| 78 | msg = "finished moving results of %d students" % (moved) |
|---|
| 79 | logger.info(msg) |
|---|
| 80 | #rwrite(msg) |
|---|
| 81 | |
|---|
| 82 | |
|---|