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. :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 update(uid, post_data, update_time=False): ''' update the infor. ''' title = post_data['title'].strip() if len(title) < 2: return False cnt_html = tools.markdown2html(post_data['cnt_md']) try: if update_time: entry2 = TabPost.update( date=datetime.now(), time_create=tools.timestamp()).where(TabPost.uid == uid) entry2.execute() except: pass cur_rec = MPost.get_by_uid(uid) entry = TabPost.update( title=title, user_name=post_data['user_name'], cnt_md=tornado.escape.xhtml_escape(post_data['cnt_md'].strip()), memo=post_data['memo'] if 'memo' in post_data else '', cnt_html=cnt_html, logo=post_data['logo'], order=post_data['order'] if 'order' in post_data else '', keywords=post_data['keywords'] if 'keywords' in post_data else '', kind=post_data['kind'] if 'kind' in post_data else 1, extinfo=post_data['extinfo'] if 'extinfo' in post_data else cur_rec.extinfo, time_update=tools.timestamp(), valid=1).where(TabPost.uid == uid) entry.execute()
def add_meta(uid, data_dic, extinfo=None): if extinfo is None: extinfo = {} if len(uid) < 4: return False title = data_dic['title'].strip() if len(title) < 2: return False TabPost.create( uid=uid, title=title, keywords='', time_create=tools.timestamp(), time_update=tools.timestamp(), create_time=tools.timestamp(), date=datetime.now(), cnt_md=data_dic['cnt_md'].strip(), memo=data_dic['memo'] if 'memo' in data_dic else '', logo=data_dic['logo'].strip(), order=data_dic['order'] if 'order' in data_dic else '', cnt_html=tools.markdown2html(data_dic['cnt_md']), view_count=0, extinfo=extinfo, user_name=data_dic['user_name'], valid=data_dic['valid'] if 'valid' in data_dic else 1, kind=data_dic['kind'], ) return uid
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 addata_init(data_dic, ext_dic=None): if ext_dic is None: ext_dic = {} if len(data_dic['sig']) < 4: return False title = data_dic['title'].strip() if len(title) < 2: return False postinfo = MPost.get_by_uid(data_dic['sig']) if postinfo: if data_dic['title'] == postinfo.title and data_dic[ 'kind'] == postinfo.kind: pass else: MPost.modify_init(data_dic['sig'], data_dic) else: time_stamp = int(time.time()) TabPost.create( uid=data_dic['sig'], title=data_dic['title'], create_time=time_stamp, time_update=time_stamp, cnt_md=data_dic['cnt_md'] if 'memo' in data_dic else '', memo=data_dic['memo'] if 'memo' in data_dic else '', cnt_html=data_dic['cnt_html'], date=datetime.now(), keywords='', extinfo=ext_dic)
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_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 modify_meta(uid, data_dic, extinfo=None): ''' 手工修改的。 :param uid: :param data_dic: :return: ''' if extinfo is None: extinfo = {} title = data_dic['title'].strip() if len(title) < 2: return False cur_info = MPost.get_by_uid(uid) if cur_info: if DB_CFG['kind'] == 's': entry = TabPost.update( title=title, user_name=data_dic['user_name'], keywords='', time_create=tools.timestamp(), time_update=tools.timestamp(), date=datetime.now(), cnt_md=data_dic['cnt_md'], memo=data_dic['memo'] if 'memo' in data_dic else '', logo=data_dic['logo'], order=data_dic['order'], cnt_html=tools.markdown2html(data_dic['cnt_md']), valid=data_dic['valid'] ).where(TabPost.uid == uid) entry.execute() else: cur_extinfo = cur_info.extinfo # Update the extinfo, Not replace for key in extinfo: cur_extinfo[key] = extinfo[key] entry = TabPost.update( title=title, user_name=data_dic['user_name'], keywords='', time_create=tools.timestamp(), time_update=tools.timestamp(), date=datetime.now(), cnt_md=data_dic['cnt_md'], memo=data_dic['memo'] if 'memo' in data_dic else '', logo=data_dic['logo'], order=data_dic['order'] if 'order' in data_dic else '', cnt_html=tools.markdown2html(data_dic['cnt_md']), extinfo=cur_extinfo, valid=data_dic['valid'] ).where(TabPost.uid == uid) entry.execute() else: return MPost.add_meta(uid, data_dic, extinfo) return uid
def update(uid, post_data, update_time=True): ''' 参数 `update_time` , 是否更新时间。对于导入的数据,有时不需要更新。 ''' title = post_data['title'].strip() if len(title) < 2: return False cnt_html = tools.markdown2html(post_data['cnt_md']) # try: # if update_time: # entry2 = TabPost.update( # date=datetime.now(), # time_create=tools.timestamp() # ).where(TabPost.uid == uid) # entry2.execute() # except: # pass cur_rec = MPost.get_by_uid(uid) if update_time: entry = TabPost.update( title=title, user_name=post_data['user_name'], cnt_md=tornado.escape.xhtml_escape( post_data['cnt_md'].strip()), memo=post_data['memo'] if 'memo' in post_data else '', cnt_html=cnt_html, logo=post_data['logo'], order=post_data['order'] if 'order' in post_data else '', keywords=post_data['keywords'] if 'keywords' in post_data else '', kind=post_data['kind'] if 'kind' in post_data else 1, extinfo=post_data['extinfo'] if 'extinfo' in post_data else cur_rec.extinfo, time_update=tools.timestamp(), valid=post_data.get('valid', 1)).where(TabPost.uid == uid) else: entry = TabPost.update( title=title, user_name=post_data['user_name'], cnt_md=tornado.escape.xhtml_escape( post_data['cnt_md'].strip()), memo=post_data['memo'] if 'memo' in post_data else '', cnt_html=cnt_html, logo=post_data['logo'], order=post_data['order'] if 'order' in post_data else '', keywords=post_data['keywords'] if 'keywords' in post_data else '', kind=post_data['kind'] if 'kind' in post_data else 1, extinfo=post_data['extinfo'] if 'extinfo' in post_data else cur_rec.extinfo, valid=post_data.get('valid', 1)).where(TabPost.uid == uid) entry.execute()
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_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_keywords_empty(kind='1'): ''' :param kind: :return: ''' return TabPost.select().where((TabPost.kind == kind) & (TabPost.keywords == ''))
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 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_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_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_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_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 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 create_post(post_uid, post_data): ''' create the post. ''' title = post_data['title'].strip() if len(title) < 2: return False cur_rec = MPost.get_by_uid(post_uid) if cur_rec: return False entry = TabPost.create( title=title, date=datetime.now(), cnt_md=tornado.escape.xhtml_escape(post_data['cnt_md'].strip()), cnt_html=tools.markdown2html(post_data['cnt_md']), uid=post_uid, time_create=(post_data['time_create'] if 'time_create' in post_data else tools.timestamp()), time_update=(post_data['time_update'] if 'time_update' in post_data else tools.timestamp()), user_name=post_data['user_name'], view_count=post_data['view_count'] if 'view_count' in post_data else 1, logo=post_data['logo'], memo=post_data['memo'] if 'memo' in post_data else '', order=post_data['order'] if 'order' in post_data else '', keywords=post_data['keywords'] if 'keywords' in post_data else '', extinfo=post_data['extinfo'] if 'extinfo' in post_data else {}, kind=post_data['kind'] if 'kind' in post_data else '1', valid=1) return entry.uid
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_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 modify_meta(uid, data_dic, extinfo=None): ''' update meta of the rec. ''' if extinfo is None: extinfo = {} title = data_dic['title'].strip() # if len(title) < 2: # return False cur_info = MPost.get_by_uid(uid) if cur_info: cur_extinfo = cur_info.extinfo # Update the extinfo, Not replace for key in extinfo: cur_extinfo[key] = extinfo[key] cur_extinfo['def_editor_name'] = data_dic['user_name'].strip() entry = TabPost.update( title=title, # user_name=data_dic['user_name'], keywords='', time_update=tools.timestamp(), date=datetime.now(), cnt_md=data_dic['cnt_md'], memo=data_dic['memo'] if 'memo' in data_dic else '', logo=data_dic['logo'], order=data_dic['order'] if 'order' in data_dic else '', cnt_html=tools.markdown2html(data_dic['cnt_md']), extinfo=cur_extinfo, valid=data_dic['valid']).where(TabPost.uid == uid) entry.execute() else: return MPost.add_meta(uid, data_dic, extinfo) return uid
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_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 query_recent_edited(timstamp): ''' 获取最近有评论的Post,以时间戳为条件 ''' return TabPost.select().join( TabReply, on=(TabPost.uid == TabReply.post_id)).where( (TabReply.timestamp > timstamp)).distinct(TabPost.uid)