def update(self, uid): ''' Update the post via ID. ''' if self.userinfo.role[0] > '0': pass else: return False post_data = self.get_post_data() post_data[ 'user_name'] = self.userinfo.user_name if self.userinfo else '' cur_info = MWiki.get_by_uid(uid) 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: MWikiHist.create_wiki_history(cur_info, self.userinfo) MWiki.update_cnt(uid, post_data) if cur_info.kind == '1': self.redirect('/wiki/{0}'.format(cur_info.title)) elif cur_info.kind == '2': self.redirect('/page/{0}.html'.format(cur_info.uid))
def restore(self, hist_uid): ''' Restore by ID ''' if self.check_post_role()['ADMIN']: pass else: return False histinfo = MWikiHist.get_by_uid(hist_uid) if histinfo: pass else: return False postinfo = MWiki.get_by_uid(histinfo.wiki_id) cur_cnt = tornado.escape.xhtml_unescape(postinfo.cnt_md) old_cnt = tornado.escape.xhtml_unescape(histinfo.cnt_md) MWiki.update_cnt(histinfo.wiki_id, { 'cnt_md': old_cnt, 'user_name': self.userinfo.user_name }) MWikiHist.update_cnt(histinfo.uid, { 'cnt_md': cur_cnt, 'user_name': postinfo.user_name }) if postinfo.kind == '1': self.redirect('/wiki/{0}'.format(postinfo.title)) elif postinfo.kind == '2': self.redirect('/page/{0}.html'.format(postinfo.uid))
def test_update_cnt(self): self.add_mess() aa = MWiki.get_by_uid(self.uid) pf = {'user_name': 'ooqwer', 'cnt_md': 'qwertyuioplkjgfdsa'} MWiki.update_cnt(self.uid, pf) bb = MWiki.get_by_uid(self.uid) assert aa.user_name != bb.user_name assert bb.user_name == pf['user_name'] self.tearDown()
def update(self, uid): ''' Update the post via ID. ''' if self.userinfo.role[0] > '0': pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name if self.userinfo else '' cur_info = MWiki.get_by_uid(uid) MWikiHist.create_wiki_history(cur_info, self.userinfo) MWiki.update_cnt(uid, post_data) if cur_info.kind == '1': self.redirect('/wiki/{0}'.format(cur_info.title)) elif cur_info.kind == '2': self.redirect('/page/{0}.html'.format(cur_info.uid))
class TestMWiki(): def setup(self): print('setup 方法执行于本类中每条用例之前') self.uu = MWiki() self.title = 'tyyyitle' self.uid = '6985' def add_page(self, **kwargs): post_data = { 'title': kwargs.get('title', self.title), 'user_name': kwargs.get('user_name', 'Tome'), 'cnt_md': kwargs.get('cnt_md', '## adslkfjasdf\n lasdfkjsadf'), } self.uu.create_page(self.uid, post_data) def test_insert(self): raw_count = self.uu.get_counts() post_data = { 'title': 'tyyyitle', 'user_name': 'Tome', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', } self.add_page(**post_data) new_count = self.uu.get_counts() tt = self.uu.get_by_uid(self.uid) assert tt.title == post_data['title'] assert tt.cnt_md == tornado.escape.xhtml_unescape(post_data['cnt_md']) assert raw_count + 1 <= new_count self.tearDown() # def test_insert_2(self): # self.tearDown() # '''Wiki insert: Test invalid title''' # post_data = { # 'title': '', # 'user_name': 'Tome', # 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', # # } # aa=self.uu.create_page(self.uid, post_data) # assert aa==False # # self.tearDown() def test_query_all(self): self.add_page() p = {'kind': '2'} aa = self.uu.query_all(**p) tf = False for i in aa: if i.uid == self.uid: tf = True self.tearDown() assert tf def test_get_by_slug(self): self.add_page() aa = self.uu.get_by_uid(self.uid) assert aa.title == self.title self.tearDown() def test_update_cnt(self): self.add_page() post_data = { 'user_name': 'name', 'cnt_md': '## adslkfjgggfdffasdf\n lasdfkjsadf', } self.uu.update_cnt(self.uid, post_data) tt = self.uu.get_by_uid(self.uid) assert tt.user_name == post_data['user_name'] assert tt.cnt_md == tornado.escape.xhtml_unescape(post_data['cnt_md']) self.tearDown() def test_update(self): self.add_page() post_data = { 'title': 'ti', 'user_name': 'Tome', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', } aa = self.uu.update(self.uid, post_data) assert aa == None post_data2 = { 'title': 'tgrgri', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', } self.uu.update(self.uid, post_data2) aa = self.uu.get_by_uid(self.uid) assert aa.title == post_data2['title'] self.tearDown() def test_query_recent_edited(self): timstamp = tools.timestamp() time.sleep(1) self.add_page() aa = self.uu.query_recent_edited(timstamp, kind='2') tf = False for i in aa: if i.uid == self.uid: tf = True self.tearDown() assert tf def tearDown(self): print("function teardown") self.uu.delete(self.uid)
class WikiManHandler(BaseHandler): def initialize(self): self.init() self.mpost = MWiki() self.mposthist = MWikiHist() self.tmpl_dir = 'wiki_man' 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] > '1': 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) if cur_info.kind == '1': self.redirect('/wiki/{0}'.format(cur_info.title)) elif cur_info.kind == '2': self.redirect('/page/{0}.html'.format(cur_info.uid)) @tornado.web.authenticated def to_edit(self, postid): if self.userinfo.role[0] > '1': pass else: return False post_rec = self.mpost.get_by_uid(postid) self.render( '{0}/wiki_man_edit.html'.format(self.tmpl_dir), userinfo=self.userinfo, unescape=tornado.escape.xhtml_escape, postinfo=post_rec, ) @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.wiki_id) self.mposthist.delete(uid) self.redirect('/wiki_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_wikiid(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( '{0}/wiki_man_view.html'.format(self.tmpl_dir), userinfo=self.userinfo, unescape=tornado.escape.xhtml_escape, view=postinfo, # Deprecated postinfo=postinfo, html_diff_arr=html_diff_arr) @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.wiki_id) cur_cnt = tornado.escape.xhtml_unescape(postinfo.cnt_md) old_cnt = tornado.escape.xhtml_unescape(histinfo.cnt_md) self.mpost.update_cnt(histinfo.wiki_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 }) if postinfo.kind == '1': self.redirect('/wiki/{0}'.format(postinfo.title)) elif postinfo.kind == '2': self.redirect('/page/{0}.html'.format(postinfo.uid))
class TestMWiki(): def setup(self): print('setup 方法执行于本类中每条用例之前') self.uu = MWiki() self.raw_count = self.uu.get_counts() self.page_slug = 'aaa' self.uid = tools.get_uuid() def test_insert(self): raw_count = self.uu.get_counts() post_data = { 'title': 'title', 'slug': 'sadfsadf', 'id_user': '******', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'view_count': 1, } self.uu.create_page('sadfsadf', post_data) new_count = self.uu.get_counts() tt = self.uu.get_by_uid(self.page_slug) # assert tt.title == post_data['title'][0] # assert tt.cnt_md == tornado.escape.xhtml_unescape(post_data['cnt_md'][0]) # assert raw_count + 1 == new_count def test_insert_2(self): '''Wiki insert: Test invalid title''' post_data = { 'title': '', 'id_user': '******', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'view_count': 1, } uu = self.uu.create_page('abcd', post_data) assert uu == False post_data = { 'title': '1', 'id_user': '******', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'view_count': 1, } uu = self.uu.create_page('bdef', post_data) assert uu == False post_data = { 'title': '天', 'id_user': '******', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'view_count': 1, } uu = self.uu.create_page('aaaa', post_data) assert uu == False def test_query_all(self): self.uu.query_all() assert True def test_get_by_slug(self): self.uu.get_by_uid('aa') assert True def test_update_cnt(self): post_data = { 'user_name': 'name', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', } self.uu.update_cnt(self.uid, post_data) assert True def test_update(self): post_data = { 'title': 'title', 'user_name': 'Tome', 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', } self.uu.update(self.uid, post_data) assert True def test_query_recent_edited(self): timstamp = tools.timestamp() self.uu.query_recent_edited(timstamp) assert True def tearDown(self): print("function teardown") tt = self.uu.get_by_uid(self.page_slug) if tt: self.uu.delete(tt.uid)