Example #1
0
    def initialize(self):

        self.mdb = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
Example #2
0
 def render(self):
     self.mcat = SpesubModel()
     all_cats = self.mcat.get_all()
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('tplite/modules/spec_menu.html', kwd=kwd)
Example #3
0
    def initialize(self):

        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost2catalog = MPost2Catalog()
Example #4
0
 def render(self):
     self.mcat = SpesubModel()
     all_cats = self.mcat.get_all()
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('{0}/modules/spec_menu.html'.format(
         config.torlite_template_name),
                               kwd=kwd)
Example #5
0
 def initialize(self):
     self.muser = MUser()
     self.mpage = MPage()
     self.mdb = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mspec = SpesubModel()
     if self.get_current_user():
         self.userinfo = self.muser.get_by_id(self.get_current_user())
     else:
         self.userinfo = None
Example #6
0
 def initialize(self):
     self.muser = MUser()
     self.mwiki = MWiki()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mspec = SpesubModel()
     self.specs = self.mspec.get_all()
     self.mwiki_hist = MWikiHist()
     if self.get_current_user():
         self.userinfo = self.muser.get_by_id(self.get_current_user())
     else:
         self.userinfo = None
Example #7
0
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mreply = MReply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #8
0
    def initialize(self):

        self.mdb = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
Example #9
0
 def render(self):
     self.mcat = SpesubModel()
     all_cats = self.mcat.get_all()
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('tplite/modules/spec_menu.html', kwd=kwd)
Example #10
0
 def render(self):
     self.mcat = SpesubModel()
     all_cats = self.mcat.get_all()
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('{0}/modules/spec_menu.html'.format(config.torlite_template_name), kwd=kwd)
Example #11
0
class ModuleSpecMenu(tornado.web.UIModule):
    def render(self):
        self.mcat = SpesubModel()
        all_cats = self.mcat.get_all()
        kwd = {
            'cats': all_cats,
        }
        return self.render_string('tplite/modules/spec_menu.html', kwd=kwd)
Example #12
0
class ModuleSpecMenu(tornado.web.UIModule):
    def render(self):
        self.mcat = SpesubModel()
        all_cats = self.mcat.get_all()
        kwd = {
            'cats': all_cats,
        }
        return self.render_string('tplite/modules/spec_menu.html', kwd=kwd)
Example #13
0
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post_ajax'

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #14
0
 def initialize(self):
     self.muser = MUser()
     self.mpage = MPage()
     self.mdb = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mspec = SpesubModel()
     if self.get_current_user():
         self.userinfo = self.muser.get_by_id(self.get_current_user())
     else:
         self.userinfo = None
Example #15
0
    def initialize(self):
        # analyzer = ChineseAnalyzer()
        # schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        # ix = config.ix
        # self.searcher = config.searcher
        # self.parser = config.parser
        # self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #16
0
class SearchHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.ysearch = yunsearch()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split('/')
        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)

    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),
        )
Example #17
0
class SearchHandler(BaseHandler):
    def initialize(self):
        # analyzer = ChineseAnalyzer()
        # schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        # ix = config.ix
        # self.searcher = config.searcher
        # self.parser = config.parser
        # self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):

        if url_str == '':
            return
        elif len(url_str) > 0:
            self.search(url_str)
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

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

    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)

    def search(self, keyword):
        results = whoosh_search(keyword, 20)
        # reswhoosh_search.keyworde limit=3limit=30)
        kwd = {'title': '查找结果'}
        self.render(
            'tplite/search/search.html',
            kwd=kwd,
            srecs=results,
        )
Example #18
0
 def initialize(self):
     self.muser = MUser()
     self.mwiki = MWiki()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mspec = SpesubModel()
     self.specs = self.mspec.get_all()
     self.mwiki_hist = MWikiHist()
     if self.get_current_user():
         self.userinfo = self.muser.get_by_id(self.get_current_user())
     else:
         self.userinfo = None
Example #19
0
class CategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost2catalog = MPost2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
        elif len(ip_arr) == 1:
            self.list_catalog(input)
        elif len(ip_arr) == 2:
            self.list_catalog(ip_arr[0], ip_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)

        cat_rec = self.mcat.get_by_slug(cat_slug)
        num_of_cat = self.mpost2catalog.catalog_record_number(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,
        }

        self.render('{0}/catalog/list.html'.format(self.tmpl_name),
                    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,
                    kwd=kwd)

    def get_random(self):
        return self.mpost.query_random()
Example #20
0
class SearchHandler(BaseHandler):
    def initialize(self):
        # analyzer = ChineseAnalyzer()
        # schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        # ix = config.ix
        # self.searcher = config.searcher
        # self.parser = config.parser
        # self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):

        if url_str == '':
            return
        elif len(url_str) > 0:
            self.search(url_str)
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

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

    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)
    def search(self, keyword):
        results = whoosh_search(keyword, 20)
        # reswhoosh_search.keyworde limit=3limit=30)
        kwd = {'title':'查找结果'}
        self.render('tplite/search/search.html',
                    kwd=kwd,
                    srecs = results,
                    )
Example #21
0
class SearchHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.ysearch = yunsearch()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    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)

    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),
                    cfg = config.cfg
                    )
Example #22
0
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mreply = MReply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #23
0
    def initialize(self):
        analyzer = ChineseAnalyzer()
        schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        ix = open_dir("lib/whoosh") # for read only
        self.searcher = ix.searcher()
        self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #24
0
    def initialize(self):
        # analyzer = ChineseAnalyzer()
        # schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        # ix = config.ix
        # self.searcher = config.searcher
        # self.parser = config.parser
        # self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.ysearch = yunsearch()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Example #25
