-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathviews.py
128 lines (96 loc) · 3.67 KB
/
views.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from flask import request, jsonify, redirect, url_for, session, current_app
from flask.views import View, MethodView
from quokka.core.templates import render_template
from quokka.utils import get_current_user
from flask.ext.security import current_user
from flask.ext.security.utils import url_for_security
from .models import Cart, Processor
import logging
logger = logging.getLogger()
class BaseView(MethodView):
requires_login = False
def needs_login(self, **kwargs):
if not current_user.is_authenticated():
next = kwargs.get('next', request.values.get('next', '/cart'))
return redirect(url_for_security('login', next=next))
def get(self):
# by default redirects to /cart on get
return self.redirect()
def as_json(self, **kwargs):
format = request.args.get('format')
if request.is_xhr or format == 'json':
for k, v in kwargs.items():
if hasattr(v, 'to_json'):
kwargs[k] = json.loads(v.to_json())
try:
return jsonify(kwargs)
except:
return jsonify({"result": str(kwargs)})
def render(self, *args, **kwargs):
return self.as_json(**kwargs) or render_template(*args, **kwargs)
def redirect(self, *args, **kwargs):
next = request.values.get('next', '/cart')
return self.as_json(**kwargs) or redirect(next)
class CartView(BaseView):
def get(self):
if not session.get('cart_id'):
return self.render(
'cart/empty_cart.html',
url=current_app.config.get('CART_CONTINUE_URL', '/')
)
cart = Cart.get_cart()
context = {"cart": cart}
if cart.items:
template = 'cart/cart.html'
else:
template = 'cart/empty_cart.html'
context['url'] = cart.continue_shopping_url
return self.render(template, **context)
class SetItemView(BaseView):
def post(self):
cart = Cart.get_cart()
params = {k: v for k, v in request.form.items() if not k == "next"}
item = cart.set_item(**params)
return self.redirect(item=item)
class RemoveItemView(BaseView):
def post(self):
cart = Cart.get_cart()
params = {k: v for k, v in request.form.items() if not k == "next"}
item = cart.remove_item(**params)
return self.redirect(item=item)
class SetProcessorView(BaseView):
def post(self):
cart = Cart.get_cart()
processor = request.form.get('processor')
cart.set_processor(processor)
return self.redirect(processor=cart.processor.identifier)
class CheckoutView(BaseView):
def post(self):
cart = Cart.get_cart()
return (cart.requires_login and self.needs_login()) \
or cart.process_pipeline()
class HistoryView(BaseView):
def get(self):
context = {
"carts": Cart.objects(belongs_to=get_current_user())
}
return self.needs_login(
next=url_for('quokka.modules.cart.history')
) or self.render(
'cart/history.html', **context
)
class ProcessorView(View):
methods = ['GET', 'POST']
def get_processor(self, identifier):
return Processor.get_instance_by_identifier(identifier)
class NotificationView(ProcessorView):
def dispatch_request(self, identifier):
processor = self.get_processor(identifier)
return processor.notification()
class ConfirmationView(ProcessorView):
def dispatch_request(self, identifier):
processor = self.get_processor(identifier)
return processor.confirmation()