def update(self, slug): ''' Update the page. :param slug: :return: ''' if self.__could_edit(slug): pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name pageinfo = MWiki.get_by_uid(slug) cnt_old = tornado.escape.xhtml_unescape(pageinfo.cnt_md).strip() cnt_new = post_data['cnt_md'].strip() if cnt_old == cnt_new: pass else: MWikiHist.create_wiki_history(MWiki.get_by_uid(slug)) MWiki.update(slug, post_data) tornado.ioloop.IOLoop.instance().add_callback(self.cele_gen_whoosh) self.redirect('/page/{0}.html'.format(post_data['slug']))
def test_view_count_plus(self): self.add_mess() aa = MWiki.get_by_uid(self.uid) for i in range(5): MWiki.view_count_plus(self.uid) bb = MWiki.get_by_uid(self.uid) assert aa.view_count + 5 <= bb.view_count self.tearDown()
def test_update_view_count(self): self.add_mess() aa = MWiki.get_by_uid(self.uid) for i in range(5): MWiki.update_view_count(self.wiki_title) bb = MWiki.get_by_uid(self.uid) assert aa.view_count + 5 <= bb.view_count self.tearDown()
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 to_modify(self, uid): ''' Try to modify the page. ''' kwd = { 'pager': '', } self.render( 'wiki_page/page_edit.html', view=MWiki.get_by_uid(uid), # Deprecated postinfo=MWiki.get_by_uid(uid), kwd=kwd, cfg=CMS_CFG, userinfo=self.userinfo)
def add_w_h(self): self.add_user() self.add_wiki() post_data = MWiki.get_by_uid(self.wiki_uid) userinfo = MUser.get_by_uid(self.user_uid) aa = MWikiHist.create_wiki_history(post_data, userinfo) self.uid = aa
def update(self, uid): ''' Update the wiki. ''' postinfo = MWiki.get_by_uid(uid) if self.check_post_role( )['EDIT'] or postinfo.user_name == self.get_current_user(): pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name cnt_old = tornado.escape.xhtml_unescape(postinfo.cnt_md).strip() cnt_new = post_data['cnt_md'].strip() if cnt_old == cnt_new: pass else: MWikiHist.create_wiki_history(postinfo) MWiki.update(uid, post_data) # cele_gen_whoosh.delay() tornado.ioloop.IOLoop.instance().add_callback(self.cele_gen_whoosh) self.redirect('/wiki/{0}'.format( tornado.escape.url_escape(post_data['title'])))
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 test_get_by_title(self): self.add_mess() ss = MWiki.get_by_uid(self.uid) assert ss.title == self.wiki_title tt = MWiki.get_by_title(self.wiki_title) assert tt.title == self.wiki_title self.tearDown()
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 view(self, uid): ''' View the wiki with hisotical infos. ''' postinfo = MWiki.get_by_uid(uid) if postinfo: pass else: return hist_recs = MWikiHist.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) hist_user = hist_rec.user_name else: infobox = '' html_diff_arr.append({ 'hist_uid': hist_rec.uid, 'html_diff': infobox, 'hist_user': hist_user }) self.render( 'man_info/wiki_man_view.html', userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, view=postinfo, # Deprecated postinfo=postinfo, html_diff_arr=html_diff_arr)
def test_get_by_title2(self): '''Test Wiki title with SPACE''' self.add_mess() ss = MWiki.get_by_uid(self.uid) assert ss.title == self.wiki_title tt = MWiki.get_by_title(self.wiki_title) assert tt.title == self.wiki_title.strip() self.tearDown()
def to_edit(self, postid): if self.userinfo.role[0] > '0': pass else: return False self.render('man_info/wiki_man_edit.html', userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, postinfo=MWiki.get_by_uid(postid))
def to_edit(self, postid): ''' Try to edit the Post. ''' if self.userinfo.role[0] > '0': pass else: return False self.render('man_info/wiki_man_edit.html', userinfo=self.userinfo, postinfo=MWiki.get_by_uid(postid))
def test_get_by_title2(self): '''Test Wiki title with SPACE''' post_data = { 'title': ' ' + self.wiki_title + ' ', } self.add_mess(**post_data) ss = MWiki.get_by_uid(self.uid) assert ss.title == self.wiki_title tt = MWiki.get_by_title(self.wiki_title) assert tt.title == post_data['title'].strip() self.tearDown()
def test_get_by_uid(self): self.add_mess() tf = False aa = MWiki.query_all() for i in aa: if i.uid == self.uid: tf = True break assert tf bb = MWiki.get_by_uid(self.uid) assert bb.title == self.wiki_title self.tearDown()
def to_modify(self, uid): ''' Try to modify the page. :param uid: :return: ''' if self.__could_edit(uid): pass else: return False kwd = { 'pager': '', } self.render('wiki_page/page_edit.html', view=MWiki.get_by_uid(uid), # Deprecated postinfo=MWiki.get_by_uid(uid), kwd=kwd, cfg=CMS_CFG, userinfo=self.userinfo)
def test_create_page(self): p_d = { 'title': self.wiki_title, 'cnt_md': '## adslkfjasdf\n lasdfkjsadf', 'user_name': 'Tome', } tf = MWiki.create_page(self.uid, p_d) assert tf aa = MWiki.get_by_uid(self.uid) assert aa.title == self.wiki_title assert aa.kind == '2' self.tearDown()
def view_or_add(self, slug): ''' When access with the slug, It will add the page if there is no record in database. ''' rec_page = MWiki.get_by_uid(slug) if rec_page: if rec_page.kind == self.kind: self.view(rec_page) else: return False else: self.to_add(slug)
def __could_edit(self, slug): ''' Test if the user could edit the page. ''' page_rec = MWiki.get_by_uid(slug) if not page_rec: return False if self.check_post_role()['EDIT']: return True elif page_rec.user_name == self.userinfo.user_name: return True else: return False
def to_edit(self, id_rec): wiki_rec = MWiki.get_by_uid(id_rec) # 用户具有管理权限,或文章是用户自己发布的。 if self.check_post_role()['EDIT'] or wiki_rec.user_name == self.get_current_user(): pass else: return False kwd = { 'pager': '', } self.render('wiki_page/wiki_edit.html', kwd=kwd, postinfo=wiki_rec, userinfo=self.userinfo)
def delete(self, uid): if self.check_post_role()['DELETE']: pass else: return False histinfo = MWikiHist.get_by_uid(uid) if histinfo: pass else: return False postinfo = MWiki.get_by_uid(histinfo.wiki_id) MWikiHist.delete(uid) self.redirect('/wiki_man/view/{0}'.format(postinfo.uid))
def render(self, *args, **kwargs): ''' ''' page_id = kwargs['page_id'] userinfo = kwargs.get('userinfo', None) count = kwargs.get('count', 0) page = MWiki.get_by_uid(page_id) kwd = {'uid': page_id, 'count': count} if page: out_str = self.render_string('modules/show_page.html', postinfo=page, userinfo=userinfo, kwd=kwd) else: out_str = '<a href="/page/{0}">{0}</a>'.format(page_id) return out_str
def render(self, *args, **kwargs): ''' ''' page_id = kwargs['page_id'] userinfo = kwargs['userinfo'] if 'userinfo' in kwargs else None count = kwargs['count'] if 'count' in kwargs else 0 page = MWiki.get_by_uid(page_id) kwd = {'uid': page_id, 'count': count} if page: return self.render_string('modules/show_page.html', unescape=tornado.escape.xhtml_unescape, postinfo=page, userinfo=userinfo, kwd=kwd) else: return '<a href="/page/{0}">{0}</a>'.format(page_id)
def add_page(self, slug): ''' Add new page. ''' post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name if MWiki.get_by_uid(slug): self.set_status(400) return False else: MWiki.create_page(slug, post_data) tornado.ioloop.IOLoop.instance().add_callback(self.cele_gen_whoosh) self.redirect('/page/{0}'.format(slug))
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))
def view(self, uid): ''' View the wiki with hisotical infos. ''' postinfo = MWiki.get_by_uid(uid) if postinfo: pass else: return hist_recs = MWikiHist.query_by_wikiid(uid, limit=5) html_diff_arr = [] if hist_recs: for hist_rec in hist_recs: 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 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/wiki_man_view.html', userinfo=self.userinfo, postinfo=postinfo, html_diff_arr=html_diff_arr, kwd=kwd) else: self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid))
def view(self, uid): ''' View the wiki with hisotical infos. ''' postinfo = MWiki.get_by_uid(uid) if postinfo: pass else: return hist_recs = MWikiHist.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) 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 }) self.render( 'man_info/wiki_man_view.html', userinfo=self.userinfo, view=postinfo, # Deprecated postinfo=postinfo, html_diff_arr=html_diff_arr)
def add_page(self, slug): ''' Add new page. ''' post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name title = post_data['title'].strip() if len(title) < 2: kwd = { 'info': 'Title cannot be less than 2 characters', 'link': '/' } self.render('misc/html/404.html', userinfo=self.userinfo, kwd=kwd) if MWiki.get_by_uid(slug): self.set_status(400) return False else: MWiki.create_page(slug, post_data) tornado.ioloop.IOLoop.instance().add_callback(self.cele_gen_whoosh) self.redirect('/page/{0}'.format(slug))
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)