0
class WikiHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mwiki = MWiki()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mwiki_hist = MWikiHist()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

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

        if url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'edit'):
            self.to_modify(url_arr[1])
        elif len(url_arr) == 1:
            self.wiki(url_str)
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    def post(self, input=''):
        if input == '':
            return
        url_arr = input.split(r'/')
        if url_arr[0] == 'edit':
            self.update(url_arr[1])
        elif url_arr[0] == 'add':
            self.wikinsert()
        else:
            self.redirect('html/404.html')

    def recent(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('{0}/wiki/wiki_list.html'.format(self.tmpl_name),
                    kwd=kwd,
                    view=self.mwiki.query_recent(),
                    format_date=tools.format_date, )

    def refresh(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('{0}/wiki/wiki_list.html'.format(self.tmpl_name),
                    kwd=kwd,
                    view=self.mwiki.query_dated(16),
                    format_date=tools.format_date, )

    def wiki(self, title):
        dbdate = self.mwiki.get_by_wiki(title)
        if dbdate:
            self.mwiki.update_view_count_by_uid(dbdate.uid)
            self.viewit(dbdate)
        else:
            self.to_add(title)

    @tornado.web.authenticated
    def to_add(self, title):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            'title': title,
            'pager': '',
        }
        self.render('{0}/wiki/wiki_add.html'.format(self.tmpl_name), kwd=kwd, )

    @tornado.web.authenticated
    def update(self, uid):
        raw_data = self.mwiki.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()
        self.mwiki.update(uid, post_data)
        self.mwiki_hist.insert_data(raw_data)
        self.redirect('/wiki/{0}'.format(tornado.escape.url_escape(post_data['title'][0])))

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

        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
        }
        self.render('{0}/wiki/wiki_edit.html'.format(self.tmpl_name),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    tag_infos=self.mcat.query_all(),
                    dbrec=wiki_rec,
                    )

    def viewit(self, view):
        kwd = {
            'pager': '',
            'editable': self.editable(),
        }

        self.render('{0}/wiki/wiki_view.html'.format(self.tmpl_name),
                    view=view,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    userinfo=self.userinfo, )

    @tornado.web.authenticated
    def wikinsert(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()
        tt = self.mwiki.get_by_wiki(post_data['title'][0])
        if tt is None:
            self.mwiki.insert_data(post_data)

        self.redirect('/wiki/{0}'.format(tornado.escape.url_escape(post_data['title'][0])))
Example #26
0
class SearchHandler(BaseHandler):
    def initialize(self):
        # analyzer = ChineseAnalyzer()
        # schema = Schema(title=TEXT(stored=True, analyzer = analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
        # ix = config.ix
        # self.searcher = config.searcher
        # self.parser = config.parser
        # self.parser = QueryParser("content", schema=ix.schema)

        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.ysearch = yunsearch()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split('/')
        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)



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

    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)

    def search(self, keyword, p_index = 1):

        res_all = self.ysearch.get_all_num(keyword)

        # res_all = get_all_num(keyword)
        # results = self.ysearch.search(keyword, 20)
        results = self.ysearch.search_pager(keyword, page_index=p_index, doc_per_page=20)
        print('-' * 40)
        page_num = int(res_all / 20)
        kwd = {'title':'查找结果',
               'pager': '',
               }
        self.render('tplite/search/search.html',
                    kwd=kwd,
                    srecs = results,
                    pager = tools.gen_pager_bootstrap_url('/search/{0}'.format(keyword), page_num, p_index),
                    )
