def add_global_template_variables(): return dict(today=JalaliDate.today().strftime('%A %d %B %Y'), session=session, base_url=config.get('base_url'), pictures=DBSession.query(Picture).all(), categories=DBSession.query(Category).options( joinedload('subcategory')).all())
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 index(self): latest = DBSession.query(Product).order_by(Product.id.desc()).limit(16).all() articles = DBSession.query(Article).order_by(Article.id.desc()).limit(2).all() top = [] return dict( latest=latest, articles=articles, top=top, title='آنلاین لوکس - خرید بدلیجات', description=u'خرید ست، نیم ست، دستبند، گوشواره' )
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 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 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 post_login(self, came_from=lurl('/')): if not request.identity: return 'False' user = DBSession.query(User).filter(User.user_name == request.remote_user).one_or_none() session['user_id'] = user.user_id session['user_name'] = user.user_name session['display_name'] = user.display_name session.save() return 'True'
def magazine(self, **kwargs): page = kwargs.get('page') offset = (page - 1) * 9 if page else 0 articles = DBSession.\ query(Article).\ order_by(Article.id.desc()).\ offset(offset).\ all() top = DBSession.query(Article).order_by(Article.views.desc()).limit(3).all() return dict(articles=articles, top=top, title=u'آنلاین لوکس - مجله')
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')
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 tearDown(self): """Tear down test fixture for each model test method.""" DBSession.rollback()
def test_query_obj(self): """Model objects can be queried""" obj = DBSession.query(self.klass).one() for key, value in self.attrs.items(): eq_(getattr(obj, key), value)
def index(self): subcategories = DBSession.query(SubCategory).all() topics = DBSession.query(Topic).all() return dict(subcategories=subcategories, topics=topics)
def current(cls): if 'REMOTE_USER' not in request.environ: abort(401) return DBSession.query(User).filter(User.user_name == request.environ['REMOTE_USER']).one()
def purchases(self): purchases = DBSession.query(Purchase).filter(Purchase.user_id == User.current().user_id).all() return dict(purchases=purchases, title=u' آنلاین لوکس - سفارشات من')