-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathviews.py
More file actions
432 lines (361 loc) · 15.6 KB
/
views.py
File metadata and controls
432 lines (361 loc) · 15.6 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import datetime
from decimal import Decimal as D
from django import http
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.db.models import Sum
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from oscar_accounts import exceptions, facade, names
from oscar_accounts.compact_oscar import get_model, currency
from oscar_accounts.dashboard import forms, reports
AccountType = get_model('oscar_accounts', 'AccountType')
Account = get_model('oscar_accounts', 'Account')
Transfer = get_model('oscar_accounts', 'Transfer')
Transaction = get_model('oscar_accounts', 'Transaction')
class AccountListView(generic.ListView):
model = Account
context_object_name = 'accounts'
template_name = 'accounts/dashboard/account_list.html'
form_class = forms.SearchForm
description = _("All %s") % names.UNIT_NAME_PLURAL.lower()
def get_context_data(self, **kwargs):
ctx = super(AccountListView, self).get_context_data(**kwargs)
ctx['form'] = self.form
ctx['title'] = names.UNIT_NAME_PLURAL
ctx['unit_name'] = names.UNIT_NAME
ctx['queryset_description'] = self.description
return ctx
def get_queryset(self):
queryset = Account.objects.all()
if 'code' not in self.request.GET:
# Form not submitted
self.form = self.form_class()
return queryset
self.form = self.form_class(self.request.GET)
if not self.form.is_valid():
# Form submitted but invalid
return queryset
# Form valid - build queryset and description
data = self.form.cleaned_data
desc_template = _(
"%(status)s %(unit)ss %(code_filter)s %(name_filter)s")
desc_ctx = {
'unit': names.UNIT_NAME.lower(),
'status': "All",
'code_filter': "",
'name_filter': "",
}
if data['name']:
queryset = queryset.filter(name__icontains=data['name'])
desc_ctx['name_filter'] = _(
" with name matching '%s'") % data['name']
if data['code']:
queryset = queryset.filter(code=data['code'])
desc_ctx['code_filter'] = _(
" with code '%s'") % data['code']
if data['status']:
queryset = queryset.filter(status=data['status'])
desc_ctx['status'] = data['status']
self.description = desc_template % desc_ctx
return queryset
class AccountCreateView(generic.CreateView):
model = Account
context_object_name = 'account'
template_name = 'accounts/dashboard/account_form.html'
form_class = forms.NewAccountForm
def get_context_data(self, **kwargs):
ctx = super(AccountCreateView, self).get_context_data(**kwargs)
ctx['title'] = _("Create a new %s") % names.UNIT_NAME.lower()
return ctx
def form_valid(self, form):
account = form.save()
# Load transaction
source = form.get_source_account()
amount = form.cleaned_data['initial_amount']
try:
facade.transfer(source, account, amount,
user=self.request.user,
description=_("Creation of account"))
except exceptions.AccountException as e:
messages.error(
self.request,
_("Account created but unable to load funds onto new "
"account: %s") % e)
else:
messages.success(
self.request,
_("New account created with code '%s'") % account.code)
return http.HttpResponseRedirect(
reverse('accounts-detail', kwargs={'pk': account.id}))
class AccountUpdateView(generic.UpdateView):
model = Account
context_object_name = 'account'
template_name = 'accounts/dashboard/account_form.html'
form_class = forms.UpdateAccountForm
def get_context_data(self, **kwargs):
ctx = super(AccountUpdateView, self).get_context_data(**kwargs)
ctx['title'] = _("Update '%s' account") % self.object.name
return ctx
def form_valid(self, form):
account = form.save()
messages.success(self.request, _("Account saved"))
return http.HttpResponseRedirect(
reverse('accounts-detail', kwargs={'pk': account.id}))
class AccountFreezeView(generic.UpdateView):
model = Account
template_name = 'accounts/dashboard/account_freeze.html'
form_class = forms.FreezeAccountForm
def get_success_url(self):
messages.success(self.request, _("Account frozen"))
return reverse('accounts-list')
class AccountThawView(generic.UpdateView):
model = Account
template_name = 'accounts/dashboard/account_thaw.html'
form_class = forms.ThawAccountForm
def get_success_url(self):
messages.success(self.request, _("Account thawed"))
return reverse('accounts-list')
class AccountTopUpView(generic.UpdateView):
model = Account
template_name = 'accounts/dashboard/account_top_up.html'
form_class = forms.TopUpAccountForm
def form_valid(self, form):
account = self.object
amount = form.cleaned_data['amount']
try:
facade.transfer(form.get_source_account(), account, amount,
user=self.request.user,
description=_("Top-up account"))
except exceptions.AccountException as e:
messages.error(self.request,
_("Unable to top-up account: %s") % e)
else:
messages.success(
self.request, _("%s added to account") % currency(amount))
return http.HttpResponseRedirect(reverse('accounts-detail',
kwargs={'pk': account.id}))
class AccountWithdrawView(generic.UpdateView):
model = Account
template_name = 'accounts/dashboard/account_withdraw.html'
form_class = forms.WithdrawFromAccountForm
def form_valid(self, form):
account = self.object
amount = form.cleaned_data['amount']
try:
facade.transfer(account, form.get_source_account(), amount,
user=self.request.user,
description=_("Return funds to source account"))
except exceptions.AccountException as e:
messages.error(self.request,
_("Unable to withdraw funds from account: %s") % e)
else:
messages.success(
self.request,
_("%s withdrawn from account") % currency(amount))
return http.HttpResponseRedirect(reverse('accounts-detail',
kwargs={'pk': account.id}))
class AccountTransactionsView(generic.ListView):
model = Transaction
context_object_name = 'transactions'
template_name = 'accounts/dashboard/account_detail.html'
def get(self, request, *args, **kwargs):
self.account = get_object_or_404(Account, id=kwargs['pk'])
return super(AccountTransactionsView, self).get(
request, *args, **kwargs)
def get_queryset(self):
return self.account.transactions.all().order_by('-date_created')
def get_context_data(self, **kwargs):
ctx = super(AccountTransactionsView, self).get_context_data(**kwargs)
ctx['account'] = self.account
return ctx
class TransferListView(generic.ListView):
model = Transfer
context_object_name = 'transfers'
template_name = 'accounts/dashboard/transfer_list.html'
form_class = forms.TransferSearchForm
description = _("All transfers")
def get_context_data(self, **kwargs):
ctx = super(TransferListView, self).get_context_data(**kwargs)
ctx['form'] = self.form
ctx['queryset_description'] = self.description
return ctx
def get_queryset(self):
queryset = self.model.objects.all()
if 'reference' not in self.request.GET:
# Form not submitted
self.form = self.form_class()
return queryset
self.form = self.form_class(self.request.GET)
if not self.form.is_valid():
# Form submitted but invalid
return queryset
# Form valid - build queryset and description
data = self.form.cleaned_data
desc_template = _(
"Transfers %(reference)s %(date)s")
desc_ctx = {
'reference': "",
'date': "",
}
if data['reference']:
queryset = queryset.filter(reference=data['reference'])
desc_ctx['reference'] = _(
" with reference '%s'") % data['reference']
if data['start_date'] and data['end_date']:
# Add 24 hours to make search inclusive
date_from = data['start_date']
date_to = data['end_date'] + datetime.timedelta(days=1)
queryset = queryset.filter(date_created__gte=date_from).filter(date_created__lt=date_to)
desc_ctx['date'] = _(" created between %(start_date)s and %(end_date)s") % {
'start_date': data['start_date'],
'end_date': data['end_date']}
elif data['start_date']:
queryset = queryset.filter(date_created__gte=data['start_date'])
desc_ctx['date'] = _(" created since %s") % data['start_date']
elif data['end_date']:
date_to = data['end_date'] + datetime.timedelta(days=1)
queryset = queryset.filter(date_created__lt=date_to)
desc_ctx['date'] = _(" created before %s") % data['end_date']
self.description = desc_template % desc_ctx
return queryset
class TransferDetailView(generic.DetailView):
model = Transfer
context_object_name = 'transfer'
template_name = 'accounts/dashboard/transfer_detail.html'
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
return queryset.get(reference=self.kwargs['reference'])
class DeferredIncomeReportView(generic.FormView):
form_class = forms.DateForm
template_name = 'accounts/dashboard/reports/deferred_income.html'
def get(self, request, *args, **kwargs):
if self.is_form_submitted():
return self.validate()
return super(DeferredIncomeReportView, self).get(request, *args,
**kwargs)
def is_form_submitted(self):
return 'date' in self.request.GET
def get_context_data(self, **kwargs):
ctx = super(DeferredIncomeReportView, self).get_context_data(**kwargs)
ctx['title'] = 'Deferred income report'
return ctx
def get_form_kwargs(self):
kwargs = {'initial': self.get_initial()}
if self.is_form_submitted():
kwargs.update({
'data': self.request.GET,
})
return kwargs
def validate(self):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
# Take cutoff as the first second of the following day, which we
# convert to a datetime instane in UTC
threshold_date = form.cleaned_data['date'] + datetime.timedelta(days=1)
threshold_datetime = datetime.datetime.combine(
threshold_date, datetime.time(tzinfo=timezone.utc))
# Get data
rows = []
totals = {'total': D('0.00'),
'num_accounts': 0}
for acc_type_name in names.DEFERRED_INCOME_ACCOUNT_TYPES:
acc_type = AccountType.objects.get(name=acc_type_name)
data = {
'name': acc_type_name,
'total': D('0.00'),
'num_accounts': 0,
'num_expiring_within_30': 0,
'num_expiring_within_60': 0,
'num_expiring_within_90': 0,
'num_expiring_outside_90': 0,
'num_open_ended': 0,
'total_expiring_within_30': D('0.00'),
'total_expiring_within_60': D('0.00'),
'total_expiring_within_90': D('0.00'),
'total_expiring_outside_90': D('0.00'),
'total_open_ended': D('0.00'),
}
for account in acc_type.accounts.all():
data['num_accounts'] += 1
total = account.transactions.filter(
date_created__lt=threshold_datetime).aggregate(
total=Sum('amount'))['total']
if total is None:
total = D('0.00')
data['total'] += total
days_remaining = account.days_remaining(threshold_datetime)
if days_remaining is None:
data['num_open_ended'] += 1
data['total_open_ended'] += total
else:
if days_remaining <= 30:
data['num_expiring_within_30'] += 1
data['total_expiring_within_30'] += total
elif days_remaining <= 60:
data['num_expiring_within_60'] += 1
data['total_expiring_within_60'] += total
elif days_remaining <= 90:
data['num_expiring_within_90'] += 1
data['total_expiring_within_90'] += total
else:
data['num_expiring_outside_90'] += 1
data['total_expiring_outside_90'] += total
totals['total'] += data['total']
totals['num_accounts'] += data['num_accounts']
rows.append(data)
ctx = self.get_context_data(form=form)
ctx['rows'] = rows
ctx['totals'] = totals
ctx['report_date'] = form.cleaned_data['date']
return self.render_to_response(ctx)
class ProfitLossReportView(generic.FormView):
form_class = forms.DateRangeForm
template_name = 'accounts/dashboard/reports/profit_loss.html'
def get(self, request, *args, **kwargs):
if self.is_form_submitted():
return self.validate()
return super(ProfitLossReportView, self).get(request, *args,
**kwargs)
def is_form_submitted(self):
return 'start_date' in self.request.GET
def get_context_data(self, **kwargs):
ctx = super(ProfitLossReportView, self).get_context_data(**kwargs)
ctx['title'] = 'Profit and loss report'
return ctx
def get_form_kwargs(self):
kwargs = {'initial': self.get_initial()}
if self.is_form_submitted():
kwargs.update({
'data': self.request.GET,
})
return kwargs
def validate(self):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
start = form.cleaned_data['start_date']
end = form.cleaned_data['end_date'] + datetime.timedelta(days=1)
report = reports.ProfitLossReport(start, end)
data = report.run()
ctx = self.get_context_data(form=form)
ctx.update(data)
ctx['show_report'] = True
ctx['start_date'] = start
ctx['end_date'] = end
return self.render_to_response(ctx)
def total(self, qs):
sales_amt = qs.aggregate(sum=Sum('amount'))['sum']
return sales_amt if sales_amt is not None else D('0.00')