1 | ## Script (Python) "purgeSFPayments" |
---|
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: purgeSFPayments.py 3132 2008-02-08 21:10:38Z 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'): |
---|
22 | return |
---|
23 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
24 | |
---|
25 | import logging |
---|
26 | import DateTime |
---|
27 | logger = logging.getLogger('Skins.purgeSFPayments') |
---|
28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
29 | |
---|
30 | paid_status = ('school_fee_paid','courses_registered','courses_validated') |
---|
31 | students = context.students_catalog(review_state = paid_status) |
---|
32 | student_total = len(students) |
---|
33 | counter = 0 |
---|
34 | counter_unpaid = 0 |
---|
35 | commit_after = 100 |
---|
36 | records_to_del = [] |
---|
37 | student_count = 0 |
---|
38 | for student in students: |
---|
39 | student_count += 1 |
---|
40 | to_del = [] |
---|
41 | student_id = student.id |
---|
42 | payments_folder = getattr(getattr(students_folder,student_id),'payments',None) |
---|
43 | payments = context.payments_catalog(student_id = student_id) |
---|
44 | for payment in payments: |
---|
45 | if payment.status != 'paid': |
---|
46 | counter_unpaid += 1 |
---|
47 | if payment.status != 'started' or payment.category != 'schoolfee': |
---|
48 | continue |
---|
49 | counter += 1 |
---|
50 | pid = 'p' + payment.order_id[6:] |
---|
51 | #logger.info('to be removed: %s, %s, %s, %s, %s' % (payment.student_id, sbrain.review_state, payment.order_id, payment.status, payment.category)) |
---|
52 | if getattr(payments_folder,pid,False): |
---|
53 | to_del += pid, |
---|
54 | logger.info('object deleted: %s (%d of %d), %s' % (payment.student_id, student_count, student_total, pid)) |
---|
55 | else: |
---|
56 | logger.info('no object: %s, %s' % (payment.student_id, pid)) |
---|
57 | records_to_del.append(payment.order_id) |
---|
58 | #context.payments_catalog.deleteRecord(payment.order_id) |
---|
59 | if not counter % commit_after: |
---|
60 | context.waeup_tool.doCommit() |
---|
61 | logger.info("committing %d of total %d" % (commit_after,counter)) |
---|
62 | #if counter > 30: |
---|
63 | # return |
---|
64 | payments_folder.manage_delObjects(to_del) |
---|
65 | context.waeup_tool.doCommit() |
---|
66 | logger.info("%d committed" % (counter)) |
---|
67 | |
---|
68 | for uid in records_to_del: |
---|
69 | context.payments_catalog.deleteRecord(uid) |
---|
70 | logger.info("%s deleted" % (uid)) |
---|
71 | |
---|
72 | logger.info("finished, %d of %d unpaid payment objects removed" % (counter, counter_unpaid)) |
---|
73 | return 'finished!' |
---|
74 | |
---|
75 | |
---|