def cart_update(): xchat_id = request.headers.get('X-Fridge-chat-id', None) if xchat_id is None or xchat_id == {} or xchat_id == "{}": xchat_id = app.config['DEFAULT_ROOM'] cart = CartController.get_or_create(chat_id=xchat_id) cart.status = 'confirmed' cart.save() Telegram.push(message=u"Корзина сформирована", chat_id=xchat_id) Telegram.push(message=u"Оплатите покупки", chat_id=xchat_id) return json.dumps({}), 200, {'Content-Type': 'application/json; charset=utf-8'}
def cart_delete(): xchat_id = request.headers.get('X-Fridge-chat-id', None) if xchat_id is None or xchat_id == {} or xchat_id == "{}": xchat_id = app.config['DEFAULT_ROOM'] cart = CartController.get_or_create(chat_id=xchat_id) items = Item.objects(cart_id=cart.id) for item in items: item.delete() Cart.objects.get(chat_id=xchat_id).delete() Telegram.push(message=u"Корзина удалена", chat_id=xchat_id) return json.dumps({}), 200, {'Content-Type': 'application/json; charset=utf-8'}
def query(): xchat_id = request.headers.get('X-Fridge-chat-id', None) if xchat_id is None or xchat_id == {} or xchat_id == "{}": xchat_id = app.config['DEFAULT_ROOM'] cart = CartController.get_or_create(chat_id=xchat_id) q = request.args.get('q', None) words = Items.filterWords(q) words = list(set(words)) for w in words: items = Item.objects(title=w, cart_id=cart.id) if len(items) > 0: continue item = Item(title=w, cart_id=cart.id) item.save() Telegram.push(message=u"Добавил %s" % w, chat_id=xchat_id) return json.dumps({}), 200, {'Content-Type': 'application/json; charset=utf-8'}
def cart_item_update(item_id): xchat_id = request.headers.get('X-Fridge-chat-id', None) if xchat_id is None or xchat_id == {} or xchat_id == "{}": xchat_id = app.config['DEFAULT_ROOM'] item = Item.objects.get(id=ObjectId(item_id)) data = json.loads(request.data) form_data = item.to_form_data() for d in form_data: if d not in data: data.update({d: form_data[d]}) form = ItemForm.from_json(data) if form.validate(): item.title = data['title'] item.shop_name = data['shop_name'] item.price = data['price'] item.count = data['count'] item.magaz = data['magaz'] item.save() Telegram.push(message=u"Список покупок уточнен, добавил %s" % item.shop_name, chat_id=xchat_id) else: print form.errors item = Item.objects.get(id=ObjectId(item_id)) return json.dumps(item.as_api()), 200, {'Content-Type': 'application/json; charset=utf-8'}