def test_insert2(self): uid = self.uid post_data = { 'title': '', 'keywords': 'sd,as', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'logo': '/static/', 'user_name': 'ss', 'kind': '9', 'extinfo': '' } extinfo = {} MPost.add_meta(uid, post_data, extinfo) tt = MPost.get_by_uid(uid) assert tt.uid == uid MPost.delete(uid) post_data = { 'title': '1', 'keywords': 'sd,as', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'logo': '/static/', 'user_name': 'ss', 'kind': '9', 'extinfo': '' } uu = MPost.add_meta(self.uid, post_data) assert uu == uid MPost.delete(uid) post_data = { 'title': '天', 'keywords': 'sd,as', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'logo': '/static/', 'user_name': 'ss', 'kind': '2', 'extinfo': '' } uu = MPost.add_meta(self.uid, post_data) assert uu == uid MPost.delete(uid) post_data = { 'title': self.title, 'keywords': 'sd,as', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'logo': '/static/', 'user_name': 'ss', 'kind': '2', 'extinfo': '' } uu = MPost.add_meta(self.uid, post_data) tt = MPost.get_by_uid(uid) assert tt.uid == uu MPost.delete(uid)
def add_message(self, **kwargs): post_data = { 'name': kwargs.get('name', 'category'), 'slug': kwargs.get('slug', self.slug), 'order': kwargs.get('order', '0'), 'kind': kwargs.get('kind', '1'), 'pid': kwargs.get('pid', '0000'), } MCategory.add_or_update(self.uid, post_data) p_d = { 'title': 'iiiii', 'cnt_md': 'grgr', 'time_create': '1992', 'time_update': '1996', 'user_name': 'yuanyuan', 'view_count': '1', 'logo': 'prprprprpr', 'memo': '', 'order': '1', 'keywords': '', 'extinfo': {}, 'kind': '1', 'valid': '1', } MPost.get_by_uid(self.postid) MPost.create_post(self.postid, p_d) MPost2Catalog.add_record(self.postid, self.uid)
def add_message(self, **kwargs): post_data = { 'name': kwargs.get('name', 'category'), 'slug': kwargs.get('slug', self.slug), 'order': kwargs.get('order', '0'), 'kind': kwargs.get('kind1', '1'), 'pid': kwargs.get('pid', '0000'), } MCategory.add_or_update(self.tag_id, post_data) p_d = { 'title': kwargs.get('title', 'iiiii'), 'cnt_md': kwargs.get('cnt_md', 'grgr'), 'time_create': kwargs.get('time_create', '1992'), 'time_update': kwargs.get('time_update', '1996070600'), 'user_name': kwargs.get('user_name', 'yuanyuan'), 'view_count': kwargs.get('view_count', 1), 'logo': kwargs.get('logo', 'prprprprpr'), 'memo': kwargs.get('memo', ''), 'order': kwargs.get('order', '1'), 'keywords': kwargs.get('keywords', ''), 'extinfo': kwargs.get('extinfo', {}), 'kind': kwargs.get('kind2', '1'), 'valid': kwargs.get('valid', 1), } post_id = kwargs.get('post_id', self.post_id) MPost.get_by_uid(post_id) MPost.create_post(post_id, p_d) MPost2Catalog.add_record(self.post_id, self.tag_id)
def tearDown(self): print("function teardown") tt = MPost.get_by_uid(self.uid) if tt: MPost.delete(self.uid) tt = MPost.get_by_uid(self.post_id) if tt: MCategory.delete(self.tag_id) MPost.delete(self.post_id) MPost2Catalog.remove_relation(self.post_id, self.tag_id) tt = MLabel.get_by_slug(self.slug) if tt: MLabel.delete(tt.uid)
def tearDown(self): print("function teardown") tt = MPost.get_by_uid(self.uid) if tt: print('翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻3') MPost.delete(self.uid) tt = MPost.get_by_uid(self.post_id) if tt: MCategory.delete(self.tag_id) MPost.delete(self.post_id) MPost2Catalog.remove_relation(self.post_id, self.tag_id) print('545456365635653')
def _delete(self, *args, **kwargs): ''' delete the post. :param args: :param kwargs: :return: ''' _ = kwargs uid = args[0] current_infor = MPost.get_by_uid(uid) if self.check_post_role()['DELETE']: pass else: return False if MPost.delete(uid): tslug = MCategory.get_by_uid(current_infor.extinfo['def_cat_uid']) MCategory.update_count(current_infor.extinfo['def_cat_uid']) if router_post[self.kind] == 'info': url = "filter" id_dk8 = current_infor.extinfo['def_cat_uid'] else: url = "category" id_dk8 = tslug.slug self.redirect('/{0}/{1}'.format(url, id_dk8)) else: self.redirect('/{0}/{1}'.format(router_post[self.kind], uid))
def j_delete(self, *args): ''' Delete the post, but return the JSON. :param uid: :return: ''' uid = args[0] if self.check_post_role()['DELETE']: pass else: return False current_infor = MPost.get_by_uid(uid) tslug = MCategory.get_by_uid(current_infor.extinfo['def_cat_uid']) is_deleted = MPost.delete(uid) MCategory.update_count(current_infor.extinfo['def_cat_uid']) if is_deleted: output = { 'del_info': 1, 'cat_slug': tslug.slug, 'cat_id': tslug.uid, 'kind': current_infor.kind } else: output = { 'del_info': 0, } return json.dump(output, self)
def test_update_field(self): self.add_message() self.add_usage() aa = MUsage.query_recent(self.userid, '1') tf = False for i in aa: if i.user_id == self.userid: self.uid = i.uid assert i.post_id == self.postid tf = True assert tf p = {'post_id': self.postid2} self.add_message(**p) a = MPost.get_by_uid(self.postid2) MUsage.update_field(self.uid, post_id=self.postid2) aa = MUsage.query_recent(self.userid, '1') tf = False for i in aa: if i.user_id == self.userid: assert i.post_id == self.postid2 tf = True assert tf MUsage.update_field(self.uid, self.postid) aa = MUsage.query_recent(self.userid, '1') tf = False for i in aa: if i.user_id == self.userid: assert i.post_id == self.postid tf = True assert tf MPost.delete(self.postid2) self.tearDown()
def test_create_post_history(self): self.tearDown() p_d = { 'title': 'qqqii', 'cnt_md': 'qwqwqw', 'time_create': '1999', 'time_update': '2019', 'user_name': 'max', 'view_count': '1', 'logo': 'opps', 'memo': '', 'order': '1', 'kind': '1', 'valid': 1, } MPost().add_meta(self.post_id, p_d) aa = MPost.get_by_uid(self.post_id) tf = MPostHist.create_post_history(aa, aa) assert tf His = MPostHist.query_by_postid(self.post_id) self.uid = His[0].uid assert His[0].cnt_md == p_d['cnt_md'] self.tearDown()
class RatingHandler(BaseHandler): def initialize(self): self.init() self.mpost = MPost() self.mrating = MRating() def post(self, url_str=''): url_arr = self.parse_url(url_str) print(url_arr) if len(url_arr) == 2 and url_arr[0] == '_update': self.update_rating(url_arr[1]) elif len(url_arr) == 2 and url_arr[0] == '_updatepost': self.update_post(url_arr[1]) def update_post(self, postid): uu = self.mrating.query_by_post(postid) if uu.count() > 10: rating = self.mrating.query_average_rating(postid) else: rating = 5 print('Get post rating:', rating) self.mpost.update_rating(postid, rating) def update_rating(self, postid): post_data = self.get_post_data() rating = float(post_data['rating']) postinfo = self.mpost.get_by_uid(postid) if postinfo and self.userinfo: self.mrating.update(postinfo.uid, self.userinfo.uid, rating=rating) else: return False
def _delete(self, *args, **kwargs): ''' delete the post. ''' _ = kwargs uid = args[0] current_infor = MPost.get_by_uid(uid) if MPost.delete(uid): tslug = MCategory.get_by_uid(current_infor.extinfo['def_cat_uid']) MCategory.update_count(current_infor.extinfo['def_cat_uid']) if router_post[self.kind] == 'info': url = "filter" id_dk8 = current_infor.extinfo['def_cat_uid'] else: url = "list" id_dk8 = tslug.slug self.redirect('/{0}/{1}'.format(url, id_dk8)) else: self.redirect('/{0}/{1}'.format(router_post[self.kind], uid))
def restore(self, hist_uid): if self.check_post_role()['ADMIN']: pass else: return False histinfo = MPostHist.get_by_uid(hist_uid) if histinfo: pass else: return False postinfo = MPost.get_by_uid(histinfo.post_id) cur_cnt = tornado.escape.xhtml_unescape(postinfo.cnt_md) old_cnt = tornado.escape.xhtml_unescape(histinfo.cnt_md) MPost.update_cnt(histinfo.post_id, { 'cnt_md': old_cnt, 'user_name': self.userinfo.user_name }) MPostHist.update_cnt(histinfo.uid, { 'cnt_md': cur_cnt, 'user_name': postinfo.user_name }) self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid))
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 __to_add(self, **kwargs): ''' Used for info1. ''' if 'catid' in kwargs: catid = kwargs['catid'] return self.__to_add_with_category(catid) else: if self.check_post_role()['ADD']: pass else: return False if 'uid' in kwargs and MPost.get_by_uid(kwargs['uid']): # todo: # self.redirect('/{0}/edit/{1}'.format(self.app_url_name, uid)) uid = kwargs['uid'] else: uid = '' self.render('post_{0}/post_add.html'.format(self.kind), tag_infos=MCategory.query_all(by_order=True, kind=self.kind), userinfo=self.userinfo, kwd={'uid': uid, })
def render(self, post_id, reply_uid): try: rec = MPost.get_by_uid(post_id) return rec.title except: # todo:进入评论列表页面后显示None,再刷新才会删除不存在post的评论 MReply2User.delete(reply_uid)
def j_delete(self, *args): ''' Delete the post, but return the JSON. ''' uid = args[0] current_infor = MPost.get_by_uid(uid) tslug = MCategory.get_by_uid(current_infor.extinfo['def_cat_uid']) is_deleted = MPost.delete(uid) MCategory.update_count(current_infor.extinfo['def_cat_uid']) if is_deleted: output = { 'del_info': 1, 'cat_slug': tslug.slug, 'cat_id': tslug.uid, 'kind': current_infor.kind } else: output = { 'del_info': 0, } return json.dump(output, self)
def add_relation(self, url_arr): ''' Add relationship. :param url_arr: :return: ''' if MPost.get_by_uid(url_arr[1]): pass else: return False last_post_id = self.get_secure_cookie('last_post_uid') if last_post_id: last_post_id = last_post_id.decode('utf-8') last_app_id = self.get_secure_cookie('use_app_uid') if last_app_id: last_app_id = last_app_id.decode('utf-8') if url_arr[0] == 'info': if last_post_id: MRelation.add_relation(last_post_id, url_arr[1], 2) MRelation.add_relation(url_arr[1], last_post_id, 1) if url_arr[0] == 'post': if last_app_id: MRelation.add_relation(last_app_id, url_arr[1], 2) MRelation.add_relation(url_arr[1], last_app_id, 1)
def view(self, uid): postinfo = MPost.get_by_uid(uid) if postinfo: pass else: return hist_recs = MPostHist.query_by_postid(uid, limit=5) html_diff_arr = [] for hist_rec in hist_recs: if hist_rec: infobox = diff_table(hist_rec.cnt_md, postinfo.cnt_md) else: infobox = '' html_diff_arr.append({ 'hist_uid': hist_rec.uid, 'html_diff': infobox }) self.render('man_info/post_man_view.html', userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, view=postinfo, postinfo=postinfo, html_diff_arr=html_diff_arr, router=router_post[postinfo.kind])
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 __could_edit(self, postid): post_rec = MPost.get_by_uid(postid) if not post_rec: return False if self.check_post_role()['EDIT'] or post_rec.user_name == self.userinfo.user_name: return True else: return False
def tearDown(self): print("function teardown") tt = MPostHist.get_by_uid(self.uid) if tt: MPostHist.delete(tt.uid) tt = MPost.get_by_uid(self.post_id) if tt: MPost.delete(tt.uid)
def to_edit(self, postid): if self.userinfo.role[0] > '0': pass else: return False post_rec = MPost.get_by_uid(postid) self.render('man_info/post_man_edit.html', userinfo=self.userinfo, postinfo=post_rec)
def __gen_uid(self): ''' Generate the ID for post. :return: the new ID. ''' cur_uid = self.kind + tools.get_uu4d() while MPost.get_by_uid(cur_uid): cur_uid = self.kind + tools.get_uu4d() return cur_uid
def tearDown(self): print("function teardown") tt = MCategory.get_by_uid(self.uid) if tt: MCategory.delete(self.uid) tt = MPost.get_by_uid(self.postid) if tt: MPost.delete(self.postid) MPost2Catalog.remove_relation(self.postid, self.uid)
def down(self, down_uid): mpost = MPost.get_by_uid(down_uid) down_url = mpost.extinfo[ 'tag_file_download'] if 'tag_file_download' in mpost.extinfo else '' if down_url: url = mpost.extinfo['tag_file_download'] ment_id = MEntity.get_id_by_impath(url) MEntity2User.create_entity2user(ment_id, self.userinfo.uid) else: return False
def _chuli_cookie_relation(self, app_id): ''' The current Info and the Info viewed last should have some relation. And the last viewed Info could be found from cookie. ''' last_app_uid = self.get_secure_cookie('use_app_uid') if last_app_uid: last_app_uid = last_app_uid.decode('utf-8') self.set_secure_cookie('use_app_uid', app_id) if last_app_uid and MPost.get_by_uid(last_app_uid): self._add_relation(last_app_uid, app_id)
def down(self, down_uid): ''' Download the entity by UID. ''' down_url = MPost.get_by_uid(down_uid).extinfo.get( 'tag_file_download', '') if down_url: ment_id = MEntity.get_id_by_impath(down_url) MEntity2User.create_entity2user(ment_id, self.userinfo.uid) else: return False
def _view_or_add(self, uid): ''' Try to get the post. If not, to add the wiki. ''' postinfo = MPost.get_by_uid(uid) if postinfo: self.viewinfo(postinfo) elif self.userinfo: self._to_add(uid=uid) else: self.show404()
def _gen_last_current_relation(self, post_id): ''' Generate the relation for the post and last post viewed. ''' last_post_id = self.get_secure_cookie('last_post_uid') if last_post_id: last_post_id = last_post_id.decode('utf-8') self.set_secure_cookie('last_post_uid', post_id) if last_post_id and MPost.get_by_uid(last_post_id): self._add_relation(last_post_id, post_id)
def tearDown(self): print("function teardown") tt = MPost.get_by_uid(self.post_id) if tt: print('777777777777') MPost.delete(self.post_id) tt = MLabel.get_by_slug(self.tag_id) if tt: print('8888888888') MLabel.delete(tt.uid)