[go: up one dir, main page]

Menu

[r787]: / trunk / demo / journal / views.py  Maximize  Restore  History

Download this file

210 lines (157 with data), 6.9 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
# Create your views here.
import decimal, re, datetime
from django.conf import settings
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from django.core.context_processors import csrf
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotAllowed, Http404
from signup.decorators import has_profile
from modules import curiousorm, utils
import pytz
db = curiousorm.Database(settings.DEMO_DSN)
_TRX_FIELD = re.compile('^trx-(\d{1,13})-(\d{1,13})-(\d{1,13})$')
_SESSION_FIELD = re.compile('^session-(\d{1,13})$')
def _get_tz(tz_name):
try:
tz = pytz.timezone(tz_name)
except pytz.UnknownTimeZoneError:
tz = pytz.utc
return tz
@has_profile(db)
def show_unconfirmed_transactions(request, user, tmpl):
if request.method == 'POST':
# TODO: Do not rely on the garbage collector for connection closing.
confirmed = 0
trx = db.transaction()
for field_name in request.POST:
tf = _TRX_FIELD.match(field_name)
if tf:
turn_id, issuer_id, deal_id = (int(i) for i in tf.group(1, 2, 3))
trx.confirm_transaction(turn_id, user['trader_id'], issuer_id, deal_id)
confirmed += 1
trx.commit()
return HttpResponseRedirect(reverse(
report_transactions_confirmation,
args=[user['trader_id'], confirmed]))
transactions = db.select_unconfirmed_transaction_list(
recipient_id=user['trader_id'],
__order_by='ts DESC')
# Render everything adding CSRF protection.
c = {'user': user, 'transactions' : transactions }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def report_transactions_confirmation(request, user, count, tmpl):
# Render everything adding CSRF protection.
c = {'user': user, 'count': int(count) }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_unconfirmed_deals(request, user, tmpl):
if request.method == 'POST':
# TODO: Do not rely on the garbage collector for connection closing.
confirmed = 0
trx = db.transaction()
for field_name in request.POST:
sf = _SESSION_FIELD.match(field_name)
if sf:
confirmed += trx.delete_finalized_session(user['trader_id'], int(sf.group(1)))
trx.commit()
return HttpResponseRedirect(reverse(
report_deals_confirmation,
args=[user['trader_id'], confirmed]))
deals = db.select_new_deal_list(
recipient_id=user['trader_id'],
__order_by='ts DESC')
# Render everything adding CSRF protection.
c = {'user': user, 'deals' : deals, 'sessions': set((d['turn_id'] for d in deals)) }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def report_deals_confirmation(request, user, count, tmpl):
# Render everything adding CSRF protection.
c = {'user': user, 'count': int(count) }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_customer_transactions(request, user, year, month, day, tmpl):
tz = _get_tz(user['time_zone'])
one_day = datetime.timedelta(1)
date = tz.localize(datetime.datetime(int(year), int(month), int(day)))
prev_date = date - one_day
next_date = date + one_day
transactions = db.customer_transaction_list(user['trader_id'], date, next_date)
if datetime.datetime.now(tz) < next_date:
next_date = None
if not db.exists_previous_customer_transaction(user['trader_id'], date):
prev_date = None
# Render everything adding CSRF protection.
c = {'user': user, 'date': date, 'prev_date': prev_date, 'next_date': next_date, 'transactions': transactions }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_todays_customer_transactions(request, user):
tz = _get_tz(user['time_zone'])
today = datetime.datetime.now(tz)
return HttpResponseRedirect(reverse(
show_customer_transactions,
args=[user['trader_id'], today.year, today.month, today.day]))
@has_profile(db)
def show_customer_deals(request, user, year, month, day, tmpl):
tz = _get_tz(user['time_zone'])
one_day = datetime.timedelta(1)
date = tz.localize(datetime.datetime(int(year), int(month), int(day)))
prev_date = date - one_day
next_date = date + one_day
deals = db.customer_deal_list(user['trader_id'], date, next_date)
if datetime.datetime.now(tz) < next_date:
next_date = None
if not db.exists_previous_customer_deal(user['trader_id'], date):
prev_date = None
# Render everything adding CSRF protection.
c = {'user': user, 'date': date, 'prev_date': prev_date, 'next_date': next_date, 'deals': deals }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_todays_customer_deals(request, user):
tz = _get_tz(user['time_zone'])
today = datetime.datetime.now(tz)
return HttpResponseRedirect(reverse(
show_customer_deals,
args=[user['trader_id'], today.year, today.month, today.day]))
@has_profile(db)
def show_my_deals(request, user, year, month, day, tmpl):
tz = _get_tz(user['time_zone'])
one_day = datetime.timedelta(1)
date = tz.localize(datetime.datetime(int(year), int(month), int(day)))
prev_date = date - one_day
next_date = date + one_day
deals = db.my_deal_list(user['trader_id'], date, next_date)
if datetime.datetime.now(tz) < next_date:
next_date = None
if not db.exists_previous_my_deal(user['trader_id'], date):
prev_date = None
# Render everything adding CSRF protection.
c = {'user': user, 'date': date, 'prev_date': prev_date, 'next_date': next_date, 'deals': deals }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_my_todays_deals(request, user):
tz = _get_tz(user['time_zone'])
today = datetime.datetime.now(tz)
return HttpResponseRedirect(reverse(
show_my_deals,
args=[user['trader_id'], today.year, today.month, today.day]))
@has_profile(db)
def show_recent_transactions(request, user, customer_id_str, tmpl):
customer_id = int(customer_id_str)
# Get customer's profile.
trader = db.select_trader_profile(trader_id=customer_id)
if trader:
# Get customer's list of recent transactions.
transactions = db.recent_transaction_list(customer_id, user['trader_id'])
# Render everything adding CSRF protection.
c = {'user': user, 'trader': trader, 'transactions': transactions }
c.update(csrf(request))
return render_to_response(tmpl, c)
raise Http404