Example #27
0
class SpecHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpage = MPage()
        self.mdb = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None


    def get(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            self.index()
        elif input == 'add':
            self.to_add()
        elif len(ip_arr) == 1:
            self.list(input)
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.to_edit(ip_arr[0])
        else:
            self.render('/html/404.html')

    def post(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
        elif input == 'add':
            self.add()
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.edit(ip_arr[0])
        else:
            self.render('/html/404.html')
        self.redirect('/spec/')

    @tornado.web.authenticated
    def edit(self, uid):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        self.mspec.update(uid, post_data)

    @tornado.web.authenticated
    def add(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        self.mspec.addata(post_data)

    @tornado.web.authenticated
    def to_edit(self, uid):
        spec_rec = self.mspec.get_by_id(uid)
        self.render('tplite/spec/edit.html', spec_rec=spec_rec)

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

        print('to_add')
        self.render('tplite/spec/add.html')

    def list(self, spec_slug):
        '''
        列出专题信息,按某一分类
        '''
        page_rec = self.mspec.get_by_slug(spec_slug)
        kwd = {
            'editable': self.editable()
        }
        self.render('tplite/spec/list.html',
                    kwd=kwd,
                    spec=page_rec,
                    unescape=tornado.escape.xhtml_unescape,
                    spec_recs=self.mdb.query_by_spec(page_rec.uid),
        )


    def index(self):
        kwd = {
            'editable': self.editable()
        }
        spec_recs = self.mspec.get_all()
        self.render('tplite/spec/index.html', spec_recs=spec_recs, kwd=kwd)
Example #28
0
class SpecHandler(BaseHandler):
    def initialize(self):
        self.mpage = MPage()
        self.mdb = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()


    def get(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            self.index()
        elif input == 'add':
            self.to_add()
        elif len(ip_arr) == 1:
            self.list(input)
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.to_edit(ip_arr[0])
        else:
            self.render('/html/404.html')

    def post(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
        elif input == 'add':
            self.add()
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.edit(ip_arr[0])
        else:
            self.render('/html/404.html')
        self.redirect('/spec/')

    @tornado.web.authenticated
    def edit(self, uid):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        self.mspec.update(uid, post_data)

    @tornado.web.authenticated
    def add(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        self.mspec.addata(post_data)

    @tornado.web.authenticated
    def to_edit(self, uid):
        spec_rec = self.mspec.get_by_id(uid)
        self.render('tplite/spec/edit.html', spec_rec=spec_rec)

    @tornado.web.authenticated
    def to_add(self):
        # spec_rec = self.mspec.get_by_id(uid)
        uuu = self.mcat.query_all()
        self.render('tplite/spec/add.html', cat_recs=uuu)

    def list(self, spec_slug):
        '''
        列出专题信息,按某一分类
        '''
        page_rec = self.mspec.get_by_slug(spec_slug)
        # content = self.mpage.get_by_wiki(page_rec.name)

        # if content is None:
        # re_url = '/page/{0}'.format(tornado.escape.url_escape(page_rec.name))
        #     self.redirect(re_url)


        recs = self.mdb.query_by_spec(page_rec.id)
        kwd = {
            # 'spec': content,
            # 'view': recs,

            'editable': self.editable()
        }
        self.render('tplite/spec/list.html',
                    kwd=kwd,
                    spec=page_rec,
                    unescape=tornado.escape.xhtml_unescape,
                    spec_recs=recs)


    def index(self):
        kwd = {
            # 'spec': content,
            # 'view': recs,
            'editable': self.editable()
        }
        spec_recs = self.mspec.get_all()
        self.render('tplite/spec/index.html', spec_recs=spec_recs, kwd=kwd)
Example #29
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mreply = MReply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):


        if url_str == '':
            return
        url_arr = url_str.split('/')

        if len(url_arr) == 1 and url_str.endswith('.html'):
            self.wiki(url_str.split('.')[0])
        elif url_str == 'find':
            self.to_find()
        elif url_arr[0] == 'find':
            self.find(url_arr[1])
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            self.to_modify(url_arr[1])

        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    def post(self, url_str=''):
        if url_str == '':
            return
        url_arr = url_str.split('/')
        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'find':
            self.post_find()
        elif url_arr[0] == 'add':
            self.add_post(url_arr[1])
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])

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


    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)

    def recent(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('tplite/post/all.html'.format(input),
                    kwd=kwd,
                    view=self.mpost.query_recent(),
                    # rand_recs=self.get_random(),
                    format_date=tools.format_date, )

    def refresh(self):
        kwd = {
            'pager': '',

            'title': '最近文档',
        }
        self.render('tplite/post/all.html'.format(input),
                    kwd=kwd,
                    view=self.mpost.query_dated(60),
                    format_date=tools.format_date,
                    unescape=tornado.escape.xhtml_unescape, )

    def post_find(self):
        keyword = self.get_argument('keyword')
        self.find(keyword)

    def find(self, keyword):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '查找结果',
        }
        self.render('tplite/post/find_list.html'.format(input),
                    kwd=kwd,
                    view=self.mpost.get_by_keyword(keyword),
        )


    def get_random(self):
        return self.mpost.query_random()

    def view_cat(self, cat_slug):
        for x in self.cats:
            if x.slug == cat_slug:
                search_str = ',{0},'.format(x.id_cat)
        dbdata = self.mpost.query_by_cat(search_str)
        self.render('tplite/post/all.html', view=dbdata, unescape=tornado.escape.xhtml_unescape)

    def wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.mpost.update_view_count_by_uid(dbdate.uid)
            self.viewit(dbdate)
        else:
            self.to_add(uid)

    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            'uid': uid,
            'pager': '',
        }
        self.render('tplite/post/addwiki.html', kwd=kwd, tag_infos=self.mcat.query_all(), )

    @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()

        # if update the time
        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.mpost_hist.insert_data(raw_data)
        self.redirect('/post/{0}.html'.format(uid))

    @tornado.web.authenticated
    def update_catalog(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)

        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']:
            if post_data[key][0] == '':
                pass
            else:
                new_tag_arr.append(int(post_data[key][0]))
                self.mpost2catalog.add_record(uid, post_data[key][0], int(key[-1]))

        for cur_info in current_infos:
            if cur_info.catalog.uid not in new_tag_arr:
                self.mpost2catalog.delete_by_id(cur_info.uid)

                # self.redirect('/post/{0}.html'.format(uid))

    @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

        id_spec = a.id_spec
        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
            'id_spec': id_spec,
        }
        self.render('tplite/post/modify.html',
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    tag_infos=self.mcat.query_all(),
                    app2tag_info=self.mpost2catalog.query_by_id(id_rec),
                    dbrec=a,
        )

    # @tornado.web.authenticated
    # def to_modify_catalog(self, id_rec):
    # # 用户具有管理权限,
    #     # 或
    #     # 文章是用户自己发布的。
    #     print(self.userinfo.privilege)
    #     if self.userinfo.privilege[4] == '1':
    #         pass
    #     else:
    #         print('Error')
    #         return False
    #     a = self.mpost.get_by_id(id_rec)
    #
    #     id_spec = a.id_spec
    #     kwd = {
    #         'pager': '',
    #         'cats': self.cats,
    #         'specs': self.specs,
    #         'view': a,
    #         'id_spec': id_spec,
    #     }
    #     self.render('tplite/post/edit_catalog.html', kwd=kwd, unescape=tornado.escape.xhtml_unescape,
    #                 tag_infos=self.mcat.query_all(),
    #                 app2tag_info=self.mpost2catalog.query_by_id(id_rec),
    #                 )

    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):
        cats = self.mpost2catalog.query_catalog(post_id)

        replys = self.mreply.get_by_id(post_id)

        if cats.count() == 0:
            cat_id = ''
        else:
            cat_id = cats.get().catalog
        kwd = {
            'pager': '',
            'editable': self.editable(),
            'cat_id': cat_id
        }

        self.render('tplite/post/viewiki.html',
                    view=post_id,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    userinfo=self.userinfo,
                    replys=replys,

        )

    @tornado.web.authenticated
    def add_post(self, id_post):
        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_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_catalog(uid)
        self.redirect('/post/{0}.html'.format(id_post))


    @tornado.web.authenticated
    def add_comment(self, id_post):
        if self.userinfo.privilege[0] == '1':
            pass
        else:
            return False

        post_data = {}

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


        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name

        comment = self.mreply.insert_data(id_post, post_data)

        if comment:

            output = {
                'pinglun': comment,

            }
        else:
            output = {
                'pinglun': 0,
            }

        return json.dump(output, self)

        #self.redirect('/post/{0}.html'.format(id_post))


    def get_zan(self, f_zan):
        zan = self.mreply.get_by_zan(f_zan)
        if zan:
            output = {
                'zan': zan.zan,
            }
        else:
            output = {
                ''
            }
