# 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