def add(self): ''' user add category. ''' if self.check_post_role()['ADD']: pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.get_current_user() cur_uid = post_data['uid'] if MCategory.add_or_update(cur_uid, post_data): output = { 'addinfo ': 1, } else: output = { 'addinfo ': 0, } return json.dump(output, self)
def _the_view_kwd(self, postinfo): ''' Generate the kwd dict for view. :param postinfo: the postinfo :return: dict ''' kwd = { 'pager': '', 'url': self.request.uri, 'cookie_str': tools.get_uuid(), 'daohangstr': '', 'signature': postinfo.uid, 'tdesc': '', 'eval_0': MEvaluation.app_evaluation_count(postinfo.uid, 0), 'eval_1': MEvaluation.app_evaluation_count(postinfo.uid, 1), 'login': 1 if self.get_current_user() else 0, 'has_image': 0, 'parentlist': MCategory.get_parent_list(), 'parentname': '', 'catname': '', 'router': router_post[postinfo.kind], } return kwd
def _to_add(self, **kwargs): ''' Used for info1. ''' if 'catid' in kwargs: catid = kwargs['catid'] return self._to_add_with_category(catid) else: 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 to_modify(self, id_rec): ''' to edit the category by ID. :param id_rec: post ID ''' if self.__could_edit(id_rec): pass else: return False category_rec = MCategory.get_by_uid(id_rec) if category_rec: pass else: return None kwd = { 'pager': '', } self.render('admin/{0}/category_edit.html'.format(self.tmpl_router), kwd=kwd, unescape=tornado.escape.xhtml_unescape, dbrec=category_rec, userinfo=self.userinfo, cfg=config.CMS_CFG)
def test_add_or_update(self): post_data = { 'name': 'category', 'slug': self.slug, 'order': '1', 'kind': '1', 'pid': 'z100', } MCategory.add_or_update(self.uid, post_data) a = MCategory.get_by_uid(self.uid) assert a.pid == post_data['pid'] post_data2 = { 'name': 'adsfdsf', 'pid': 'z222', 'slug': 'ssskug', } MCategory.add_or_update(self.uid, post_data2) a2 = MCategory.get_by_uid(self.uid) assert a2.name == post_data2['name']
def test_get_by_slug(self): self.add_message() a = MCategory.get_by_slug(self.slug) assert a
def render(self, with_count=True): kwd = { 'cats': MCategory.query_all(by_count=True), 'with_count': with_count, } return self.render_string('modules/post/menu_post.html', kwd=kwd)
def render(self): return self.render_string('modules/post/showcat_list.html', recs=MCategory.query_all(), unescape=tornado.escape.xhtml_unescape)
def render(self, map_id): rec = MPost2Catalog.query_by_post(map_id) for x in rec: result = MCategory.get_by_uid(x.tag_id) return result.slug
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' logger.warning('info kind:{0} '.format(postinfo.kind)) # If not, there must be something wrong. if postinfo.kind == self.kind: pass else: self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid), permanent=True) ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self.__chuli_cookie_relation(postinfo.uid) cookie_str = tools.get_uuid() catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag.uid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = { 'pager': '', 'url': self.request.uri, 'cookie_str': cookie_str, 'daohangstr': '', 'signature': postinfo.uid, 'tdesc': '', 'eval_0': MEvaluation.app_evaluation_count(postinfo.uid, 0), 'eval_1': MEvaluation.app_evaluation_count(postinfo.uid, 1), 'login': 1 if self.get_current_user() else 0, 'has_image': 0, 'parentlist': MCategory.get_parent_list(), 'parentname': '', 'catname': '', 'router': router_post[postinfo.kind] } MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', cookie_str) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6)[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, calc_info=postinfo, # Deprecated post_info=postinfo, # Deprecated userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), recent_apps=recent_apps, cat_enum=cat_enum1)
class InfoPublishHandler(BaseHandler): def initialize(self, hinfo=''): self.init() self.minforcatalog = MCategory() def get(self, url_str=''): url_arr = self.parse_url(url_str) if len(url_str) == 1: self.view_class1(url_str) elif len(url_str) == 4: self.view_class2(url_str) elif len(url_str) == 5: self.echo_class2(url_str) elif len(url_arr) == 2 and url_arr[1] == 'vip': self.view_class2(url_arr[0], 1) @tornado.web.authenticated def echo_class2(self, input=''): ''' 弹出的二级发布菜单 ''' fatherid = input[1:] self.write(self.format_class2(fatherid)) @tornado.web.authenticated def format_class2(self, fatherid): # dbdata = self.minforcatalog.get_qian2(fatherid[:2]) catinfo = self.minforcatalog.get_by_uid(fatherid) dbdata = self.minforcatalog.query_sub_cat(fatherid) outstr = '<ul class="list-group">' for rec in dbdata: outstr += ''' <a href="/{0}/_cat_add/{1}" class="btn btn-primary" style="display: inline-block;margin:3px;" >{2}</a> '''.format(router_post[catinfo.kind], rec.uid, rec.name) outstr += '</ul>' return (outstr) @tornado.web.authenticated def view_class1(self, kind_sig): dbdata = self.minforcatalog.get_parent_list(kind=kind_sig) class1str = '' for rec in dbdata: class1str += ''' <a onclick="select_sub_tag('/publish/2{0}');" class="btn btn-primary" style="display: inline-block;margin:3px;" >{1}</a> '''.format(rec.uid, rec.name) kwd = { 'class1str': class1str, 'parentid': '0', 'parentlist': self.minforcatalog.get_parent_list(), } self.render('infor/publish/publish.html', userinfo=self.userinfo, kwd=kwd) @tornado.web.authenticated def view_class2(self, fatherid=''): ''' 从第二级分类发布 :param fatherid: :return: ''' if self.is_admin(): pass else: return False kwd = { 'class1str': self.format_class2(fatherid), 'parentid': '0', 'parentlist': self.minforcatalog.get_parent_list(), } self.render('infor/publish/publish2.html', userinfo=self.userinfo, kwd=kwd)
def viewinfo(self, postinfo): ''' In infor. ''' self.redirect_kind(postinfo) # ToDo: 原为下面代码。 # ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' # ext_catid2 = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None ext_catid = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' cat_enum1 = MCategory.get_qian2(ext_catid[:2]) if ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self._chuli_cookie_relation(postinfo.uid) catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) if self.get_current_user() and self.userinfo: MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', kwd['cookie_str']) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6).objects()[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render( tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, author=postinfo.user_name, # Todo: remove the key `author`. catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, subcats=MCategory.query_sub_cat(p_catinfo.uid), ad_switch=random.randint(1, 18), tag_info=filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).objects()), recent_apps=recent_apps, cat_enum=cat_enum1)
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'] elif 'gcat0' in postinfo.extinfo: catid = postinfo.extinfo['gcat0'] 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 = { 'gcat0': 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 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(): assert MCategory()
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' logger.warning('info kind:{0} '.format(postinfo.kind)) # If not, there must be something wrong. if postinfo.kind == self.kind: pass else: self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], postinfo.uid), permanent=True) ###################################################### if DB_CFG['kind'] == 's': cat_enum1 = [] else: ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] ###################################################### catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) catalog_infors = None if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag.uid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) catalog_infors = MPost2Catalog.query_pager_by_slug(catinfo.slug, current_page_num=1, order=True) kwd = { 'pager': '', 'url': self.request.uri, 'daohangstr': '', 'signature': postinfo.uid, 'tdesc': '', 'eval_0': MEvaluation.app_evaluation_count(postinfo.uid, 0), 'eval_1': MEvaluation.app_evaluation_count(postinfo.uid, 1), 'login': 1 if self.get_current_user() else 0, 'has_image': 0, 'parentlist': MCategory.get_parent_list(), 'parentname': '', 'catname': '', 'router': router_post[postinfo.kind] } MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) tmpl = 'post_{0}/leaf_view.html'.format(self.kind) logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), catalog_infos=catalog_infors, cat_enum=cat_enum1)
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 initialize(self, hinfo=''): self.init() self.minforcatalog = MCategory()
def render(self, *args, **kwargs): kind = kwargs['kind'] if 'kind' in kwargs else '1' return self.render_string('modules/post/showcat_list.html', recs=MCategory.query_all(kind=kind))
def index(self): tag_enum = MCategory.query_pcat() self.render('misc/search/search_index.html', userinfo=self.userinfo, cat_enum=tag_enum, tag_enum=tag_enum)
def render(self, uid_with_str): return self.render_string( 'modules/post/catalog_of.html', recs=MCategory.query_uid_starts_with(uid_with_str))
def list(self, catid): ''' 页面打开后的渲染方法,不包含 list 的查询结果与分页导航 ''' logger.info('Infocat input: {0}'.format(catid)) condition = self.gen_redis_kw() sig = catid bread_title = '' bread_crumb_nav_str = '<li>当前位置:<a href="/">信息</a></li>' _catinfo = MCategory.get_by_uid(catid) logger.info('Infocat input: {0}'.format(_catinfo)) if _catinfo.pid == '0000': pcatinfo = _catinfo catinfo = None parent_id = catid parent_catname = MCategory.get_by_uid(parent_id).name condition['parentid'] = [parent_id] catname = MCategory.get_by_uid(sig).name bread_crumb_nav_str += '<li><a href="/list/{0}">{1}</a></li>'.format( sig, catname) bread_title = '{0}'.format(catname) else: catinfo = _catinfo pcatinfo = MCategory.get_by_uid(_catinfo.pid) condition['def_cat_uid'] = [sig] parent_id = _catinfo.uid parent_catname = MCategory.get_by_uid(parent_id).name catname = MCategory.get_by_uid(sig).name bread_crumb_nav_str += '<li><a href="/list/{0}">{1}</a></li>'.format( parent_id, parent_catname) bread_crumb_nav_str += '<li><a href="/list/{0}">{1}</a></li>'.format( sig, catname) bread_title += '{0} - '.format(parent_catname) bread_title += '{0}'.format(catname) num = MPost.get_num_condition(condition) kwd = { 'catid': catid, 'daohangstr': bread_crumb_nav_str, 'breadtilte': bread_title, 'parentid': parent_id, 'parentlist': MCategory.get_parent_list(), 'condition': condition, 'catname': catname, 'rec_num': num } # cat_rec = MCategory.get_by_uid(catid) if self.get_current_user() and self.userinfo: redis_kw = redisvr.smembers(CMS_CFG['redis_kw'] + self.userinfo.user_name) else: redis_kw = [] kw_condition_arr = [] for the_key in redis_kw: kw_condition_arr.append(the_key.decode('utf-8')) self.render('autogen/list/list_{0}.html'.format(catid), userinfo=self.userinfo, kwd=kwd, widget_info=kwd, condition_arr=kw_condition_arr, cat_enum=MCategory.get_qian2(parent_id[:2]), pcatinfo=pcatinfo, catinfo=catinfo)
def render(self, *args, **kwargs): uid_with_str = args[0] return self.render_string( 'modules/post/catalog_of.html', recs=MCategory.query_uid_starts_with(uid_with_str))
def initialize(self): self.init() self.mpost = MPost() self.mcat = MCategory() self.mpage = MPage() self.mlink = MLink()
def list_catalog(self, cat_slug, **kwargs): ''' listing the posts via category ''' post_data = self.get_post_data() tag = post_data.get('tag', '') def get_pager_idx(): ''' Get the pager index. ''' cur_p = kwargs.get('cur_p') the_num = int(cur_p) if cur_p else 1 the_num = 1 if the_num < 1 else the_num return the_num current_page_num = get_pager_idx() cat_rec = MCategory.get_by_slug(cat_slug) if not cat_rec: return False num_of_cat = MPost2Catalog.count_of_certain_category(cat_rec.uid, tag=tag) page_num = int(num_of_cat / CMS_CFG['list_num']) + 1 cat_name = cat_rec.name kwd = {'cat_name': cat_name, 'cat_slug': cat_slug, 'title': cat_name, 'router': router_post[cat_rec.kind], 'current_page': current_page_num, 'kind': cat_rec.kind, 'tag': tag} # tmpl = 'list/catalog_list.html' # Todo: review the following codes. # if self.order: # tmpl = 'list/catalog_list.html' # else: if self.order: tmpl = 'list/category_list.html' infos = MPost2Catalog.query_pager_by_slug( cat_slug, current_page_num, # order=self.order), tag=tag) else: tmpl = 'list/catalog_list.html' infos = MCatalog.query_by_slug(cat_slug) self.render(tmpl, catinfo=cat_rec, infos=infos, pager=tools.gen_pager_purecss( '/category/{0}'.format(cat_slug), page_num, current_page_num), userinfo=self.userinfo, html2text=html2text, cfg=CMS_CFG, kwd=kwd, router=router_post[cat_rec.kind])
class PageHandler(BaseHandler): def initialize(self): self.init() self.mpage = MPage() self.mpage_hist = MPageHist() self.mcat = MCategory() self.cats = self.mcat.query_all() self.kind = '2' def get(self, url_str=''): url_arr = self.parse_url(url_str) if len(url_arr) == 1 and url_str.endswith('.html'): self.redirect(url_str.split('.')[0]) elif url_arr[0] in ['modify', 'edit', '_edit']: self.to_modify(url_arr[1]) elif url_str == 'list': self.list() elif url_arr[0] == 'ajax_count_plus': self.ajax_count_plus(url_arr[1]) elif len(url_arr) == 1: self.to_view(url_str) else: self.render('html/404.html', userinfo=self.userinfo, kwd={}) def post(self, url_str=''): url_arr = self.parse_url(url_str) if url_arr[0] in ['modify', 'edit', '_add']: self.update(url_arr[1]) else: self.add_page() def to_view(self, slug): rec_page = self.mpage.get_by_uid(slug) if rec_page: if rec_page.kind == self.kind: self.viewit(rec_page) else: return False else: self.to_add(slug) @tornado.web.authenticated def to_add(self, citiao): if self.check_post_role(self.userinfo)['ADD']: pass else: return False kwd = { 'cats': self.cats, 'slug': citiao, 'pager': '', } self.render('doc/page/page_add.html', kwd=kwd, userinfo=self.userinfo, ) def __could_edit(self, slug): page_rec = self.mpage.get_by_uid(slug) if not page_rec: return False if self.check_post_role(self.userinfo)['EDIT'] or page_rec.user_name == self.userinfo.user_name: return True else: return False @tornado.web.authenticated def update(self, slug): if self.__could_edit(slug): pass else: return False post_data = self.get_post_data() if 'slug' in post_data: pass else: self.set_status(400) return False self.mpage_hist.insert_data(self.mpage.get_by_uid(slug)) self.mpage.update(slug, post_data) self.redirect('/page/{0}.html'.format(post_data['slug'])) @tornado.web.authenticated def to_modify(self, uid): if self.__could_edit(uid): pass else: return False kwd = { 'pager': '', } self.render('doc/page/page_edit.html', view=self.mpage.get_by_uid(uid), # Deprecated postinfo = self.mpage.get_by_uid(uid), kwd=kwd, unescape=tornado.escape.xhtml_unescape, cfg=config.cfg, userinfo=self.userinfo, ) def viewit(self, rec): kwd = { 'pager': '', } # rec.user_name = rec.user_name self.render('doc/page/page_view.html', view=rec, # Deprecated postinfo = rec, unescape=tornado.escape.xhtml_unescape, kwd=kwd, format_date=tools.format_date, userinfo=self.userinfo, cfg=config.cfg ) def ajax_count_plus(self, slug): output = { 'status': 1 if self.mpage.view_count_plus(slug) else 0, } return json.dump(output, self) def list(self): kwd = { 'pager': '', 'unescape': tornado.escape.xhtml_unescape, 'title': '单页列表', } self.render('doc/page/page_list.html', kwd=kwd, view=self.mpage.query_recent(), view_all=self.mpage.query_all(), format_date=tools.format_date, userinfo=self.userinfo, cfg=config.cfg ) @tornado.web.authenticated def add_page(self): if self.check_post_role(self.userinfo)['ADD']: pass else: return False # post_data = {} # for key in self.request.arguments: # post_data[key] = self.get_arguments(key) post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name if 'slug' in post_data: pass else: self.set_status(400) return False if self.mpage.get_by_uid(post_data['slug']): self.set_status(400) return False else: self.mpage.insert_data(post_data) self.redirect('/page/{0}.html'.format(post_data['slug']))
def test_update_count(self): self.add_message() a = MCategory.update_count(self.uid)
class PageAjaxHandler(PageHandler): def initialize(self): self.init() self.mpage = MPage() self.mcat = MCategory() self.cats = self.mcat.query_all()
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 initialize(self): self.init() self.mpage = MPage() self.mcat = MCategory() self.cats = self.mcat.query_all()