# 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