def add_record(post_id, catalog_id, order=0): ''' Create the record of post 2 tag, and update the count in g_tag. :param post_id: :param catalog_id: :param order: :return: ''' rec = MPost2Catalog.__get_by_info(post_id, catalog_id) if rec: entry = TabPost2Tag.update( order=order, # For migration. the value should be added when created. par_id=rec.tag_id[:2] + '00', ).where(TabPost2Tag.uid == rec.uid) entry.execute() else: TabPost2Tag.create( uid=tools.get_uuid(), par_id=catalog_id[:2] + '00', post_id=post_id, tag_id=catalog_id, order=order, ) MCategory.update_count(catalog_id)
def count_of_certain_category(cat_id, tag=''): ''' Get the count of certain category. ''' 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 = TabPost2Tag.select().join( TabPost, on=((TabPost2Tag.post_id == TabPost.uid) & (TabPost.valid == 1)) ).where( cat_con & TabPost.extinfo.contains(condition) ) else: recs = TabPost2Tag.select().where( cat_con ) return recs.count()
def get_by_info(post_id, catalog_id): tmp_recs = TabPost2Tag.select().join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid )).where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == catalog_id) & (TabTag.kind == 'z')) if tmp_recs.count() > 1: ''' Remove the rests if the count greater than 1. ''' out_rec = None for tmp_rec in tmp_recs: if out_rec: entry = TabPost2Tag.delete().where( TabPost2Tag.uid == tmp_rec.uid) entry.execute() else: out_rec = tmp_rec elif tmp_recs.count() == 1: out_rec = tmp_recs.get() else: out_rec = None return out_rec
def get_by_info(post_id, catalog_id): tmp_recs = TabPost2Tag.select().join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid )).where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == catalog_id) & (TabTag.kind == 'z')) if tmp_recs.count() > 1: ''' 如果多于1个,则全部删除 ''' idx = 0 out_rec = None for tmp_rec in tmp_recs: if idx == 0: out_rec = tmp_rec else: entry = TabPost2Tag.delete().where( TabPost2Tag.uid == tmp_rec.uid) entry.execute() idx += 1 return out_rec elif tmp_recs.count() == 1: return tmp_recs.get() else: return None
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 query_by_entity_uid(idd, kind=''): if kind == '': return TabPost2Tag.select( TabPost2Tag, TabTag.slug.alias('tag_slug'), TabTag.name.alias('tag_name') ).join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid) ).where( (TabPost2Tag.post_id == idd) & (TabTag.kind != 'z') ).order_by( TabPost2Tag.order ) else: return TabPost2Tag.select( TabPost2Tag, TabTag.slug.alias('tag_slug'), TabTag.name.alias('tag_name') ).join(TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)).where( (TabTag.kind == kind) & (TabPost2Tag.post_id == idd) ).order_by( TabPost2Tag.order )
def get_app_relations(app_id, num=20, kind='1'): ''' The the related infors. ''' info_tag = MInfor2Catalog.get_first_category(app_id) if info_tag: return TabPost2Tag.select( TabPost2Tag, TabPost.title.alias('post_title'), TabPost.valid.alias('post_valid') ).join( TabPost, on=(TabPost2Tag.post_id == TabPost.uid) ).where( (TabPost2Tag.tag_id == info_tag.tag_id) & (TabPost.kind == kind) ).order_by( peewee.fn.Random() ).limit(num) return TabPost2Tag.select( TabPost2Tag, TabPost.title.alias('post_title'), TabPost.valid.alias('post_valid') ).join( TabPost, on=(TabPost2Tag.post_id == TabPost.uid) ).where( TabPost.kind == kind ).order_by(peewee.fn.Random()).limit(num)
def update_field(uid, post_id=None, tag_id=None): if post_id: entry = TabPost2Tag.update( post_id=post_id ).where(TabPost2Tag.uid == uid) entry.execute() if tag_id: entry2 = TabPost2Tag.update( tag_id=tag_id ).where(TabPost2Tag.uid == uid) entry2.execute()
def get_app_relations(app_id, num=20, kind='1'): ''' The the related infors. 如有标签按标签推荐,如无标签按分类推荐 ''' tag_info = filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(app_id).objects()) info_tag = MInfor2Catalog.get_first_category(app_id) tag_arr = [] for tag in tag_info: tag_arr.append(tag.tag_uid) if len(tag_arr) > 0: recs = TabPost2Tag.select( TabPost2Tag, TabPost.title.alias('post_title'), TabPost.valid.alias('post_valid')).join( TabPost, on=(TabPost2Tag.post_id == TabPost.uid )).where((TabPost2Tag.tag_id << tag_arr) & (TabPost.uid != app_id) & (TabPost.kind == kind) & (TabPost.valid == 1)).distinct( TabPost2Tag.post_id).order_by( TabPost2Tag.post_id).limit(num) if recs.count() == 0: recs = TabPost2Tag.select( TabPost2Tag, TabPost.title.alias('post_title'), TabPost.valid.alias('post_valid')).join( TabPost, on=(TabPost2Tag.post_id == TabPost.uid )).where((TabPost.uid != app_id) & (TabPost2Tag.tag_id == info_tag.tag_id) & (TabPost.kind == kind) & (TabPost.valid == 1)).order_by( peewee.fn.Random()).limit(num) else: recs = TabPost2Tag.select( TabPost2Tag, TabPost.title.alias('post_title'), TabPost.valid.alias('post_valid')).join( TabPost, on=(TabPost2Tag.post_id == TabPost.uid )).where((TabPost.uid != app_id) & (TabPost2Tag.tag_id == info_tag.tag_id) & (TabPost.kind == kind) & (TabPost.valid == 1)).order_by( peewee.fn.Random()).limit(num) return recs
def add_record(post_id, tag_name, order=1, kind='z'): logger.info('Add label kind: {0}'.format(kind)) tag_id = MLabel.get_id_by_name(tag_name, 'z') labelinfo = MPost2Label.get_by_info(post_id, tag_id) if labelinfo: entry = TabPost2Tag.update( order=order, ).where(TabPost2Tag.uid == labelinfo.uid) entry.execute() else: entry = TabPost2Tag.create(uid=tools.get_uuid(), post_id=post_id, tag_id=tag_id, order=order, kind='z') return entry.uid
def remove_relation(post_id, tag_id): ''' Remove the relation of post and label. ''' entry = TabPost2Tag.delete().where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == tag_id)) entry.execute()
def query_by_post(postid): ''' Query records by post. ''' return TabPost2Tag.select().where( TabPost2Tag.post_id == postid ).order_by(TabPost2Tag.order)
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 delete(uid): ''' Delete by uid :param uid: :return: ''' q_u1 = TabPostHist.delete().where(TabPostHist.post_id == uid) q_u1.execute() q_u2 = TabRel.delete().where(TabRel.post_f_id == uid or TabRel.post_t_id == uid) q_u2.execute() q_u3 = TabCollect.delete().where(TabCollect.post_id == uid) q_u3.execute() q_u4 = TabPost2Tag.delete().where(TabPost2Tag.post_id == uid) q_u4.execute() q_u5 = TabUsage.delete().where(TabUsage.post_id == uid) q_u5.execute() reply_arr = [] for reply in TabUser2Reply.select().where( TabUser2Reply.reply_id == uid): reply_arr.append(reply.reply_id.uid) q_u6 = TabUser2Reply.delete().where(TabUser2Reply.reply_id == uid) q_u6.execute() for replyid in reply_arr: TabReply.delete().where(TabReply.uid == replyid).execute() q_u7 = TabEvaluation.delete().where(TabEvaluation.post_id == uid) q_u7.execute() q_u8 = TabRating.delete().where(TabRating.post_id == uid) q_u8.execute() return MHelper.delete(TabPost, uid)
def query_by_catid(catid): ''' Query the records by ID of catalog. ''' return TabPost2Tag.select().where( TabPost2Tag.tag_id == catid )
def get_by_uid(idd): return TabPost2Tag.select(TabPost2Tag, TabTag.name.alias('tag_name'), TabTag.uid.alias('tag_uid')).join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid )).where((TabPost2Tag.post_id == idd) & (TabTag.kind == 'z'))
def remove_relation(post_id, tag_id): ''' Delete the record of post 2 tag. ''' entry = TabPost2Tag.delete().where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == tag_id)) entry.execute() MCategory.update_count(tag_id)
def remove_tag(tag_id): ''' Delete the records of certain tag. ''' entry = TabPost2Tag.delete().where( TabPost2Tag.tag_id == tag_id ) entry.execute()
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 query_count(): recs = TabPost2Tag.select( TabPost2Tag.tag_id, peewee.fn.COUNT(TabPost2Tag.tag_id).alias('num') ).group_by( TabPost2Tag.tag_id ) return recs
def query_all(): ''' Query all the records from TabPost2Tag. ''' recs = TabPost2Tag.select( TabPost2Tag, TabTag.kind.alias('tag_kind'), ).join(TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)) return recs
def del_by_uid(uid): ''' Just Query all the records from TabPost2Tag. ''' entry = TabPost2Tag.delete().where(TabPost2Tag.uid == uid) try: entry.execute() return True except: return False
def update_field(uid, post_id=None, tag_id=None, par_id=None): ''' Update the field of post2tag. ''' if post_id: entry = TabPost2Tag.update(post_id=post_id).where( TabPost2Tag.uid == uid) entry.execute() if tag_id: entry2 = TabPost2Tag.update( par_id=tag_id[:2] + '00', tag_id=tag_id, ).where(TabPost2Tag.uid == uid) entry2.execute() if par_id: entry2 = TabPost2Tag.update(par_id=par_id).where( TabPost2Tag.uid == uid) entry2.execute()
def get_by_uid(post_id): ''' Get records by post id. ''' return TabPost2Tag.select( TabPost2Tag, TabTag.name.alias('tag_name'), TabTag.uid.alias('tag_uid')).join( TabTag, on=(TabPost2Tag.tag_id == TabTag.uid )).where((TabPost2Tag.post_id == post_id) & (TabTag.kind == 'z'))
def del_by_uid(uid): ''' Just Query all the records from TabPost2Tag. ''' entry = TabPost2Tag.delete().where(TabPost2Tag.uid == uid) try: entry.execute() return True except Exception as err: print(repr(err)) return False
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_by_info(post_id, catalog_id): recs = TabPost2Tag.select().where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == catalog_id)) if recs.count() == 1: return recs.get() elif recs.count() > 1: # return the first one, and delete others. idx = 0 out_rec = None for rec in recs: if idx == 0: out_rec = rec else: entry = TabPost2Tag.delete().where( TabPost2Tag.uid == rec.uid) entry.execute() idx += idx return out_rec.get() else: return None
def __get_by_info(post_id, catalog_id): ''' Geo the record by post and catalog. ''' recs = TabPost2Tag.select().where((TabPost2Tag.post_id == post_id) & (TabPost2Tag.tag_id == catalog_id)) if recs.count() == 1: return recs.get() elif recs.count() > 1: # return the first one, and delete others. out_rec = None for rec in recs: if out_rec: entry = TabPost2Tag.delete().where( TabPost2Tag.uid == rec.uid) entry.execute() else: out_rec = rec return out_rec return None
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 add_record(post_id, catalog_id, order=0): ''' Create the record of post 2 tag, and update the count in g_tag. :param post_id: :param catalog_id: :param order: :return: ''' rec = MPost2Catalog.__get_by_info(post_id, catalog_id) if rec: entry = TabPost2Tag.update( order=order, ).where(TabPost2Tag.uid == rec.uid) entry.execute() else: TabPost2Tag.create( uid=tools.get_uuid(), post_id=post_id, tag_id=catalog_id, order=order, ) MCategory.update_count(catalog_id)