Example #30
0
class SpecialHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpage = MPage()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

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

        if url_str == '':
            self.index()
        elif url_str == 'add':
            self.to_add()
        elif len(url_arr) == 1:
            self.list(url_str)

        elif len(url_arr) == 2 and url_arr[1] == 'edit':
            self.to_edit(url_arr[0])
        elif url_arr[0] == 'delete':
            self.delete(url_arr[1])
        else:
            self.render('html/404.html')

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

        if url_str == 'add':
            self.add()
        elif len(url_arr) == 2 and url_arr[1] == 'edit':
            self.edit(url_arr[0])
        else:
            self.render('html/404.html')
        # self.redirect('/spec/')

    @tornado.web.authenticated
    def edit(self, uid):
        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.mspec.update(uid, post_data)

    @tornado.web.authenticated
    def add(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

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

    @tornado.web.authenticated
    def to_edit(self, uid):
        spec_rec = self.mspec.get_by_id(uid)
        self.render('{0}/spec/edit.html'.format(self.tmpl_name), spec_rec=spec_rec,
                    cfg = config.cfg,
        )

    @tornado.web.authenticated
    def to_add(self):
        self.render('{0}/spec/add.html'.format(self.tmpl_name))

    def list(self, spec_slug):
        '''
        列出专题信息,按某一分类
        '''
        page_rec = self.mspec.get_by_slug(spec_slug)
        kwd = {
            'editable': self.editable()
        }
        self.render('{0}/spec/list.html'.format(self.tmpl_name),
                    kwd=kwd,
                    spec=page_rec,
                    unescape=tornado.escape.xhtml_unescape,
                    spec_recs=self.mpost.query_by_spec(page_rec.uid),
                    cfg = config.cfg,
                    )

    def index(self):
        kwd = {
            'editable': self.editable()
        }
        self.render('{0}/spec/index.html'.format(self.tmpl_name),
                    kwd=kwd,
                    spec_recs=self.mspec.get_all(),
                    cfg = config.cfg,
                    )

    @tornado.web.authenticated
    def delete(self, del_id):
        is_deleted = self.mspec.delete(del_id)
        if is_deleted:
            self.redirect('/spec/')
        else:
            return False
Example #31
0
class WikiHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mwiki = MWiki()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mwiki_hist = MWikiHist()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=""):
        if url_str == "":
            return
        url_arr = url_str.split(r"/")

        if url_str == "find":
            self.to_find()
        # elif url_arr[0] == 'find':
        # self.find(url_arr[1])
        elif url_str == "recent":
            self.recent()
        elif url_str == "refresh":
            self.refresh()
        elif url_arr[0] == "edit":
            self.to_modify(url_arr[1])
        elif len(url_arr) == 1:
            self.wiki(url_str)
        else:
            kwd = {"info": "页面未找到"}
            self.render("html/404.html", kwd=kwd)

    def post(self, input=""):
        if input == "":
            return
        url_arr = input.split(r"/")
        if url_arr[0] == "edit":
            self.update(url_arr[1])
        elif url_arr[0] == "add":
            self.wikinsert()
        else:
            self.redirect("static/404.html")

    def to_find(self,):
        kwd = {"pager": ""}
        self.render("tplite/post/find.html", topmenu="", kwd=kwd)

    def recent(self):
        kwd = {"pager": "", "unescape": tornado.escape.xhtml_unescape, "title": "最近文档"}
        self.render(
            "tplite/wiki/all.html".format(input), kwd=kwd, view=self.mwiki.query_recent(), format_date=tools.format_date
        )

    def refresh(self):
        kwd = {"pager": "", "unescape": tornado.escape.xhtml_unescape, "title": "最近文档"}
        self.render(
            "tplite/wiki/all.html".format(input),
            kwd=kwd,
            view=self.mwiki.query_dated(16),
            format_date=tools.format_date,
        )

    def wiki(self, title):
        dbdate = self.mwiki.get_by_wiki(title)
        if dbdate:
            self.mwiki.update_view_count_by_uid(dbdate.uid)
            self.viewit(dbdate)
        else:
            self.to_add(title)

    @tornado.web.authenticated
    def to_add(self, title):
        kwd = {"cats": self.cats, "specs": self.specs, "title": title, "pager": ""}
        self.render("tplite/wiki/addwiki.html", kwd=kwd)

    @tornado.web.authenticated
    def update(self, uid):
        raw_data = self.mwiki.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()
        self.mwiki.update(uid, post_data)
        self.mwiki_hist.insert_data(raw_data)
        self.redirect("/wiki/{0}".format(tornado.escape.url_escape(post_data["title"][0])))

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

        kwd = {"pager": "", "cats": self.cats, "specs": self.specs}
        self.render(
            "tplite/wiki/modify.html",
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(),
            dbrec=wiki_rec,
        )

    def viewit(self, view):
        # cats = self.mpost2catalog.query_catalog(post_id)
        kwd = {"pager": "", "editable": self.editable()}

        self.render(
            "tplite/wiki/viewiki.html",
            view=view,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
        )

    @tornado.web.authenticated
    def wikinsert(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()
        tt = self.mwiki.get_by_wiki(post_data["title"][0])
        if tt is None:
            uid = self.mwiki.insert_data(post_data)

        self.redirect("/wiki/{0}".format(tornado.escape.url_escape(post_data["title"][0])))
Example #32
0
class CatHandler(BaseHandler):
    def initialize(self):

        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost2catalog = MPost2Catalog()

    def get(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
        elif len(ip_arr) == 1 :
            self.list_catalog(input)
        elif len(ip_arr) == 2:
            self.list_catalog(ip_arr[0], ip_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)

        cat_rec = self.mcat.get_by_slug(cat_slug)
        num_of_cat = self.mpost2catalog.catalog_record_number(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,
        }

        self.render('tplite/catalog/list.html',
                    infos=self.mpost2catalog.query_pager_by_slug(cat_slug,current_page_num),
                    pager = tools.gen_pager(cat_slug, page_num, current_page_num),
                    kwd=kwd)

    # def view_cat_old(self, cat_slug, cur_p=''):
    #     if cur_p == '':
    #         current = 1
    #     else:
    #         current = int(cur_p)
    #
    #     cat_rec = self.mcat.get_by_slug(cat_slug)
    #     num_of_cat = self.mpost.get_num_by_cat(cat_rec.id_cat)
    #     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,
    #         'pager': tools.gen_pager(cat_slug, page_num, current),
    #         'title': cat_name,
    #     }
    #     for x in self.cats:
    #         if x.slug == cat_slug:
    #             search_str = ',{0},'.format(x.id_cat)
    #     dbdata = self.mpost.query_cat_by_pager(search_str, current)
    #     self.render('tplite/post/all.html',
    #                 kwd = kwd,
    #                 view=dbdata,
    #                 rand_recs = self.get_random(),
    #                 format_date = tools.format_date)



    def get_random(self):
        return self.mpost.query_random()
Example #33
0
class WikiHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mwiki = MWiki()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mwiki_hist = MWikiHist()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

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

        if url_str == 'find':
            self.to_find()
        # elif url_arr[0] == 'find':
        # self.find(url_arr[1])
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'edit'):
            self.to_modify(url_arr[1])
        elif len(url_arr) == 1:
            self.wiki(url_str)
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    def post(self, input=''):
        if input == '':
            return
        url_arr = input.split(r'/')
        if url_arr[0] == 'edit':
            self.update(url_arr[1])
        elif url_arr[0] == 'add':
            self.wikinsert()
        else:
            self.redirect('static/404.html')

    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)

    def recent(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render(
            'tplite/wiki/all.html'.format(input),
            kwd=kwd,
            view=self.mwiki.query_recent(),
            format_date=tools.format_date,
        )

    def refresh(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render(
            'tplite/wiki/all.html'.format(input),
            kwd=kwd,
            view=self.mwiki.query_dated(16),
            format_date=tools.format_date,
        )

    def wiki(self, title):
        dbdate = self.mwiki.get_by_wiki(title)
        if dbdate:
            self.mwiki.update_view_count_by_uid(dbdate.uid)
            self.viewit(dbdate)
        else:
            self.to_add(title)

    @tornado.web.authenticated
    def to_add(self, title):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            'title': title,
            'pager': '',
        }
        self.render(
            'tplite/wiki/addwiki.html',
            kwd=kwd,
        )

    @tornado.web.authenticated
    def update(self, uid):
        raw_data = self.mwiki.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()
        self.mwiki.update(uid, post_data)
        self.mwiki_hist.insert_data(raw_data)
        self.redirect('/wiki/{0}'.format(
            tornado.escape.url_escape(post_data['title'][0])))

    @tornado.web.authenticated
    def to_modify(self, id_rec):
        a = self.mwiki.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,
            'specs': self.specs,
        }
        self.render(
            'tplite/wiki/modify.html',
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(),
            dbrec=a,
        )

    def viewit(self, view):
        # cats = self.mpost2catalog.query_catalog(post_id)
        kwd = {
            'pager': '',
            'editable': self.editable(),
        }

        self.render(
            'tplite/wiki/viewiki.html',
            view=view,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
        )

    @tornado.web.authenticated
    def wikinsert(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()
        tt = self.mwiki.get_by_wiki(post_data['title'][0])
        if tt is None:
            uid = self.mwiki.insert_data(post_data)

        self.redirect('/wiki/{0}'.format(
            tornado.escape.url_escape(post_data['title'][0])))
Example #34
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post'

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):

        if url_str == '':
            self.recent()
        url_arr = url_str.split('/')

        if 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])

        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    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 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,
                    )

    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, )

    def get_random(self):
        return self.mpost.query_random()

    def wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.mpost.update_view_count_by_uid(dbdate.uid)
            self.viewit(uid)
        else:

            self.to_add(uid)

    def to_add_document(self, ):
        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
            '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,
                    )

    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            '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(), )

    @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))

    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[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)

        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(int(post_data[key][0]))
                self.mpost2catalog.add_record(uid, post_data[key][0], int(key[-1]))

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

    @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

        id_spec = a.id_spec
        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
            'id_spec': id_spec,
        }
        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,
                    )

    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)
            return False

        if cats.count() == 0:
            cat_id = ''
        else:
            cat_id = cats.get().catalog
        kwd = {
            'pager': '',
            'editable': self.editable(),
            'cat_id': cat_id
        }

        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=self.mrel.get_app_relations(rec.uid),
                    replys=replys,

                    )

    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 = {}
        print(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_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 #35
0
class SpecHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpage = MPage()
        self.mdb = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            self.index()
        elif input == 'add':
            self.to_add()
        elif len(ip_arr) == 1:
            self.list(input)
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.to_edit(ip_arr[0])
        else:
            self.render('/html/404.html')

    def post(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
        elif input == 'add':
            self.add()
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.edit(ip_arr[0])
        else:
            self.render('/html/404.html')
        self.redirect('/spec/')

    @tornado.web.authenticated
    def edit(self, uid):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        self.mspec.update(uid, post_data)

    @tornado.web.authenticated
    def add(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        self.mspec.addata(post_data)

    @tornado.web.authenticated
    def to_edit(self, uid):
        spec_rec = self.mspec.get_by_id(uid)
        self.render('tplite/spec/edit.html', spec_rec=spec_rec)

    @tornado.web.authenticated
    def to_add(self):
        # spec_rec = self.mspec.get_by_id(uid)
        # uuu = self.mcat.query_all()
        print('to_add')
        self.render('tplite/spec/add.html')

    def list(self, spec_slug):
        '''
        列出专题信息,按某一分类
        '''
        page_rec = self.mspec.get_by_slug(spec_slug)
        # content = self.mpage.get_by_wiki(page_rec.name)

        # if content is None:
        # re_url = '/page/{0}'.format(tornado.escape.url_escape(page_rec.name))
        #     self.redirect(re_url)

        kwd = {'editable': self.editable()}
        self.render(
            'tplite/spec/list.html',
            kwd=kwd,
            spec=page_rec,
            unescape=tornado.escape.xhtml_unescape,
            spec_recs=self.mdb.query_by_spec(page_rec.uid),
        )

    def index(self):
        kwd = {
            # 'spec': content,
            # 'view': recs,
            'editable': self.editable()
        }
        spec_recs = self.mspec.get_all()
        self.render('tplite/spec/index.html', spec_recs=spec_recs, kwd=kwd)
Example #36
0
class PageHandler(BaseHandler):
    def initialize(self):

        self.mdb = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()

    def get(self, input=''):
        if input == '':
            return
        ip_arr = input.split(r'/')
        if (ip_arr[0] == 'modify'):
            self.to_modify(ip_arr[1])
        elif len(ip_arr) == 1 and input.endswith('.html'):
            self.wiki(input.split('.')[0])
        else:
            self.render('/html/404.html')

    def post(self, input=''):
        if len(input) > 0:
            ip_arr = input.split(r'/')
        if input == '':
            pass
            # uuid = self.insert()
        elif ip_arr[0] == 'modify':
            self.update(ip_arr[1])
        else:
            uuid = self.wikinsert()

    def wiki(self, slug):
        dbdate = self.mdb.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,
            'specs': self.specs,
            'slug': citiao,
            'pager': '',
        }
        self.render('tplite/page/addwiki.html', kwd=kwd)

    @tornado.web.authenticated
    def update(self, slug):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        self.mdb.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(
            'tplite/page/modify.html',
            view=self.mdb.get_by_slug(slug),
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
        )

    def viewit(self, dbdata):
        kwd = {
            'pager': '',
            'editable': 1 if self.get_current_user() else 0,
        }
        self.render('tplite/page/page_view.html',
                    view=dbdata,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    format_date=tools.format_date)

    @tornado.web.authenticated
    def wikinsert(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        tt = self.mdb.get_by_slug(post_data['slug'][0])
        if tt is None:
            uid = self.mdb.insert_data(post_data)

        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))
Example #37
0
class PageHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mdb = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, input=''):
        if input == '':
            return
        ip_arr = input.split('/')
        if (ip_arr[0] == 'modify'):
            self.to_modify(ip_arr[1])
        elif len(ip_arr) == 1 and input.endswith('.html'):
            self.wiki(input.split('.')[0])
        else:
            self.render('/html/404.html')

    def post(self, input=''):

        if input == '':
            return False
        else:
            ip_arr = input.split('/')

        if ip_arr[0] == 'modify':
            self.update(ip_arr[1])
        else:

            self.wikinsert()

    def wiki(self, slug):
        dbdate = self.mdb.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,
            'specs': self.specs,
            '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.mdb.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.mdb.get_by_slug(slug),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape, )

    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,
                    )

    @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.mdb.get_by_slug(post_data['slug'][0]) is None:
            self.mdb.insert_data(post_data)
        else:
            self.set_status(400)
            return False

        self.redirect('/page/{0}.html'.format(post_data['slug'][0]))
