Example #1
0
class IndexHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.mpage = MPage()
        self.mlink = MLink()

    def get_current_user(self):
        return self.get_secure_cookie("user")

    def get(self, input=''):
        if input == '':
            self.index()
        else:
            self.render('html/404.html', kwd={}, userinfo=self.userinfo)

    def index(self):
        cstr = tools.get_uuid()
        self.set_cookie('user_pass', cstr)
        kwd = {'cookie_str': cstr}
        self.render(
            'index/index.html',
            userinfo=self.userinfo,
            catalog_info=self.mcat.query_all(by_order=True),
            link=self.mlink.query_all(),
            unescape=tornado.escape.xhtml_unescape,
            cfg=config.cfg,
            view=self.mpost.query_most_pic(20),
            kwd=kwd,
        )
Example #2
0
class IndexHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.mpage = MPage()
        self.mlink = MLink()

    def get_current_user(self):
        return self.get_secure_cookie("user")

    def get(self, input=''):
        if input == '':
            self.index()
        else:
            self.render('html/404.html', kwd={}, userinfo=self.userinfo)

    def index(self):
        cstr = tools.get_uuid()
        self.set_cookie('user_pass', cstr)
        kwd = {
            'cookie_str': cstr
        }
        self.render('index/index.html',
                    userinfo=self.userinfo,
                    catalog_info=self.mcat.query_all(by_order=True),
                    link=self.mlink.query_all(),
                    unescape=tornado.escape.xhtml_unescape,
                    cfg=config.cfg,
                    view=self.mpost.query_most_pic(20),
                    kwd=kwd, )
