class Share(ModelBase): __table__ = 'share_info' id = IntField("id") openid = StringField("openid") open_gid = StringField("open_gid") page = StringField("page") created = IntField("created", int(time.time() * 1000)) times = IntField("times", 0) op = StringField("op") type = StringField("type", "group") def save(self): assert self.openid and self.open_gid self._data.pop('id', '') return self._save(self._data) def find_share_by_openid(self, page, count): assert self.openid condition = {'openid': self.openid, "type": self.type} return self._find(condition, page, count) def check_share_info(self): assert self.openid and self.open_gid and self.page condition = { 'openid': self.openid, 'open_gid': self.open_gid, 'page': self.page, 'op': self.op } return self._find_one(condition) def inc_times(self): assert self.id return self._increase(self.id, 'times')
class HintHistory(ModelBase): __table__ = 'hint_history' __dbcon__ = POETRY_DB word = StringField("word", '') index = IntField("index", None) openid = StringField("openid", '') level = IntField("level", None) created = IntField("created", 0) def get_hint_data(self): assert self.openid and self.level cond = {"openid": self.openid, 'level': self.level} return self._find_mongo(cond) def save_hint_data(self): data = {} LOG.info(self._data) assert self.word and self.index is not None and self.openid \ and self.level data['word'] = self.word data['openid'] = self.openid data['level'] = self.level data['index'] = self.index data['created'] = int(time.time() * 1000) return self._save(data)
class Order(ModelBase): '''order object''' __table__ = 'orders' out_trade_no = StringField("out_trade_no", None) openid = StringField("openid", None) pay_type = StringField("pay_type", '') client_ip = StringField("client_ip", '') transaction_id = StringField("transaction_id", '') status = IntField("status", OrderStatus.NOTPAYED.value) total_fee = IntField("total_fee") body = StringField("body", '') update_time = IntField('update_time', int(time.time() * 1000)) def save(self, commit=True): '''save order obj''' assert self.out_trade_no is not None and self.openid is not None \ and self.total_fee is not None return self._save(self._data, commit) def update(self, update_data): assert self.out_trade_no condition = {'out_trade_no': self.out_trade_no} return self._update(condition, update_data) def find_one_order(self): assert self.out_trade_no condition = {'out_trade_no': self.out_trade_no} return self._find_one(condition)
class Author(ModelBase): '''author object''' __table__ = 'authors' id = StringField("id") name = StringField("name", None) description = StringField("description", None) headimg = StringField("headimg", None) total = IntField("total", 0) created = IntField("created", int(time.time() * 1000)) dynasty = StringField('dynasty', None) poetry_link = StringField("poetry_link", '') def find_author_by_name(self): '''find author object by author name''' assert self.name cond = {'name': self.name} return self._find_one(cond) def find_author_by_id(self): assert self.id return self._find_one({'id': self.id}) def save_author(self): '''save author data''' assert self.name and self.description and self.total is not None\ and self.dynasty self._data.pop("id", '') return self._save(self._data) def find_authors(self, condition, page, count): return self._find(condition, page=page, count=count)
class Category(ModelBase): __table__ = 'category' __dbcon__ = MONGO_DB id = IntField("id", None) name = StringField("name", None) recommend = IntField("recommend", None) def find_category_by_ids(self, ids): if not isinstance(ids, list): ids = [ids] condition = {'id': {'$in': ids}} return self._find_mongo(condition) def all_category(self, page=1, count=50): condition = {} if self.recommend is not None: condition.update({'recommend': self.recommend}) return self._find_mongo(condition, page, count) def find_category_by_id(self): assert self.id return self._find_one({"id": self.id}) def save_category(self): data = {'id': self.id, 'name': self.name} if self.recommend: data.update({'recommend': 1}) ori_data = self.find_category_by_id() if ori_data: return ori_data['_id'] return self._save(data)
class User(ModelBase): '''user object''' __table__ = 'userinfo' id = IntField("id", 0) unionid = StringField("unionid", '') openid = StringField("openid", '') nickname = StringField("nickname", '') sex = IntField("sex", 0) headimgurl = StringField("headimgurl", '') language = StringField("language", 'zh_CN') country = StringField("country", '') province = StringField("province", '') city = StringField("city", '') created = IntField('created', int(time.time() * 1000)) def find_by_openid(self): '''get user by user openid''' assert self.openid != '' return self._find_one({'openid': self.openid}) def save_user(self): assert self.openid != '' self._data.pop("id", '') return self._save(self._data) def update_user(self, update_data): assert self.openid != '' and update_data condition = {'openid': self.openid} return self._update(condition, update_data)
class KeyWords(ModelBase): __table__ = 'keywords' __dbcon__ = MONGO_DB category_id = IntField("category_id") enabled = IntField("enabled", 1) word = StringField("word", '') def get_keywords(self, condition, page, count): condition.update({'enabled': self.enabled}) return self._find_mongo(condition, page, count)
class Tab(ModelBase): '''tab object''' __table__ = 'tab' id = IntField("id") created = IntField("created", int(time.time() * 1000)) tab_icon = StringField("icon", '') enabled = IntField("enabled", 0) tab_name = StringField("name", '') tab_id = IntField("tab_id") def get_tab(self): condition = {'enabled': 1} return self._find(condition)
class TbkConfig(ModelBase): __table__ = 'tbk_config' __dbcon__ = MONGO_DB key = StringField("key") start = IntField("start") end = IntField("end") desc = StringField("desc") switch = IntField("switch") proxy_end = IntField("proxy_end") phone = StringField("phone", '') api_key = IntField("api_key") api_sec = StringField("api_sec") pid = StringField("pid") def get_config_data(self): assert self.key cond = {'key': self.key} return self._find_one(cond)
class TbkOrder(ModelBase): __table__ = 'orders' __dbcon__ = MONGO_DB order_id = IntField("order_id") # 订单id goods_id = IntField("goods_id") # 商品id count = IntField("count") # 商品个数 creatd = StringField("created", '') # 同步订单的时间 goods_title = StringField("title", '') # 商品标题 order_status = StringField("order_status", '') # 订单状态 paid_fee = FloatField("paid_fee", 0) # 实际支付金额 expect_fee = FloatField("expect_fee", 0) # 效果预估, income_fee = FloatField("income_fee", 0) # 实际结算金额 income_time = StringField("income_time", '') # 结算时间 comm_rate = FloatField("comm_rate", 0) # 佣金比例 created_time = StringField("created_time", '') # 订单创建时间 pid = StringField("pid", '') # 订单所属的pid uid = StringField("uid", '') # 订单属于哪个用户 status = IntField("status", 0) # 订单在本系统中的状态,如是否累加到用户结算
class SubCategory(ModelBase): __table__ = 'sub_category' __dbcon__ = MONGO_DB id = IntField("id", None) parent = IntField("parent") name = StringField("name", "") def find_one(self): assert self.id return self._find_one({'id': self.id}) def save_category(self): assert self.id and self.parent is not None data = {'id': self.id, "name": self.name, 'parent': self.parent} ori_data = self.find_one() if ori_data: return ori_data['id'] return self._save(data)
class TbkHotWord(ModelBase): __table__ = 'hot_words' __dbcon__ = MONGO_DB word = StringField("word") enabled = IntField("enabled", None) def all_hot_words(self): cond = {'enabled': 1 if self.enabled else 0} return self._find_mongo(cond)
class Goods(ModelBase): '''goods object''' __table__ = 'goods' id = IntField("id", None) name = StringField("name", None) description = StringField("description", None) price = IntField("price", None) feature = StringField("feature", None) sales = IntField("sales", None) enabled = IntField("enabled", None) count = IntField("count", 0) def find_by_id(self): """find goods by goods id""" assert self.id is not None return self._find_one({'id': self.id}) def find_all(self): """get all goods""" return self._find({})
class Sentence(ModelBase): __table__ = 'sentence' id = IntField("id") # source title title = StringField("title", None) # the sentence content = StringField('content', None) created = IntField("created", int(time.time() * 1000)) banner = StringField("banner", '') tags = StringField("tags", None) author_id = StringField("author_id", None) author = StringField("author") poetry_id = IntField("poetry_id") likes = IntField("likes", 0) type = IntField("type", SentenceType.POETRY.value) def find_by_content(self): assert self.content return self._find_one({'content': self.content}) def find_by_id(self): assert self.id return self._find_one({'id': self.id}) def save(self): ori_content = self.find_by_content() if ori_content: return ori_content['id'] self._data.pop("id", '') LOG.debug("save centence: %s", self.content) return self._save(self._data) def find_sentence_by_cond(self, condition, page, count): return self._find(condition, page, count, sort={"likes": -1}) def update_sentence_by_id(self, data): assert self.id condition = {'id': self.id} return self._update(condition, data)
class SentenceDaily(ModelBase): __table__ = 'sentence_daily' id = IntField("id") # the English content content_en = StringField('content_en', None) content_cn = StringField('content_cn', None) note = StringField('note', None) created = IntField("created", int(time.time() * 1000)) banner = StringField("banner", '') tags = StringField("tags", None) likes = IntField("likes", 0) type = IntField('type', None) voice_url = StringField('voice_url', None) date_str = StringField('date_str', '') def save_data(self): assert self.content_en or self.content_cn return self._save(self._data) def _build_cond(self): cond = {} if self.id: cond['id'] = self.id if self.date_str: cond['date_str'] = self.date_str if self.type: cond['type'] = self.type return cond def get_one_sentence(self): cond = self._build_cond() return self._find_one(cond) def list_sentences(self, condition, page=1, count=20, sort={'likes': -1}): cond = self._build_cond() if condition: cond.update(condition) return self._find(cond, page, count, sort=sort)
class SearchKeyword(ModelBase): '''poetry object''' __table__ = 'search_keyword' id = IntField("id") keyword = StringField("keyword") created = IntField("created", int(time.time() * 1000)) openid = StringField("openid", '') times = IntField('times', 0) def save(self): self._data.pop("id", '') return self._save(self._data) def check_search(self): assert self.keyword and self.openid condition = {"keyword": self.keyword, 'openid': self.openid} return self._find_one(condition) def inc_times(self): assert self.id return self._increase(self.id, 'times')
class FormId(ModelBase): '''user formid''' __table__ = 'formid' id = IntField("id") created = IntField("created", int(time.time() * 1000)) openid = StringField("openid", '') enabled = IntField("enabled", 1) formid = StringField("formid", '') def save(self): assert self.openid and self.formid data = {} data['created'] = self.created data['openid'] = self.openid data['enabled'] = self.enabled data['formid'] = self.formid return self._save(data) def get_one_formid(self): assert self.openid condition = {'openid': self.openid, 'enabled': 1} record = self._find_one(condition) if not record: return None self.disabled_formid(record['id']) return record def disabled_formid(self, _id): condition = {'id': _id} return self._update(condition, {'enabled': 0}) def get_sended_formids(self, page, count): condition = {'enabled': 1} group_by_fields = ['openid'] ret_fields = ['openid', 'min(formid) as formid'] return self._group_by(condition, group_by_fields, ret_fields, page, count)
class GridPoetry(ModelBase): '''poetry object''' __table__ = 'poetries' __dbcon__ = POETRY_DB answer = StringField("answer", '') author = StringField("author", '') dynasty = StringField("dynasty", '') enabled = IntField("enabled", 1) title = StringField("title", '') poetry = StringField("poetry", '') recited = IntField("recited", 1) level = IntField("level", 1) type = IntField("type", 1) def get_level_data(self): assert self.level cond = { 'level': self.level, 'enabled': self.enabled, 'type': self.type } return self._find_one(cond)
class MpUser(ModelBase): __table__ = 'userdata' __dbcon__ = MONGO_DB uid = StringField("uid", '') openid = StringField("openid", '') nickname = StringField("nickname", '') avatar = StringField("avatar", '') status = IntField("status", -1) login_time = IntField("login_time") def save_mpuser(self): assert self.uid and self.nickname and self.avatar and self.openid if self.get_user_by_openid(): update_data = { 'nickname': self.nickname, 'avatar': self.avatar, 'status': self.status, 'uid': self.uid } if self.status == 0: update_data.update({'login_time': time.time()}) cond = {'openid': self.openid} ret = self._update(cond, update_data) else: ret = self._save(self._data) return ret def get_user_by_uid(self): assert self.uid cond = {'uid': self.uid} return self._find_one(cond) def get_user_by_openid(self): assert self.openid cond = {'openid': self.openid} return self._find_one(cond)
class GridUser(User): credit = IntField("credit", 0) level = IntField("level", 1) new_buy = IntField("new_buy", 0) def answer_right(self): assert self.openid condition = {'openid': self.openid} return self._increase_condition(condition, 'level') def find_by_uid(self): assert self.id condition = {'id': self.id} return self._find_one(condition) def increase_credit(self, count): assert self.id or self.openid condition = {} if self.id: condition.update({'id': self.id}) if self.openid: condition.update({'openid': self.openid}) return self._increase_condition(condition, 'credit', count)
class Banner(ModelBase): '''poetry object''' __table__ = 'banner' id = IntField("id") page_path = StringField('page_path') created = IntField("created", int(time.time() * 1000)) banner_url = StringField("banner_url", '') enabled = IntField("enabled", 0) appid = StringField("appid", '') def get_banner(self, enabled=1, page=1, count=50): condition = {} if enabled is not None: condition.update({"enabled": enabled}) return self._find(condition, page=page, count=count) def count(self): return self._count({}) def get_banner_by_id(self): assert self.id return self._find_one({"id": self.id}) def update_banner_by_id(self, data): assert self.id cond = {"id": self.id} return self._update(cond, data) def save_banner(self, data): return self._save(data) def delete_banner(self): assert self.id cond = {"id": self.id} return self._delete_(cond)
class OrderDetail(ModelBase): '''order detail object''' __table__ = 'order_detail' __dbcon__ = None order_detail_id = IntField("order_detail_id") out_trade_no = StringField("out_trade_no") goods_id = IntField("goods_id") price = IntField("price") count = IntField("count") fee = IntField("fee") def save(self, commit=True): '''save order detail''' assert self.out_trade_no is not None and self.price is not None and \ self.count is not None and self.fee is not None if self.order_detail_id is None: self._data.pop('order_detail_id') return self._save(self._data, commit) def get_detail(self): assert self.out_trade_no condition = {'out_trade_no': self.out_trade_no} return self._find(condition)
class NavDiandi(ModelBase): __table__ = 'diandi_user' __dbcon__ = MONGO_DB openid = StringField("openid", '') created = IntField("created", 0) def save(self): assert self.openid data = {"openid": self.openid, 'created': self.created} return self._save(data) def find_one(self): assert self.openid condition = {'openid': self.openid} return self._find_one(condition)
class GridFormId(ModelBase): __table__ = "formid" __dbcon__ = MONGO_DB appid = StringField("appid", '') openid = StringField("openid", '') formid = StringField("formid", '') created = IntField("created", 0) def save(self): assert self.openid and self.formid data = { "openid": self.openid, "formid": self.formid, "created": int(time.time() * 1000), "appid": self.appid, "enabled": 1 } return self._save(data) def find_one(self): assert self.openid now = int(time.time() * 1000) start = (now - 7 * 86400 * 1000) condition = { "openid": self.openid, "created": { "$gte": start }, "enabled": 1 } # LOCK record = self._find_one(condition) if record: self.disabled_form(record['_id']) return record def disabled_form(self, _id): condition = {"_id": _id} return self._update(condition, {'enabled': 0})
class TbkBanner(ModelBase): __table__ = 'banner' __dbcon__ = MONGO_DB image = StringField("img", '') url = StringField("url", '') open_type = StringField("open_type", '') enabled = IntField("enabled", 1) def get_banners(self, enabled=1): cond = {'enabled': enabled} banners = self._find_mongo(cond) result = [] for banner in banners: result.append({ "url": banner['url'], 'open_type': banner['open_type'], 'img': banner['img'], 'id': str(banner['_id']) }) return result
class ModelBase(object): '''database object model base''' __metaclass__ = ModelMetaClass created = IntField("created", int(time.time() * 1000)) def __init__(self, **kwargs): self._data = {} for key, value in self.fields.items(): field_name = self.fields_map.get(key) real_value = kwargs.get(field_name) if real_value is None: self._data[key] = value.default else: self._data[key] = real_value def __repr__(self): _repr = "{klass}({name})".format(klass=self.__class__.__name__, name=repr(self._data)) return _repr def _find_one(self, condition): return self.__dbcon__.get_one(self.__table__, condition) def _find(self, condition, page=1, count=20, sort=None): return self.__dbcon__.get(self.__table__, condition, page, count, sort=sort) def _save(self, data, commit=True): return self.__dbcon__.insert(self.__table__, data, commit) def commit(self): return self.__dbcon__.commit() def rollback(self): return self.__dbcon__.return_error() def _update(self, condition, update_data): return self.__dbcon__.update(self.__table__, condition, update_data) def _update_multi(self, condition, update_data, multi=False): return self.__dbcon__.update(self.__table__, condition, update_data, multi=multi) def _increase(self, id, field, count=1): return self.__dbcon__.increase(self.__table__, {'id': id}, field, count) def _increase_condition(self, condition, field, count=1): return self.__dbcon__.increase(self.__table__, condition, field, count) def _search(self, condition, page, count, fields=['*']): return self.__dbcon__.match_data(self.__table__, condition, fields, page, count) def _search_fulltext(self, keyword, page, count, fields): if not isinstance(keyword, unicode): keyword = unicode(keyword) return self.__dbcon__.search_poetry(keyword, page, count, fields) def _search_widget(self, keyword, page, count, fields, sort): if not isinstance(keyword, unicode): keyword = unicode(keyword) return self.__dbcon__.search_poetry_widget(keyword, page, count, fields, sort) def _rand_get(self, fields, page, count): return self.__dbcon__.random_get(self.__table__, fields, page, count) def _delete_(self, condition): return self.__dbcon__.delete(self.__table__, condition) def _delete(self, condition): return self.__dbcon__.remove(self.__table__, condition) def _find_mongo(self, condition, page=1, count=20, fields=[]): return self.__dbcon__.find(self.__table__, condition, fields, page, count) def _count(self, condition): return self.__dbcon__.count(self.__table__, condition) def _group_by(self, condition, group_by_fields, ret_fields, page, count): return self.__dbcon__.group_by(self.__table__, condition, group_by_fields, ret_fields, page, count)
class UserBrowseHistory(ModelBase): __table__ = 'user_history' id = IntField('id') openid = StringField("openid") poetry_id = IntField("poetry_id") created = IntField("created", int(time.time() * 1000)) # 0 --- browse 1--- like -1 --- dislike operation = IntField("operation", PoetryOp.BROWSE.value) times = IntField("times", 1) qrcode = StringField("qrcode", '') def check_browse(self): assert self.openid and self.poetry_id condition = {'openid': self.openid, 'poetry_id': self.poetry_id} return self._find_one(condition) def save(self): self._data.pop("id", '') return self._save(self._data) def inc_times(self): assert self.id return self._increase(self.id, 'times') def user_operation(self, op_code): assert self.openid and self.poetry_id condition = {'openid': self.openid, 'poetry_id': self.poetry_id} update_data = {'operation': op_code} return self._update(condition, update_data) def get_user_history(self, page, count): assert self.openid condition = {'openid': self.openid} return self._find(condition, page, count, {'created': -1}) def get_user_like(self, page, count): assert self.openid condition = {'openid': self.openid, 'operation': PoetryOp.LIKE.value} return self._find(condition, page, count, {'created': -1}) def update_qrcode(self, qrcode_url): assert self.id or (self.openid and self.poetry_id) if self.id: condition = {'id': self.id} else: condition = {'openid': self.openid, 'poetry_id': self.poetry_id} update_data = {'qrcode': qrcode_url} return self._update(condition, update_data) def is_browsed(self, openid, poetry_id): cond = {'openid': openid, 'poetry_id': poetry_id} return self._find_one(cond) def get_today_history(self): today = datetime.now() today = datetime(today.year, today.month, today.day) stamp = time.mktime(today.timetuple()) * 1000 cond = {'openid': self.openid, 'created': {'>=': stamp}} return self._find_one(cond) def get_sended_users(self, page, count): group_by_fields = ['openid'] select_fields = ['openid', 'count(poetry_id) as recited', 'poetry_id'] return self._group_by({}, group_by_fields, select_fields, page, count)
class TbkGoods(ModelBase): __table__ = 'goods' __dbcon__ = MONGO_DB coupon_total_count = IntField("coupon_total_count", 0) coupon_info = StringField("coupon_info", '') is_tmall = IntField("is_tmall", 0) end = StringField("end", '') start = StringField("start", '') created = IntField("created", int(time.time() * 1000)) small_images = ListField("small_images", []) sales = IntField("sales", 0) num_id = IntField("num_id", 0) coupon_id = StringField("coupon_id", '') coupon_start = IntField("coupon_start", 0) coupon_share_url = StringField("coupon_share_url", '') price = FloatField("price") coupon_amount = IntField("coupon_amount") category_id = IntField("category_id") category_name = StringField("category_name") commssion_rate = FloatField("commssion_rate") pic_url = StringField("pic_url") title = StringField("title") coupon_remain = IntField("coupon_remain") update_time = IntField("update_time", int(time.time() * 1000)) coupon_expire = IntField("coupon_expire", 0) similar_goods = ListField("similar_goods", []) source = StringField("source", "") coupon_fee = FloatField("coupon_fee", 0) sub_category_id = IntField("sub_category_id") sub_category_name = StringField("sub_category_name", '') mid = IntField("mid", 0) def find_goods_by_id(self): cond = {'num_id': self.num_id} return self._find_one(cond) def update(self, update_data): assert self.num_id cond = {"num_id": self.num_id} return self._update(cond, update_data) def delete(self): assert self.num_id return self._delete({'num_id': self.num_id}) def disabled_goods_by_id(self): assert self.num_id cond = {'num_id': self.num_id} update_data = {'coupon_expire': 1} LOG.info("disabled goods: %s", self.num_id) searcher.delete_index(self.num_id) return self._delete(cond) def check_save(self): coupon_amount = float(self.coupon_amount) price = float(self.price) coupon_fee = price - coupon_amount if coupon_fee <= 0: return False if coupon_amount / coupon_fee >= 0.1: return True return False def find_goods_by_cond(self, condition, page, count, fields=[]): return self._find_mongo(condition, page, count, fields) def count(self, condition): return self._count(condition) def save(self): assert self.num_id and self.coupon_total_count is not None and self.price and\ self.title and self.coupon_amount is not None old_goods = self.find_goods_by_id() if old_goods: ret = old_goods['_id'] update_data = self._get_update_data(old_goods) update_data.update({ 'update_time': int(time.time() * 1000), 'coupon_expire': 0 }) ret = self.update(update_data) LOG.info("update goods: %s, data: %s, ret: %s", self.num_id, update_data, ret) else: ret = self._save(self._data) LOG.info("save goods: %s, id: %s", ret, self.num_id) return str(ret) def _get_update_data(self, old_data): change_keys = ("coupon_total_count", "coupon_info", "is_tmall", "end", "start", "sales", "coupon_id", "coupon_start", "coupon_share_url", "price", "coupon_amount", "category_id", "category_name", "commssion_rate", "title", "coupon_remain", "sub_category_id") update_data = {} for key in change_keys: old_value = old_data.get(key) new_value = getattr(self, key) if new_value and old_value != new_value: update_data[key] = new_value return update_data
class TbkMenuConfig(ModelBase): __table__ = 'tabs' __dbcon__ = MONGO_DB id = IntField("id") name = StringField("name") enabled = IntField("enabled") icon = StringField("icon") parent = IntField("parent") type = StringField("type") sort_tabs = ListField("sort_tabs") def get_parent_tab(self, parent_id=None, enabled=1): cond = {'enabled': enabled, 'parent': 0} if parent_id: cond.update({'_id': parent_id}) return self._find_mongo(cond) def get_one_menu(self, mid): cond = {'_id': mid} return self._find_one(cond) def get_sort_tabs(self): sort_tabs = self.__dbcon__.find("sort_tabs", {}, count=100) sort_tab_dict = {} for tab in sort_tabs: _id = tab['_id'] name = tab['name'] sort_tab_dict[_id] = {'id': _id, "name": name} return sort_tab_dict def get_cond_tabs(self): cond_tabs = self.__dbcon__.find("cond_tabs", {}) cond_tab_dict = {} for tab in cond_tabs: _id = tab['_id'] name = tab['name'] params = tab['params'] tmp = {'id': _id, 'name': name, 'params': params} cond_tab_dict[_id] = tmp return cond_tab_dict def _ship_sort_tab(self, sort_ids, sort_tab_dict): data = [] for sort_id in sort_ids: sort_config = sort_tab_dict.get(sort_id) if not sort_config: continue data.append(sort_config) return data def get_sub_menu(self, parent=None): if parent: cond = {'parent': parent} else: cond = {'parent': {'$ne': 0}} cond.update({'enabled': 1}) cats = self._find_mongo(cond, count=100) result_dict = {} for cat in cats: parent = cat['parent'] tmp = {'id': cat['_id'], 'name': cat['name'], 'icon': cat['icon']} result_dict.setdefault(parent, []) result_dict[parent].append(tmp) return result_dict def get_config_tabs(self, enabled=1): result = {} sort_tab_dict = self.get_sort_tabs() cond_tab_dict = self.get_cond_tabs() parent_tabs = self.get_parent_tab() sub_cat = self.get_sub_menu() for tab in parent_tabs: _type = tab.get("type", '') if not _type: continue result.setdefault(_type, []) tmp = {"name": tab['name'], 'id': tab['_id'], 'icon': tab['icon']} sort_tab = self._ship_sort_tab(tab.get('sort_tabs', []), sort_tab_dict) cond_tab = self._ship_sort_tab(tab.get("cond_tabs", []), cond_tab_dict) _id = tab['_id'] cats = sub_cat.get(_id, []) result[_type].append({ 'parent': tmp, 'tabs': sort_tab, 'cats': cats, 'condition': cond_tab }) for _type, data in result.items(): data.sort(key=lambda x: x['parent']['id']) return result
class Poetry(ModelBase): '''poetry object''' __table__ = 'poetry' id = IntField("id") title = StringField("title", None) content = StringField('content', None) created = IntField("created", int(time.time() * 1000)) banner = StringField("banner", '') tags = StringField("tags", None) author_id = StringField("author_id", None) dynasty = StringField("dynasty") author = StringField("author") translate = StringField("translate") shangxi = StringField("shangxi") likes = IntField("likes") plink = StringField("plink") def save(self): self._data.pop("id", '') return self._save(self._data) def increase(self, count): assert self.id return self._increase(self.id, 'likes', count) def find_poetry(self, condition, page, count, sort=None): return self._find(condition, page=page, count=count, sort=sort) def count(self, condition): return self._count(condition) def find_poetry_by_id(self): assert self.id return self._find_one({'id': self.id}) def find_poetry_by_title(self): assert self.title return self._find_one({'title': self.title}) def find_poetry_by_author_id(self, page, count): assert self.author_id return self._find({'author_id': self.author_id}, page, count) def check_duplicate(self): assert self.title and self.author and self.content condition = { 'title': self.title, 'author': self.author, 'content': self.content } return self._find_one(condition) def find_poetry_by_ids(self): assert self.id return self._find({'id': self.id}) def search(self, page, count): assert self.author or self.content condition = {} # if self.author: # condition.update({'author': self.author}) # if self.content: condition.update({'content': self.content}) # condition.update({'search_op': 'or'}) fields = [ 'id', 'title', 'content', 'author', 'dynasty', 'likes', 'banner' ] return self._search(condition, page, count, fields) def search_fulltext(self, page, count, fields=None): assert self.content return self._search_fulltext(self.content, page, count, fields) def search_widget(self, page, count, fields=[], sort=None): assert self.content return self._search_widget(self.content, page, count, fields, sort) def random_get(self, page, count): fields = [ 'id', 'title', 'content', 'banner', 'author', 'dynasty', 'likes' ] return self._rand_get(fields, page, count) def update_poetry_by_id(self, update_data): assert self.id condition = {'id': self.id} return self._update(condition, update_data)