def query_random(**kwargs): ''' Return the random records of centain kind. :param num: :param kind: :return: ''' if 'limit' in kwargs: limit = kwargs['limit'] elif 'num' in kwargs: limit = kwargs['num'] else: limit = 10 if 'kind' in kwargs: kind = kwargs['kind'] else: kind = None if kind: rand_recs = TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) ).order_by( peewee.fn.Random() ).limit(limit) else: rand_recs = TabPost.select().order_by( peewee.fn.Random() ).limit(limit) return rand_recs
def query_under_condition(condition, kind='9', sort_option=''): ''' Get All data of certain kind according to the condition ''' if DB_CFG['kind'] == 's': return TabPost.select().where((TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.time_update.desc()) if sort_option: if sort_option == 'time_update': sort_criteria = TabPost.time_update.desc() elif sort_option == 'time_create': sort_criteria = TabPost.time_create.desc() elif sort_option == 'access_1d': sort_criteria = TabPost.access_1d.desc() elif sort_option == 'access_7d': sort_criteria = TabPost.access_7d.desc() elif sort_option == 'access_30d': sort_criteria = TabPost.access_30d.desc() else: sort_criteria = TabPost.view_count.desc() else: sort_criteria = TabPost.time_update.desc() return TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) & TabPost.extinfo.contains(condition)).order_by(sort_criteria)
def query_access(kind, day_sig, limit=10): ''' 返回最近几天访问的列表。 day_sig为标识 : 'd' 为 近24小时, 'w'为近1击, 'm' 为近1月 ''' if day_sig == 'd': return TabPost.select().where( TabPost.kind == kind ).order_by( TabPost.access_1d.desc() ).limit(limit) elif day_sig == 'w': return TabPost.select().where( TabPost.kind == kind ).order_by( TabPost.access_7d.desc() ).limit(limit) elif day_sig == 'm': return TabPost.select().where( TabPost.kind == kind ).order_by( TabPost.access_30d.desc() ).limit(limit) return None
def query_recent(num=8, **kwargs): ''' 获取最近发布,或更新的Post列表 ''' order_by_create = kwargs.get('order_by_create', False) kind = kwargs.get('kind', None) if order_by_create: if kind: recent_recs = TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) ).order_by( TabPost.time_create.desc() ).limit(num) else: recent_recs = TabPost.select().where( TabPost.valid == 1 ).order_by( TabPost.time_create.desc() ).limit(num) else: if kind: recent_recs = TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) ).order_by( TabPost.time_update.desc() ).limit(num) else: recent_recs = TabPost.select().where( TabPost.valid == 1 ).order_by( TabPost.time_update.desc() ).limit(num) return recent_recs
def query_random(**kwargs): ''' Return the random records of centain kind. ''' if 'limit' in kwargs: limit = kwargs['limit'] elif 'num' in kwargs: limit = kwargs['num'] else: limit = 10 kind = kwargs.get('kind', None) if kind: rand_recs = TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) ).order_by( peewee.fn.Random() ).limit(limit) else: rand_recs = TabPost.select().where( TabPost.valid == 1 ).order_by( peewee.fn.Random() ).limit(limit) return rand_recs
def query_under_condition(condition, kind='2'): ''' Get All data of certain kind according to the condition ''' if DB_CFG['kind'] == 's': return TabPost.select().where((TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.time_update.desc()) return TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) & TabPost.extinfo.contains(condition)).order_by( TabPost.time_update.desc())
def query_recent(num=8, **kwargs): ''' query recent posts. ''' if 'kind' in kwargs: kind = kwargs['kind'] recent_recs = TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.time_update.desc()).limit(num) else: recent_recs = TabPost.select().where(TabPost.valid == 1).order_by( TabPost.time_update.desc()).limit(num) return recent_recs
def count_of_classify(tagid): if tagid.endswith('00'): recs = TabPost.select().join( TabPost2Tag, on=(TabPost2Tag.post_id == TabPost.uid)).join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)).where( TabTag.uid.startswith(tagid[:2])) else: recs = TabPost.select().join( TabPost2Tag, on=(TabPost2Tag.post_id == TabPost.uid)).join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)).where( TabTag.uid == tagid) return recs.count()
def query_pager_by_slug(slug, current_page_num=1, tag='', order=False): ''' Query pager via category slug. ''' cat_rec = MCategory.get_by_slug(slug) if cat_rec: cat_id = cat_rec.uid else: return None # The flowing code is valid. if cat_id.endswith('00'): # The first level category, using the code bellow. cat_con = TabPost2Tag.par_id == cat_id else: cat_con = TabPost2Tag.tag_id == cat_id if tag: condition = { 'def_tag_arr': [tag] } recs = TabPost.select().join( TabPost2Tag, on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1)) ).where( cat_con & TabPost.extinfo.contains(condition) ).order_by( TabPost.time_update.desc() ).paginate(current_page_num, CMS_CFG['list_num']) elif order: recs = TabPost.select().join( TabPost2Tag, on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1)) ).where( cat_con ).order_by( TabPost.order.asc() ) else: recs = TabPost.select().join( TabPost2Tag, on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1)) ).where( cat_con ).order_by( TabPost.time_update.desc() ).paginate(current_page_num, CMS_CFG['list_num']) return recs
def query_postinfo_by_cat(catid): cat_con = TabPost2Tag.tag_id == catid recs = TabPost.select().join(TabPost2Tag, on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1))).where(cat_con) return recs
def query_pager_by_slug(kind, current_page_num=1): ''' Query pager ''' return TabPost.select().where(TabPost.kind == kind).order_by( TabPost.time_create.desc()).paginate(current_page_num, CMS_CFG['list_num'])
def query_recent_edited(timstamp): ''' 获取最近有评论的Post,以时间戳为条件 ''' return TabPost.select().join( TabReply, on=(TabPost.uid == TabReply.post_id)).where( (TabReply.timestamp > timstamp)).distinct(TabPost.uid)
def query_dated(num=8, kind='1'): ''' Query posts, outdate. ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.time_update.asc()).limit(num)
def query_keywords_empty(kind='1'): ''' :param kind: :return: ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.keywords == ''))
def get_by_keyword(par2, kind='2'): ''' 根据关键字对标题进行检索 ''' return TabPost.select().where((TabPost.kind == kind) & ( TabPost.valid == 1) & (TabPost.title.contains(par2))).order_by( TabPost.time_update.desc()).limit(20)
def query_recent_edited(timstamp, kind='1'): ''' 获取最近更新的Post,以时间戳为条件 ''' return TabPost.select().where((TabPost.kind == kind) & ( TabPost.time_update > timstamp)).order_by( TabPost.time_update.desc())
def query_pager_by_valid(current_page_num=1): recs = TabPost.select().where(TabPost.valid == 0).order_by( TabPost.time_update.desc()).paginate(current_page_num, CMS_CFG['list_num']) return recs
def get_all(kind='2'): ''' Get All the records. ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.time_update.desc())
def query_pager_by_slug(slug, kind='1', current_page_num=1): return TabPost.select().join( TabPost2Tag, on=(TabPost.uid == TabPost2Tag.post_id )).where((TabPost2Tag.tag_id == slug) & (TabPost.kind == kind)).paginate( current_page_num, CMS_CFG['list_num'])
def query_least_by_cat(num=8, cat_str=1, kind='2'): return TabPost.select().join( TabPost2Tag, on=(TabPost.uid == TabPost2Tag.post_id )).where((TabPost.kind == kind) & (TabPost.valid == 1) & (TabPost2Tag.tag_id == cat_str)).order_by( TabPost.view_count).limit(num)
def query_most_pic(num, kind='1'): ''' Query most pics. ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.logo != "")).order_by( TabPost.view_count.desc()).limit(num)
def query_most(num=8, kind='1'): ''' Query most viewed. ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.valid == 1)).order_by( TabPost.view_count.desc()).limit(num)
def query_cat_random(catid, **kwargs): ''' Get random lists of certain category. ''' num = kwargs.get('limit', 8) if catid == '': rand_recs = TabPost.select().order_by( peewee.fn.Random()).limit(num) else: rand_recs = TabPost.select().join( TabPost2Tag, on=(TabPost.uid == TabPost2Tag.post_id )).where((TabPost.valid == 1) & (TabPost2Tag.tag_id == catid)).order_by( peewee.fn.Random()).limit(num) return rand_recs
def query_recent_edited(timstamp, kind='1'): ''' Query posts recently update. ''' return TabPost.select().where((TabPost.kind == kind) & ( TabPost.time_update > timstamp)).order_by( TabPost.time_update.desc())
def query_by_tagname(tag_name, kind='2'): return TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) & (TabPost.extinfo['def_tag_arr'].contains(tag_name)) ).order_by( TabPost.time_update.desc() )
def count_of_certain_kind(kind): ''' Get the count of certain kind. ''' recs = TabPost.select().where(TabPost.kind == kind) return recs.count()
def total_number(slug, kind='1'): ''' Return the number of certian slug. ''' return TabPost.select().join( TabPost2Tag, on=(TabPost.uid == TabPost2Tag.post_id )).where((TabPost2Tag.tag_id == slug) & (TabPost.kind == kind)).count()
def total_number(kind): ''' Return the number of certian slug. ''' return TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) ).count()
def query_extinfo_by_cat(cat_id, kind='2'): return TabPost.select().where( (TabPost.kind == kind) & (TabPost.valid == 1) & (TabPost.extinfo['def_cat_uid'] == cat_id) ).order_by( TabPost.time_update.desc() )
def query_keywords_empty(kind='1'): ''' Query keywords, empty. ''' return TabPost.select().where( (TabPost.kind == kind) & (TabPost.keywords == '') )