Exemple #1
0
    def new_purchase(jwt):
        data = request.get_json()
        user_id = data.get('user_id', None)
        product_id = data.get('product_id', None)

        if user_id is None:
            abort(400)

        if product_id is None:
            abort(400)

        try:
            product = Product.query.filter(
                Product.id == product_id).one_or_none()

            if product is None or product.availability_status is False:
                abort(404)
            user_product = UserProduct(user_id=user_id, product_id=product_id)
            user_product.insert()

        except:
            abort(422)

        return jsonify({
            'success': True,
            'user_id': user_id,
            'product_id': product_id,
            'message': 'Thankyou for shopping with us!!'
        })
Exemple #2
0
def add_product_to_catalog(user_id, product_name):

    product = Product.create(
        product_name=product_name,
        description="",
        price_per_unit=100.0,
        number_in_stock=1,
    )
    UserProduct.create(user_id=user_id, product_id=product)
    return product
Exemple #3
0
def finish_order(order):
    import nova
    from user import serv
    from json import dumps
    order.status = 2
    useraccount = serv.get_user_account(order.user_id)
    user = serv.get_user_login(order.user_id)
    if not useraccount.tenant_id:
        tenant = nova.api().get_tenent(useraccount)
        useraccount.tenant_id = tenant['id']
        usertenant = UserTenant(user)
        usertenant.tenant_id = tenant['id']
        usertenant.tenant_name = tenant['name']
        usertenant.admin_user_id = tenant['user_id']
        key_pair = nova.api().gen_key(useraccount, tenant["name"])
        usertenant.keypair = dumps(key_pair)
        uk = UserKey(key_content=key_pair)
        g.db.add(uk)
        g.db.add(usertenant)
    orderproducts = g.db.query(OrderProduct).filter(
        OrderProduct.order_id == order.id).all()
    for orderproduct in orderproducts:
        userproduct = UserProduct(user, orderproduct)
        g.db.add(userproduct)
    g.db.flush()
    g.db.commit()
Exemple #4
0
def purchase_do(request, product):
    if not product:
        raise Http404("Product cannot be found")
    if UserProduct.get_product(auth.get_current_user(request), product):
        return render_to_response('store/details.html', 
                                  {'p': product, 
                                   'msg': "You already own this application - to associate with a different device please contact support or use another account."},
                                   RequestContext(request))
    if request.method == "POST":
        form = PurchaseForm(data=request.POST)
        if form.is_valid():
            data = form.cleaned_data
            pin = data["pin"]
            email = data["email"]
            user = auth.get_current_user(request)
            user.preferred_email = email
            user.put()
        else:
            return render_to_response('store/purchase_info.html', 
                                      { 'form': form },
                                      RequestContext(request)) 
    else:
        pin = request.GET.get("pin", None)
        email = auth.get_current_user(request).preferred_email
    if not (pin and email):
        form = PurchaseForm(data={'email': email, 'pin': pin})
        return render_to_response('store/purchase_info.html', 
                                  { 'form': form, 'product': product },
                                  RequestContext(request))        
    tx = PaypalRequest.prepare(product, auth.get_current_user(request), pin)
    return render_to_response('store/purchase.html', 
                              {'product': product, 
                               'pin': pin, 
                               'txid': tx.key() },
                              RequestContext(request))
Exemple #5
0
def activate(request):
    """
    Make sure the user has the product (identify using PIN), and return
    activation code.
    """
    pin = request.GET['pin']
    p = Product.get_by_key_name(request.GET["app"], None)
    if not p:
        raise Http404("Product not found")
    userproduct = UserProduct.gql("where pin = :1 and product = :2", 
                                  pin, p).get()
    if not userproduct:
        raise Http404("Activation data not found")
    return HttpResponse(userproduct.get_activation_code(), mimetype='text/plain')
Exemple #6
0
def update_stock(product_id, new_quantity):
    product = UserProduct.get(UserProduct.product_id == product_id)
    new_quantity = product.quantity
    product.save()
Exemple #7
0
def list_user_products(user_id):
    return (UserProduct.select(
        User.first_name,
        User.last_name,
        Product.product_name,
    ).join(UserProduct).join(User).where(UserProduct.user_id == user_id))