-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
214 lines (193 loc) · 8.95 KB
/
Copy pathadmin.py
File metadata and controls
214 lines (193 loc) · 8.95 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import webapp2
import jinja2
import os
import logging
import settings
#from utils import *
from database import Category,Product, SubProduct,Payment
template_dir = os.path.join(os.path.dirname(__file__), 'templatesadmin')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
class AdminBaseHandler(webapp2.RequestHandler):
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.response.out.write(self.render_str(template, **kw))
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
#check for admin coockie
def adminrender(self, template, **kw):
if self.request.cookies.get('admin_secret') == settings.admin_secret:
self.response.out.write(self.render_str(template, **kw))
else:
self.redirect("/admin/login")
def checkAdminCookie(self):
if self.request.cookies.get('admin_secret') == settings.admin_secret:
return True
class AdminPage(AdminBaseHandler):
def get(self):
self.redirect("/admin/index")
class AdminLogin(AdminBaseHandler):
def get(self):
self.render("adminlogin.html")
def post(self):
username = self.request.get("username")
password = self.request.get("password")
if username == settings.right_admin_username and password == settings.right_admin_password:
self.response.headers.add_header('Set-Cookie', 'admin_secret=%s; Path=/' % settings.admin_secret)
self.redirect("/admin/index")
else:
errortext = "user or password not correct"
self.render("adminlogin.html", errortext = errortext, username = username)
#Admin main site
class AdminIndex(AdminBaseHandler):
def get(self):
self.adminrender("adminindex.html")
class AdminNewCategory(AdminBaseHandler):
def get(self):
self.adminrender("adminnewcategory.html")
def post(self):
if self.checkAdminCookie():
categoryname = self.request.get("name")
Category.addCategory(categoryname)
self.redirect("/admin/index")
else:
self.redirect("/admin/login")
#todo
class AdminEditCategories(AdminBaseHandler):
def get(self):
self.adminrender("adminindex.html")
class AdminNewProduct(AdminBaseHandler):
def get(self):
categories = Category.getAllCategories()
self.adminrender("adminnewproduct.html", categories = categories)
def post(self):
if self.checkAdminCookie():
name = self.request.get("name")
category = self.request.get("category")
price = self.request.get("price")
try:
price = int(price)
except ValueError:
price = 0
product_key = Product.addProduct(name, price, category)
self.redirect("/admin/product/editproduct/%s" % product_key.id())
else:
self.redirect("/admin/login")
#todo
class AdminEditProduct(AdminBaseHandler):
def get(self,product_id_string):
if product_id_string.isdigit():
product_id = int(product_id_string)
product = Product.getProductById(product_id)
category = Category.getCategoryById(product.category.id())
categories = Category.getAllCategories()
self.adminrender("admineditproduct.html",
product = product,
category = category,
categories = categories)
else:
self.redirect("/admin/index")
def post(self,product_id_string):
if product_id_string.isdigit() and self.checkAdminCookie():
product_id = int(product_id_string)
name = self.request.get("name")
category = self.request.get("category")
price = self.request.get("price")
description = self.request.get("description")
pictureurl = self.request.get("pictureurl")
position = self.request.get("position")
showcaseposition = self.request.get("showcaseposition")
try:
price = int(price)
position = int(position)
showcaseposition = int(showcaseposition)
except ValueError:
price = 0
position = -1
showcaseposition = -1
Product.editProduct(product_id, name, price, category, description, pictureurl, position, showcaseposition)
self.redirect("/admin/product/editproduct/%s" % product_id_string)
else:
self.redirect("/admin/index")
class AdminEditProducts(AdminBaseHandler):
def get(self):
products = Product.getAllProducts()
self.adminrender("admineditproducts.html", products = products)
class AdminAddSubProduct(AdminBaseHandler):
def get(self, product_id_string):
if product_id_string.isdigit() and self.checkAdminCookie():
product_id = int(product_id_string)
product = Product.getProductById(product_id)
subproducts = Product.getSubProducts(product_id)
self.adminrender("adminaddsubproduct.html",
product = product,
subproducts = subproducts)
else:
self.redirect("/admin/index")
def post(self, product_id_string):
if product_id_string.isdigit() and self.checkAdminCookie():
product_id = int(product_id_string)
value = self.request.get("value")
position = self.request.get("position")
position = int(position)
Product.addSubProduct(product_id, value, position)
self.redirect("/admin/product/%s/addsubproduct" % product_id_string)
else:
self.redirect("/admin/index")
class AdminEditSubProducts(AdminBaseHandler):
def get(self, product_id_string):
if product_id_string.isdigit() and self.checkAdminCookie():
product_id = int(product_id_string)
product = Product.getProductById(product_id)
subproducts = Product.getSubProducts(product_id)
self.adminrender("admineditsubproduct.html",
product = product,
subproducts = subproducts)
else:
self.redirect("/admin/index")
class AdminEditSubProduct(AdminBaseHandler):
def get(self, subproduct_id_string):
if subproduct_id_string.isdigit() and self.checkAdminCookie():
subproduct_id = int(subproduct_id_string)
product = SubProduct.getProduct(subproduct_id)
command = self.request.get("command")
if command == "delete":
SubProduct.deleteSubProduct(subproduct_id)
elif command == "available":
SubProduct.setAvailability(subproduct_id, True)
elif command == "notavailable":
SubProduct.setAvailability(subproduct_id, False)
self.redirect("/admin/product/%s/editsubproducts" % product.key.id())
class AdminEditPayments(AdminBaseHandler):
def get(self):
paymentdicts = Payment.getActivePaymentDicts()
self.adminrender("admineditpayments.html", paymentdicts = paymentdicts)
class AdminEditPayment(AdminBaseHandler):
def get(self, payment_id_string):
payment_id = int(payment_id_string)
command = self.request.get("command")
if command == "delete":
Payment.deletePayment(payment_id)
elif command == "payed":
Payment.setPayed(payment_id, True)
elif command == "notpayed":
Payment.setPayed(payment_id, False)
else:
Payment.setPaymentState(payment_id, command)
self.redirect("/admin/payments")
app = webapp2.WSGIApplication([('/admin/*', AdminPage),
('/admin/login', AdminLogin),
('/admin/index', AdminIndex),
('/admin/newcategory', AdminNewCategory),
('/admin/editcategory', AdminEditCategories),
('/admin/product/newproduct', AdminNewProduct),
('/admin/product/editproduct/(\d+)',AdminEditProduct),
('/admin/product/editproduct', AdminEditProducts),
('/admin/product/(\d+)/addsubproduct', AdminAddSubProduct),
('/admin/product/(\d+)/editsubproducts', AdminEditSubProducts),
('/admin/product/editsubproduct/(\d+)', AdminEditSubProduct),
('/admin/payments',AdminEditPayments),
('/admin/payment/(\d+)',AdminEditPayment)
], debug=True)