Example #38
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mreply = MReply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):

        if url_str == '':
            return
        url_arr = url_str.split('/')

        if len(url_arr) == 1 and url_str.endswith('.html'):
            self.wiki(url_str.split('.')[0])
        elif url_str == 'find':
            self.to_find()
        elif url_arr[0] == 'find':
            self.find(url_arr[1])
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            self.to_modify(url_arr[1])

        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    def post(self, url_str=''):
        if url_str == '':
            return
        url_arr = url_str.split('/')
        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'find':
            self.post_find()
        elif url_arr[0] == 'add':
            self.add_post(url_arr[1])
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])

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

    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)

    def recent(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render(
            'tplite/post/all.html'.format(input),
            kwd=kwd,
            view=self.mpost.query_recent(),
            # rand_recs=self.get_random(),
            format_date=tools.format_date,
        )

    def refresh(self):
        kwd = {
            'pager': '',
            'title': '最近文档',
        }
        self.render(
            'tplite/post/all.html'.format(input),
            kwd=kwd,
            view=self.mpost.query_dated(60),
            format_date=tools.format_date,
            unescape=tornado.escape.xhtml_unescape,
        )

    def post_find(self):
        keyword = self.get_argument('keyword')
        self.find(keyword)

    def find(self, keyword):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '查找结果',
        }
        self.render(
            'tplite/post/find_list.html'.format(input),
            kwd=kwd,
            view=self.mpost.get_by_keyword(keyword),
        )

    def get_random(self):
        return self.mpost.query_random()

    def view_cat(self, cat_slug):
        for x in self.cats:
            if x.slug == cat_slug:
                search_str = ',{0},'.format(x.id_cat)
        dbdata = self.mpost.query_by_cat(search_str)
        self.render('tplite/post/all.html',
                    view=dbdata,
                    unescape=tornado.escape.xhtml_unescape)

    def wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.mpost.update_view_count_by_uid(dbdate.uid)
            self.viewit(dbdate)
        else:
            self.to_add(uid)

    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            'uid': uid,
            'pager': '',
        }
        self.render(
            'tplite/post/addwiki.html',
            kwd=kwd,
            tag_infos=self.mcat.query_all(),
        )

    @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()

        # if update the time
        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.mpost_hist.insert_data(raw_data)
        self.redirect('/post/{0}.html'.format(uid))

    @tornado.web.authenticated
    def update_catalog(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)

        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']:
            if post_data[key][0] == '':
                pass
            else:
                new_tag_arr.append(int(post_data[key][0]))
                self.mpost2catalog.add_record(uid, post_data[key][0],
                                              int(key[-1]))

        for cur_info in current_infos:
            if cur_info.catalog.uid not in new_tag_arr:
                self.mpost2catalog.delete_by_id(cur_info.uid)

                # self.redirect('/post/{0}.html'.format(uid))

    @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

        id_spec = a.id_spec
        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
            'id_spec': id_spec,
        }
        self.render(
            'tplite/post/modify.html',
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(),
            app2tag_info=self.mpost2catalog.query_by_id(id_rec),
            dbrec=a,
        )

    # @tornado.web.authenticated
    # def to_modify_catalog(self, id_rec):
    # # 用户具有管理权限,
    #     # 或
    #     # 文章是用户自己发布的。
    #     print(self.userinfo.privilege)
    #     if self.userinfo.privilege[4] == '1':
    #         pass
    #     else:
    #         print('Error')
    #         return False
    #     a = self.mpost.get_by_id(id_rec)
    #
    #     id_spec = a.id_spec
    #     kwd = {
    #         'pager': '',
    #         'cats': self.cats,
    #         'specs': self.specs,
    #         'view': a,
    #         'id_spec': id_spec,
    #     }
    #     self.render('tplite/post/edit_catalog.html', kwd=kwd, unescape=tornado.escape.xhtml_unescape,
    #                 tag_infos=self.mcat.query_all(),
    #                 app2tag_info=self.mpost2catalog.query_by_id(id_rec),
    #                 )

    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):
        cats = self.mpost2catalog.query_catalog(post_id)

        replys = self.mreply.get_by_id(post_id)

        if cats.count() == 0:
            cat_id = ''
        else:
            cat_id = cats.get().catalog
        kwd = {'pager': '', 'editable': self.editable(), 'cat_id': cat_id}

        self.render(
            'tplite/post/viewiki.html',
            view=post_id,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
            replys=replys,
        )

    @tornado.web.authenticated
    def add_post(self, id_post):
        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_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_catalog(uid)
        self.redirect('/post/{0}.html'.format(id_post))

    @tornado.web.authenticated
    def add_comment(self, id_post):
        if self.userinfo.privilege[0] == '1':
            pass
        else:
            return False

        post_data = {}

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

        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name

        comment = self.mreply.insert_data(id_post, post_data)

        if comment:

            output = {
                'pinglun': comment,
            }
        else:
            output = {
                'pinglun': 0,
            }

        return json.dump(output, self)

        #self.redirect('/post/{0}.html'.format(id_post))

    def get_zan(self, f_zan):
        zan = self.mreply.get_by_zan(f_zan)
        if zan:
            output = {
                'zan': zan.zan,
            }
        else:
            output = {''}
