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 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 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_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 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 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 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 query_by_post(postid): ''' Query records by post. ''' return TabPost2Tag.select().where( TabPost2Tag.post_id == postid ).order_by(TabPost2Tag.order)
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 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 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 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 query_by_catid(catid): return TabPost2Tag.select().where( TabPost2Tag.tag_id == catid )
def count_of_certain_category(cat_id): return TabPost2Tag.select().where( TabPost2Tag.tag_id == cat_id ).count()
def query_count(uid): return TabPost2Tag.select().where(TabPost2Tag.tag_id == uid).count()
def just_query_all(): ''' Just Query all the records from TabPost2Tag. ''' recs = TabPost2Tag.select() return recs