forked from emfcamp/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
411 lines (341 loc) · 14.5 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
# coding=utf-8
import ofxparse
from flask.ext.script import Command, Manager, Option
from flask import render_template
from flaskext.mail import Message
from sqlalchemy.orm.exc import NoResultFound
import random
from datetime import datetime, timedelta
from main import app, mail, db
from models import (
User, TicketType, Ticket, TicketPrice, TicketToken,
)
from models.payment import (
GoCardlessPayment,
BankPayment, BankAccount, BankTransaction,
)
from sqlalchemy import text
manager = Manager(app)
class CreateDB(Command):
def run(self):
from main import db
db.create_all()
class CreateBankAccounts(Command):
def run(self):
gbp = BankAccount('492900', '20716473590526', 'GBP')
eur = BankAccount('492900', '20716472954433', 'EUR')
for acct in [gbp, eur]:
try:
BankAccount.query.filter_by(acct_id=acct.acct_id, sort_code=acct.sort_code).one()
except NoResultFound:
app.logger.info('Adding %s account %s %s', acct.currency, acct.sort_code, acct.acct_id)
db.session.add(acct)
db.session.commit()
class LoadOfx(Command):
option_list = [Option('-f', '--file', dest='filename', help="The .ofx file to load")]
def run(self, filename):
ofx = ofxparse.OfxParser.parse(file(filename))
acct_id = ofx.account.account_id
sort_code = ofx.account.routing_number
account = BankAccount.get(sort_code, acct_id)
if ofx.account.statement.currency.lower() != account.currency.lower():
app.logger.error("Currency %s doesn't match account currency %s",
ofx.account.statement.currency, account.currency)
return
added = 0
duplicate = 0
dubious = 0
for txn in ofx.account.statement.transactions:
# date is actually dtposted and is a datetime
if txn.date < datetime(2014, 1, 1):
app.logger.debug('Ignoring historic transaction from %s', txn.date)
continue
if txn.amount <= 0:
app.logger.info('Ignoring non-credit transaction for %s', txn.amount)
continue
dbtxn = BankTransaction(
account_id=account.id,
posted=txn.date,
type=txn.type,
amount=txn.amount,
payee=txn.payee,
fit_id=txn.id,
)
# Check for matching/duplicate transactions.
# Insert if possible - conflicts can be sorted out within the app.
matches = dbtxn.get_matching()
# Euro payments have a blank fit_id
if dbtxn.fit_id == '00000000':
# There seems to be a serial in the payee field. Assume that's enough for uniqueness.
if matches.count():
app.logger.debug('Ignoring duplicate transaction from %s', dbtxn.payee)
duplicate += 1
else:
db.session.add(dbtxn)
added += 1
else:
different_fit_ids = matches.filter( BankTransaction.fit_id != dbtxn.fit_id )
same_fit_ids = matches.filter( BankTransaction.fit_id == dbtxn.fit_id )
if same_fit_ids.count():
app.logger.debug('Ignoring duplicate transaction %s', dbtxn.fit_id)
duplicate += 1
elif BankTransaction.query.filter( BankTransaction.fit_id == dbtxn.fit_id ).count():
app.logger.error('Non-matching transactions with same fit_id %s', dbtxn.fit_id)
dubious += 1
elif different_fit_ids.count():
app.logger.warn('%s matching transactions with different fit_ids for %s',
different_fit_ids.count(), dbtxn.fit_id)
# fit_id may have been changed, so add it anyway
db.session.add(dbtxn)
added += 1
dubious += 1
else:
db.session.add(dbtxn)
added += 1
db.session.commit()
app.logger.info('Import complete: %s new, %s duplicate, %s dubious',
added, duplicate, dubious)
class Reconcile(Command):
option_list = [Option('-d', '--doit', action='store_true', help="set this to actually change the db")]
def run(self, doit):
txns = BankTransaction.query.filter_by(payment_id=None, suppressed=False)
paid = 0
failed = 0
for txn in txns:
if txn.type.lower() not in ('other', 'directdep'):
raise ValueError('Unexpected transaction type for %s: %s', txn.id, txn.type)
if txn.payee.startswith("GOCARDLESS "):
app.logger.info('Suppressing GoCardless transfer %s', txn.id)
if doit:
txn.suppressed = True
db.session.commit()
continue
if txn.payee.startswith("STRIPE PAYMENTS EU "):
app.logger.info('Suppressing Stripe transfer %s', txn.id)
if doit:
txn.suppressed = True
db.session.commit()
continue
app.logger.info("Processing txn %s: %s", txn.id, txn.payee)
payment = txn.match_payment()
if not payment:
app.logger.warn("Could not match payee, skipping")
failed += 1
continue
app.logger.info("Matched to payment %s by %s for %s %s",
payment.id, payment.user.name, payment.amount, payment.currency)
if txn.amount != payment.amount:
app.logger.warn("Transaction amount %s doesn't match %s, skipping",
txn.amount, payment.amount)
failed += 1
continue
if txn.account.currency != payment.currency:
app.logger.warn("Transaction currency %s doesn't match %s, skipping",
txn.account.currency, payment.currency)
failed += 1
continue
if payment.state == 'paid':
app.logger.error("Payment %s has already been paid", payment.id)
failed += 1
continue
if doit:
txn.payment = payment
payment.paid()
db.session.commit()
msg = Message("Electromagnetic Field ticket purchase update",
sender=app.config['TICKETS_EMAIL'],
recipients=[payment.user.email])
msg.body = render_template("tickets-paid-email-banktransfer.txt",
user=payment.user, payment=payment)
mail.send(msg)
app.logger.info("Payment reconciled")
paid += 1
app.logger.info('Reconciliation complete: %s paid, %s failed', paid, failed)
class TestEmails(Command):
"""
Test our email templates
"""
def run(self):
self.make_test_user()
for p in self.user.payments.all():
if p.provider == "gocardless":
for t in ("tickets-purchased-email-gocardless.txt", "tickets-paid-email-gocardless.txt"):
print "template:", t
print
self.test(t, p)
print
print "*" * 42
print
elif p.provider == "banktransfer":
for t in ("tickets-purchased-email-banktransfer.txt", "tickets-paid-email-banktransfer.txt"):
print "template:", t
print
self.test(t, p)
print
print "*" * 42
print
t = "welcome-email.txt"
print "template:", t
print
output = render_template(t, user = self.user)
print output
def make_test_user(self):
try:
user = User.query.filter(User.email == "test@example.com").one()
except NoResultFound:
user = User('test@example.com', 'testuser')
user.set_password('happycamper')
db.session.add(user)
amounts = {
"full": TicketType.query.get('full')
}
#
# FIXME: this is a complete mess
#
# TODO: needs to cover:
#
# single full ticket
# multiple full tickets
#
# kids & campervans?
#
# full
for full in ([1], [0], [3], [0], [2]):
for pt in (BankPayment, GoCardlessPayment):
for curr in ['GBP', 'EUR']:
total = (full * amounts['full'].get_price(curr))
payment = pt(curr, total)
payment.state = "inprogress"
if payment.provider == "gocardless":
payment.gcid = "%3dSDJADG" % (int(random.random() * 1000 ))
db.session.add(payment)
for i in range(full):
t = Ticket(code='full')
t.payment = payment
if payment.currency == 'GBP':
t.expires = datetime.utcnow() + timedelta(days=app.config['EXPIRY_DAYS_TRANSFER'])
elif payment.currency == 'EUR':
t.expires = datetime.utcnow() + timedelta(days=app.config['EXPIRY_DAYS_TRANSFER_EURO'])
user.tickets.append(t)
user.payments.append(payment)
db.session.commit()
self.user = user
print user.name
for p in user.payments.all():
print p.provider, p.amount
print p.tickets.all()
def test(self, template, payment):
output = render_template(template, user=self.user, payment=payment)
print "To: \"%s\" <%s>" % (self.user.name, self.user.email)
print
print output.encode("utf-8")
class CreateTickets(Command):
def run(self):
#
# if you change these, change ticket_forms in views/tickets.py as well.
#
data = [
#(order, code, name, capacity, max per person, GBP, EUR, Description)
(0, 'full_free',
'Full Camp Ticket (Complimentary)', 1, 1, 0, 0, None),
(1, 'full_discount',
'Full Camp Ticket (Discount)', 10, 2, 70.00, 90.00, None),
(2, 'full_earlybird',
'Full Camp Ticket', 0, 10, 90.00, 115.00, None),
(3, 'full_initial',
'Full Camp Ticket', 0, 10, 95.00, 120.00, None),
(5, 'full',
'Full Camp Ticket', 1000, 10, 105.00, 135.00, None),
(8, 'full_supporter',
'Full Camp Ticket (Supporter)', 1000, 10, 125.00, 160.00,
"Support this non-profit event by paying a little more. "
"All money will go towards making EMF more awesome."),
(8, 'full_supporter_2',
'Full Camp Ticket (Supporter)', 0, 10, 150.00, 190.00,
"Support this non-profit event by paying a little more. "
"All money will go towards making EMF more awesome."),
(10, 'kids_u16',
'Under-16 Camp Ticket', 500, 10, 40.00, 50.00,
"For visitors born after August 28th, 1998. "
"All under-16s must be accompanied by an adult."),
(15, 'kids_u5',
'Under-5 Camp Ticket', 200, 4, 0, 0,
"For children born after August 28th, 2009. "
"All children must be accompanied by an adult."),
(30, 'parking',
'Parking Ticket', 400, 4, 15.00, 20.00,
"We're trying to keep cars on-site to a minimum. "
"Please take public transport or find someone to share with if possible."),
(35, 'campervan',
'Caravan/Campervan Ticket', 50, 2, 30.00, 40.00,
"If you bring a caravan, you won't need a separate parking ticket for the towing car."),
]
types = []
for row in data:
tt = TicketType(*row[1:5], order=row[0], notice=row[7])
tt.prices = [TicketPrice('GBP', row[5]), TicketPrice('EUR', row[6])]
types.append(tt)
for tt in types:
existing_tt = TicketType.query.get(tt.code)
if existing_tt:
print 'Refreshing TicketType %s' % tt.code
for f in ['name', 'capacity', 'limit', 'order', 'notice']:
cur_val = getattr(existing_tt, f)
new_val = getattr(tt, f)
if cur_val != new_val:
print ' %10s: %r -> %r' % (f, cur_val, new_val)
setattr(existing_tt, f, new_val)
else:
print 'Adding TicketType %s' % tt.code
db.session.add(tt)
db.session.commit()
print 'Tickets created'
class CreateTicketTokens(Command):
def run(self):
tokens = [
('full_discount', 'testtoken'),
]
for code, token in tokens:
tt = TicketToken(token, datetime.utcnow() + timedelta(days=7))
tt.type = TicketType.query.get(code)
db.session.commit()
print 'Tokens added'
class MakeAdmin(Command):
"""
Make userid one an admin for testing purposes.
"""
option_list = (Option('-u', '--userid', dest='userid', help="The userid to make an admin (defaults to 1)"),)
def run(self, userid):
if not userid:
userid = 1
user = User.query.get(userid)
user.admin = True
s = db.object_session(user)
s.commit()
print 'userid 1 (%s) is now an admin' % (user.name)
class SendTickets(Command):
def run(self):
query = text("""select distinct "user".id from "user", ticket
where ticket.user_id = "user".id and ticket.paid = true""")
for row in db.engine.execute(query):
user = User.query.filter_by(id=row[0]).one()
msg = Message("Your Electromagnetic Field Ticket",
sender=app.config['TICKETS_EMAIL'],
recipients=[user.email]
)
user.create_receipt()
msg.body = render_template("ticket.txt", user=user)
print "Sending to", user.email, "..."
mail.send(msg)
if __name__ == "__main__":
manager.add_command('createdb', CreateDB())
manager.add_command('createbankaccounts', CreateBankAccounts())
manager.add_command('loadofx', LoadOfx())
manager.add_command('reconcile', Reconcile())
#manager.add_command('testemails', TestEmails())
manager.add_command('createtickets', CreateTickets())
manager.add_command('makeadmin', MakeAdmin())
manager.add_command('createtokens', CreateTicketTokens())
#manager.add_command('sendtickets', SendTickets())
manager.run()