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 fridge_login(request):
    if request.method == "POST":
        item_name = request.POST['name']
        item_amount = request.POST['amount']
        item_initamount = request.POST['initial_amount']
        item_fridge = request.POST['fridge_id']
        item_upc = request.POST['upc']

        item_to_save = Item(name=item_name, amount=item_amount, initial_amount=item_initamount, fridge=item_fridge, upc=item_upc)

        item_to_save.save()

        #['item_to_add']
        #response = json_item
        #t = Template("%s" % dir(request.POST))
        t = Template("%s" % item_name)

        context = RequestContext(request, {"": "", }, [ip_address_processor])

        return HttpResponse(t.render(context))
    elif request.method == "GET":
        """
	#response = HttpResponse(request, Template("%s" % getFridge(request),c))
        csrf_token = csrf.get_token(request)
        response = HttpResponse(simplejson.dumps({'csrf_token':csrf_token}), 'application/json')
        """

        t = Template("%s" % getFridge(request))

        context = RequestContext(request, {"": "", }, [ip_address_processor])

        return HttpResponse(t.render(context))



    t = Template("%s" % getFridge(request))

    context = RequestContext(request, {"": "", }, [ip_address_processor])

    return HttpResponse(t.render(context))
Exemple #4
0
def update_item(request):
    response = HttpResponse()

    if request.method == "POST":
        item_name = request.POST['name']
        item_amount = int(request.POST['amount'])
        item_initamount = request.POST['initial_amount']
        item_fridge_id = request.POST['fridge_id']
        item_upc = request.POST['upc']

        fridge_obj = Fridge.objects.get(id=item_fridge_id)

        # if UPC code and Item pair already match add to
        # current amount
        if len(Item.objects.filter(fridge__id=item_fridge_id).filter(name=item_name)) == 0:
            item_to_save = Item(name=item_name, amount=item_amount, initial_amount=item_initamount, fridge=fridge_obj, upc=item_upc)
        else:
            item_to_save = Item.objects.get(name=item_name)
            if item_to_save.amount >= 0:
                if item_upc == "999999999":
                    item_to_save.amount = item_amount
                else:
                    item_to_save.amount += item_amount
            else:
                item_to_save.amount = item_amount
            item_to_save.initial_amount = item_amount

        item_to_save.save()

        posted_item = Item.objects.filter(fridge_id=item_fridge_id).filter(name=item_name)
        serializers.serialize("json", posted_item, stream=response)

    return response
Exemple #5
0
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'}
Exemple #6
0
def cart_items_list():
    xview = request.headers.get('X-Fridge-view', None)
    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)

    if xview == 'ios':
        if cart.status == 'confirmed':
            data = ItemShopController.items_as_ios(items)
        else:
            data = ItemController.items_as_ios(items)
    else:
        data = ItemController.items_as_dict(items)

    return json.dumps(data), 200, {'Content-Type': 'application/json; charset=utf-8'}
Exemple #7
0
def cart_item_del(item_id):
    Item.objects(id=ObjectId(item_id)).delete()
    return json.dumps({}), 200, {'Content-Type': 'application/json; charset=utf-8'}