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)
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))
def submit_article(self, **kw): image = StorageManager().store(kw.get('image')) del kw['image'] a = Article() a.image = image for k, v in kw.items(): a.__setattr__(k, v) DBSession.add(a) DBSession.flush() redirect('/area51')
def submit_product(self, **kw): images = ','.join([StorageManager().store(i) for i in kw.get('form-images')]) del kw['form-images'] p = Product() p.images = images for k, v in kw.items(): p.__setattr__(k, v) DBSession.add(p) DBSession.flush() redirect('/area51')
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)
def setUp(self): """Setup test fixture for each model test method.""" try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
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')
def a(self, a_id, a_title): article = DBSession.query(Article).filter(Article.id == a_id).one_or_none() if not article: abort(404) related = DBSession.\ query(Article).\ filter(Article.topic_id == article.topic_id).\ order_by(Article.id.desc()).\ limit(5).\ all() article.views = Article.views + 1 DBSession.flush() return dict( related=related, article=article, title=u'مجله آنلاین لوکس - {}'.format(dash_for_space(article.title)) )
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')
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')
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')