Example #1
0
def build_whoosh_database():
    analyzer = ChineseAnalyzer()
    schema = Schema(title=TEXT(stored=True, analyzer=analyzer),
                    type=TEXT(stored=True),
                    link=ID(stored=True),
                    content=TEXT(stored=True, analyzer=analyzer))
    ix = create_in(whoosh_database, schema)

    writer = ix.writer()

    uu = MApp()

    tt = uu.get_all()
    for rec in tt:
        text2 = html2text.html2text(tornado.escape.xhtml_unescape(
            rec.cnt_html))
        writer.add_document(
            title=rec.title,
            type='<span style="color:red;">[信息]</span>',
            link='/info/{0}'.format(rec.uid),
            content=text2,
        )

    mpost = MPost()
    recs = mpost.query_all()
    for rec in recs:
        text2 = html2text.html2text(tornado.escape.xhtml_unescape(
            rec.cnt_html))
        print(text2)
        writer.add_document(title=rec.title,
                            type='<span style="color:blue;">[文档]</span>',
                            link='/post/{0}.html'.format(rec.uid),
                            content=text2)

    writer.commit()
Example #2
0
def build_whoosh_database():
    analyzer = ChineseAnalyzer()
    schema = Schema(title=TEXT(stored=True, analyzer=analyzer), type=TEXT(stored=True), link=ID(stored=True),
                    content=TEXT(stored=True, analyzer=analyzer))
    ix = create_in(whoosh_database, schema)

    writer = ix.writer()


    uu = MApp()

    tt = uu.get_all()
    for rec in tt:
        text2 = html2text.html2text(tornado.escape.xhtml_unescape(rec.cnt_html))
        writer.add_document(
             title=rec.title,
             type='<span style="color:red;">[信息]</span>',
             link='/info/{0}'.format(rec.uid),
             content= text2,
        )

    mpost = MPost()
    recs = mpost.query_all()
    for rec in recs:
        text2 = html2text.html2text(tornado.escape.xhtml_unescape(rec.cnt_html))
        print(text2)
        writer.add_document(
            title=rec.title,
            type='<span style="color:blue;">[文档]</span>',
            link='/post/{0}.html'.format(rec.uid),
            content=text2
        )


    writer.commit()
Example #3
0
 def render(self, current_id):
     self.mpost = MPost()
     prev_record = self.mpost.get_previous_record(current_id)
     if prev_record is None:
         outstr = '<a>已经是最后一篇了</a>'
     else:
         outstr = '''<a href="/post/{0}.html">上一篇</a>'''.format(
             prev_record.uid, prev_record.title)
     return outstr
Example #4
0
 def render(self, current_id):
     self.mpost = MPost()
     next_record = self.mpost.get_next_record(current_id)
     if next_record is None:
         outstr = '<a>已经是最新一篇了</a>'
     else:
         outstr = '''<a href="/post/{0}.html">下一篇</a>'''.format(
             next_record.uid)
     return outstr
Example #5
0
 def render(self, cat_id, list_num):
     self.mpost = MPost()
     recs = self.mpost.query_by_cat(cat_id, list_num)
     out_str = ''
     for rec in recs:
         tmp_str = '''<li><a href="/{0}">{1}</a></li>'''.format(
             rec.title, rec.title)
         out_str += tmp_str
     return out_str
Example #6
0
 def render(self, num, recent, with_date=True, with_catalog=True):
     self.mpost = MPost()
     recs = self.mpost.query_recent_most(num, recent)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string('doc/modules/post_list.html',
                               recs=recs,
                               kwd=kwd)
Example #7
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mpost_hist = MPostHist()
     self.mpost2catalog = MPost2Catalog()
     self.mpost2reply = MPost2Reply()
     self.mpost2label = MPost2Label()
     self.mrel = MRelation()
Example #8
0
 def render(self, cat_id, num, with_date=True, with_catalog=True):
     self.mpost = MPost()
     recs = self.mpost.query_cat_random(cat_id, num)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string('doc/modules/post_list.html',
                               recs=recs,
                               kwd=kwd)
Example #9
0
 def render(self, num=10, with_catalog=True, with_date=True):
     self.mpost = MPost()
     recs = self.mpost.query_recent(num)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string(
         'doc/modules/post_list.html',
         recs=recs,
         unescape=tornado.escape.xhtml_unescape,
         kwd=kwd,
     )
