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 2963 2007-12-20 08:23:46Z henrik $ |
---|
11 | """ |
---|
12 | """ |
---|
13 | try: |
---|
14 | from Products.zdb import set_trace |
---|
15 | except: |
---|
16 | def set_trace(): |
---|
17 | pass |
---|
18 | |
---|
19 | |
---|
20 | # purgePayments is obsolete. We need a new function which uses the payments_catalog |
---|
21 | # and which also purges accommodation payments correctly. |
---|
22 | |
---|
23 | return |
---|
24 | |
---|
25 | mtool = context.portal_membership |
---|
26 | member = mtool.getAuthenticatedMember() |
---|
27 | if str(member) not in ('admin','joachim'): |
---|
28 | return |
---|
29 | |
---|
30 | |
---|
31 | import logging |
---|
32 | import DateTime |
---|
33 | logger = logging.getLogger('Skins.purgePayments') |
---|
34 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
35 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
36 | aq_student = context.students_catalog.evalAdvancedQuery |
---|
37 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
38 | |
---|
39 | request = context.REQUEST |
---|
40 | session = request.SESSION |
---|
41 | response = request.RESPONSE |
---|
42 | setheader = request.RESPONSE.setHeader |
---|
43 | #def rwrite(s): |
---|
44 | # response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
45 | # response.write("%s<br>\n\r" % s) |
---|
46 | |
---|
47 | count = 1 |
---|
48 | d = {} |
---|
49 | l = [] |
---|
50 | sum_total = 0.0 |
---|
51 | log_after = 100 |
---|
52 | logger.info("start") |
---|
53 | query = Eq('portal_type','Payment') & Eq('review_state','opened') |
---|
54 | payments = aq_portal(query,sortSpecs=('container_path',)) |
---|
55 | total = len(payments) |
---|
56 | logger.info("found %d payments" % total) |
---|
57 | #return |
---|
58 | sid = "xxx" |
---|
59 | to_del = [] |
---|
60 | delete_count = 0 |
---|
61 | delete_count_student = 1 |
---|
62 | keep_count = 0 |
---|
63 | payments_folder = None |
---|
64 | count = 0 |
---|
65 | commit_after = 50 |
---|
66 | for payment in payments: |
---|
67 | p_sid = payment.getPath().split('/')[-3] |
---|
68 | if sid != p_sid: |
---|
69 | if count and to_del: |
---|
70 | if not delete_count_student % commit_after: |
---|
71 | context.waeup_tool.doCommit() |
---|
72 | logger.info("committing %d total %d" % (commit_after,delete_count_student)) |
---|
73 | msg = "(%d/%d) found %d payments to remove, %d to keep for %s" % (count, |
---|
74 | delete_count_student % commit_after, |
---|
75 | len(to_del), |
---|
76 | student_keep_count, |
---|
77 | sid) |
---|
78 | #rwrite( msg) |
---|
79 | delete_count_student += 1 |
---|
80 | logger.info(msg) |
---|
81 | payments_folder = getattr(getattr(students_folder,sid),'payments',None) |
---|
82 | msg = "deleted %s of %s" % (' '.join(to_del),sid) |
---|
83 | payments_folder.manage_delObjects(to_del) |
---|
84 | logger.info(msg) |
---|
85 | elif count and not to_del: |
---|
86 | msg = "%d: found %d payments to keep for %s in review_state %s" % (count, |
---|
87 | student_keep_count, |
---|
88 | sid, |
---|
89 | review_state,) |
---|
90 | #rwrite( msg) |
---|
91 | sid = p_sid |
---|
92 | count += 1 |
---|
93 | to_del = [] |
---|
94 | not_del_count = 0 |
---|
95 | student_keep_count = 0 |
---|
96 | res = aq_student(Eq('id',sid)) |
---|
97 | if not res:# or payments_folder is None: |
---|
98 | logger.info("student_id %s not found" % pin.student) |
---|
99 | continue |
---|
100 | student = res[0] |
---|
101 | review_state = student.review_state |
---|
102 | if review_state in ("school_fee_paid", |
---|
103 | "courses_registered", |
---|
104 | "courses_validated", |
---|
105 | ): |
---|
106 | delete_count += 1 |
---|
107 | to_del += payment.getId, |
---|
108 | # if payment.review_state == "opened": |
---|
109 | # #rwrite("%s : %s" % (sid,payment.Title)) |
---|
110 | # delete_count += 1 |
---|
111 | # to_del += payment.getId, |
---|
112 | # else: |
---|
113 | # keep_count += 1 |
---|
114 | # student_keep_count += 1 |
---|
115 | else: |
---|
116 | keep_count += 1 |
---|
117 | student_keep_count += 1 |
---|
118 | |
---|
119 | logger.info("%d of %d to purge %d to keep" % (delete_count,total,keep_count)) |
---|