def test_get_by_uid(self): self.tearDown() self.add_mess() a = MPost2Label.get_by_uid(self.post_id) print(a.count()) assert a.count() == 0 self.add_M2L() a = MPost2Label.get_by_uid(self.post_id) assert a[0].tag_id == self.uid self.tearDown()
def viewinfo(self, postinfo): ''' 查看 Post. ''' self.redirect_kind(postinfo) __ext_catid = postinfo.extinfo.get('def_cat_uid', '') cat_enum1 = MCategory.get_qian2(__ext_catid[:2]) if __ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self._chuli_cookie_relation(postinfo.uid) catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) MAcces.add(postinfo.uid) if self.get_current_user() and self.userinfo: MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', kwd['cookie_str']) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6).objects()[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render( tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, author=postinfo.user_name, # Todo: remove the key `author`. catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, subcats=MCategory.query_sub_cat(p_catinfo.uid) if p_catinfo else '', ad_switch=random.randint(1, 18), tag_info=filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).objects()), recent_apps=recent_apps, cat_enum=cat_enum1)
def update_label(signature, post_data): ''' Update the label . ''' current_tag_infos = MPost2Label.get_by_uid(signature).objects() if 'tags' in post_data: pass else: return False if ';' in post_data['tags']: tags_arr = [x.strip() for x in post_data['tags'].split(';')] elif ',' in post_data['tags']: tags_arr = [x.strip() for x in post_data['tags'].split(',')] elif ',' in post_data['tags']: tags_arr = [x.strip() for x in post_data['tags'].split(',')] elif ';' in post_data['tags']: tags_arr = [x.strip() for x in post_data['tags'].split(';')] else: tags_arr = [x.strip() for x in post_data['tags'].split(' ')] if len(tags_arr) > 5: del tags_arr[5:] for tag_name in tags_arr: if tag_name == '': pass else: MPost2Label.add_record(signature, tag_name, 1) for cur_info in current_tag_infos: if cur_info.tag_name in tags_arr: pass else: MPost2Label.remove_relation(signature, cur_info.tag_id)
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' self.redirect_kind(postinfo) ext_catid = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self._chuli_cookie_relation(postinfo.uid) catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', kwd['cookie_str']) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6).naive()[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render( tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, author=postinfo.user_name, # Todo: remove the key `author`. catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), # tag_info=MPost2Label.get_by_uid(postinfo.uid).naive(), tag_info=filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).naive()), recent_apps=recent_apps, cat_enum=cat_enum1)
def update_label(self, signature): ''' Update the label when updating. :param signature: :return: ''' current_tag_infos = MPost2Label.get_by_uid(signature, kind=self.kind) post_data = self.get_post_data() if 'tags' in post_data: pass else: return False tags_arr = [x.strip() for x in post_data['tags'].split(',')] for tag_name in tags_arr: if tag_name == '': pass else: MPost2Label.add_record(signature, tag_name, 1) for cur_info in current_tag_infos: if cur_info.tag.name in tags_arr: pass else: MPost2Label.remove_relation(signature, cur_info.tag)
def _to_edit(self, infoid): ''' render the HTML page for post editing. ''' postinfo = MPost.get_by_uid(infoid) if postinfo: pass else: return self.show404() if 'def_cat_uid' in postinfo.extinfo: catid = postinfo.extinfo['def_cat_uid'] else: catid = '' if len(catid) == 4: pass else: catid = '' catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catid = post2catinfo.tag_id catinfo = MCategory.get_by_uid(catid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = { 'def_cat_uid': catid, 'parentname': '', 'catname': '', 'parentlist': MCategory.get_parent_list(), 'userip': self.request.remote_ip, 'extinfo': json.dumps(postinfo.extinfo, indent=2, ensure_ascii=False), } if self.filter_view: tmpl = 'autogen/edit/edit_{0}.html'.format(catid) else: tmpl = 'post_{0}/post_edit.html'.format(self.kind) logger.info('Meta template: {0}'.format(tmpl)) self.render(tmpl, kwd=kwd, postinfo=postinfo, catinfo=catinfo, pcatinfo=p_catinfo, userinfo=self.userinfo, cat_enum=MCategory.get_qian2(catid[:2]), tag_infos=MCategory.query_all(by_order=True, kind=self.kind), tag_infos2=MCategory.query_all(by_order=True, kind=self.kind), app2tag_info=MPost2Catalog.query_by_entity_uid(infoid, kind=self.kind).objects(), app2label_info=MPost2Label.get_by_uid(infoid).objects())
def test_add_record(self): a = MPost2Label.get_by_uid(self.post_id) tf = True for i in a: if i.tag_id == self.tag_id: tf = False assert tf self.add_mess() MPost2Label.add_record(self.post_id, self.name) a = MPost2Label.get_by_uid(self.post_id) tf = False for i in a: if i.tag_id == self.tag_id: tf = True assert tf self.tearDown()
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' self.redirect_kind(postinfo) ###################################################### if DB_CFG['kind'] == 's': cat_enum1 = [] else: ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] ###################################################### catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) catalog_infors = None if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) catalog_infors = MPost2Catalog.query_pager_by_slug(catinfo.slug, current_page_num=1, order=True) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) tmpl = 'post_{0}/leaf_view.html'.format(self.kind) logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), catalog_infos=catalog_infors, cat_enum=cat_enum1)
def test_remove_relation(self): self.add_mess() self.add_M2L() a = MPost2Label.get_by_uid(self.post_id) tf = False for i in a: if i.tag_id == self.tag_id: tf = True assert tf MPost2Label.remove_relation(self.post_id, self.tag_id) a = MPost2Label.get_by_uid(self.post_id) tf = True for i in a: if i.tag_id == self.tag_id: tf = False assert tf self.tearDown()
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 render(self, postinfo=None): if postinfo: tag_info = MPost2Label.get_by_uid(postinfo.uid) idx = 1 outstr = '<span class="post_label">' for x in tag_info: outstr += '''<a href = "/label/{kind}/{tag_uid}" class ="app_label tag{index}" > {tag_name} </a> '''.format(tag_uid=x.tag.uid, kind=postinfo.kind, tag_name=x.tag.name, index=idx) idx += 1 return outstr + '</span>' else: return ''
def render(self, *args, **kwargs): postinfo = kwargs['postinfo'] if 'postinfo' in kwargs else None if postinfo: tag_info = MPost2Label.get_by_uid(postinfo.uid).naive() idx = 1 outstr = '<span class="post_label">' for tag in tag_info: outstr += '''<a href="/label/{kind}/{tag_uid}" class ="app_label tag{index}"> {tag_name} </a> '''.format(tag_uid=tag.tag_id, kind=postinfo.kind, tag_name=tag.tag_name, index=idx) idx += 1 return outstr + '</span>' else: return ''
def render(self, *args, **kwargs): postinfo = kwargs.get('postinfo', None) if postinfo: tag_info = filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).objects()) idx = 1 outstr = '<span class="post_label">' for tag in tag_info: outstr += '''<a href="/label/{kind}/{tag_uid}" class ="app_label tag{index}"> {tag_name} </a> '''.format(tag_uid=tag.tag_id, kind=postinfo.kind, tag_name=tag.tag_name, index=idx) idx += 1 return outstr + '</span>' return ''
def update_label(signature, post_data): ''' Update the label when updating. ''' current_tag_infos = MPost2Label.get_by_uid(signature).objects() if 'tags' in post_data: pass else: return False tags_arr = [x.strip() for x in post_data['tags'].split(',')] for tag_name in tags_arr: if tag_name == '': pass else: MPost2Label.add_record(signature, tag_name, 1) for cur_info in current_tag_infos: if cur_info.tag_name in tags_arr: pass else: MPost2Label.remove_relation(signature, cur_info.tag_id)
def test_get_by_uid(self): MPost2Label.get_by_uid(self.post_id) assert True
def __to_edit(self, infoid): ''' render the HTML page for post editing. :param infoid: :return: ''' if self.check_post_role()['EDIT']: pass else: return False rec_info = MPost.get_by_uid(infoid) postinfo = rec_info if rec_info: pass else: self.render('html/404.html') return if 'def_cat_uid' in rec_info.extinfo: catid = rec_info.extinfo['def_cat_uid'] else: catid = '' if len(catid) == 4: pass else: catid = '' catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catid = post2catinfo.tag.uid catinfo = MCategory.get_by_uid(catid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = { 'def_cat_uid': catid, 'parentname': '', 'catname': '', 'parentlist': MCategory.get_parent_list(), 'userip': self.request.remote_ip} if self.filter_view: tmpl = 'autogen/edit/edit_{0}.html'.format(catid) else: tmpl = 'post_{0}/post_edit.html'.format(self.kind) logger.info('Meta template: {0}'.format(tmpl)) self.render(tmpl, kwd=kwd, calc_info=rec_info, # Deprecated post_info=rec_info, # Deprecated app_info=rec_info, # Deprecated postinfo=rec_info, catinfo=catinfo, pcatinfo=p_catinfo, userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, cat_enum=MCategory.get_qian2(catid[:2]), tag_infos=MCategory.query_all(by_order=True, kind=self.kind), tag_infos2=MCategory.query_all(by_order=True, kind=self.kind), app2tag_info=MPost2Catalog.query_by_entity_uid(infoid, kind=self.kind), app2label_info=MPost2Label.get_by_uid(infoid, kind=self.kind + '1'))
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' logger.warning('info kind:{0} '.format(postinfo.kind)) # If not, there must be something wrong. if postinfo.kind == self.kind: pass else: self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid), permanent=True) ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self.__chuli_cookie_relation(postinfo.uid) cookie_str = tools.get_uuid() catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag.uid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = { 'pager': '', 'url': self.request.uri, 'cookie_str': cookie_str, 'daohangstr': '', 'signature': postinfo.uid, 'tdesc': '', 'eval_0': MEvaluation.app_evaluation_count(postinfo.uid, 0), 'eval_1': MEvaluation.app_evaluation_count(postinfo.uid, 1), 'login': 1 if self.get_current_user() else 0, 'has_image': 0, 'parentlist': MCategory.get_parent_list(), 'parentname': '', 'catname': '', 'router': router_post[postinfo.kind] } MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', cookie_str) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6)[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, calc_info=postinfo, # Deprecated post_info=postinfo, # Deprecated userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), recent_apps=recent_apps, cat_enum=cat_enum1)
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' logger.warning('info kind:{0} '.format(postinfo.kind)) # If not, there must be something wrong. if postinfo.kind == self.kind: pass else: self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid), permanent=True) ###################################################### if DB_CFG['kind'] == 's': cat_enum1 = [] else: ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] ###################################################### catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) catalog_infors = None if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag.uid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) catalog_infors = MPost2Catalog.query_pager_by_slug(catinfo.slug, current_page_num=1, order=True) kwd = { 'pager': '', 'url': self.request.uri, 'daohangstr': '', 'signature': postinfo.uid, 'tdesc': '', 'eval_0': MEvaluation.app_evaluation_count(postinfo.uid, 0), 'eval_1': MEvaluation.app_evaluation_count(postinfo.uid, 1), 'login': 1 if self.get_current_user() else 0, 'has_image': 0, 'parentlist': MCategory.get_parent_list(), 'parentname': '', 'catname': '', 'router': router_post[postinfo.kind] } MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) tmpl = 'post_{0}/leaf_view.html'.format(self.kind) logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), catalog_infos=catalog_infors, cat_enum=cat_enum1)