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()
def test_update_cnt(self): self.addHis() post_data = {'user_name': 'giser', 'cnt_md': 'gisersdfsdfsdf'} MPostHist.update_cnt(self.uid, post_data) pp = MPostHist.get_by_uid(self.uid) assert pp.cnt_md == post_data['cnt_md'] self.tearDown()
def get_diff_str(*args): mpost = MPost() mposthist = MPostHist() diff_str = '' for key in router_post.keys(): recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind=key) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: raw_title = hist_rec.title new_title = recent_post.title infobox = diff_table(raw_title, new_title) # infobox = test[start:end] + '</table>' # if ('diff_add' in infobox) or ('diff_chg' in infobox) or ('diff_sub' in infobox): diff_str = diff_str + '<h2 style="color:red; font-size:larger; font-weight:70;">TITLE: {0}</h2>'.format( recent_post.title) + infobox infobox = diff_table(hist_rec.cnt_md, recent_post.cnt_md) diff_str = diff_str + '<h3>CONTENT:{0}</h3>'.format( recent_post.title) + infobox + '</hr>' else: continue return diff_str
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 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 initialize(self): self.init() self.mpost = MPost() self.mcat = MCategory() self.cats = self.mcat.query_all() self.mpost_hist = MPostHist() self.mpost2catalog = MPost2Catalog() self.mpost2label = MPost2Label() self.mrel = MRelation() self.kind = '1'
def test_delete(self): aa = MPostHist.get_by_uid(self.uid) assert aa == None self.addHis() aa = MPostHist.get_by_uid(self.uid) assert aa.post_id == self.post_id aa = MPostHist.delete(self.post_id) assert aa == False self.tearDown()
def update(self, uid): if self.userinfo.role[0] > '0': pass else: return False post_data = self.get_post_data() if self.userinfo: post_data['user_name'] = self.userinfo.user_name else: post_data['user_name'] = '' cur_info = MPost.get_by_uid(uid) MPostHist.create_post_history(cur_info) MPost.update_cnt(uid, post_data) self.redirect('/{0}/{1}'.format(router_post[cur_info.kind], uid))
def delete(self, uid): if self.check_post_role()['DELETE']: pass else: return False histinfo = MPostHist.get_by_uid(uid) if histinfo: pass else: return False postinfo = MPost.get_by_uid(histinfo.post_id) MPostHist.delete(uid) self.redirect('/post_man/view/{0}'.format(postinfo.uid))
def __get_diff_recent(): ''' Generate the difference of posts. recently. ''' diff_str = '' for key in router_post: recent_posts = MPost.query_recent_edited(tools.timestamp() - TIME_LIMIT, kind=key) for recent_post in recent_posts: hist_rec = MPostHist.get_last(recent_post.uid) if hist_rec: raw_title = hist_rec.title new_title = recent_post.title infobox = diff_table(raw_title, new_title) diff_str = diff_str + ''' <h2 style="color:red;font-size:larger;font-weight:70;">TITLE: {0}</h2> '''.format(recent_post.title) + infobox infobox = diff_table(hist_rec.cnt_md, recent_post.cnt_md) diff_str = diff_str + '<h3>CONTENT:{0}</h3>'.format( recent_post.title) + infobox + '</hr>' else: continue return diff_str
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 test_query_by_postid(self): p_t = {'cnt_md': 'bbrreedd', 'user_name': 'ggggbabybaby'} self.addHis(**p_t) aa = MPostHist.query_by_postid(self.post_id) assert aa[0].cnt_md == p_t['cnt_md'] assert aa[0].user_name == p_t['user_name'] self.tearDown()
def test_get_last(self): p_t = {'cnt_md': 'bbrreedd', 'user_name': 'snow'} self.addHis(**p_t) aa = MPostHist.get_last(self.post_id) assert aa.user_name == p_t['user_name'] self.tearDown()
def __get_post_review(email_cnt, idx): ''' Review for posts. ''' for key in router_post: recent_posts = MPost.query_recent_edited(tools.timestamp() - TIME_LIMIT, kind=key) for recent_post in recent_posts: hist_rec = MPostHist.get_last(recent_post.uid) editor_name = recent_post.extinfo[ 'def_editor_name'] if 'def_editor_name' in recent_post.extinfo else recent_post.user_name if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, editor_name, recent_post.title, os.path.join(SITE_CFG['site_url'], router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, editor_name, recent_post.title, os.path.join(SITE_CFG['site_url'], router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 return email_cnt, idx
def update(self, uid): ''' in infor. ''' postinfo = MPost.get_by_uid(uid) if postinfo.kind == self.kind: pass else: return False post_data, ext_dic = self.fetch_post_data() if 'gcat0' in post_data: pass else: return False if 'valid' in post_data: post_data['valid'] = int(post_data['valid']) else: post_data['valid'] = postinfo.valid ext_dic['def_uid'] = str(uid) cnt_old = tornado.escape.xhtml_unescape(postinfo.cnt_md).strip() cnt_new = post_data['cnt_md'].strip() if cnt_old == cnt_new: pass else: MPostHist.create_post_history(postinfo) MPost.modify_meta(uid, post_data, extinfo=ext_dic) self._add_download_entity(ext_dic) # self.update_tag(uid=uid) update_category(uid, post_data) update_label(uid, post_data) # self.update_label(uid) logger.info('post kind:' + self.kind) # cele_gen_whoosh.delay() tornado.ioloop.IOLoop.instance().add_callback(self.cele_gen_whoosh) self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], uid))
def update(self, uid): if self.userinfo.role[0] > '0': pass else: return False post_data = self.get_post_data() if self.userinfo: post_data['user_name'] = self.userinfo.user_name else: post_data['user_name'] = '' cur_info = MPost.get_by_uid(uid) cur_info.user_name = post_data['user_name'] cnt_old = tornado.escape.xhtml_unescape(cur_info.cnt_md).strip() cnt_new = post_data['cnt_md'].strip() if cnt_old == cnt_new: pass else: MPostHist.create_post_history(cur_info, self.userinfo) MPost.update_cnt(uid, post_data) self.redirect('/{0}/{1}'.format(router_post[cur_info.kind], uid))
def initialize(self): self.init() self.mpost = MPost() self.mcat = MCategory() self.cats = self.mcat.query_all() self.mpost_hist = MPostHist() self.mpost2catalog = MPost2Catalog() # self.mpost2reply = MPost2Reply() self.mpost2label = MPost2Label() self.mrel = MRelation() self.tmpl_dir = 'admin' self.tmpl_router = 'post_ajax'
def addHis(self, **kwargs): 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('kind', '1'), 'valid': kwargs.get('valid', 1), } MPost().add_meta(self.post_id, p_d) aa = MPost.get_by_uid(self.post_id) MPostHist.create_post_history(aa, aa) His = MPostHist.query_by_postid(self.post_id) self.uid = His[0].uid
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) hist_user = hist_rec.user_name hist_time = hist_rec.time_update hist_words_num = len((hist_rec.cnt_md).strip()) post_words_num = len((postinfo.cnt_md).strip()) up_words_num = post_words_num - hist_words_num else: infobox = '' hist_user = '' hist_time = '' up_words_num = '' html_diff_arr.append({ 'hist_uid': hist_rec.uid, 'html_diff': infobox, 'hist_user': hist_user, 'hist_time': hist_time, 'up_words_num': up_words_num }) kwd = {} self.render('man_info/post_man_view.html', userinfo=self.userinfo, view=postinfo, postinfo=postinfo, html_diff_arr=html_diff_arr, router=router_post[postinfo.kind], kwd=kwd)
class PostHandler(BaseHandler): def initialize(self): self.init() self.mpost = MPost() self.mcat = MCategory() self.cats = self.mcat.query_all() self.mpost_hist = MPostHist() self.mpost2catalog = MPost2Catalog() self.mpost2label = MPost2Label() self.mrel = MRelation() self.tmpl_dir = 'doc' self.kind = '1' self.tmpl_router = 'post' def get(self, url_str=''): url_arr = self.parse_url(url_str) if url_str == '': self.recent() elif len(url_arr) == 1 and url_str.endswith('.html'): self.view_or_add(url_str.split('.')[0]) elif url_str == 'add_document': self.to_add_document() elif url_arr[0] == 'add_document': self.to_add_document() elif url_str == 'recent': self.recent() elif url_str == 'refresh': self.refresh() elif url_arr[0] in ['modify', 'edit']: self.to_modify(url_arr[1]) elif url_arr[0] == 'delete': self.delete(url_arr[1]) elif url_arr[0] == 'ajax_count_plus': self.ajax_count_plus(url_arr[1]) elif len(url_arr) == 1: self.view_or_add(url_str) else: kwd = { 'info': '页面未找到', } self.render( 'html/404.html', kwd=kwd, userinfo=self.userinfo, ) def post(self, url_str=''): if url_str == '': return url_arr = self.parse_url(url_str) if url_arr[0] in ['modify', 'edit']: self.update(url_arr[1]) elif url_arr[0] in ['add_document', '_add']: self.user_add_post() elif len(url_arr) == 1 and url_str.endswith('.html'): self.add_post(url_str) else: self.redirect('html/404.html') def ajax_count_plus(self, uid): output = { 'status': 1 if self.mpost.update_view_count_by_uid(uid) else 0, } return json.dump(output, self) def recent(self, with_catalog=True, with_date=True): kwd = { 'pager': '', 'unescape': tornado.escape.xhtml_unescape, 'title': '最近文档', 'with_catalog': with_catalog, 'with_date': with_date, } self.render( '{1}/{0}/post_list.html'.format(self.tmpl_router, self.tmpl_dir), kwd=kwd, view=self.mpost.query_recent(), view_all=self.mpost.query_all(), format_date=tools.format_date, userinfo=self.userinfo, cfg=config.cfg, ) @tornado.web.authenticated def __could_edit(self, postid): post_rec = self.mpost.get_by_uid(postid) if not post_rec: return False if self.check_post_role( self.userinfo )['EDIT'] or post_rec.user_name == self.userinfo.user_name: return True else: return False def refresh(self): kwd = { 'pager': '', 'title': '最近文档', } self.render( 'doc/post/post_list.html', kwd=kwd, userinfo=self.userinfo, view=self.mpost.query_dated(10), format_date=tools.format_date, unescape=tornado.escape.xhtml_unescape, cfg=config.cfg, ) # def get_random(self): # return self.mpost.query_random() def view_or_add(self, uid): if self.mpost.get_by_id(uid): self.view_post(uid) else: self.to_add(uid) @tornado.web.authenticated def to_add_document(self, ): if self.check_post_role(self.userinfo)['ADD']: pass else: return False kwd = { 'pager': '', 'cats': self.cats, 'uid': '', } self.render( '{1}/{0}/post_add.html'.format(self.tmpl_router, self.tmpl_dir), kwd=kwd, tag_infos=self.mcat.query_all(), userinfo=self.userinfo, cfg=config.cfg, ) @tornado.web.authenticated def to_add(self, uid): if self.check_post_role(self.userinfo)['ADD']: pass else: return False kwd = { 'cats': self.cats, 'uid': uid, 'pager': '', } self.render( 'doc/post/post_add.html', kwd=kwd, tag_infos=self.mcat.query_all(), cfg=config.cfg, userinfo=self.userinfo, ) @tornado.web.authenticated def update(self, uid): if self.__could_edit(uid): pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.get_current_user() is_update_time = True # if post_data['is_update_time'][0] == '1' else False self.mpost_hist.insert_data(self.mpost.get_by_id(uid)) self.mpost.update(uid, post_data, update_time=is_update_time) self.update_catalog(uid) self.update_tag(uid) self.redirect('/post/{0}.html'.format(uid)) @tornado.web.authenticated def update_tag(self, signature): current_tag_infos = self.mpost2label.get_by_id(signature, kind=self.kind + '1') post_data = self.get_post_data() if 'tags' in post_data: pass else: return False print('tags: {0}'.format(post_data['tags'])) tags_arr = [x.strip() for x in post_data['tags'].split(',')] for tag_name in tags_arr: if tag_name == '': pass else: self.mpost2label.add_record(signature, tag_name, 1, kind=self.kind + '1') for cur_info in current_tag_infos: print(cur_info.tag.name) if cur_info.tag.name in tags_arr: pass else: self.mpost2label.remove_relation(signature, cur_info.tag) @tornado.web.authenticated def update_catalog(self, uid): post_data = self.get_post_data() current_infos = self.mpost2catalog.query_by_entity_uid(uid, kind=self.kind + '0') new_tag_arr = [] # HTML中预定义的 def_cate_arr = ['gcat{0}'.format(x) for x in range(10)] # todo: next line should be deleted. keep here for historical reason. def_cate_arr.append('def_cat_uid') for key in def_cate_arr: if key in post_data: pass else: continue print('a' * 4) print(post_data[key]) if post_data[key] == '' or post_data[key] == '0': continue # if len(post_data[key]) != 4: # continue print(post_data[key]) print(new_tag_arr) # 有可能选重复了。保留前面的 if post_data[key] in new_tag_arr: continue new_tag_arr.append(post_data[key] + ' ' * (4 - len(post_data[key]))) for idx, val in enumerate(new_tag_arr): self.mpost2catalog.add_record(uid, val, idx) # 对原来的进行处理,如果不在现有中,则删除 for cur_info in current_infos: if str(cur_info.tag.uid).strip() not in new_tag_arr: self.mpost2catalog.remove_relation(uid, cur_info.tag) @tornado.web.authenticated def to_modify(self, id_rec): if self.__could_edit(id_rec): pass else: return False kwd = { 'pager': '', 'cats': self.cats, } self.render( 'doc/post/post_edit.html', kwd=kwd, unescape=tornado.escape.xhtml_unescape, tag_infos=self.mcat.query_all(kind=constant['cate_post']), app2label_info=self.mpost2label.get_by_id( id_rec, kind=constant['tag_post']), app2tag_info=self.mpost2catalog.query_by_entity_uid( id_rec, kind=constant['cate_post']), dbrec=self.mpost.get_by_id(id_rec), userinfo=self.userinfo, cfg=config.cfg, ) def get_cat_str(self, cats): cat_arr = cats.split(',') out_str = '' for xx in self.cats: if str(xx.uid) in cat_arr: tmp_str = '''<li><a href="/category/{0}" style="margin:10px auto;"> {1} </a></li> '''.format(xx.slug, tornado.escape.xhtml_escape(xx.name)) out_str += tmp_str return (out_str) def get_cat_name(self, id_cat): for x in self.cats: if x['id_cat'] == id_cat: return (x['name']) def __gen_last_current_relation(self, post_id): ''' Generate the relation for the post and last post viewed. :param post_id: :return: ''' 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 self.mpost.get_by_id(last_post_id): self.add_relation(last_post_id, post_id) def view_post(self, post_id): self.__gen_last_current_relation(post_id) cats = self.mpost2catalog.query_by_entity_uid(post_id) # replys = self.mpost2reply.get_by_id(post_id) tag_info = self.mpost2label.get_by_id(post_id) rec = self.mpost.get_by_id(post_id) if not rec: kwd = { 'info': '您要查看的页面不存在。', } self.render('html/404.html', kwd=kwd, userinfo=self.userinfo) return False if cats.count() == 0: cat_id = '' else: cat_id = cats.get().tag kwd = {'pager': '', 'editable': self.editable(), 'cat_id': cat_id} rel_recs = self.mrel.get_app_relations(rec.uid, 4) rand_recs = self.mpost.query_random(4 - rel_recs.count() + 2) self.render( 'doc/post/post_view.html', view=rec, postinfo=rec, unescape=tornado.escape.xhtml_unescape, kwd=kwd, userinfo=self.userinfo, tag_info=tag_info, relations=rel_recs, rand_recs=rand_recs, replys=[], cfg=config.cfg, ) def add_relation(self, f_uid, t_uid): if self.mpost.get_by_id(t_uid) is False: return False if f_uid == t_uid: ''' 关联其本身 ''' return False # 双向关联,但权重不一样. self.mrel.add_relation(f_uid, t_uid, 2) self.mrel.add_relation(t_uid, f_uid, 1) return True @tornado.web.authenticated def add_post(self, url_str): url_arr = url_str.split('.') if len(url_arr) == 2: id_post = url_arr[0] if len(id_post) == 5: pass else: return False else: return False if self.check_post_role(self.userinfo)['ADD']: pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name cur_post_rec = self.mpost.get_by_id(id_post) if cur_post_rec is None: uid = self.mpost.insert_data(id_post, post_data) self.update_tag(uid) self.update_catalog(uid) self.redirect('/post/{0}.html'.format(id_post)) @tornado.web.authenticated def user_add_post(self): if self.check_post_role(self.userinfo)['ADD']: pass else: return False post_data = self.get_post_data() if not ('title' in post_data): self.set_status(400) return False else: pass post_data['user_name'] = self.get_current_user() cur_uid = tools.get_uu5d() while self.mpost.get_by_id(cur_uid): cur_uid = tools.get_uu5d() uid = self.mpost.insert_data(cur_uid, post_data) self.update_tag(uid) self.update_catalog(uid) self.redirect('/post/{0}.html'.format(cur_uid)) @tornado.web.authenticated def delete(self, del_id): if self.check_post_role(self.userinfo)['DELETE']: pass else: return False is_deleted = self.mpost.delete(del_id) if is_deleted: self.redirect('/post/recent') else: return False
def test_update_cnt(self): post_data = {'user_name': 'giser', 'cnt_md': 'gisersdfsdfsdf'} MPostHist.update_cnt(self.uid, post_data) assert True
def test_query_by_postid(self): MPostHist.query_by_postid(self.post_id) assert True
def test_get_last(self): MPostHist.get_last(self.post_id) assert True
def run_edit_diff(): email_cnt = '''<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style></head><body>''' idx = 1 email_cnt = email_cnt + '<table border=1>' mpost = MPost() mposthist = MPostHist() for key in router_post.keys(): recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind=key) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 ## wiki mpost = MWiki() mposthist = MWikiHist() recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str idx = idx + 1 ## page. recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind='2') for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format(idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'page', recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format(idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'page', recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 email_cnt = email_cnt + '</table>' mpost = MPost() mposthist = MPostHist() diff_str = '' ###################################################### ###################################################### for key in router_post.keys(): recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind=key) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: print('=' * 10) print(recent_post.title) raw_title = hist_rec.title new_title = recent_post.title infobox = diff_table(raw_title, new_title) # infobox = test[start:end] + '</table>' # if ('diff_add' in infobox) or ('diff_chg' in infobox) or ('diff_sub' in infobox): diff_str = diff_str + '<h2 style="color:red; font-size:larger; font-weight:70;">TITLE: {0}</h2>'.format( recent_post.title) + infobox infobox = diff_table(hist_rec.cnt_md, recent_post.cnt_md) diff_str = diff_str + '<h3>CONTENT</h3>'.format( recent_post.title) + infobox + '</hr>' else: continue ########################################################### if len(diff_str) < 20000: email_cnt = email_cnt + diff_str email_cnt = email_cnt + '''</body></html>''' # print (email_cnt) print('edit diff count:', idx) if idx > 1: send_mail(post_emails, "{0}|{1}|{2}".format(smtp_cfg['name'], '文档更新情况', datestr), email_cnt)
def test_get_by_uid(self): p_t = {'cnt_md': 'bbrreedd'} self.addHis(**p_t) pp = MPostHist.get_by_uid(self.uid) assert pp.cnt_md == p_t['cnt_md'] self.tearDown()
def run_edit_diff(*args): email_cnt = '''<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style></head><body>''' idx = 1 email_cnt = email_cnt + '<table border=1>' mpost = MPost() mposthist = MPostHist() for key in router_post.keys(): recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind=key) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], router_post[key], recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 ## wiki mpost = MWiki() mposthist = MWikiHist() recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str idx = idx + 1 ## page. recent_posts = mpost.query_recent_edited(tools.timestamp() - time_limit, kind='2') for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], 'page', recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(SITE_CFG['site_url'], 'page', recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 email_cnt = email_cnt + '</table>' ########################################################### diff_str = get_diff_str() if len(diff_str) < 20000: email_cnt = email_cnt + diff_str email_cnt = email_cnt + '''</body></html>''' if idx > 1: send_mail(post_emails, "{0}|{1}|{2}".format(SMTP_CFG['name'], '文档更新情况', datestr), email_cnt)
def run_edit_diff(): email_cnt = '''<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style></head><body>''' idx = 1 email_cnt = email_cnt + '<table border=1>' mpost = MPost() mposthist = MPostHist() recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'post', recent_post.uid + '.html')) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'post', recent_post.uid + '.html')) email_cnt = email_cnt + foo_str idx = idx + 1 recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60, kind='2') for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, router_post['2'], recent_post.uid)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, router_post['2'], recent_post.uid)) email_cnt = email_cnt + foo_str idx = idx + 1 mpost = MWiki() mposthist = MWikiHist() recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'wiki', recent_post.title)) email_cnt = email_cnt + foo_str idx = idx + 1 recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60, kind='2') for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_chg">Edit</td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'page', recent_post.uid + '.html')) email_cnt = email_cnt + foo_str else: foo_str = ''' <tr><td>{0}</td><td>{1}</td><td class="diff_add">New </td><td>{2}</td> <td><a href="{3}">{3}</a></td></tr> '''.format( idx, recent_post.user_name, recent_post.title, os.path.join(site_url, 'page', recent_post.uid + '.html')) email_cnt = email_cnt + foo_str idx = idx + 1 email_cnt = email_cnt + '</table>' mpost = MPost() mposthist = MPostHist() diff_str = '' ###################################################### recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60) for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: print('=' * 10) print(recent_post.title) raw_title = hist_rec.title new_title = recent_post.title infobox = diff_table(raw_title, new_title) # if len(test) > 1: # start = test.find('<table class="diff"') # 起点记录查询位置 # end = test.find('</table>') # infobox = test[start:end] + '</table>' # if ('diff_add' in infobox) or ('diff_chg' in infobox) or ('diff_sub' in infobox): diff_str = diff_str + '<h2 style="color:red; font-size:larger; font-weight:70;">TITLE: {0}</h2>'.format( recent_post.title) + infobox test = diff_table(hist_rec.cnt_md, recent_post.cnt_md) print(test) # if len(test) >1 : # start = test.find('<table class="diff"') # 起点记录查询位置 # end = test.find('</table>') # infobox = test[start:end] + '</table>' # if ('diff_add' in infobox) or ('diff_chg' in infobox) or ('diff_sub' in infobox): diff_str = diff_str + '<h3>CONTENT</h3>'.format( recent_post.title) + test + '</hr>' else: continue ###################################################### recent_posts = mpost.query_recent_edited(tools.timestamp() - 24 * 60 * 60, kind='2') for recent_post in recent_posts: hist_rec = mposthist.get_last(recent_post.uid) if hist_rec: print('=' * 10) print(recent_post.title) raw_title = hist_rec.title new_title = recent_post.title infobox = diff_table(raw_title, new_title) # infobox = test[start:end] + '</table>' # if ('diff_add' in infobox) or ('diff_chg' in infobox) or ('diff_sub' in infobox): diff_str = diff_str + '<h2 style="color:red; font-size:larger; font-weight:70;">TITLE: {0}</h2>'.format( recent_post.title) + infobox infobox = diff_table(hist_rec.cnt_md, recent_post.cnt_md) diff_str = diff_str + '<h3>CONTENT</h3>'.format( recent_post.title) + infobox + '</hr>' else: continue ########################################################### if len(diff_str) < 20000: email_cnt = email_cnt + diff_str email_cnt = email_cnt + '''<table class="diff" summary="Legends"> <tr> <th colspan="2"> Legends </th> </tr> <tr> <td> <table border="" summary="Colors"> <tr><th> Colors </th> </tr> <tr><td class="diff_add"> Added </td></tr> <tr><td class="diff_chg">Changed</td> </tr> <tr><td class="diff_sub">Deleted</td> </tr> </table></td> <td> <table border="" summary="Links"> <tr><th colspan="2"> Links </th> </tr> <tr><td>(f)irst change</td> </tr> <tr><td>(n)ext change</td> </tr> <tr><td>(t)op</td> </tr> </table></td> </tr> </table></body>''' # print (email_cnt) print('edit diff count:', idx) if idx > 1: send_mail(post_emails, "{0}|{1}|{2}".format(smtp_cfg['name'], '文档更新情况', datestr), email_cnt)
def initialize(self): self.init() self.mpost = MPost() self.mposthist = MPostHist()
class PostManHandler(BaseHandler): def initialize(self): self.init() self.mpost = MPost() self.mposthist = MPostHist() def get(self, url_str=''): url_arr = self.parse_url(url_str) if url_arr[0] == 'view': self.view(url_arr[1]) elif url_arr[0] == 'edit': self.to_edit(url_arr[1]) elif url_arr[0] == 'restore': self.restore(url_arr[1]) elif url_arr[0] == 'delete': self.delete(url_arr[1]) else: kwd = { 'info': '页面未找到', } self.render( 'html/404.html', kwd=kwd, userinfo=self.userinfo, ) def post(self, url_str=''): url_arr = self.parse_url(url_str) if url_arr[0] == 'edit': self.update(url_arr[1]) else: self.redirect('html/404.html') @tornado.web.authenticated def update(self, uid): if self.userinfo.role[0] > '0': pass else: return False post_data = self.get_post_data() if self.userinfo: post_data['user_name'] = self.userinfo.user_name else: post_data['user_name'] = '' cur_info = self.mpost.get_by_id(uid) self.mposthist.insert_data(cur_info) self.mpost.update_cnt(uid, post_data) self.redirect('/{0}/{1}'.format(router_post[cur_info.kind], uid)) @tornado.web.authenticated def to_edit(self, postid): if self.userinfo.role[0] > '0': pass else: return False post_rec = self.mpost.get_by_uid(postid) self.render( 'man_post/post_man_edit.html', userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, postinfo=post_rec, ) @tornado.web.authenticated def __could_edit(self, postid): post_rec = self.mpost.get_by_uid(postid) if not post_rec: return False if self.check_post_role( self.userinfo )['EDIT'] or post_rec.user_name == self.userinfo.user_name: return True else: return False @tornado.web.authenticated def delete(self, uid): if self.check_post_role(self.userinfo)['DELETE']: pass else: return False histinfo = self.mposthist.get_by_id(uid) if histinfo: pass else: return False postinfo = self.mpost.get_by_id(histinfo.post_id) self.mposthist.delete(uid) self.redirect('/post_man/view/{0}'.format(postinfo.uid)) def view(self, uid): postinfo = self.mpost.get_by_id(uid) if postinfo: pass else: return hist_recs = self.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_post/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], ) @tornado.web.authenticated def restore(self, hist_uid): if self.check_post_role(self.userinfo)['ADMIN']: pass else: return False histinfo = self.mposthist.get_by_id(hist_uid) if histinfo: pass else: return False postinfo = self.mpost.get_by_id(histinfo.post_id) cur_cnt = tornado.escape.xhtml_unescape(postinfo.cnt_md) old_cnt = tornado.escape.xhtml_unescape(histinfo.cnt_md) self.mpost.update_cnt(histinfo.post_id, { 'cnt_md': old_cnt, 'user_name': self.userinfo.user_name }) self.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 test_get_by_uid(self): MPostHist.get_by_uid(self.uid) assert True