Example #39
0
class SpecialHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpage = MPage()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=""):

        url_str = url_str.strip()

        ip_arr = []
        if len(url_str) > 0:
            ip_arr = url_str.split(r'/')

        if url_str == '':
            self.index()
        elif url_str == 'add':
            self.to_add()
        elif len(ip_arr) == 1:
            self.list(url_str)

        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.to_edit(ip_arr[0])
        elif ip_arr[0] == 'delete':
            self.delete(ip_arr[1])
        else:
            self.render('html/404.html')

    def post(self, input=''):
        if len(input) > 0:
            ip_arr = input.split('/')
        else:
            return False

        if input == 'add':
            self.add()
        elif len(ip_arr) == 2 and ip_arr[1] == 'edit':
            self.edit(ip_arr[0])
        else:
            self.render('html/404.html')
        self.redirect('/spec/')

    @tornado.web.authenticated
    def edit(self, uid):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        self.mspec.update(uid, post_data)

    @tornado.web.authenticated
    def add(self):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)

        self.mspec.addata(post_data)

    @tornado.web.authenticated
    def to_edit(self, uid):
        spec_rec = self.mspec.get_by_id(uid)
        self.render('{0}/spec/edit.html'.format(self.tmpl_name),
                    spec_rec=spec_rec)

    @tornado.web.authenticated
    def to_add(self):
        self.render('{0}/spec/add.html'.format(self.tmpl_name))

    def list(self, spec_slug):
        '''
        列出专题信息,按某一分类
        '''
        page_rec = self.mspec.get_by_slug(spec_slug)
        kwd = {'editable': self.editable()}
        self.render(
            '{0}/spec/list.html'.format(self.tmpl_name),
            kwd=kwd,
            spec=page_rec,
            unescape=tornado.escape.xhtml_unescape,
            spec_recs=self.mpost.query_by_spec(page_rec.uid),
        )

    def index(self):
        kwd = {'editable': self.editable()}
        self.render(
            '{0}/spec/index.html'.format(self.tmpl_name),
            kwd=kwd,
            spec_recs=self.mspec.get_all(),
        )

    @tornado.web.authenticated
    def delete(self, del_id):
        is_deleted = self.mspec.delete(del_id)
        if is_deleted:
            self.redirect('/spec/')
        else:
            return False