Example #3
0
class PostAjaxHandler(PostHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()

    @tornado.web.authenticated
    def delete(self, del_id):
        if self.check_doc_priv(self.userinfo)['DELETE']:
            pass
        else:
            return False
        is_deleted = self.mpost.delete(del_id)

        if is_deleted:
            output = {
                'del_info ': 1,
            }
        else:
            output = {
                'del_info ': 0,
            }
        return json.dump(output, self)
Example #4
0
class PageAjaxHandler(PageHandler):
    def initialize(self):
        self.init()
        self.mpage = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.tmpl_router = 'page_ajax'
Example #5
0
class category_menu(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        recs = self.mcat.query_all()
        return self.render_string('doc/modules/showcat_list.html',
                                  recs=recs,
                                  unescape=tornado.escape.xhtml_unescape,
                                  )
Example #6
0
class get_footer(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        all_cats = self.mcat.query_all()
        kwd = {
            'cats': all_cats,
        }
        return self.render_string('doc/modules/menu.html', kwd=kwd)
Example #7
0
class get_footer(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        all_cats = self.mcat.query_all()
        kwd = {
            'cats': all_cats,
        }
        return self.render_string('tmpl_torlite/modules/menu.html',
                                  kwd=kwd)
Example #8
0
class ModuleCatMenu(tornado.web.UIModule):
    def render(self, with_count=True):
        self.mcat = MCatalog()
        all_cats = self.mcat.query_all(by_count=True)
        kwd = {
            'cats': all_cats,
            'with_count': with_count,
        }
        return self.render_string('doc/modules/menu_post.html', kwd=kwd)
Example #9
0
class category_menu(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        recs = self.mcat.query_all()
        return self.render_string(
            'doc/modules/showcat_list.html',
            recs=recs,
            unescape=tornado.escape.xhtml_unescape,
        )
Example #10
0
class category_menu(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        recs = self.mcat.query_all()
        out_str = ''
        for rec in recs:
            tmp_str = '''<li><a href="/category/{0}" title="{1}">{1}</a></li>'''.format(rec.slug, rec.name)
            out_str += tmp_str
        return out_str
Example #11
0
class ModuleCatMenu(tornado.web.UIModule):
    def render(self, with_count=True):
        self.mcat = MCatalog()
        all_cats = self.mcat.query_all(by_count=True)
        kwd = {
            'cats': all_cats,
            'with_count': with_count,
        }
        return self.render_string('tmpl_torlite/modules/menu_post.html',
                                  kwd=kwd)
Example #12
0
class PostAjaxHandler(PostHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post_ajax'
Example #13
0
class PostAjaxHandler(PostHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post_ajax'
Example #14
0
class category_menu(tornado.web.UIModule):
    def render(self):
        self.mcat = MCatalog()
        recs = self.mcat.query_all()
        #out_str = ''
        #for rec in recs:
        #    tmp_str = '''<li><a href="/category/{0}" title="{1}">{1}</a></li>'''.format(rec.slug, rec.name)
        #    out_str += tmp_str
        #return out_str
        return self.render_string('doc/modules/showcat_list.html',
                                  recs=recs,
                                  unescape=tornado.escape.xhtml_unescape,
                                  )
Example #15
0
class CategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost2catalog = MPost2Catalog()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if len(url_arr) == 1:
            self.list_catalog(url_str)
        elif len(url_arr) == 2:
            self.list_catalog(url_arr[0], url_arr[1])
        else:
            self.render('html/404.html')

    def list_catalog(self, cat_slug, cur_p=''):
        if cur_p == '':
            current_page_num = 1
        else:
            current_page_num = int(cur_p)

        current_page_num = 1 if current_page_num < 1 else current_page_num
        cat_rec = self.mcat.get_by_slug(cat_slug)
        num_of_cat = self.mpost2catalog.count_of_certain_catalog(cat_rec.uid)
        page_num = int(num_of_cat / config.page_num) + 1
        cat_name = cat_rec.name
        kwd = {
            'cat_name': cat_name,
            'cat_slug': cat_slug,
            'unescape': tornado.escape.xhtml_unescape,
            'title': cat_name,
            'current_page': current_page_num
        }

        self.render('doc/catalog/list.html',
                    infos=self.mpost2catalog.query_pager_by_slug(
                        cat_slug, current_page_num),
                    pager=tools.gen_pager_purecss(
                        '/category/{0}'.format(cat_slug), page_num,
                        current_page_num),
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    kwd=kwd)

    def get_random(self):
        return self.mpost.query_random()
Example #16
0
class CategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost2catalog = MPost2Catalog()


    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if len(url_arr) == 1:
            self.list_catalog(url_str)
        elif len(url_arr) == 2:
            self.list_catalog(url_arr[0], url_arr[1])
        else:
            self.render('html/404.html')

    def list_catalog(self, cat_slug, cur_p=''):
        if cur_p == '':
            current_page_num = 1
        else:
            current_page_num = int(cur_p)

        current_page_num = 1 if current_page_num < 1 else current_page_num
        cat_rec = self.mcat.get_by_slug(cat_slug)
        num_of_cat = self.mpost2catalog.count_of_certain_catalog(cat_rec.uid)
        page_num = int(num_of_cat / config.page_num) + 1
        cat_name = cat_rec.name
        kwd = {
            'cat_name': cat_name,
            'cat_slug': cat_slug,
            'unescape': tornado.escape.xhtml_unescape,
            'title': cat_name,
            'current_page': current_page_num
        }

        self.render('doc/catalog/list.html',
                    infos=self.mpost2catalog.query_pager_by_slug(cat_slug, current_page_num),
                    pager=tools.gen_pager_purecss('/category/{0}'.format(cat_slug), page_num, current_page_num),
                    userinfo=self.userinfo,
                    cfg = config.cfg,
                    kwd=kwd)

    def get_random(self):
        return self.mpost.query_random()
Example #17
0
class SearchHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost2catalog = MPost2Catalog()
        self.ysearch = yunsearch()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if url_str == '':
            return
        elif len(url_arr) == 1:
            self.search(url_str)
        elif len(url_arr) == 2:
            self.search(url_arr[0], int(url_arr[1]))
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html',
                        kwd=kwd,
                        userinfo=  self.userinfo)

    def post(self, url_str=''):
        keyword = self.get_argument('keyword')
        self.search(keyword)

    def search(self, keyword, p_index=1):
        res_all = self.ysearch.get_all_num(keyword)
        results = self.ysearch.search_pager(keyword, page_index=p_index, doc_per_page=20)
        page_num = int(res_all / 20)
        kwd = {'title': '查找结果',
               'pager': '',
               }
        self.render('{0}/search/search.html'.format(self.tmpl_name),
                    kwd=kwd,
                    srecs=results,
                    pager=tools.gen_pager_bootstrap_url('/search/{0}'.format(keyword), page_num, p_index),
                    userinfo = self.userinfo,
                    cfg = config.cfg,
                    )
Example #18
0
class MaintainCategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mclass = MCatalog()
        self.tmpl_router = 'maintain_category'

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if url_str == 'add':
            self.to_add()
        elif url_str == 'list':
            self.list_catalog()
        elif url_arr[0] == 'modify':
            self.to_modify(url_arr[1])

        elif url_arr[0] == 'delete':

            self.delete_by_uid(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)
        print(url_str)
        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'add':
            self.p_add_catalog()
        elif url_arr[0] == 'add':

            self.p_add_catalog()
        else:
            self.redirect('html/404.html')

    def list_catalog(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('doc/{0}/category_list.html'.format(self.tmpl_router),
                    kwd=kwd,
                    view=self.mclass.query_all(by_order=True),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
                    cfg=config.cfg)

    @tornado.web.authenticated
    def to_add(self):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        kwd = {
            'pager': '',
            'uid': '',
        }
        self.render('doc/{0}/category_add.html'.format(self.tmpl_router),
                    topmenu='',
                    kwd=kwd,
                    userinfo=self.userinfo,
                    cfg=config.cfg)

    def __could_edit(self, uid):
        raw_data = self.mclass.get_by_id(uid)
        if not raw_data:

            return False
        if self.check_doc_priv(
                self.userinfo
        )['EDIT'] or raw_data.id_user == self.userinfo.user_name:
            return True
        else:
            return False

    @tornado.web.authenticated
    def update(self, uid):
        if self.__could_edit(uid):
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_name'] = self.get_current_user()

        if self.tmpl_router == "maintain_category":
            self.mclass.update(uid, post_data)
            self.redirect('/maintain/category/list'.format(uid))
        else:
            if self.mclass.update(uid, post_data):

                output = {
                    'addinfo ': 1,
                }
            else:
                output = {
                    'addinfo ': 0,
                }
            return json.dump(output, self)

    @tornado.web.authenticated
    def to_modify(self, id_rec):
        if self.__could_edit(id_rec):
            pass
        else:
            return False
        a = self.mclass.get_by_id(id_rec)
        kwd = {
            'pager': '',
        }
        self.render(
            'doc/{0}/category_edit.html'.format(self.tmpl_router),
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            dbrec=a,
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

    @tornado.web.authenticated
    def add_post(self):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()
        id_post = post_data['uid'][0]
        cur_post_rec = self.mclass.get_by_id(id_post)
        if cur_post_rec is None:
            uid = self.mclass.insert_data(id_post, post_data)

        self.redirect('/maintain/category/list'.format(id_post))

    @tornado.web.authenticated
    def p_add_catalog(self):

        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()

        cur_uid = tools.get_uudd(2)
        while self.mclass.get_by_id(cur_uid):
            cur_uid = tools.get_uudd(2)

        if self.mclass.insert_data(post_data['uid'][0], post_data):

            output = {
                'addinfo ': 1,
            }
        else:
            output = {
                'addinfo ': 0,
            }
        return json.dump(output, self)

    @tornado.web.authenticated
    def add_catalog(self):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()

        cur_uid = tools.get_uudd(2)
        while self.mclass.get_by_id(cur_uid):
            cur_uid = tools.get_uudd(2)

        uid = self.mclass.insert_data(cur_uid, post_data)

        self.redirect('/maintain/category/list'.format(cur_uid))

    @tornado.web.authenticated
    def delete_by_uid(self, del_id):
        if self.check_doc_priv(self.userinfo)['DELETE']:
            pass
        else:
            return False
        if self.tmpl_router == "maintain_category":
            is_deleted = self.mclass.delete(del_id)

            if is_deleted:
                self.redirect('/maintain/category/list')
            else:
                return False
        else:
            if self.mclass.delete(del_id):
                output = {'del_category': 1}
            else:
                output = {
                    'del_category': 0,
                }

            return json.dump(output, self)
Example #19
0
class PageHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpage = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)
        if url_arr[0] == 'modify':
            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 and url_str.endswith('.html'):
            self.wiki(url_str.split('.')[0])
        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] == 'modify':
            self.update(url_arr[1])
        else:
            self.wikinsert()

    def wiki(self, slug):
        dbdate = self.mpage.get_by_slug(slug)
        if dbdate:
            self.viewit(dbdate)
        else:
            self.to_add(slug)

    @tornado.web.authenticated
    def to_add(self, citiao):
        kwd = {
            'cats': self.cats,
            'slug': citiao,
            'pager': '',
        }
        self.render('{0}/page/page_add.html'.format(self.tmpl_name), kwd=kwd)

    @tornado.web.authenticated
    def update(self, slug):

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        if 'slug' in post_data:
            pass
        else:
            self.set_status(400)
            return False

        self.mpage.update(slug, post_data)
        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))

    @tornado.web.authenticated
    def to_modify(self, slug):
        kwd = {
            'pager': '',

        }
        self.render('{0}/page/page_edit.html'.format(self.tmpl_name),
                    view=self.mpage.get_by_slug(slug),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    cfg  = config.cfg,
                    userinfo = self.userinfo,
                    )

    def viewit(self, dbdata):
        kwd = {
            'pager': '',
            'editable': 1 if self.get_current_user() else 0,
        }
        self.render('{0}/page/page_view.html'.format(self.tmpl_name),
                    view=dbdata,
                    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('{0}/{1}/page_list.html'.format(self.tmpl_name, self.tmpl_router),
                    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 wikinsert(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        if 'slug' in post_data:
            pass
        else:
            self.set_status(400)
            return False

        if self.mpage.get_by_slug(post_data['slug'][0]) is None:
            self.mpage.insert_data(post_data)
        else:
            self.set_status(400)
            return False

        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))
Example #20
0
class MaintainCategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mclass = MCatalog()
        self.tmpl_router = 'maintain_category'


    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if url_str == 'add':
            self.to_add()
        elif url_str == 'list':
            self.list_catalog()
        elif url_arr[0] == 'modify':
            self.to_modify(url_arr[1])

        elif url_arr[0] == 'delete':

            self.delete_by_uid(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] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'add':
            self.add_catalog()
        elif url_arr[0] == 'add':

            self.p_add_catalog()
        else:
            self.redirect('html/404.html')

    def list_catalog(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('{0}/{1}/category_list.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    view=self.mclass.query_all(by_order=True),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
                    cfg=config.cfg
                    )

    @tornado.web.authenticated
    def to_add(self):
        kwd = {
            'pager': '',
            'uid': '',
        }
        self.render('{0}/{1}/category_add.html'.format(self.tmpl_name, self.tmpl_router),
                    topmenu='',
                    kwd=kwd,
                    userinfo=self.userinfo,
                    cfg=config.cfg
                    )

    @tornado.web.authenticated
    def update(self, uid):
        raw_data = self.mclass.get_by_id(uid)
        if self.userinfo.privilege[2] == '1' or raw_data.user_name == self.get_current_user():
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_name'] = self.get_current_user()

        if self.tmpl_router == "maintain_category":
            self.mclass.update(uid, post_data)
            self.redirect('/maintain/category/list'.format(uid))
        else:
            if self.mclass.update(uid, post_data):

                output = {
                    'addinfo ': 1,
                }
            else:
                output = {
                    'addinfo ': 0,
                }
            return json.dump(output, self)

    @tornado.web.authenticated
    def to_modify(self, id_rec):
        a = self.mclass.get_by_id(id_rec)
        if self.userinfo.privilege[4] == '1':
            pass
        else:
            return False

        kwd = {
            'pager': '',

        }
        self.render('{0}/{1}/category_edit.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    dbrec=a,
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    )

    @tornado.web.authenticated
    def add_post(self):
        if self.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()
        id_post = post_data['uid'][0]
        cur_post_rec = self.mclass.get_by_id(id_post)
        if cur_post_rec is None:
            uid = self.mclass.insert_data(id_post, post_data)

        self.redirect('/maintain/category/list'.format(id_post))

    @tornado.web.authenticated
    def p_add_catalog(self):


        if self.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()

        cur_uid = tools.get_uudd(2)
        while self.mclass.get_by_id(cur_uid):
            cur_uid = tools.get_uudd(2)

        if self.mclass.insert_data(post_data['uid'], post_data):

            output = {
                'addinfo ': 1,
            }
        else:
            output = {
                'addinfo ': 0,
            }
        return json.dump(output, self)

    @tornado.web.authenticated
    def add_catalog(self):
        if self.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()

        cur_uid = tools.get_uudd(2)
        while self.mclass.get_by_id(cur_uid):
            cur_uid = tools.get_uudd(2)

        uid = self.mclass.insert_data(cur_uid, post_data)

        self.redirect('/maintain/category/list'.format(cur_uid))

    @tornado.web.authenticated
    def delete_by_uid(self, del_id):
        if self.tmpl_router == "maintain_category":
            is_deleted = self.mclass.delete(del_id)

            if is_deleted:
                self.redirect('/maintain/category/list')
            else:
                return False
        else:
            if self.mclass.delete(del_id):
                output = {
                    'del_category': 1
                }
            else:
                output = {
                    'del_category': 0,
                }

            return json.dump(output, self)
Example #21
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()
        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.wiki(url_str.split('.')[0])
        elif url_str == 'add_document':
            self.to_add_document()
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            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])
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render(
                'html/404.html',
                kwd=kwd,
                userinfo=self.userinfo,
            )

    def post(self, url_str=''):
        if url_str == '':
            return
        url_arr = url_str.split('/')

        if len(url_arr) == 1 and url_str.endswith('.html'):
            self.add_post()
        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'add_document':
            self.user_add_post()
        elif url_arr[0] == 'add_document':
            self.user_add_post()
        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(
            'doc/{0}/post_list.html'.format(self.tmpl_router),
            kwd=kwd,
            view=self.mpost.query_recent(),
            view_all=self.mpost.query_all(),
            format_date=tools.format_date,
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

    def __could_edit(self, postid):
        post_rec = self.mpost.get_by_uid(postid)
        if not post_rec:
            return False
        if self.check_doc_priv(
                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/{0}/post_list.html'.format(self.tmpl_router),
            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 wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.viewit(uid)
        else:
            self.to_add(uid)

    def to_add_document(self, ):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        kwd = {
            'pager': '',
            'cats': self.cats,
            'uid': '',
        }
        self.render(
            'doc/{0}/post_add.html'.format(self.tmpl_router),
            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_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        kwd = {
            'cats': self.cats,
            'uid': uid,
            'pager': '',
        }
        self.render(
            'doc/{0}/post_add.html'.format(self.tmpl_router),
            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 = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_name'] = self.get_current_user()
        is_update_time = True if post_data['is_update_time'][
            0] == '1' else False
        self.mpost.update(uid, post_data, update_time=is_update_time)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.mpost_hist.insert_data(self.mpost.get_by_id(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)
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        if 'tags' in post_data:
            pass
        else:
            return False

        tags_arr = [x.strip() for x in post_data['tags'][0].split(',')]

        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mpost2label.add_record(signature, tag_name, 1)

        for cur_info in current_tag_infos:
            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 = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']:
            if key in post_data:
                pass
            else:
                continue
            if post_data[key][0] == '':
                pass
            else:
                new_tag_arr.append(post_data[key][0])
                self.mpost2catalog.add_record(uid, post_data[key][0],
                                              int(key[-1]))

        for cur_info in current_infos:
            if str(cur_info.catalog.uid).strip() not in new_tag_arr:
                self.mpost2catalog.remove_relation(uid, cur_info.catalog)

    @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/{0}/post_edit.html'.format(self.tmpl_router),
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(),
            app2label_info=self.mpost2label.get_by_id(id_rec),
            app2tag_info=self.mpost2catalog.query_by_id(id_rec),
            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 viewit(self, post_id):
        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)

        cats = self.mpost2catalog.query_entry_catalog(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().catalog
        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/{0}/post_view.html'.format(self.tmpl_router),
            view=rec,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
            tag_info=tag_info,
            relations=rel_recs,
            rand_recs=rand_recs,
            replys=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):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.userinfo.user_name
        id_post = post_data['uid'][0]
        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_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}

        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        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_doc_priv(self.userinfo)['DELETE']:
            pass
        else:
            return False
        is_deleted = self.mpost.delete(del_id)
        if self.tmpl_router == "post":
            if is_deleted:
                self.redirect('/post/recent')
            else:
                return False
        else:
            if is_deleted:
                output = {
                    'del_info ': 1,
                }
            else:
                output = {
                    'del_info ': 0,
                }
            return json.dump(output, self)
Example #22
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()
        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.wiki(url_str.split('.')[0])
        elif url_str == 'add_document':
            self.to_add_document()
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            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])
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd,
                        userinfo=self.userinfo, )

    def post(self, url_str=''):
        if url_str == '':
            return
        url_arr = url_str.split('/')

        if len(url_arr) == 1 and url_str.endswith('.html'):
            self.add_post()
        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'add_document':
            self.user_add_post()
        elif url_arr[0] == 'add_document':
            self.user_add_post()
        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('{0}/{1}/post_list.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    view=self.mpost.query_recent(),
                    view_all=self.mpost.query_all(),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    )

    def refresh(self):

        kwd = {
            'pager': '',
            'title': '最近文档',
        }
        self.render('{0}/{1}/post_list.html'.format(self.tmpl_name, self.tmpl_router),
                    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 wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.viewit(uid)
        else:
            self.to_add(uid)

    def to_add_document(self, ):
        kwd = {
            'pager': '',
            'cats': self.cats,
            'uid': '',

        }
        self.render('{0}/{1}/post_add.html'.format(self.tmpl_name, self.tmpl_router),
                    topmenu='',
                    kwd=kwd,
                    tag_infos=self.mcat.query_all(),
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    )

    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {
            'cats': self.cats,
            'uid': uid,
            'pager': '',
        }
        self.render('{0}/{1}/post_add.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    tag_infos=self.mcat.query_all(),
                    cfg=config.cfg,
                    userinfo=self.userinfo, )

    @tornado.web.authenticated
    def update(self, uid):
        raw_data = self.mpost.get_by_id(uid)
        if self.userinfo.privilege[2] == '1' or raw_data.user_name == self.get_current_user():
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_name'] = self.get_current_user()
        is_update_time = True if post_data['is_update_time'][0] == '1' else False
        self.update_tag(uid)
        self.mpost.update(uid, post_data, update_time=is_update_time)
        self.update_catalog(uid)
        self.mpost_hist.insert_data(raw_data)
        self.redirect('/post/{0}.html'.format(uid))

    @tornado.web.authenticated
    def update_tag(self, signature):
        if self.userinfo.privilege[4] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        if 'tags' in post_data:
            pass
        else:
            return False
        current_tag_infos = self.mapp2tag.get_by_id(signature)

        tags_arr = [x.strip() for x in post_data['tags'][0].split(',')]

        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mapp2tag.add_record(signature, tag_name, 1)

        for cur_info in current_tag_infos:
            if cur_info.tag.name in tags_arr:
                pass
            else:
                self.mapp2tag.remove_relation(signature, cur_info.tag)

    @tornado.web.authenticated
    def update_catalog(self, uid):
        raw_data = self.mpost.get_by_id(uid)
        if self.userinfo.privilege[4] == '1' or raw_data.user_name == self.get_current_user():
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']:
            if key in post_data:
                pass
            else:
                continue
            if post_data[key][0] == '':
                pass
            else:
                new_tag_arr.append(post_data[key][0])
                self.mpost2catalog.add_record(uid, post_data[key][0], int(key[-1]))

        for cur_info in current_infos:
            if str(cur_info.catalog.uid).strip() not in new_tag_arr:
                self.mpost2catalog.remove_relation(uid, cur_info.catalog)

    @tornado.web.authenticated
    def to_modify(self, id_rec):
        a = self.mpost.get_by_id(id_rec)
        # 用户具有管理权限,
        # 或
        # 文章是用户自己发布的。
        if self.userinfo.privilege[2] == '1' or a.user_name == self.get_current_user():
            pass
        else:
            return False

        kwd = {
            'pager': '',
            'cats': self.cats,

        }
        self.render('{0}/{1}/post_edit.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    tag_infos=self.mcat.query_all(),
                    app2label_info=self.mapp2tag.get_by_id(id_rec),
                    app2tag_info=self.mpost2catalog.query_by_id(id_rec),
                    dbrec=a,
                    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 viewit(self, post_id):
        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)

        cats = self.mpost2catalog.query_catalog(post_id)
        replys = self.mpost2reply.get_by_id(post_id)
        tag_info = self.mapp2tag.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().catalog
        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('{0}/{1}/post_view.html'.format(self.tmpl_name, self.tmpl_router),
                    view=rec,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    userinfo=self.userinfo,
                    tag_info=tag_info,
                    relations=rel_recs,
                    rand_recs=rand_recs,
                    replys=replys,
                    cfg=config.cfg,
                    )

    def add_relation(self, f_uid, t_uid):
        if False == self.mpost.get_by_id(t_uid):
            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):
        if self.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.get_current_user()
        id_post = post_data['uid'][0]
        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.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}

        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        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):
        is_deleted = self.mpost.delete(del_id)
        if self.tmpl_router == "post":
            if is_deleted:
                self.redirect('/post/recent')
            else:
                return False
        else:
            if is_deleted:
                output = {
                    'del_info ': 1,
                }
            else:
                output = {
                    'del_info ': 0,
                }
            return json.dump(output, self)
Example #23
0
class PageHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpage = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)
        if url_arr[0] in ['modify', '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 and url_str.endswith('.html'):
            self.to_view(url_str.split('.')[0])
        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']:
            self.update(url_arr[1])
        else:
            self.add_page()

    def to_view(self, slug):
        rec_page = self.mpage.get_by_slug(slug)
        if rec_page:
            self.viewit(rec_page)
        else:
            self.to_add(slug)

    @tornado.web.authenticated
    def to_add(self, citiao):
        if self.check_doc_priv(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_slug(slug)
        if not page_rec:

            return False
        if self.check_doc_priv(self.userinfo)['EDIT'] or page_rec.id_user == 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 = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        if 'slug' in post_data:
            pass
        else:
            self.set_status(400)
            return False

        self.mpage.update(slug, post_data)
        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))

    @tornado.web.authenticated
    def to_modify(self, slug):
        if self.__could_edit(slug):
            pass
        else:
            return False

        kwd = {
            'pager': '',

        }
        self.render('doc/page/page_edit.html',
                    view=self.mpage.get_by_slug(slug),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    cfg=config.cfg,
                    userinfo=self.userinfo,
                    )

    def viewit(self, rec):
        kwd = {
            'pager': '',
        }
        rec.user_name = rec.id_user
        self.render('doc/page/page_view.html',
                    view=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_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        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_slug(post_data['slug'][0]):
            self.set_status(400)
            return False
        else:
            self.mpage.insert_data(post_data)
            self.redirect('/page/{0}.html'.format(post_data['slug'][0]))
Example #24
0
class PageHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpage = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)
        if url_arr[0] in ['modify', '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 and url_str.endswith('.html'):
            self.to_view(url_str.split('.')[0])
        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']:
            self.update(url_arr[1])
        else:
            self.add_page()

    def to_view(self, slug):
        rec_page = self.mpage.get_by_slug(slug)
        if rec_page:
            self.viewit(rec_page)
        else:
            self.to_add(slug)

    @tornado.web.authenticated
    def to_add(self, citiao):
        if self.check_doc_priv(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_slug(slug)
        if not page_rec:
            return False
        if self.check_doc_priv(
                self.userinfo
        )['EDIT'] or page_rec.id_user == 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 = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        if 'slug' in post_data:
            pass
        else:
            self.set_status(400)
            return False

        self.mpage.update(slug, post_data)
        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))

    @tornado.web.authenticated
    def to_modify(self, slug):
        if self.__could_edit(slug):
            pass
        else:
            return False

        kwd = {
            'pager': '',
        }
        self.render(
            'doc/page/page_edit.html',
            view=self.mpage.get_by_slug(slug),
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            cfg=config.cfg,
            userinfo=self.userinfo,
        )

    def viewit(self, rec):
        kwd = {
            'pager': '',
        }
        rec.user_name = rec.id_user
        self.render('doc/page/page_view.html',
                    view=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_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        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_slug(post_data['slug'][0]):
            self.set_status(400)
            return False
        else:
            self.mpage.insert_data(post_data)
            self.redirect('/page/{0}.html'.format(post_data['slug'][0]))