1
|
|
|
from pyramid.events import subscriber |
2
|
|
|
from pyramid.events import BeforeRender |
3
|
|
|
from pyramid.httpexceptions import HTTPFound |
4
|
|
|
from pyramid.renderers import render |
5
|
|
|
from pyramid.renderers import render_to_response |
6
|
|
|
from pyramid.response import Response |
7
|
|
|
from pyramid.view import view_config, forbidden_view_config |
8
|
|
|
|
9
|
|
|
from pyramid.i18n import TranslationStringFactory |
10
|
|
|
_ = TranslationStringFactory('betty') |
11
|
|
|
|
12
|
|
|
from sqlalchemy.sql import func |
13
|
|
|
from sqlalchemy.exc import DBAPIError |
14
|
|
|
from sqlalchemy.orm.exc import NoResultFound |
15
|
|
|
|
16
|
|
|
from .models import * |
17
|
|
|
from .models.model import * |
18
|
|
|
from .models import user as __user |
19
|
|
|
from .models.user import User |
20
|
|
|
from .models.item import Item |
21
|
|
|
from .models.box import Box |
22
|
|
|
from .models.transaction import Transaction, BTCDeposit, PurchaseLineItem |
23
|
|
|
from .models.account import Account, VirtualAccount, CashAccount |
24
|
|
|
from .models.event import Event |
25
|
|
|
from .models.request import Request |
26
|
|
|
from .models.announcement import Announcement |
27
|
|
|
from .models.btcdeposit import BtcPendingDeposit |
28
|
|
|
from .models.pool import Pool |
29
|
|
|
|
30
|
|
|
from .utility import user_password_reset |
31
|
|
|
from .utility import send_email |
32
|
|
|
from .utility import post_stripe_payment |
33
|
|
|
|
34
|
|
|
from pyramid.security import Allow, Everyone, remember, forget |
35
|
|
|
|
36
|
|
|
import chezbetty.datalayer as datalayer |
37
|
|
|
import transaction |
38
|
|
|
|
39
|
|
|
import traceback |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@view_config(route_name='about', renderer='templates/public/about.jinja2') |
44
|
|
|
def about(request): |
45
|
|
|
return {} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
@view_config(route_name='items', renderer='templates/public/items.jinja2') |
49
|
|
|
def items(request): |
50
|
|
|
items = DBSession.query(Item)\ |
51
|
|
|
.filter(Item.enabled==True)\ |
52
|
|
|
.filter(Item.in_stock>0)\ |
53
|
|
|
.order_by(Item.name).all() |
54
|
|
|
out_of_stock_items = DBSession.query(Item)\ |
55
|
|
|
.filter(Item.enabled==True)\ |
56
|
|
|
.filter(Item.in_stock==0)\ |
57
|
|
|
.order_by(Item.name).all() |
58
|
|
|
disabled_items = DBSession.query(Item)\ |
59
|
|
|
.filter(Item.enabled==False)\ |
60
|
|
|
.order_by(Item.name).all() |
61
|
|
|
return {'items': items, |
62
|
|
|
'out_of_stock_items': out_of_stock_items, |
63
|
|
|
'disabled_items': disabled_items} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
#def _get_shame_users(): |
68
|
|
|
# users = DBSession.query(User)\ |
69
|
|
|
# .filter(User.balance < -5)\ |
70
|
|
|
# .order_by(User.balance).all() |
71
|
|
|
# for user in users: |
72
|
|
|
# user.days_on_shame = Transaction.get_days_in_debt_for_user(user) |
73
|
|
|
# return users |
74
|
|
|
# |
75
|
|
|
#@view_config(route_name='shame', renderer='templates/public/shame.jinja2') |
76
|
|
|
#def shame(request): |
77
|
|
|
# users = _get_shame_users() |
78
|
|
|
# return {'users': users} |
79
|
|
|
# |
80
|
|
|
#@view_config(route_name='shame_csv', renderer='csv') |
81
|
|
|
#def shame_csv(request): |
82
|
|
|
# users = _get_shame_users() |
83
|
|
|
# header = ['uniqname','balance','name','days_on_shame'] |
84
|
|
|
# rows = [[user.uniqname, user.balance, user.name, user.days_on_shame] for user in users] |
85
|
|
|
# |
86
|
|
|
# return { |
87
|
|
|
# 'header': header, |
88
|
|
|
# 'rows': rows, |
89
|
|
|
# } |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
@view_config(route_name='paydebt', renderer='templates/public/paydebt.jinja2') |
93
|
|
|
def paydebt(request): |
94
|
|
|
uniqname = request.matchdict['uniqname'] |
95
|
|
|
user = User.from_uniqname(uniqname, local_only=True) |
96
|
|
|
|
97
|
|
|
# Calculate all of the important balance metrics server side |
98
|
|
|
values = {} |
99
|
|
|
for newval in [0, 10, 25, 50, 100]: |
100
|
|
|
amount = newval - user.balance # how much will go into the user's account |
101
|
|
|
total = (amount + Decimal('0.3')) / Decimal('0.971') # how much we must charge to offset stripe |
102
|
|
|
fee = total - amount # how much more to make stripe happy |
103
|
|
|
total_cents = (total*100).quantize(Decimal('1'), rounding=ROUND_HALF_UP) |
104
|
|
|
values[newval] = { |
105
|
|
|
'amount': amount, |
106
|
|
|
'total': total, |
107
|
|
|
'fee': fee, |
108
|
|
|
'total_cents': total_cents |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return {'user': user, |
112
|
|
|
'stripe_pk': request.registry.settings['stripe.publishable_key'], |
113
|
|
|
'values': values} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
@view_config(route_name='paydebt_submit', |
117
|
|
|
request_method='POST', |
118
|
|
|
renderer='json') |
119
|
|
|
def paydebt_submit(request): |
120
|
|
|
uniqname = request.matchdict['uniqname'] |
121
|
|
|
user = User.from_uniqname(uniqname, local_only=True) |
122
|
|
|
|
123
|
|
|
token = request.POST['stripeToken'] |
124
|
|
|
amount = Decimal(request.POST['betty_amount']) |
125
|
|
|
total_cents = int(request.POST['betty_total_cents']) |
126
|
|
|
|
127
|
|
|
post_stripe_payment( |
128
|
|
|
datalayer, |
129
|
|
|
request, |
130
|
|
|
token, |
131
|
|
|
amount, |
132
|
|
|
total_cents, |
133
|
|
|
user, |
134
|
|
|
user) |
135
|
|
|
|
136
|
|
|
return {} |
137
|
|
|
|