Example #40
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

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

        if len(url_arr) == 1 and url_str.endswith('.html'):

            self.wiki(url_str.split('.')[0])
        # 弃用, /search 代替
        # elif url_arr[0] == 'find':
        # self.find(url_arr[1])
        # elif url_str == 'find':
        #     self.to_find()
        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])

        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd)

    def post(self, url_str=''):
        if url_str == '':
            return

        url_arr = url_str.split('/')

        if len(url_arr) == 1 and url_str.endswith('.html'):
            sig = url_str.split('.')[0]
            self.add_post()

        if url_arr[0] == 'modify':
            self.update(url_arr[1])
        elif url_str == 'find':
            self.post_find()
        elif url_str == 'add_document':
            self.user_add_post()
        elif url_arr[0] == 'add':
            self.add_post()
        else:
            self.redirect('/html/404.html')


    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render('tplite/post/find.html', topmenu='', kwd=kwd)


    def recent(self):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
        }
        self.render('tplite/post/all.html'.format(input),
                    kwd=kwd,
                    view=self.mpost.query_recent(),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
        )

    def refresh(self):

        kwd = {
            'pager': '',
            'title': '最近文档',
        }
        self.render('tplite/post/refresh.html',
                    kwd=kwd,
                    userinfo=self.userinfo,
                    view=self.mpost.query_dated(10),
                    format_date=tools.format_date,
                    unescape=tornado.escape.xhtml_unescape, )

    def post_find(self):
        keyword = self.get_argument('keyword')
        self.find(keyword)

    def find(self, keyword):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '查找结果',
        }
        self.render('tplite/post/find_list.html'.format(input),
                    kwd=kwd,
                    view=self.mpost.get_by_keyword(keyword),
        )


    def get_random(self):
        return self.mpost.query_random()

    def view_cat(self, cat_slug):
        for x in self.cats:
            if x.slug == cat_slug:
                search_str = ',{0},'.format(x.id_cat)
        dbdata = self.mpost.query_by_cat(search_str)
        self.render('tplite/post/all.html', view=dbdata, unescape=tornado.escape.xhtml_unescape)

    def wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.mpost.update_view_count_by_uid(dbdate.uid)
            self.viewit(uid)
        else:

            self.to_add(uid)

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

        }
        self.render('tplite/post/addwiki.html', topmenu='',
                    kwd=kwd,
                    tag_infos=self.mcat.query_all(),
                    userinfo = self.userinfo,
        )


    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {
            'cats': self.cats,
            'specs': self.specs,
            'uid': uid,
            'pager': '',
        }
        self.render('tplite/post/addwiki.html', kwd=kwd, tag_infos=self.mcat.query_all(), )

    @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))

    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)
        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.delete_by_id(cur_info.uid)

    @tornado.web.authenticated
    def update_catalog(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)

        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']:
            if post_data[key][0] == '':
                pass
            else:
                new_tag_arr.append(int(post_data[key][0]))
                self.mpost2catalog.add_record(uid, post_data[key][0], int(key[-1]))

        for cur_info in current_infos:
            if cur_info.catalog.uid not in new_tag_arr:
                self.mpost2catalog.delete_by_id(cur_info.uid)


    @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

        id_spec = a.id_spec
        kwd = {
            'pager': '',
            'cats': self.cats,
            'specs': self.specs,
            'id_spec': id_spec,
        }
        self.render('tplite/post/modify.html',
                    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,
        )

    # @tornado.web.authenticated
    # def to_modify_catalog(self, id_rec):
    # # 用户具有管理权限,
    # # 或
    # # 文章是用户自己发布的。
    # print(self.userinfo.privilege)
    # if self.userinfo.privilege[4] == '1':
    # pass
    #     else:
    #         print('Error')
    #         return False
    #     a = self.mpost.get_by_id(id_rec)
    #
    #     id_spec = a.id_spec
    #     kwd = {
    #         'pager': '',
    #         'cats': self.cats,
    #         'specs': self.specs,
    #         'view': a,
    #         'id_spec': id_spec,
    #     }
    #     self.render('tplite/post/edit_catalog.html', kwd=kwd, unescape=tornado.escape.xhtml_unescape,
    #                 tag_infos=self.mcat.query_all(),
    #                 app2tag_info=self.mpost2catalog.query_by_id(id_rec),
    #                 )

    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_cookie('last_post_uid')
        self.set_cookie('last_post_uid', post_id)

        if 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_uid(post_id)

        if rec == False:
            kwd = {
                'info': '您要找的云算应用不存在。',
            }
            self.render('html/404.html', kwd=kwd)
            return False

        if cats.count() == 0:
            cat_id = ''
        else:
            cat_id = cats.get().catalog
        kwd = {
            'pager': '',
            'editable': self.editable(),
            'cat_id': cat_id
        }

        self.render('tplite/post/viewiki.html',
                    view=rec,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    userinfo=self.userinfo,
                    tag_info=tag_info,
                    relations=self.mrel.get_app_relations(rec.uid),
                    replys=replys,

        )


    def add_relation(self, f_uid, t_uid):
        print('-'* 40)
        print(f_uid)
        print(t_uid)
        if False == self.mpost.get_by_uid(t_uid):
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        self.mrel.add_relation(f_uid, t_uid)
        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)

        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))