Example #10
0
class showout_recent(tornado.web.UIModule):
    def render(self,
               cat_id,
               num=10,
               with_catalog=True,
               with_date=True,
               width=160,
               height=120):
        self.mpost = MPost()
        self.mpost2cat = MPost2Catalog()
        recs = self.mpost.query_cat_recent(cat_id, num)

        kwd = {
            'with_catalog': with_catalog,
            'with_date': with_date,
            'width': width,
            'height': height,
        }

        return self.render_string(
            'doc/modules/showout_list.html',
            recs=recs,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
        )
Example #11
0
class IndexHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.mpage = MPage()
        self.mlink = MLink()

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

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

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

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

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

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

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

        if is_deleted:
            output = {
                'del_info ': 1,
            }
        else:
            output = {
                'del_info ': 0,
            }
        return json.dump(output, self)
Example #14
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MInforCatalog()
     # self.cats = self.mcat.query_all()
     # self.mpost2catalog = MPost2Catalog()
     self.ysearch = yunsearch()
Example #15
0
 def render(self, current_id):
     self.mpost = MPost()
     next_record = self.mpost.get_next_record(current_id)
     if next_record is None:
         outstr = '<a>已经是最新一篇了</a>'
     else:
         outstr = '''<a href="/post/{0}.html">下一篇</a>'''.format(next_record.uid)
     return outstr
Example #16
0
 def render(self, cat_id, list_num):
     self.mpost = MPost()
     recs = self.mpost.query_by_cat(cat_id, list_num)
     out_str = ''
     for rec in recs:
         tmp_str = '''<li><a href="/{0}">{1}</a></li>'''.format(rec.title, rec.title)
         out_str += tmp_str
     return out_str
Example #17
0
 def render(self, current_id):
     self.mpost = MPost()
     prev_record = self.mpost.get_previous_record(current_id)
     if prev_record is None:
         outstr = '<a>已经是最后一篇了</a>'
     else:
         outstr = '''<a href="/post/{0}.html">上一篇</a>'''.format(prev_record.uid, prev_record.title)
     return outstr
Example #18
0
 def render(self, num, recent, with_date=True, with_catalog=True):
     self.mpost = MPost()
     recs = self.mpost.query_recent_most(num, recent)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string('tmpl_torlite/modules/post_list.html', recs=recs, kwd=kwd)
Example #19
0
class post_most_view(tornado.web.UIModule):
    def render(self, num, with_date=True, with_catalog=True):
        self.mpost = MPost()
        recs = self.mpost.query_most(num)
        kwd = {
            'with_date': with_date,
            'with_catalog': with_catalog,
        }
        return self.render_string('tmpl_torlite/modules/post_list.html', recs=recs, kwd=kwd)
Example #20
0
 def render(self, cat_id, num, with_date=True, with_catalog=True):
     self.mpost = MPost()
     recs = self.mpost.query_cat_random(cat_id, num)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string('doc/modules/post_list.html',
                               recs=recs, kwd=kwd)
Example #21
0
class post_random(tornado.web.UIModule):
    def render(self, num, with_date=True, with_catalog=True):
        self.mpost = MPost()
        recs = self.mpost.query_random(num)
        kwd = {
            'with_date': with_date,
            'with_catalog': with_catalog,
        }
        return self.render_string('doc/modules/post_list.html',
                                  recs=recs,
                                  kwd=kwd)
Example #22
0
 def render(self, num=10, with_catalog=True, with_date=True):
     self.mpost = MPost()
     recs = self.mpost.query_recent(num)
     kwd = {
         'with_date': with_date,
         'with_catalog': with_catalog,
     }
     return self.render_string('tmpl_torlite/modules/post_list.html',
                               recs=recs,
                               unescape=tornado.escape.xhtml_unescape,
                               kwd=kwd, )
Example #23
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mpost_hist = MPostHist()
     self.mpost2catalog = MPost2Catalog()
     self.mpost2reply = MPost2Reply()
     self.mapp2tag = MPost2Label()
     self.mrel = MRelation()
     self.tmpl_router = 'post'
Example #24
0
 def render(self, cat_id, num=10, with_catalog=True, with_date=True):
     self.mpost = MPost()
     self.mpost2cat = MPost2Catalog()
     recs = self.mpost.query_cat_recent(cat_id, num)
     kwd = {
         'with_catalog': with_catalog,
         'with_date': with_date,
     }
     return self.render_string('doc/modules/post_list.html',
                               recs=recs,
                               unescape=tornado.escape.xhtml_unescape,
                               kwd=kwd, )
