def add_relation(self, f_uid, t_uid): ''' Add the relation. And the from and to, should have different weight. :param f_uid: :param t_uid: :return: return True if the relation has been succesfully added. ''' if not MPost.get_by_uid(t_uid): return False if f_uid == t_uid: return False # 针对分类进行处理。只有落入相同分类的,才加1 f_cats = MPost2Catalog.query_by_entity_uid(f_uid) t_cats = MPost2Catalog.query_by_entity_uid(t_uid) flag = False for f_cat in f_cats: for t_cat in t_cats: if f_cat.tag == t_cat.tag: flag = True if flag: pass else: return False # 双向关联,但权重不一样. MRelation.add_relation(f_uid, t_uid, 2) MRelation.add_relation(t_uid, f_uid, 1) return True
def update_category(self, uid, **kwargs): ''' Update the category of the post. :param uid: The ID of the post. Extra info would get by requests. :return: ''' catid = kwargs['catid'] if ('catid' in kwargs and MCategory.get_by_uid( kwargs['catid'])) else None post_data = self.get_post_data() current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').naive() new_category_arr = [] # Used to update post2category, to keep order. def_cate_arr = ['gcat{0}'.format(x) for x in range(10)] # for old page. def_cate_arr.append('def_cat_uid') # Used to update post extinfo. cat_dic = {} for key in def_cate_arr: if key not in post_data: continue if post_data[key] == '' or post_data[key] == '0': continue # 有可能选重复了。保留前面的 if post_data[key] in new_category_arr: continue new_category_arr.append(post_data[key] + ' ' * (4 - len(post_data[key]))) cat_dic[key] = post_data[key] + ' ' * (4 - len(post_data[key])) if catid: def_cat_id = catid elif new_category_arr: def_cat_id = new_category_arr[0] else: def_cat_id = None if def_cat_id: cat_dic['def_cat_uid'] = def_cat_id cat_dic['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid # Add the category logger.info('Update category: {0}'.format(new_category_arr)) logger.info('Update category: {0}'.format(cat_dic)) MPost.update_jsonb(uid, cat_dic) for index, catid in enumerate(new_category_arr): MPost2Catalog.add_record(uid, catid, index) # Delete the old category if not in post requests. for cur_info in current_infos: if cur_info.tag_id not in new_category_arr: MPost2Catalog.remove_relation(uid, cur_info.tag_id)
def test_query_by_entity_uid(self): self.add_message() self.add_P2C() a = MPost2Catalog.query_by_entity_uid(self.post_id) assert a[0].tag_id == self.tag_id self.tearDown()
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 render(self, uid): out_str = '' ii = 1 for tag_info in MPost2Catalog.query_by_entity_uid(uid, kind='m'): tmp_str = '<a href="/tag/{0}" class="tag{1}">{2}</a>'.format( tag_info.tag.slug, ii, tag_info.tag.name) out_str += tmp_str ii += 1 return out_str
def update_category(uid, postdata, kwargs): ''' Update the category of the post. ''' catid = kwargs['catid'] if ( 'catid' in kwargs and MCategory.get_by_uid(kwargs['catid'])) else None post_data = postdata current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').objects() new_category_arr = [] # Used to update post2category, to keep order. def_cate_arr = ['gcat{0}'.format(x) for x in range(10)] # for old page. def_cate_arr.append('def_cat_uid') # Used to update post extinfo. cat_dic = {} for key in def_cate_arr: if key not in post_data: continue if post_data[key] == '' or post_data[key] == '0': continue # 有可能选重复了。保留前面的 if post_data[key] in new_category_arr: continue new_category_arr.append(post_data[key] + ' ' * (4 - len(post_data[key]))) cat_dic[key] = post_data[key] + ' ' * (4 - len(post_data[key])) if catid: def_cat_id = catid elif new_category_arr: def_cat_id = new_category_arr[0] else: def_cat_id = None if def_cat_id: cat_dic['def_cat_uid'] = def_cat_id cat_dic['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid print('=' * 40) print(uid) print(cat_dic) MPost.update_jsonb(uid, cat_dic) for index, catid in enumerate(new_category_arr): MPost2Catalog.add_record(uid, catid, index) # Delete the old category if not in post requests. for cur_info in current_infos: if cur_info.tag_id not in new_category_arr: MPost2Catalog.remove_relation(uid, cur_info.tag_id)
def render(self, *args, **kwargs): uid = args[0] out_str = '' iii = 1 for tag_info in MPost2Catalog.query_by_entity_uid(uid).naive(): tmp_str = '''<a data-inline="true" href="/tag/{0}" class="tag{1}">{2}</a>'''.format(tag_info.tag_slug, iii, tag_info.tag_name) out_str += tmp_str iii += 1 return out_str
def render(self, *args, **kwargs): uid = args[0] out_str = '' idx = 1 for tag_info in MPost2Catalog.query_by_entity_uid(uid, kind='m').objects(): tmp_str = '<a href="/list/{0}" class="tag{1}">{2}</a>'.format( tag_info.tag_slug, idx, tag_info.tag_name) out_str += tmp_str idx += 1 return out_str
def render(self, *args, **kwargs): uid = args[0] kind = args[1] out_str = '' ii = 1 for tag_info in MPost2Catalog.query_by_entity_uid(uid, kind=kind).naive(): tmp_str = '<a href="/category/{0}" class="tag{1}">{2}</a>'.format( tag_info.tag_slug, ii, tag_info.tag_name) out_str += tmp_str ii += 1 return out_str
def fetch_additional_posts(self, uid): ''' fetch the rel_recs, and random recs when view the post. ''' cats = MPost2Catalog.query_by_entity_uid(uid, kind=self.kind) cat_uid_arr = [] for cat_rec in cats: cat_uid = cat_rec.tag_id cat_uid_arr.append(cat_uid) logger.info('info category: {0}'.format(cat_uid_arr)) rel_recs = MRelation.get_app_relations(uid, 8, kind=self.kind).objects() logger.info('rel_recs count: {0}'.format(rel_recs.count())) if cat_uid_arr: rand_recs = MPost.query_cat_random(cat_uid_arr[0], limit=4 - rel_recs.count() + 4) else: rand_recs = MPost.query_random(num=4 - rel_recs.count() + 4, kind=self.kind) return rand_recs, rel_recs
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 update_category(uid, post_data): ''' Update the category of the post. :param uid: The ID of the post. Extra info would get by requests. ''' # deprecated # catid = kwargs['catid'] if MCategory.get_by_uid(kwargs.get('catid')) else None # post_data = self.get_post_data() if 'gcat0' in post_data: pass else: return False # Used to update MPost2Category, to keep order. the_cats_arr = [] # Used to update post extinfo. the_cats_dict = {} # for old page. deprecated # def_cate_arr.append('def_cat_uid') def_cate_arr = ['gcat{0}'.format(x) for x in range(10)] for key in def_cate_arr: if key not in post_data: continue if post_data[key] == '' or post_data[key] == '0': continue # 有可能选重复了。保留前面的 if post_data[key] in the_cats_arr: continue the_cats_arr.append(post_data[key] + ' ' * (4 - len(post_data[key]))) the_cats_dict[key] = post_data[key] + ' ' * (4 - len(post_data[key])) # if catid: # def_cat_id = catid if the_cats_arr: def_cat_id = the_cats_arr[0] else: def_cat_id = None if def_cat_id: the_cats_dict['gcat0'] = def_cat_id the_cats_dict['def_cat_uid'] = def_cat_id the_cats_dict['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid # Add the category logger.info('Update category: {0}'.format(the_cats_arr)) logger.info('Update category: {0}'.format(the_cats_dict)) MPost.update_jsonb(uid, the_cats_dict) for index, idx_catid in enumerate(the_cats_arr): MPost2Catalog.add_record(uid, idx_catid, index) # Delete the old category if not in post requests. current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').objects() for cur_info in current_infos: if cur_info.tag_id not in the_cats_arr: MPost2Catalog.remove_relation(uid, cur_info.tag_id)
def test_query_by_entity_uid(self): MPost2Catalog.query_by_entity_uid(self.post_id) assert True