-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
145 lines (124 loc) · 4.73 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# coding: utf-8
import random
from flask import request, url_for, redirect, current_app
from flask.views import MethodView
from flask.ext.security.utils import login_user
from quokka.utils import get_current_user
from quokka.core.templates import render_template
from quokka.modules.cart.models import Cart, Item
from quokka.modules.accounts.models import User
from .models import Course, Subscriber, CourseSubscription, CourseVariant
class SubscribeView(MethodView):
def post(self):
course_id = request.form.get('course_id')
classroom = request.form.get('classroom')
phone = request.form.get('phone')
name = request.form.get('name')
email = request.form.get('email')
variant = request.form.get('variant')
self.current_user = get_current_user()
self.cart = Cart.get_cart()
try:
course = Course.objects.get(id=course_id)
except:
self.cart.addlog("Error getting course %s" % course_id)
return render_template('classes/subscription_error.html')
student = self.get_student(email, name, phone)
if not student:
self.cart.addlog("Error getting student")
return render_template('classes/subscription_error.html')
if not variant in ['regular', None, False, '']:
course_variant = course.variants.get(slug=variant)
_variant = CourseVariant(
title=course_variant.title + "<!-- {0} -->".format(
str(random.getrandbits(8))
),
description=course_variant.description,
unity_value=course_variant.unity_value,
slug=course_variant.slug
)
else:
_variant = None
subscription = CourseSubscription(
subscriber=self.get_subscriber(), # if none will set on pipeline
student=student,
course=course,
classroom=classroom,
variant=_variant,
cart=self.cart
)
subscription.save()
item = Item(
uid=subscription.get_uid(),
product=course,
reference=subscription,
title=subscription.get_title(),
description=subscription.get_description(),
unity_value=subscription.get_unity_value(),
)
self.cart.items.append(item)
# think on this
# in sites with multiple e-commerce apps
# if cart has items from multiple apps ex:
# items: course, product, signature etc..
# which app has precedence in cart settings?
self.cart.requires_login = current_app.config.get(
"CLASSES_CART_REQUIRES_LOGIN",
self.cart.requires_login
)
self.cart.continue_shopping_url = current_app.config.get(
"CLASSES_CART_CONTINUE_SHOPPING_URL",
self.cart.continue_shopping_url
)
self.cart.pipeline = current_app.config.get(
"CLASSES_CART_PIPELINE",
self.cart.pipeline
)
self.cart.config = current_app.config.get(
"CLASSES_CART_CONFIG",
self.cart.config
)
self.cart.course_subscription_id = subscription.id
self.cart.addlog(u"Item added %s" % item.title, save=True)
return redirect(url_for('cart.cart'))
def get_subscriber(self):
if not self.current_user:
return None
try:
return Subscriber.objects.get(user=self.current_user)
except:
self.cart.addlog("Creating a new subscriber", save=False)
return Subscriber.objects.create(
name=self.current_user.name,
email=self.current_user.email,
user=self.current_user
)
def get_student(self, email, name, phone):
if self.current_user and self.current_user.email == email:
try:
return Subscriber.objects.get(user=self.current_user)
except:
self.cart.addlog("Creating a new student", save=False)
return Subscriber.objects.create(
name=name,
email=email,
phone=phone,
user=self.current_user
)
try:
user = User.objects.get(email=email)
except:
self.cart.addlog("Creating new user %s" % email)
user = User.objects.create(
name=name,
email=email,
password=""
)
# autenticar e mandar email password
login_user(user)
return Subscriber.objects.create(
name=name,
email=email,
phone=phone,
user=user
)