Example #25
0
    def render(
        self,
        uid,
        num,
    ):
        self.mpost = MPost()
        self.relation = MRelApp2Post()
        kwd = {
            'app_f': 'info',
            'app_t': 'post',
            'uid': uid,
        }
        rel_recs = self.relation.get_app_relations(uid, num)

        rand_recs = self.mpost.query_random(num - rel_recs.count() + 2)

        return self.render_string(
            'infor/modules/relation_app2post.html',
            relations=rel_recs,
            rand_recs=rand_recs,
            kwd=kwd,
        )
class RelHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mapp = MApp()
        self.mpost = MPost()
        self.rel_post2app = MRelPost2App()
        self.rel_app2post = MRelApp2Post()

    def get(self, url_str=''):
        if len(url_str) > 0:
            ip_arr = url_str.split('/')
        else:
            return False
        if len(ip_arr) == 2:
            self.add_relation(ip_arr)

    def check_app(self, tt, uid):

        if tt == 'post':
            if False == self.mpost.get_by_id(uid):
                return False
        if tt == 'app':
            if False == self.mapp.get_by_uid(uid):
                return False
        return True

    def add_relation(self, url_arr):
        if self.check_app(url_arr[0], url_arr[1]):
            pass
        else:
            return False

        last_post_id = self.get_secure_cookie('last_post_uid')
        if last_post_id:
            last_post_id = last_post_id.decode('utf-8')

        last_app_id = self.get_secure_cookie('use_app_uid')
        if last_app_id:
            last_app_id = last_app_id.decode('utf-8')

        if url_arr[0] == 'info':
            if last_post_id:
                self.rel_post2app.add_relation(last_post_id, url_arr[1], 2)
                self.rel_app2post.add_relation(url_arr[1], last_post_id, 1)

        if url_arr[0] == 'post':
            if last_app_id:
                self.rel_app2post.add_relation(last_app_id, url_arr[1], 2)
                self.rel_post2app.add_relation(url_arr[1], last_app_id, 1)
Example #27
0
class CategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost2catalog = MPost2Catalog()

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

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

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

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

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

    def get_random(self):
        return self.mpost.query_random()
Example #28
0
class post_category_recent(tornado.web.UIModule):
    def render(self, cat_id, num=10, with_catalog=True, with_date=True):
        self.mpost = MPost()
        self.mpost2cat = MPost2Catalog()
        recs = self.mpost.query_cat_recent(cat_id, num)
        kwd = {
            'with_catalog': with_catalog,
            'with_date': with_date,
        }
        return self.render_string(
            'doc/modules/post_list.html',
            recs=recs,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
        )
Example #29
0
    def render(self, uid, num, ):
        self.mpost = MPost()
        self.relation = MRelApp2Post()
        kwd = {
            'app_f': 'info',
            'app_t': 'post',
            'uid': uid,
        }
        rel_recs = self.relation.get_app_relations(uid, num)

        rand_recs = self.mpost.query_random(num - rel_recs.count() + 2)

        return self.render_string('infor/modules/relation_app2post.html',
                                  relations= rel_recs,
                                  rand_recs = rand_recs,
                                  kwd=kwd, )
Example #30
0
    def render(self, cat_id, num=10, with_catalog=True, with_date=True, width=160, height=120):
        self.mpost = MPost()
        self.mpost2cat = MPost2Catalog()
        recs = self.mpost.query_cat_recent(cat_id, num)

        kwd = {
            'with_catalog': with_catalog,
            'with_date': with_date,
            'width': width,
            'height': height,
        }

        return self.render_string('tmpl_torlite/modules/showout_list.html',
                                  recs=recs,
                                  unescape=tornado.escape.xhtml_unescape,
                                  kwd=kwd, )
Example #31
0
class CategoryHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost2catalog = MPost2Catalog()


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

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

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

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

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

    def get_random(self):
        return self.mpost.query_random()
Example #32
0
 def setup(self):
     print('setup 方法执行于本类中每条用例之前')
     self.uu = MPost()
     self.raw_count = self.uu.get_counts()
     self.post_title = 'ccc'
     self.uid = tools.get_uu4d()
