/
app_mobile.py
332 lines (275 loc) · 8.5 KB
/
app_mobile.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# coding: utf-8
"""
Backend for mobile clients
"""
from init_app import init_app
from flask import json, render_template, request, abort
from flask.ext.mail import Mail, Message
from flask.ext.restful import fields, marshal, marshal_with
from database import init_connection, select_db
from decorators import check_api_key, crossdomain_dec
from mongokit import ObjectId
#
# Init
#
# Create mobile backend app object
app = init_app()
# init DB connection and register models
connection = init_connection(app)
# select DB and authenticate
db = select_db(app, connection)
# mail
mail = Mail(app)
#
# Helpers
#
# TODO: refactor. partially overlaps with admin api.
class DateTimeField(fields.Raw):
"""Return formated datetime value"""
def format(self, value):
return value.strftime('%s %s'.format(DATE_FORMAT, TIME_FORMAT))
# cart item fields
cartitem_fields = {
"id": fields.String,
"name": fields.String,
"count": fields.Integer,
"total": fields.Float,
"custom_properties": fields.Raw, # no marshaling here!
}
# address fields
address_fields = {
"street": fields.String,
"house": fields.String,
"building": fields.String,
"porch": fields.String,
"floor": fields.String,
"room": fields.String,
"comments": fields.String,
}
# person fields
person_fields = {
"firstname": fields.String,
"lastname": fields.String,
"fathersname": fields.String,
}
# order fields
order_fields = {
"id": fields.String(attribute = '_id'),
"datetime": DateTimeField,
"status": fields.String,
"address": fields.Nested(address_fields),
"person": fields.Nested(person_fields),
"phones": fields.List(fields.String),
"comments": fields.String,
"operator_comments": fields.String,
"count": fields.Integer,
"total": fields.Float,
"cart": fields.List(fields.Nested(cartitem_fields)),
}
#
# Mobile client API
#
@app.route('/db_version', methods = ['GET', 'OPTIONS'])
@check_api_key
@crossdomain_dec
def get_db_version():
"""
Return current db version
Hash of prepared DB in cache (Redis). If differes from previously saved
value on client request for full DB is made
TODO:
"""
return json.jsonify(version = 1)
@app.route('/db', methods = ['GET', 'OPTIONS'])
@check_api_key
@crossdomain_dec
def get_db():
"""
Return DB in format suitable for client app
It is designed to return full DB. Client app should work offline.
Total size is roughly 300 Kb for every 500 products (not gziped).
Format as follows (JSON):
{
"collections": [
"categories",
"dishes"
],
"categories": {
"<id>": {
"id": "<id>",
"parent": "<cat_id>",
"label": "Category",
...
},
...
},
"dishes": {
"<id>": {
"id": "<id>",
"categories": ["<cat1_id>", ...],
"label": "Dish",
...
},
...
}
}
"""
def _adapt_category(obj):
return (
unicode(obj['_id']),
{
"id": obj['_id'],
"parent": obj['parent'],
"label": obj['name'],
"description": obj['description'],
"icon_small": obj['icon_small'],
"icon_big": obj['icon_big'],
"order": obj['order'],
"items_order": obj['items_order'] if 'items_order' in obj else [],
},
)
def _adapt_product(obj):
props = {
"id": obj['_id'],
"categories": obj["categories"],
"label": obj['name'],
"description": obj['description'],
"price": obj['price'],
"units": obj['units'],
"icon_small": obj['icon_small'],
"icon_big": obj['icon_big'],
"parent": obj['parent'],
}
# TODO: may overwrite product property
for custom_prop in obj['properties']:
props[custom_prop['name']] = custom_prop['value']
return (
unicode(obj['_id']),
props,
)
categories = list(db.categories.Category.find({
"is_hidden": False
}))
products = list(db.products.Product.find({
"is_template": False,
"is_hidden": False,
"categories": {
"$ne": []
},
}))
db_data = {
"collections": [
"categories",
"dishes",
],
"categories": dict(map(_adapt_category, categories)),
"dishes": dict(map(_adapt_product, products)),
}
return json.jsonify(**db_data)
@app.route('/order', methods = ['POST', 'OPTIONS'])
@check_api_key
@crossdomain_dec
def post_order():
"""
Post order
Create DB entry and notify restaurant staff via email letter.
Order freezes values (prices especially) for products in itself.
"""
cart = json.loads(request.form.get('cart', '[]'))
address = json.loads(request.form.get('address', '{}'))
phones = json.loads(request.form.get('phones', '[]'))
total = 0.0
for p in cart:
prod = db.products.Product.find_one({u"_id": ObjectId(p['id'])})
if prod is None:
continue
prod['total'] = prod['price'] * p['count']
total += float(prod['total'])
for custom_opt in prod['properties']:
prod[custom_opt['name']] = custom_opt['value']
p.update(prod)
# add custom properties
p['custom_properties'] = prod.get_properties()
# form fields
person = {
"firstname": request.form.get('firstname', u''),
"lastname": request.form.get('lastname', u''),
"fathersname": request.form.get('fathersname', u''),
}
order_comments = request.form.get('comments', u'')
# address
address = {
"street": address.get('street', u''),
"house": address.get('house', u''),
"building": address.get('building', u''),
"porch": address.get('porch', u''),
"floor": address.get('floor', u''),
"room": address.get('room', u''),
"comments": address.get('comments', u''),
}
# Secret hash to change order status via Public API
# Needed for restaurant operators. They are changing status right
# from email.
secret_key = uuid.uuid4().get_hex()
# REMOVED IN DEMO: get settings
#settings = get_settings_from_db(db)
# get emails
email_from = 'order@example.com'
#if 'order_email_from' in settings and settings['order_email_from']:
# email_from = settings['order_email_from']
emails_to = ['twildor@gmail.com']
#if 'order_email_to' in settings and settings['order_email_to']:
# # may be comma separated list
# emails_to = [e.strip() for e in settings['order_email_to'].split(',') if e.strip()]
# send message
# log errors
if emails_to:
try:
msg = Message(
u'[FOOD] new order',
sender = email_from,
recipients = emails_to
)
order_text = render_template('new_order_email.txt',
person = person,
phones = phones,
cart = cart,
total = total,
order_comments = order_comments,
address = address,
secret = secret_key,
#settings = settings
)
msg.body = order_text
mail.send(msg)
except Exception as e:
app.logger.error(pprint.pformat(e))
# create order in DB
# REMOVED IN DEMO:
#order = db.orders.Order()
order = {}
order['person'] = person
order['address'] = address
order['phones'] = phones
#order.phone_hidden = phone_hidden
order['comments'] = order_comments
order['datetime'] = datetime.now()
order['status'] = u'new'
order['count'] = len(cart)
order['total'] = total
order['secret'] = unicode(secret_key)
# need to filter cart fields
order_cart = []
for i in cart:
order_cart.append({
"id": ObjectId(i["id"]),
"name": i["name"],
"count": i["count"],
"total": i["total"],
"custom_properties": i["custom_properties"],
})
order['cart'] = order_cart
# REMOVED IN DEMO:
#order.save()
data = marshal(dict(order), order_fields)
return json.jsonify(data)