def query_account(self, user_id): if user_id is None: result = self.session.query(Account).filter( Account.user_id != 1).all() return [row2dict(item) for item in result] if result else '' else: account = self.session.query(Account).filter( Account.user_id == user_id).first() return row2dict(account) if account else ''
def query_category(self, category_id, page, size): if category_id is None: query = self.session.query(Category).order_by(Category.is_top.desc(), Category.created_at.desc()) result = self.query_one_page(query, page, size) data = [row2dict(item) for item in result] if result else [] total = self.query_total(query) else: result = self.session.query(Category).filter( Category.category_id == category_id ).first() data = row2dict(result) if result else '' total = 0 return data, total
def login(self, name, password): account = self.session.query(Account).filter( Account.username == name).first() if not account: return False else: account = row2dict(account) if account['password'] == password: # if check_password(account['password'], hash_password(password)): del account['password'] return account return False
def query_community_info(self, community_id, page, size): if community_id is None: query = self.session.query(Community) total = self.query_total(query) query = query.order_by(Community.is_top.desc(), Community.created_at.desc()) result = self.query_one_page(query, page, size) data = [row2dict(item) for item in result] if result else [] for item in data: item['province'] = self.get_region_name(item['province_id']) item['city'] = self.get_region_name(item['city_id']) item['area_name'] = self.get_region_name(item['area']) else: result = self.session.query(Community).filter( Community.community_id == community_id ).first() data = row2dict(result) if result else '' if data: data['province'] = self.get_region_name(data['province_id']) data['city'] = self.get_region_name(data['city_id']) data['area_name'] = self.get_region_name(data['area']) total = 0 return data, total
def get_group_order_form_info(self, community_id, category_id, product_id): if product_id is None or not product_id: query = self.session.query( Product.product_id, Product.name, Product.category_ids, Product.image_0, Product.image_1, Product.image_2, Product.image_3, Product.image_4, Product.community_id, Product.group_number, Product.group_price, Product.market_price, Product.charge_unit, Product.rate, Product.brief, Product.sell_point).filter(Product.deleted == 0) if community_id: query = query.filter( Product.community_id.like("%{}%".format( str(community_id)))) if category_id: query = query.filter( Product.category_ids.like("%{}%".format(str(category_id)))) product_list = query.order_by(Product.is_top.desc(), Product.created_at.desc()).all() # product_list = [row2dict(product) for product in product_list] if product_list else [] product_list = [{ "product_id": item[0], "name": item[1], "category_ids": item[2], "image_0": item[3], "image_1": item[4], "image_2": item[5], "image_3": item[6], "image_4": item[7], "community_id": item[8], "group_number": item[9], "group_price": item[10], "market_price": item[11], "charge_unit": item[12], "rate": item[13], "brief": item[14], "sell_point": item[15] } for item in product_list] if product_list else [] for product in product_list: self._format_product(product) return product_list else: query = self.session.query(Product) product = query.filter(Product.product_id == product_id).first() if not product: return '' else: product = row2dict(product) return self._format_product(product)
def query_product(self, product_id, page, size): # 带转换类型和社区名,relationship if product_id is None: count = self.session.query(func.count(Product.product_id)).scalar() query = self.session.query( Product.product_id, Product.name, Product.category_ids, Product.image_0, Product.image_1, Product.image_2, Product.image_3, Product.image_4, Product.community_id).filter(Product.deleted == 0).order_by( Product.is_top.desc(), Product.created_at.desc()) result = self.query_one_page(query, page, size) data = [{ "product_id": item[0], "name": item[1], "category_ids": item[2], "image_0": item[3], "image_1": item[4], "image_2": item[5], "image_3": item[6], "image_4": item[7], "community_id": item[8] } for item in result] if result else [] # data = [row2dict(item) for item in result] if result else [] for item in data: import json ids = item["category_ids"].split(",") item["categorys"] = [] for id in ids: name = self.session.query(Category.name).filter( Category.category_id == int(id)).first() item["categorys"].append(name[0] if name else '') item["categorys"] = json.dumps(item["categorys"]) item["category_id"] = json.dumps( item["category_ids"].split(",")) ids = item["community_id"].split(',') item["community_id"] = json.dumps(ids) return data, count else: result = self.session.query(Product).filter( Product.product_id == product_id).first() data = row2dict(result) if result else '' if data: data['category_id'] = data['category_ids'].split(",") data['community_id'] = data['community_id'].split(',') return data, 0
def _get_newest_orders(self, product_id): order_list = self.session.query(OrderFormNew).filter( and_(OrderForm.product_id == product_id, OrderForm.review_status == 1)).order_by( OrderForm.created_at.desc()).limit(5).all() detail_list = list() order_list = [row2dict(item) for item in order_list] if order_list else [] for item in order_list: name = str(item['name']).replace( str(item['name'])[1:], len(str(item['name'])[1:]) * "*") telephone = str(item["telephone"]).replace( str(item['telephone'])[3:8], 4 * "*") if len(str(item["telephone"])) >= 11 else str( item["telephone"]) detail_list.append( str(item["community"]) + "的" + name + ", " + telephone + ", 拼团成功!") return detail_list
def query_all_order_form(self, telephone, community_id, review_status, page, size): query = self.session.query(OrderFormNew) if telephone: query = query.filter( OrderForm.telephone.like("%{}%".format(telephone))) if community_id or community_id == 0: query = query.filter(OrderForm.community_id == int(community_id)) if review_status or review_status == 0: query = query.filter(OrderForm.review_status == int(review_status)) query = query.order_by(OrderForm.created_at.desc()) result = self.query_one_page(query, page, size) total = self.query_total(query) data = [row2dict(item) for item in result] if result else [] for item in data: item["created_at"] = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime(item["created_at"])) item["order_form_detail"] = str(item['name']) + " " + str( item["telephone"]) + " " + str( item["community"] ) + " " + item['address'] + " " + APARTMENT_DICT.get( item['apartment'], "") + " " + str(item['product']) return data, total
def query_basic_info(self): data = self.session.query(BasicInfo).all() return row2dict(data[0]) if data else ''
def query_province(self, address_id): result = self.session.query(Region).filter( Region.parent_id == address_id).all() return [row2dict(item) for item in result] if result else []
def query_city(self, parent_id): result = self.session.query(Region).filter( Region.parent_id == parent_id).all() return [row2dict(item) for item in result] if result else []