def create_tag(tag_name, kind='z'): ''' Create tag record by tag_name ''' cur_recs = TabTag.select().where((TabTag.name == tag_name) & (TabTag.kind == kind)) if cur_recs.count(): uid = cur_recs.get().uid # TabTag.delete().where( # (TabTag.name == tag_name) & # (TabTag.kind == kind) # ).execute() else: uid = tools.get_uu4d_v2() # Label with the ID of v2. while TabTag.select().where(TabTag.uid == uid).count() > 0: uid = tools.get_uu4d_v2() TabTag.create( uid=uid, slug=uid, name=tag_name, order=1, count=0, kind='z', tmpl=9, pid='zzzz', ) return uid
def get_id_by_name(tag_name, kind='z'): ''' Get ID by tag_name of the label. ''' recs = TabTag.select().where((TabTag.name == tag_name) & (TabTag.kind == kind)) logger.info('tag count of {0}: {1} '.format(tag_name, recs.count())) # the_id = '' if recs.count() == 1: the_id = recs.get().uid elif recs.count() > 1: rec0 = None for rec in recs: # Only keep one. if rec0: TabPost2Tag.delete().where( TabPost2Tag.tag_id == rec.uid).execute() TabTag.delete().where(TabTag.uid == rec.uid).execute() else: rec0 = rec the_id = rec0.uid else: the_id = MLabel.create_tag(tag_name) return the_id
def update(uid, post_data): ''' Update the category. ''' raw_rec = TabTag.get(TabTag.uid == uid) entry = TabTag.update( name=post_data['name'] if 'name' in post_data else raw_rec.name, slug=post_data['slug'] if 'slug' in post_data else raw_rec.slug, order=post_data['order'] if 'order' in post_data else raw_rec.order, kind=post_data['kind'] if 'kind' in post_data else raw_rec.kind, pid=post_data['pid'], ).where(TabTag.uid == uid) entry.execute()
def query_all(kind='1', by_count=False, by_order=True): ''' Qeury all the categories, order by count or defined order. ''' if by_count: recs = TabTag.select().where(TabTag.kind == kind).order_by( TabTag.count.desc()) elif by_order: recs = TabTag.select().where(TabTag.kind == kind).order_by( TabTag.order) else: recs = TabTag.select().where(TabTag.kind == kind).order_by( TabTag.uid) return recs
def update_count(cat_id): ''' Update the count of certain category. ''' entry2 = TabTag.update(count=TabPost2Tag.select().where( TabPost2Tag.tag_id == cat_id).count()).where(TabTag.uid == cat_id) entry2.execute()
def create_tag_with_uid(uid, tag_name): if TabTag.select().where(TabTag.uid == uid).count(): return False TabTag.create( uid=uid, slug=uid, name=tag_name, order=1, count=0, kind='z', tmpl=9, pid='zzzz', ) return uid
def query_field_count(limit_num, kind='1'): ''' Query the posts count of certain category. :param limit_num: :param kind: :return: ''' return TabTag.select().where(TabTag.kind == kind).order_by(TabTag.count.desc()).limit(limit_num)
def update_count(cat_id): ''' Update the count of certain category. ''' # Todo: the record not valid should not be counted. entry2 = TabTag.update(count=TabPost2Tag.select().where( TabPost2Tag.tag_id == cat_id).count()).where(TabTag.uid == cat_id) entry2.execute()
def add_or_update(uid, post_data): ''' Add or update the data by the given ID of post. ''' catinfo = MCategory.get_by_uid(uid) if catinfo: MCategory.update(uid, post_data) else: TabTag.create( uid=uid, name=post_data['name'], slug=post_data['slug'], order=post_data['order'], kind=post_data['kind'] if 'kind' in post_data else '1', pid=post_data['pid'], ) return uid
def query_all(): ''' 查询大类记录 ''' recs = TabTag.select().where(TabTag.uid.endswith('00')).order_by( TabTag.uid) return recs
def get_qian2(qian2): ''' 用于首页。根据前两位,找到所有的大类与小类。 :param qian2: 分类id的前两位 :return: 数组,包含了找到的分类 ''' return TabTag.select().where(TabTag.uid.startswith(qian2)).order_by( TabTag.order)
def get_by_slug(slug): ''' return the category record . ''' rec = TabTag.select().where(TabTag.slug == slug) if rec.count() > 0: return rec.get() return None
def get_kind(tagid): ''' 得到类别的 kind 值。 ''' query = TabTag.select().where(TabTag.uid == tagid) if query: return query.get().kind return None
def query_by_kind(kind): ''' 查询大类记录 ''' recs = TabTag.select().where( TabTag.uid.endswith('00') and TabTag.kind == kind).order_by(TabTag.uid) return recs
def get_by_slug(slug): ''' return the category record . :param slug: :return: ''' uu = TabTag.select().where(TabTag.slug == slug) if uu.count() > 0: return uu.get() else: return None
def update_count(cat_id): ''' Update the count of certain category. ''' entry2 = TabTag.update(count=TabPost2Tag.select().join( TabPost, on=(TabPost.uid == TabPost2Tag.post_id)).where( (TabPost.valid == 1) & (TabPost2Tag.tag_id == cat_id)).count()).where( TabTag.uid == cat_id) entry2.execute()
def get_id_by_name(tag_name, kind='z'): recs = TabTag.select().where((TabTag.name == tag_name) & (TabTag.kind == kind)) logger.info('tag count of {0}: {1} '.format(tag_name, recs.count())) if recs.count() == 1: return recs.get().uid elif recs.count() > 1: idx = 0 rec0 = None for rec in recs: rec0 = rec # Only keep one. if idx == 0: pass else: TabPost2Tag.delete().where( TabPost2Tag.tag_id == rec.uid).execute() TabTag.delete().where(TabTag.uid == rec.uid).execute() idx += 1 return rec0.uid else: return MLabel.create_tag(tag_name)
def create_tag(tag_name, kind='z'): cur_count = TabTag.select().where((TabTag.name == tag_name) & (TabTag.kind == kind)).count() if cur_count > 0: TabTag.delete().where((TabTag.name == tag_name) & (TabTag.kind == kind)).execute() uid = tools.get_uu4d_v2() while TabTag.select().where(TabTag.uid == uid).count() > 0: uid = tools.get_uu4d_v2() TabTag.create( uid=uid, slug=uid, name=tag_name, order=1, count=0, kind='z', tmpl=9, pid='zzzz', ) return uid
def query_pager_by_classify_all(): recs = TabTag.select().where(TabTag.uid.endswith("00")).order_by(TabTag.uid) return recs
def count_of_certain(): recs = TabTag.select().where(TabTag.uid.endswith("00")) return recs.count()
def query_pcat(**kwargs): _ = kwargs return TabTag.select().where(TabTag.pid == '0000').order_by( TabTag.order)
def query_kind_cat(kind_sig): return TabTag.select().where((TabTag.kind == kind_sig) & (TabTag.pid == '0000')).order_by( TabTag.order)
def query_sub_cat(pid): return TabTag.select().where(TabTag.pid == pid).order_by(TabTag.order)
def get_parent_list(kind='1'): return TabTag.select().where((TabTag.kind == kind) & (TabTag.uid.endswith('00'))).order_by( TabTag.uid)
def query_pager_by_classify(current_page_num=1): recs = TabTag.select().where(TabTag.uid.endswith("00")).order_by(TabTag.uid).paginate(current_page_num, CMS_CFG['list_num']) return recs
def get_by_slug(tag_slug): ''' Get label by slug. ''' label_recs = TabTag.select().where(TabTag.slug == tag_slug) return label_recs.get() if label_recs else False
def get_by_slug(tag_slug): uu = TabTag.select().where(TabTag.slug == tag_slug) if uu: return uu.get() else: return False
def get_taglist(kind): taglist = TabTag.select().where(TabTag.kind == kind) return taglist