1 | ## Script (Python) "purgePayments.py" |
---|
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: purgePayments.py 2884 2007-12-08 15:30:09Z 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.purgePayments') |
---|
28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
29 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
30 | aq_student = context.students_catalog.evalAdvancedQuery |
---|
31 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
32 | |
---|
33 | request = context.REQUEST |
---|
34 | session = request.SESSION |
---|
35 | response = request.RESPONSE |
---|
36 | setheader = request.RESPONSE.setHeader |
---|
37 | #def rwrite(s): |
---|
38 | # response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
39 | # response.write("%s<br>\n\r" % s) |
---|
40 | |
---|
41 | count = 1 |
---|
42 | d = {} |
---|
43 | l = [] |
---|
44 | sum_total = 0.0 |
---|
45 | log_after = 100 |
---|
46 | logger.info("start") |
---|
47 | query = Eq('portal_type','Payment') & Eq('review_state','opened') |
---|
48 | payments = aq_portal(query,sortSpecs=('container_path',)) |
---|
49 | total = len(payments) |
---|
50 | logger.info("found %d payments" % total) |
---|
51 | #return |
---|
52 | sid = "xxx" |
---|
53 | to_del = [] |
---|
54 | delete_count = 0 |
---|
55 | delete_count_student = 1 |
---|
56 | keep_count = 0 |
---|
57 | payments_folder = None |
---|
58 | #set_trace() |
---|
59 | count = 0 |
---|
60 | s_payments = {} |
---|
61 | commit_after = 50 |
---|
62 | #set_trace() |
---|
63 | for payment in payments: |
---|
64 | p_sid = payment.getPath().split('/')[-3] |
---|
65 | # if not s_payments.has_key(p_sid): |
---|
66 | # s_payments[p_sid] = [payment,] |
---|
67 | # else: |
---|
68 | # s_payments[p_sid] = context.waeup_tool.addtodict(s_payments,p_sid, payment) |
---|
69 | if sid != p_sid: |
---|
70 | if count and to_del: |
---|
71 | if not delete_count_student % commit_after: |
---|
72 | context.waeup_tool.doCommit() |
---|
73 | logger.info("committing %d total %d" % (commit_after,delete_count_student)) |
---|
74 | delete_count_student += 1 |
---|
75 | msg = "(%d) found %d payments to remove, %d to keep for %s" % (count, |
---|
76 | len(to_del), |
---|
77 | student_keep_count, |
---|
78 | sid) |
---|
79 | #rwrite( msg) |
---|
80 | payments_folder = getattr(getattr(students_folder,sid),'payments',None) |
---|
81 | logger.info("deleting %s of %s %d" % (' '.join(to_del),sid,delete_count_student)) |
---|
82 | payments_folder.manage_delObjects(to_del) |
---|
83 | logger.info(msg) |
---|
84 | elif count and not to_del: |
---|
85 | msg = "%d: found %d payments to keep for %s in review_state %s" % (count, |
---|
86 | student_keep_count, |
---|
87 | sid, |
---|
88 | review_state) |
---|
89 | #rwrite( msg) |
---|
90 | sid = p_sid |
---|
91 | count += 1 |
---|
92 | to_del = [] |
---|
93 | not_del_count = 0 |
---|
94 | student_keep_count = 0 |
---|
95 | res = aq_student(Eq('id',sid)) |
---|
96 | if not res:# or payments_folder is None: |
---|
97 | logger.info("student_id %s not found" % pin.student) |
---|
98 | continue |
---|
99 | student = res[0] |
---|
100 | review_state = student.review_state |
---|
101 | if review_state in ("school_fee_paid", |
---|
102 | "courses_registered", |
---|
103 | "courses_validated", |
---|
104 | ): |
---|
105 | if payment.review_state == "opened": |
---|
106 | #rwrite("%s : %s" % (sid,payment.Title)) |
---|
107 | delete_count += 1 |
---|
108 | to_del += payment.getId, |
---|
109 | else: |
---|
110 | keep_count += 1 |
---|
111 | student_keep_count += 1 |
---|
112 | else: |
---|
113 | keep_count += 1 |
---|
114 | student_keep_count += 1 |
---|
115 | |
---|
116 | logger.info("%d of %d to purge %d to keep" % (delete_count,total,keep_count)) |
---|