Exemple #1
0
def cart_items():
    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']
    print 'cart_items add'
    print xchat_id
    cart = CartController.get_or_create(chat_id=xchat_id)

    data = json.loads(request.data)
    words = Items.filterWords(data['title'])
    if len(words) == 0:
        return json.dumps({'error': 'can not add product'}), 400, {'Content-Type': 'application/json; charset=utf-8'}

    data['title'] = " ".join(words)

    items = Item.objects(title=data['title'], cart_id=cart.id)
    if len(items) > 0:
        return json.dumps(items[0].as_api()), 200, {'Content-Type': 'application/json; charset=utf-8'}

    form = ItemForm.from_json(data)
    if form.validate():
        print 'creating product'
        data = form.data
        data.update({'cart_id': cart.id})
        item = Item(**data)
        item.save()
        return json.dumps(item.as_api()), 200, {'Content-Type': 'application/json; charset=utf-8'}
    else:
        return json.dumps({'error': 'can not add product'}), 400, {'Content-Type': 'application/json; charset=utf-8'}
Exemple #2
0
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'}
Exemple #3
0
def cart_item_define(item_id):
    title = request.args.get('title', None)
    item = Item.objects.get(id=ObjectId(item_id))
    if title is None:
        title = item.title
    q, a, s = Items.do(title, json.loads(item.state))
    item.state = json.dumps(s)
    f = False
    if q is None:
        print "cart_item_define"
        print a
        item.magaz = 'utkonos'
        item.shop_name = a[0]
        item.count = a[1]
        item.price = float(a[2])
        item.magaz = a[3] or 'utkonos'
        q = u"Принято!"
        a = []
        f = True
    item.save()
    return json.dumps({'question': q, 'answers': a, 'finished': f}), 200, {
        'Content-Type': 'application/json; charset=utf-8'}