Example #33
0
class TestPost():
    def setup(self):
        print('setup 方法执行于本类中每条用例之前')
        self.uu = MPost()
        self.raw_count = self.uu.get_counts()
        self.post_title = 'ccc'
        self.uid = tools.get_uu4d()

    def test_insert(self):
        raw_count = self.uu.get_counts()

        post_data = {
            'title': [self.post_title],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        self.uu.insert_data(self.uid, post_data)
        new_count = self.uu.get_counts()

        tt = self.uu.get_by_uid(self.uid)

        assert tt.title == post_data['title'][0]
        assert tt.cnt_md == tornado.escape.xhtml_unescape(
            post_data['cnt_md'][0])
        assert tt.cnt_html == tools.markdown2html(post_data['cnt_md'][0])
        assert raw_count + 1 == new_count

    def test_insert_2(self):
        '''Wiki insert: Test invalid title'''

        post_data = {
            'title': [''],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        uu = self.uu.insert_data(self.uid, post_data)
        assert uu == False

        post_data = {
            'title': ['1'],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        uu = self.uu.insert_data(self.uid, post_data)
        assert uu == False

        post_data = {
            'title': ['天'],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        uu = self.uu.insert_data(self.uid, post_data)
        assert uu == False

    def test_get_by_title(self):

        post_data = {
            'title': [self.post_title],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        uid = self.uu.insert_data(self.uid, post_data)

        ss = self.uu.get_by_uid(uid)
        assert ss.title == post_data['title'][0]

    def test_get_by_title2(self):
        '''Test Wiki title with SPACE'''

        post_data = {
            'title': ['  ' + self.post_title + '  '],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        uid = self.uu.insert_data(self.uid, post_data)

        ss = self.uu.get_by_uid(uid)
        assert ss.title == self.post_title

    def test_update_by_uid(self):
        uid = self.uid
        post_data = {
            'title': 'a123sdf',
            'cnt_md': '1212sadf',
            'user_name': 'asdf',
            'logo': 'asqwef',
            'keywords': 'aseef',
        }
        self.uu.insert_data(uid, post_data)
        new_count = self.uu.get_counts()

        #assert self.raw_count + 1 == new_count

        post_data2 = {
            'title': 'a123sdf',
            'cnt_md': '1212sadf',
            'user_name': 'asdf',
            'logo': '1111asqwef',
            'keywords': '111aseef',
        }

        self.uu.update(uid, post_data2)

        new_count = self.uu.get_counts()

        #assert self.raw_count + 1 == new_count

        tt = self.uu.get_by_uid(uid)

        #assert tt.title != post_data['title'][0]
        #assert tt.cnt_md != post_data['cnt_md'][0]
        #assert tt.user_name != int(post_data['user_name'][0])
        #assert tt.logo != post_data['logo'][0]
        #assert tt.keywords != post_data['keywords'][0]
#
#assert tt.title == post_data['title'][0]
#assert tt.cnt_md == post_data['cnt_md'][0]
#assert tt.user_name == int(post_data['user_name'][0])
#assert tt.logo == post_data['logo'][0]
#assert tt.keywords == post_data['keywords'][0]

    def test_query_cat_random(self):
        self.uu.query_cat_random('')
        assert True

    def test_query_recent(self):
        self.uu.query_recent()
        assert True

    def test_query_all(self):
        self.uu.query_all()
        assert True

    def test_query_keywords_empty(self):
        self.uu.query_keywords_empty()
        assert True

    def test_query_dated(self):
        self.uu.query_dated()
        assert True

    def test_query_most_pic(self):
        self.uu.query_most_pic(3)
        assert True

    #def test_get_num_by_cat(self):
    #    self.uu.get_num_by_cat(3)
    #    assert True

    def test_query_cat_recent(self):
        self.uu.query_cat_recent(3, 3)
        assert True

    def test_query_most(self):
        self.uu.query_most()
        assert True

    #def test_query_cat_by_pager(self):
    #    self.uu.query_cat_by_pager()
    #    assert True

    def test_update_keywords(self):
        self.uu.update_keywords(self.uid, 'adf')
        assert True

    def test_update_view_count_by_uid(self):
        uid = tools.get_uu4d()
        post_data = {
            'title': [self.post_title],
            'cnt_md': '## adslkfjasdf\n lasdfkjsadf',
            'user_name': 'Tome',
            'view_count': 1,
            'logo': '/static/',
            'keywords': 'sdf',
        }
        self.uu.insert_data(uid, post_data)

        rec = self.uu.get_by_uid(uid)

        viewcount0 = rec.view_count
        assert viewcount0 == 1
        for x in range(100):
            self.uu.update_view_count_by_uid(rec.uid)

        viewcount1 = self.uu.get_by_uid(uid).view_count

        assert viewcount1 == 101

    def test_upate(self):
        assert True

    def tearDown(self):
        print("function teardown")
        tt = self.uu.get_by_uid(self.uid)
        if tt:
            self.uu.delete(tt.uid)
Example #34
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mpost2catalog = MPost2Catalog()
Example #35
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mapp2tag = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post'

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

        if url_str == '':
            self.recent()
        elif len(url_arr) == 1 and url_str.endswith('.html'):
            self.wiki(url_str.split('.')[0])
        elif url_str == 'add_document':
            self.to_add_document()
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            self.to_modify(url_arr[1])
        elif url_arr[0] == 'delete':
            self.delete(url_arr[1])
        elif url_arr[0] == 'ajax_count_plus':
            self.ajax_count_plus(url_arr[1])
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render('html/404.html', kwd=kwd,
                        userinfo=self.userinfo, )

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

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

    def ajax_count_plus(self, uid):
        output = {
            'status': 1 if self.mpost.update_view_count_by_uid(uid) else 0,
        }

        return json.dump(output, self)

    def recent(self, with_catalog=True, with_date=True):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
            'with_catalog': with_catalog,
            'with_date': with_date,
        }
        self.render('{0}/{1}/post_list.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    view=self.mpost.query_recent(),
                    view_all=self.mpost.query_all(),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    )

    def refresh(self):

        kwd = {
            'pager': '',
            'title': '最近文档',
        }
        self.render('{0}/{1}/post_list.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    userinfo=self.userinfo,
                    view=self.mpost.query_dated(10),
                    format_date=tools.format_date,
                    unescape=tornado.escape.xhtml_unescape,
                    cfg=config.cfg, )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        }
        self.render('{0}/{1}/post_edit.html'.format(self.tmpl_name, self.tmpl_router),
                    kwd=kwd,
                    unescape=tornado.escape.xhtml_unescape,
                    tag_infos=self.mcat.query_all(),
                    app2label_info=self.mapp2tag.get_by_id(id_rec),
                    app2tag_info=self.mpost2catalog.query_by_id(id_rec),
                    dbrec=a,
                    userinfo=self.userinfo,
                    cfg=config.cfg,
                    )

    def get_cat_str(self, cats):
        cat_arr = cats.split(',')
        out_str = ''
        for xx in self.cats:
            if str(xx.uid) in cat_arr:
                tmp_str = '''<li><a href="/category/{0}" style="margin:10px auto;"> {1} </a></li>
                '''.format(xx.slug, tornado.escape.xhtml_escape(xx.name))
                out_str += tmp_str

        return (out_str)

    def get_cat_name(self, id_cat):
        for x in self.cats:
            if x['id_cat'] == id_cat:
                return (x['name'])

    def viewit(self, post_id):
        last_post_id = self.get_secure_cookie('last_post_uid')
        if last_post_id:
            last_post_id = last_post_id.decode('utf-8')
        self.set_secure_cookie('last_post_uid', post_id)

        if last_post_id and self.mpost.get_by_id(last_post_id):
            self.add_relation(last_post_id, post_id)

        cats = self.mpost2catalog.query_catalog(post_id)
        replys = self.mpost2reply.get_by_id(post_id)
        tag_info = self.mapp2tag.get_by_id(post_id)

        rec = self.mpost.get_by_id(post_id)

        if not rec:
            kwd = {
                'info': '您要查看的页面不存在。',
            }
            self.render('html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo)
            return False

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

        rel_recs = self.mrel.get_app_relations(rec.uid, 4)

        rand_recs = self.mpost.query_random(4 - rel_recs.count() + 2)

        self.render('{0}/{1}/post_view.html'.format(self.tmpl_name, self.tmpl_router),
                    view=rec,
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    userinfo=self.userinfo,
                    tag_info=tag_info,
                    relations=rel_recs,
                    rand_recs=rand_recs,
                    replys=replys,
                    cfg=config.cfg,
                    )

    def add_relation(self, f_uid, t_uid):
        if False == self.mpost.get_by_id(t_uid):
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        # 双向关联,但权重不一样.
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True

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

        post_data['user_name'] = self.get_current_user()
        id_post = post_data['uid'][0]
        cur_post_rec = self.mpost.get_by_id(id_post)
        if cur_post_rec is None:
            uid = self.mpost.insert_data(id_post, post_data)
            self.update_tag(uid)
            self.update_catalog(uid)
        self.redirect('/post/{0}.html'.format(id_post))

    @tornado.web.authenticated
    def user_add_post(self):
        if self.userinfo.privilege[1] == '1':
            pass
        else:
            return False
        post_data = {}

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

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

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

        cur_uid = tools.get_uu5d()
        while self.mpost.get_by_id(cur_uid):
            cur_uid = tools.get_uu5d()

        uid = self.mpost.insert_data(cur_uid, post_data)
        self.update_tag(uid)
        self.update_catalog(uid)
        self.redirect('/post/{0}.html'.format(cur_uid))

    @tornado.web.authenticated
    def delete(self, del_id):
        is_deleted = self.mpost.delete(del_id)
        if self.tmpl_router == "post":
            if is_deleted:
                self.redirect('/post/recent')
            else:
                return False
        else:
            if is_deleted:
                output = {
                    'del_info ': 1,
                }
            else:
                output = {
                    'del_info ': 0,
                }
            return json.dump(output, self)
Example #36
0
 def initialize(self):
     self.init()
     self.mequa = MPost()
     self.mtag = MLabel()
     self.mapp2tag = MPost2Label()
Example #37
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.mpage = MPage()
     self.mlink = MLink()
Example #38
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCatalog()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2reply = MPost2Reply()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_router = 'post'

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

        if url_str == '':
            self.recent()
        elif len(url_arr) == 1 and url_str.endswith('.html'):
            self.wiki(url_str.split('.')[0])
        elif url_str == 'add_document':
            self.to_add_document()
        elif url_str == 'recent':
            self.recent()
        elif url_str == 'refresh':
            self.refresh()
        elif (url_arr[0] == 'modify'):
            self.to_modify(url_arr[1])
        elif url_arr[0] == 'delete':
            self.delete(url_arr[1])
        elif url_arr[0] == 'ajax_count_plus':
            self.ajax_count_plus(url_arr[1])
        else:
            kwd = {
                'info': '页面未找到',
            }
            self.render(
                'html/404.html',
                kwd=kwd,
                userinfo=self.userinfo,
            )

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

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

    def ajax_count_plus(self, uid):
        output = {
            'status': 1 if self.mpost.update_view_count_by_uid(uid) else 0,
        }

        return json.dump(output, self)

    def recent(self, with_catalog=True, with_date=True):
        kwd = {
            'pager': '',
            'unescape': tornado.escape.xhtml_unescape,
            'title': '最近文档',
            'with_catalog': with_catalog,
            'with_date': with_date,
        }
        self.render(
            'doc/{0}/post_list.html'.format(self.tmpl_router),
            kwd=kwd,
            view=self.mpost.query_recent(),
            view_all=self.mpost.query_all(),
            format_date=tools.format_date,
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

    def __could_edit(self, postid):
        post_rec = self.mpost.get_by_uid(postid)
        if not post_rec:
            return False
        if self.check_doc_priv(
                self.userinfo
        )['EDIT'] or post_rec.user_name == self.userinfo.user_name:
            return True
        else:
            return False

    def refresh(self):

        kwd = {
            'pager': '',
            'title': '最近文档',
        }
        self.render(
            'doc/{0}/post_list.html'.format(self.tmpl_router),
            kwd=kwd,
            userinfo=self.userinfo,
            view=self.mpost.query_dated(10),
            format_date=tools.format_date,
            unescape=tornado.escape.xhtml_unescape,
            cfg=config.cfg,
        )

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

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

    def to_add_document(self, ):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        kwd = {
            'pager': '',
            'cats': self.cats,
            'uid': '',
        }
        self.render(
            'doc/{0}/post_add.html'.format(self.tmpl_router),
            kwd=kwd,
            tag_infos=self.mcat.query_all(),
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

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

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

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_name'] = self.get_current_user()
        is_update_time = True if post_data['is_update_time'][
            0] == '1' else False
        self.mpost.update(uid, post_data, update_time=is_update_time)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.mpost_hist.insert_data(self.mpost.get_by_id(uid))
        self.redirect('/post/{0}.html'.format(uid))

    @tornado.web.authenticated
    def update_tag(self, signature):
        current_tag_infos = self.mpost2label.get_by_id(signature)
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        if 'tags' in post_data:
            pass
        else:
            return False

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

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

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

    @tornado.web.authenticated
    def update_catalog(self, uid):

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

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

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

        kwd = {
            'pager': '',
            'cats': self.cats,
        }
        self.render(
            'doc/{0}/post_edit.html'.format(self.tmpl_router),
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(),
            app2label_info=self.mpost2label.get_by_id(id_rec),
            app2tag_info=self.mpost2catalog.query_by_id(id_rec),
            dbrec=self.mpost.get_by_id(id_rec),
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

    def get_cat_str(self, cats):
        cat_arr = cats.split(',')
        out_str = ''
        for xx in self.cats:
            if str(xx.uid) in cat_arr:
                tmp_str = '''<li><a href="/category/{0}" style="margin:10px auto;"> {1} </a></li>
                '''.format(xx.slug, tornado.escape.xhtml_escape(xx.name))
                out_str += tmp_str

        return (out_str)

    def get_cat_name(self, id_cat):
        for x in self.cats:
            if x['id_cat'] == id_cat:
                return (x['name'])

    def viewit(self, post_id):
        last_post_id = self.get_secure_cookie('last_post_uid')
        if last_post_id:
            last_post_id = last_post_id.decode('utf-8')
        self.set_secure_cookie('last_post_uid', post_id)

        if last_post_id and self.mpost.get_by_id(last_post_id):
            self.add_relation(last_post_id, post_id)

        cats = self.mpost2catalog.query_entry_catalog(post_id)
        replys = self.mpost2reply.get_by_id(post_id)
        tag_info = self.mpost2label.get_by_id(post_id)

        rec = self.mpost.get_by_id(post_id)

        if not rec:
            kwd = {
                'info': '您要查看的页面不存在。',
            }
            self.render('html/404.html', kwd=kwd, userinfo=self.userinfo)
            return False

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

        rel_recs = self.mrel.get_app_relations(rec.uid, 4)

        rand_recs = self.mpost.query_random(4 - rel_recs.count() + 2)

        self.render(
            'doc/{0}/post_view.html'.format(self.tmpl_router),
            view=rec,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
            tag_info=tag_info,
            relations=rel_recs,
            rand_recs=rand_recs,
            replys=replys,
            cfg=config.cfg,
        )

    def add_relation(self, f_uid, t_uid):
        if self.mpost.get_by_id(t_uid) is False:
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        # 双向关联,但权重不一样.
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True

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

        post_data['user_name'] = self.userinfo.user_name
        id_post = post_data['uid'][0]
        cur_post_rec = self.mpost.get_by_id(id_post)
        if cur_post_rec is None:
            uid = self.mpost.insert_data(id_post, post_data)
            self.update_tag(uid)
            self.update_catalog(uid)
        self.redirect('/post/{0}.html'.format(id_post))

    @tornado.web.authenticated
    def user_add_post(self):
        if self.check_doc_priv(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = {}

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

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

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

        cur_uid = tools.get_uu5d()
        while self.mpost.get_by_id(cur_uid):
            cur_uid = tools.get_uu5d()

        uid = self.mpost.insert_data(cur_uid, post_data)
        self.update_tag(uid)
        self.update_catalog(uid)
        self.redirect('/post/{0}.html'.format(cur_uid))

    @tornado.web.authenticated
    def delete(self, del_id):
        if self.check_doc_priv(self.userinfo)['DELETE']:
            pass
        else:
            return False
        is_deleted = self.mpost.delete(del_id)
        if self.tmpl_router == "post":
            if is_deleted:
                self.redirect('/post/recent')
            else:
                return False
        else:
            if is_deleted:
                output = {
                    'del_info ': 1,
                }
            else:
                output = {
                    'del_info ': 0,
                }
            return json.dump(output, self)
 def initialize(self):
     self.init()
     self.mapp = MApp()
     self.mpost = MPost()
     self.rel_post2app = MRelPost2App()
     self.rel_app2post = MRelApp2Post()
Example #40
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.cats = self.mcat.query_all()
     self.mpost2catalog = MPost2Catalog()
Example #41
0
 def initialize(self):
     self.init()
     self.mpost = MPost()
     self.mcat = MCatalog()
     self.mpage = MPage()
     self.mlink = MLink()