Example #41
0
class PageHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.muser = MUser()
        self.mpage = MPage()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    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_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')

    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,
            'specs': self.specs,
            '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,
                    )

    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)

    @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 #42
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mspec = SpesubModel()
        self.specs = self.mspec.get_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=""):

        if url_str == "":
            return
        url_arr = url_str.split("/")

        if len(url_arr) == 1 and url_str.endswith(".html"):

            self.wiki(url_str.split(".")[0])
        # 弃用, /search 代替
        # elif url_arr[0] == 'find':
        # self.find(url_arr[1])
        # elif url_str == 'find':
        #     self.to_find()
        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])

        else:
            kwd = {"info": "页面未找到"}
            self.render("html/404.html", kwd=kwd)

    def post(self, url_str=""):
        if url_str == "":
            return

        url_arr = url_str.split("/")

        if len(url_arr) == 1 and url_str.endswith(".html"):
            sig = url_str.split(".")[0]
            self.add_post()

        if url_arr[0] == "modify":
            self.update(url_arr[1])
        elif url_str == "find":
            self.post_find()
        elif url_str == "add_document":
            self.user_add_post()
        elif url_arr[0] == "add":
            self.add_post()
        else:
            self.redirect("/html/404.html")

    def to_find(self,):
        kwd = {"pager": ""}
        self.render("tplite/post/find.html", topmenu="", kwd=kwd)

    def recent(self):
        kwd = {"pager": "", "unescape": tornado.escape.xhtml_unescape, "title": "最近文档"}
        self.render(
            "tplite/post/all.html".format(input),
            kwd=kwd,
            view=self.mpost.query_recent(),
            format_date=tools.format_date,
            userinfo=self.userinfo,
        )

    def refresh(self):

        kwd = {"pager": "", "title": "最近文档"}
        self.render(
            "tplite/post/refresh.html",
            kwd=kwd,
            userinfo=self.userinfo,
            view=self.mpost.query_dated(10),
            format_date=tools.format_date,
            unescape=tornado.escape.xhtml_unescape,
        )

    def post_find(self):
        keyword = self.get_argument("keyword")
        self.find(keyword)

    def find(self, keyword):
        kwd = {"pager": "", "unescape": tornado.escape.xhtml_unescape, "title": "查找结果"}
        self.render("tplite/post/find_list.html".format(input), kwd=kwd, view=self.mpost.get_by_keyword(keyword))

    def get_random(self):
        return self.mpost.query_random()

    def view_cat(self, cat_slug):
        for x in self.cats:
            if x.slug == cat_slug:
                search_str = ",{0},".format(x.id_cat)
        dbdata = self.mpost.query_by_cat(search_str)
        self.render("tplite/post/all.html", view=dbdata, unescape=tornado.escape.xhtml_unescape)

    def wiki(self, uid):
        dbdate = self.mpost.get_by_id(uid)
        if dbdate:
            self.mpost.update_view_count_by_uid(dbdate.uid)
            self.viewit(uid)
        else:

            self.to_add(uid)

    def to_add_document(self,):
        kwd = {"pager": "", "cats": self.cats, "specs": self.specs, "uid": ""}
        self.render(
            "tplite/post/addwiki.html", topmenu="", kwd=kwd, tag_infos=self.mcat.query_all(), userinfo=self.userinfo
        )

    @tornado.web.authenticated
    def to_add(self, uid):
        kwd = {"cats": self.cats, "specs": self.specs, "uid": uid, "pager": ""}
        self.render("tplite/post/addwiki.html", kwd=kwd, tag_infos=self.mcat.query_all())

    @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))

    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)
        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.delete_by_id(cur_info.uid)

    @tornado.web.authenticated
    def update_catalog(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)

        current_infos = self.mpost2catalog.query_by_id(uid)
        new_tag_arr = []
        for key in ["tag1", "tag2", "tag3", "tag4", "tag5"]:
            if post_data[key][0] == "":
                pass
            else:
                new_tag_arr.append(int(post_data[key][0]))
                self.mpost2catalog.add_record(uid, post_data[key][0], int(key[-1]))

        for cur_info in current_infos:
            if cur_info.catalog.uid not in new_tag_arr:
                self.mpost2catalog.delete_by_id(cur_info.uid)

    @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

        id_spec = a.id_spec
        kwd = {"pager": "", "cats": self.cats, "specs": self.specs, "id_spec": id_spec}
        self.render(
            "tplite/post/modify.html",
            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,
        )

    # @tornado.web.authenticated
    # def to_modify_catalog(self, id_rec):
    # # 用户具有管理权限,
    # # 或
    # # 文章是用户自己发布的。
    # print(self.userinfo.privilege)
    # if self.userinfo.privilege[4] == '1':
    # pass
    #     else:
    #         print('Error')
    #         return False
    #     a = self.mpost.get_by_id(id_rec)
    #
    #     id_spec = a.id_spec
    #     kwd = {
    #         'pager': '',
    #         'cats': self.cats,
    #         'specs': self.specs,
    #         'view': a,
    #         'id_spec': id_spec,
    #     }
    #     self.render('tplite/post/edit_catalog.html', kwd=kwd, unescape=tornado.escape.xhtml_unescape,
    #                 tag_infos=self.mcat.query_all(),
    #                 app2tag_info=self.mpost2catalog.query_by_id(id_rec),
    #                 )

    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_cookie("last_post_uid")
        self.set_cookie("last_post_uid", post_id)

        if 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_uid(post_id)

        if rec == False:
            kwd = {"info": "您要找的云算应用不存在。"}
            self.render("html/404.html", kwd=kwd)
            return False

        if cats.count() == 0:
            cat_id = ""
        else:
            cat_id = cats.get().catalog
        kwd = {"pager": "", "editable": self.editable(), "cat_id": cat_id}

        self.render(
            "tplite/post/viewiki.html",
            view=rec,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
            tag_info=tag_info,
            relations=self.mrel.get_app_relations(rec.uid),
            replys=replys,
        )

    def add_relation(self, f_uid, t_uid):
        print("-" * 40)
        print(f_uid)
        print(t_uid)
        if False == self.mpost.get_by_uid(t_uid):
            return False
        if f_uid == t_uid:
            """
            关联其本身
            """
            return False
        self.mrel.add_relation(f_uid, t_uid)
        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)

        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))