[go: up one dir, main page]

Menu

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

Download this file

214 lines (155 with data), 7.4 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
# 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 _nearest_midnights(year, month, day, timezone=pytz.utc):
base_date = datetime.date(year, month, day)
for i in (-1, 0, 1):
date = base_date + datetime.timedelta(days=i)
yield timezone.localize(datetime.datetime(date.year, date.month, date.day))
@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]))
# Get user's list of unconfirmed transactions.
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_str, tmpl):
# Render everything adding CSRF protection.
c = {'user': user, 'count': int(count_str) }
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]))
# Get user's list of unconfirmed deals.
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_str, tmpl):
# Render everything adding CSRF protection.
c = {'user': user, 'count': int(count_str) }
c.update(csrf(request))
return render_to_response(tmpl, c)
@has_profile(db)
def show_customer_transactions(request, user, year, month, day, tmpl):
tz = utils.get_tzinfo(user['time_zone'])
prev_date, date, next_date = _nearest_midnights(int(year), int(month), int(day), timezone=tz)
# Get user's customer transactions for the given period of time.
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, 'prev_date': prev_date, 'date': 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):
# Get current timestamp in user's time zone.
tz = utils.get_tzinfo(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 = utils.get_tzinfo(user['time_zone'])
prev_date, date, next_date = _nearest_midnights(int(year), int(month), int(day), timezone=tz)
# Get user's customer deals for the given period of time.
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, 'prev_date': prev_date, 'date': 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):
# Get current timestamp in user's time zone.
tz = utils.get_tzinfo(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 = utils.get_tzinfo(user['time_zone'])
prev_date, date, next_date = _nearest_midnights(int(year), int(month), int(day), timezone=tz)
# Get user's own deals for the given period of time.
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, 'prev_date': prev_date, 'date': 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):
# Get current timestamp in user's time zone.
tz = utils.get_tzinfo(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