Example #1
0
 def add_to_basket(self, p_id):
     user = User.current()
     basket = DBSession. \
         query(Purchase). \
         filter(Purchase.user_id == user.user_id). \
         order_by(Purchase.id.desc()). \
         first()
     product = DBSession.query(Product).filter(Product.id == p_id).one_or_none()
     if product.quantity < 1:
         return dict(ok=False)
     if basket and basket.status == 'Selection':
         if product in basket.product:
             return dict(ok=True)
         elif product not in basket.product:
             basket.product.append(product)
             tmp = basket.items
             tmp[product.id] = 1
             basket.items = tmp
             DBSession.flush()
         return dict(ok=True)
     if not basket or basket.status != 'Selection':
         basket = Purchase(
             user_id=user.user_id,
             items={}
         )
         basket.set_uid()
         basket.product.append(product)
         tmp = basket.items
         tmp[product.id] = 1
         basket.items = tmp
         DBSession.add(basket)
         DBSession.flush()
         return dict(ok=True)
Example #2
0
 def comment(self, **kwargs):
     text = kwargs.get('text')
     product_id = kwargs.get('product_id')
     product_title = kwargs.get('product_title')
     c = Comment(text=text, product_id=product_id, user_id=User.current().user_id)
     DBSession.add(c)
     DBSession.flush()
     redirect('/p/{}/{}'.format(product_id, product_title))
Example #3
0
 def basket(self):
     basket = DBSession.\
         query(Purchase).\
         filter(Purchase.user_id == User.current().user_id).\
         order_by(Purchase.id.desc()).\
         first()
     basket = basket if basket and basket.status == 'Selection' and len(basket.product) > 0 else None
     return dict(basket=basket, title=u'آنلاین لوکس - سبد خرید')
Example #4
0
 def finalize(self, basket_id):
     user = User.current()
     basket = DBSession. \
         query(Purchase). \
         filter(Purchase.user_id == user.user_id). \
         filter(Purchase.id == basket_id). \
         order_by(Purchase.id.desc()). \
         first()
     if not basket or basket.status != 'Selection':
         redirect('/basket')
     return dict(user=user, basket_id=basket_id, title=u'آنلاین لوکس - تایید نهایی فاکتور {}'.format(basket.uid))
Example #5
0
 def register_handler(self, **kwargs):
     username = kwargs.get('username')
     password = kwargs.get('password')
     DBSession.add(User(
         user_name=username,
         password=password
     ))
     try:
         DBSession.flush()
         return dict(ok=True)
     except IntegrityError:
         return dict(ok=False)
Example #6
0
 def remove_from_basket(self, p_id):
     user = User.current()
     basket = DBSession. \
         query(Purchase). \
         filter(Purchase.user_id == user.user_id). \
         order_by(Purchase.id.desc()). \
         first()
     product = DBSession.query(Product).filter(Product.id == p_id).one_or_none()
     if basket and basket.status == 'Selection':
         if product in basket.product:
             basket.product.remove(product)
             tmp = basket.items
             del tmp[str(product.id)]
             basket.items = tmp
             DBSession.flush()
         redirect('/basket')
Example #7
0
 def change_count(self, product_id, value):
     user = User.current()
     basket = DBSession. \
         query(Purchase). \
         filter(Purchase.user_id == user.user_id). \
         order_by(Purchase.id.desc()). \
         first()
     product = DBSession.query(Product).filter(Product.id == product_id).one_or_none()
     tmp = basket.items
     if value == 'up':
         if int(basket.items.get(str(product.id))) >= int(product.quantity):
             return
         tmp[product_id] += 1
     elif value == 'down':
         if int(basket.items.get(str(product.id))) == 1:
             return
         tmp[product_id] += -1
     basket.items = tmp
     DBSession.flush()
     redirect('/basket')
Example #8
0
    def order_basket(self, **k):
        user = User.current()
        basket = DBSession. \
            query(Purchase). \
            filter(Purchase.user_id == user.user_id). \
            filter(Purchase.id == k.get('basket_id')).\
            order_by(Purchase.id.desc()). \
            first()
        if not basket or basket.status != 'Selection':
            return dict(ok=False, error='Invalid state')

        out_of_stock = False
        for p in basket.product:
            if p.quantity < 1:
                out_of_stock = True
                basket.product.remove(p)
            else:
                p.quantity = Product.quantity - basket.items.get(str(p.id))
        if out_of_stock:
            DBSession.flush()
            return dict(ok=False, error='Invalid state')

        dis_name, address, code, phone = k.get('name'), k.get('address'), k.get('code'), k.get('phone')
        user.display_name = dis_name
        user.postal_address = address
        user.postal_code = code
        user.phone_number = phone
        zp = ZarinpalClient(amount=basket.final_price, basket_uid=basket.uid)
        authority = zp.make()
        payment_url = config.get('payment')
        basket.authority = authority
        basket.status = 'Payment'
        try:
            DBSession.flush()
            return dict(ok=True, paymentUrl=payment_url, authority=authority)
        except IntegrityError:
            return dict(ok=False, error='Invalid state')
Example #9
0
 def purchase_callback(self, **kwargs):
     authority = kwargs.get('Authority')
     status = kwargs.get('Status')
     user = User.current()
     if status == 'NOK':
         redirect('/basket')
     elif status == 'OK':
         basket = DBSession. \
             query(Purchase). \
             filter(Purchase.authority == authority). \
             filter(Purchase.user_id == user.user_id). \
             one_or_none()
         if basket and basket.status == 'Payment':
             zp = ZarinpalClient(basket.final_price, basket.uid)
             ref_id, status = zp.verify_payment(authority)
             if ref_id:
                 basket.ref_id = ref_id
                 basket.status = 'Preparing'
                 TelegramNotifier(basket, basket.final_price)
                 DBSession.flush()
             else:
                 redirect('/basket')
         else:
             redirect('/basket')
Example #10
0
 def purchases(self):
     purchases = DBSession.query(Purchase).filter(Purchase.user_id == User.current().user_id).all()
     return dict(purchases=purchases, title=u' آنلاین لوکس - سفارشات من')