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 = MInfor()

    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()
Exemple #2
0
    def test_query_all(self):

        kwargs = {

            'user_name': '7788',
            'kind': '2',

        }
        self.add_message(**kwargs)

        kwargs = {
            'order_by_create': True,
            'kind': '2',
            'limit': 100
        }
        pp = MPost.query_all(**kwargs)
        TF = False
        for i in range(pp.count()):
            if pp[i].uid == self.uid:
                assert pp[i].user_name == '7788'
                TF = True

            else:
                continue
        assert TF == True
        self.tearDown()
Exemple #3
0
def check_tag():
    '''
    Checking the post of error tags.
    '''
    print('Checking tag error: ')
    tstr = ''
    idx = 1
    for kind in config.router_post.keys():
        posts = MPost.query_all(kind=kind, limit=20000)

        for post in posts:

            p_catinfo = None

            post2catinfo = MPost2Catalog.get_first_category(post.uid)
            if post2catinfo:
                catinfo = MCategory.get_by_uid(post2catinfo.tag_id)
                if catinfo:
                    p_catinfo = MCategory.get_by_uid(catinfo.pid)

            if post.extinfo.get('def_cat_pid') and post.extinfo.get(
                    'gcat0') and p_catinfo:

                pass
            else:
                the_url0 = '{site_url}/{kind_url}/{uid}'.format(
                    site_url=config.SITE_CFG['site_url'],
                    kind_url=config.router_post[post.kind],
                    uid=post.uid)

                the_url = '{site_url}/{kind_url}/_edit/{uid}'.format(
                    site_url=config.SITE_CFG['site_url'],
                    kind_url=config.router_post[post.kind],
                    uid=post.uid)
                the_url2 = '{site_url}/{kind_url}/_edit_kind/{uid}'.format(
                    site_url=config.SITE_CFG['site_url'],
                    kind_url=config.router_post[post.kind],
                    uid=post.uid)
                req = requests.get(the_url0)

                if req.status_code == 200:
                    pass
                else:
                    print(the_url0)
                    tstr = tstr + DT_STR.format(
                        idx=str(idx).zfill(2),
                        url0=the_url0,
                        code=req.status_code,
                        edit_link=the_url,
                        edit_tag_link=the_url2,
                    )
                    idx = idx + 1

    time_local = time.localtime(timestamp())
    with open(
            'xx_err_tag_{d}.html'.format(
                d=str(time.strftime("%Y_%m_%d", time_local))), 'w') as fileo:
        fileo.write(HTML_TMPL.format(cnt=tstr))
    print('Checking 200 finished.')
Exemple #4
0
def gen_post_map():
    mpost = MPost()
    with open(sitemap_file, 'a') as fo:
        for kind_key in router_post:
            recent_posts = mpost.query_all(kind=kind_key)
            for recent_post in recent_posts:
                url = os.path.join(SITE_CFG['site_url'],
                                   router_post[recent_post.kind],
                                   recent_post.uid)
                fo.write('{url}\n'.format(url=url))
Exemple #5
0
def gen_post_map(file_name, ext_url=''):
    '''
    Generate the urls for posts.
    :return: None
    '''
    with open(file_name, 'a') as fout:
        for kind_key in router_post:
            recent_posts = MPost.query_all(kind=kind_key, limit=1000000)
            for recent_post in recent_posts:
                url = os.path.join(SITE_CFG['site_url'],
                                   router_post[recent_post.kind], ext_url,
                                   recent_post.uid)
                fout.write('{url}\n'.format(url=url))
Exemple #6
0
def check200():
    '''
    对可以通过 WEB 访问的 URL 进行检查
    '''
    print('Checking HTTP 200 error: ')

    tstr = ''
    idx = 1
    for kind in config.router_post.keys():
        posts = MPost.query_all(kind=kind, limit=20000)
        for post in posts:
            the_url0 = '{site_url}/{kind_url}/{uid}'.format(
                site_url=config.SITE_CFG['site_url'],
                kind_url=config.router_post[post.kind],
                uid=post.uid)

            the_url = '{site_url}/{kind_url}/_edit/{uid}'.format(
                site_url=config.SITE_CFG['site_url'],
                kind_url=config.router_post[post.kind],
                uid=post.uid)

            the_url2 = '{site_url}/{kind_url}/_edit_kind/{uid}'.format(
                site_url=config.SITE_CFG['site_url'],
                kind_url=config.router_post[post.kind],
                uid=post.uid)

            req = requests.get(the_url0)

            if req.status_code == 200:
                pass
            else:
                print(the_url0)
                tstr = tstr + DT_STR.format(
                    idx=str(idx).zfill(2),
                    url0=the_url0,
                    code=req.status_code,
                    edit_link=the_url,
                    edit_tag_link=the_url2,
                )
                idx = idx + 1

    time_local = time.localtime(timestamp())
    with open(
            'xx_err_200_{d}.html'.format(
                d=str(time.strftime("%Y_%m_%d", time_local))), 'w') as fileo:
        fileo.write(HTML_TMPL.format(cnt=tstr))
    print('Checking 200 finished.')
Exemple #7
0
def run_update_lon():
    '''
    :return:
    '''

    def fix_lon(lonval):
        return int(((lonval + 180) % 360 - 180) * 1000) / 1000

    map_recs = MPost.query_all(limit_num=5000, kind='2')
    for map_rec in map_recs:
        lon = float(map_rec.extinfo['ext_lon'])
        if lon > -180 and lon < 180:
            pass
        else:
            print('=' * 20)
            print(map_rec.uid)
            print(lon)
            out_dic = {'ext_lon': fix_lon(lon)}
            print(out_dic)
            MPost.update_jsonb(map_rec.uid, out_dic)
def run_export():
    all_recs = MPost.query_all(kind='m', limit=10000)
    out_arr = []
    for postinfo in all_recs:
        out_arr.append({
            'uid': postinfo.uid,
            'title': postinfo.title,
            'keywords': postinfo.keywords,
            'date': postinfo.date,
            'extinfo': postinfo.extinfo,
            'cnt_md': postinfo.cnt_md,
            'cnt_html': postinfo.cnt_html,
            'kind': postinfo.kind,
            'user_name': postinfo.user_name,
            'logo': '',
        })

    dumper = ruamel.yaml.RoundTripDumper

    with open('xx_posts.yaml', 'w') as fo:
        yaml.dump(out_arr, fo, Dumper=dumper, allow_unicode=True)
Exemple #9
0
class PostHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mpost = MPost()
        self.mcat = MCategory()
        self.cats = self.mcat.query_all()
        self.mpost_hist = MPostHist()
        self.mpost2catalog = MPost2Catalog()
        self.mpost2label = MPost2Label()
        self.mrel = MRelation()
        self.tmpl_dir = 'doc'
        self.kind = '1'
        self.tmpl_router = 'post'

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

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

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

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

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

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

    @tornado.web.authenticated
    def __could_edit(self, postid):

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

    def refresh(self):

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

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

    def view_or_add(self, uid):
        if self.mpost.get_by_id(uid):
            self.view_post(uid)
        else:
            self.to_add(uid)

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

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

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

        post_data = self.get_post_data()

        post_data['user_name'] = self.get_current_user()
        is_update_time = True  # if post_data['is_update_time'][0] == '1' else False

        self.mpost_hist.insert_data(self.mpost.get_by_id(uid))
        self.mpost.update(uid, post_data, update_time=is_update_time)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.redirect('/post/{0}.html'.format(uid))

    @tornado.web.authenticated
    def update_tag(self, signature):
        current_tag_infos = self.mpost2label.get_by_id(signature,
                                                       kind=self.kind + '1')
        post_data = self.get_post_data()
        if 'tags' in post_data:
            pass
        else:
            return False

        print('tags: {0}'.format(post_data['tags']))
        tags_arr = [x.strip() for x in post_data['tags'].split(',')]
        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mpost2label.add_record(signature,
                                            tag_name,
                                            1,
                                            kind=self.kind + '1')

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

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

        current_infos = self.mpost2catalog.query_by_entity_uid(uid,
                                                               kind=self.kind +
                                                               '0')
        new_tag_arr = []
        # HTML中预定义的
        def_cate_arr = ['gcat{0}'.format(x) for x in range(10)]
        # todo: next line should be deleted. keep here for historical reason.
        def_cate_arr.append('def_cat_uid')

        for key in def_cate_arr:
            if key in post_data:
                pass
            else:
                continue
            print('a' * 4)
            print(post_data[key])
            if post_data[key] == '' or post_data[key] == '0':
                continue
            # if len(post_data[key]) != 4:
            #     continue
            print(post_data[key])
            print(new_tag_arr)
            # 有可能选重复了。保留前面的
            if post_data[key] in new_tag_arr:
                continue

            new_tag_arr.append(post_data[key] + ' ' *
                               (4 - len(post_data[key])))

        for idx, val in enumerate(new_tag_arr):
            self.mpost2catalog.add_record(uid, val, idx)

        # 对原来的进行处理,如果不在现有中,则删除
        for cur_info in current_infos:
            if str(cur_info.tag.uid).strip() not in new_tag_arr:
                self.mpost2catalog.remove_relation(uid, cur_info.tag)

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

        kwd = {
            'pager': '',
            'cats': self.cats,
        }
        self.render(
            'doc/post/post_edit.html',
            kwd=kwd,
            unescape=tornado.escape.xhtml_unescape,
            tag_infos=self.mcat.query_all(kind=constant['cate_post']),
            app2label_info=self.mpost2label.get_by_id(
                id_rec, kind=constant['tag_post']),
            app2tag_info=self.mpost2catalog.query_by_entity_uid(
                id_rec, kind=constant['cate_post']),
            dbrec=self.mpost.get_by_id(id_rec),
            userinfo=self.userinfo,
            cfg=config.cfg,
        )

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

        return (out_str)

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

    def __gen_last_current_relation(self, post_id):
        '''
        Generate the relation for the post and last post viewed.
        :param post_id:
        :return:
        '''
        last_post_id = self.get_secure_cookie('last_post_uid')
        if last_post_id:
            last_post_id = last_post_id.decode('utf-8')
        self.set_secure_cookie('last_post_uid', post_id)

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

    def view_post(self, post_id):
        self.__gen_last_current_relation(post_id)

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

        rec = self.mpost.get_by_id(post_id)

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

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

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

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

        self.render(
            'doc/post/post_view.html',
            view=rec,
            postinfo=rec,
            unescape=tornado.escape.xhtml_unescape,
            kwd=kwd,
            userinfo=self.userinfo,
            tag_info=tag_info,
            relations=rel_recs,
            rand_recs=rand_recs,
            replys=[],
            cfg=config.cfg,
        )

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

    @tornado.web.authenticated
    def add_post(self, url_str):
        url_arr = url_str.split('.')
        if len(url_arr) == 2:
            id_post = url_arr[0]
            if len(id_post) == 5:
                pass
            else:
                return False
        else:
            return False

        if self.check_post_role(self.userinfo)['ADD']:
            pass
        else:
            return False
        post_data = self.get_post_data()

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

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

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

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

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

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

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

    @tornado.web.authenticated
    def delete(self, del_id):
        if self.check_post_role(self.userinfo)['DELETE']:
            pass
        else:
            return False
        is_deleted = self.mpost.delete(del_id)
        if is_deleted:
            self.redirect('/post/recent')
        else:
            return False
Exemple #10
0
class TestMPost():
    def setup(self):
        print('setup 方法执行于本类中每条用例之前')
        self.uu = MPost()
        self.m2c = MPost2Catalog()
        self.ml = MLabel()
        self.m2l = MPost2Label()
        self.labeluid = '9999'
        self.raw_count = self.uu.get_counts()
        self.post_title = 'ccc'
        self.uid = tools.get_uu4d()
        self.post_id = '66565'
        self.tag_id = '2342'
        self.post_id2 = '89898'
        self.slug = 'huio'

    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.create_post(self.uid, post_data)
        new_count = self.uu.get_counts()

        tt = self.uu.get_by_uid(self.uid)
        assert tt.title == post_data['title']
        assert tt.cnt_md == tornado.escape.xhtml_unescape(post_data['cnt_md'])
        assert tt.cnt_html == tools.markdown2html(post_data['cnt_md'])
        assert raw_count + 1 == new_count
        self.tearDown()

    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.create_post(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.create_post(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.create_post(self.uid, post_data)
        self.tearDown()
        assert uu == False

    def add_message(self, **kwargs):
        post_data = {
            'name': kwargs.get('name', 'category'),
            'slug': kwargs.get('slug', self.slug),
            'order': kwargs.get('order', '0'),
            'kind': kwargs.get('kind1', '1'),
            'pid': kwargs.get('pid', '0000'),
        }
        MCategory.add_or_update(self.tag_id, post_data)

        p_d = {
            'title': kwargs.get('title', 'iiiii'),
            'cnt_md': kwargs.get('cnt_md', 'grgr'),
            'time_create': kwargs.get('time_create', '1992'),
            'time_update': kwargs.get('time_update', '1996070600'),
            'user_name': kwargs.get('user_name', 'yuanyuan'),
            'view_count': kwargs.get('view_count', 1),
            'logo': kwargs.get('logo', 'prprprprpr'),
            'memo': kwargs.get('memo', ''),
            'order': kwargs.get('order', '1'),
            'keywords': kwargs.get('keywords', ''),
            'extinfo': kwargs.get('extinfo', {}),
            'kind': kwargs.get('kind2', '1'),
            'valid': kwargs.get('valid', 1),
        }
        post_id = kwargs.get('post_id', self.post_id)
        MPost.get_by_uid(post_id)

        MPost.create_post(post_id, p_d)

        MPost2Catalog.add_record(self.post_id, self.tag_id)

    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.create_post(self.uid, post_data)

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

        self.tearDown()

    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.create_post(self.uid, post_data)

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

        self.tearDown()

    def test_query_cat_random(self):
        self.add_message()
        p = {'limit': 300}
        TF = False
        qq = self.uu.query_cat_random(self.tag_id, **p)
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_recent_most(self):
        self.add_message()
        qq = self.uu.query_recent_most(num=300)
        TF = False
        for i in qq:

            if i.uid == self.post_id:

                TF = True
        self.tearDown()
        assert TF

    def test_query_recent(self):
        self.add_message()
        qq = self.uu.query_recent(num=300)
        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_all(self):
        self.add_message()
        kwargs = {
            'limit': 300,
        }
        pp = self.uu.query_all(**kwargs)
        TF = False
        for i in pp:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_keywords_empty(self):
        self.add_message()
        pp = self.uu.query_keywords_empty()
        TF = False
        for i in pp:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_dated(self):
        self.add_message()
        qq = self.uu.query_dated(num=2000)

        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_most_pic(self):
        self.add_message()
        qq = self.uu.query_most_pic(300)
        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_cat_recent(self):
        self.add_message()
        qq = self.uu.query_cat_recent(self.tag_id, num=300)
        TF = False

        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_most(self):
        self.add_message()
        qq = self.uu.query_most(num=300)
        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_delete(self):
        self.add_message()
        qq = self.uu.delete(self.post_id)

        assert qq

        self.tearDown()

    def test_update_view_count_by_uid(self):

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

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

        viewcount0 = rec.view_count
        assert viewcount0 >= 1
        for x in range(100):
            self.uu.update_misc(rec.uid, count=True)

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

        self.tearDown()
        assert viewcount1 >= 101

    def test_upate(self):
        y = {'logo': '123123'}
        self.add_message(**y)
        qq = {
            'title': 'oppssss',
            'logo': 'fffff',
            'cnt_md': 'iiii',
            'user_name': 'yy'
        }
        self.uu.update(self.post_id, qq)
        aa = self.uu.get_by_uid(self.post_id)
        assert aa.logo == qq['logo']
        assert aa.title == qq['title']

        self.tearDown()

    def test_get_by_uid(self):
        y = {'logo': '123123'}
        self.add_message(**y)
        a = self.uu.get_by_uid(self.post_id)
        assert a.logo == y['logo']
        self.tearDown()

    def test_get_counts(self):
        a = self.uu.get_counts()
        self.add_message()
        b = self.uu.get_counts()
        assert a + 1 >= b

        self.tearDown()

    def test_update_field(self):
        y = {'logo': '123123'}
        self.add_message(**y)
        self.uu.update_field(self.post_id, self.post_id2)
        bb = self.uu.get_by_uid(self.post_id)
        aa = self.uu.get_by_uid(self.post_id2)

        assert bb == None
        assert aa.logo == y['logo']

        self.tearDown()

    def test_update_cnt(self):
        self.add_message()
        qq = {'cnt_md': 'iiii', 'user_name': ' yy  '}
        self.uu.update_cnt(self.post_id, qq)
        bb = self.uu.get_by_uid(self.post_id)
        assert bb.cnt_md == qq['cnt_md']

        self.tearDown()

    def test_update_order(self):
        self.add_message()

        self.uu.update_order(self.post_id, '1')
        bb = self.uu.get_by_uid(self.post_id)
        assert bb.order == '1'

        self.tearDown()

    def test_add_or_update(self):
        bb = self.uu.get_by_uid(self.post_id)

        assert bb == None
        p_d = {
            'title': 'iiiii',
            'cnt_md': 'grgr',
            'time_create': '1992',
            'time_update': '1996070600',
            'user_name': 'yuanyuan',
            'view_count': '1',
            'logo': 'prprprprpr',
            'memo': '',
            'order': '1',
            'keywords': '',
            'extinfo': {},
            'kind': '1',
            'valid': '1',
        }
        self.uu.add_or_update(self.post_id, p_d)
        bb = self.uu.get_by_uid(self.post_id)
        assert bb.title == p_d['title']
        qq = {
            'title': 'oppssss',
            'logo': 'fffff',
            'cnt_md': 'iiii',
            'user_name': 'yy'
        }
        self.uu.add_or_update(self.post_id, qq)
        aa = self.uu.get_by_uid(self.post_id)
        self.tearDown()
        assert aa.logo == qq['logo']
        assert aa.title == qq['title']
        self.tearDown()

    def test_query_random(self):
        self.add_message()
        q = {'num': 300}
        qq = self.uu.query_random(**q)

        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_recent_edited(self):
        self.add_message()
        qq = self.uu.query_recent_edited(1555)

        TF = False
        for i in qq:

            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_by_tag(self):
        TF = False
        self.add_message()
        qq = self.uu.query_by_tag(self.tag_id)
        print(qq.count())

        for i in qq:
            print(i.title)
            if i.uid == self.post_id:
                TF = True
        self.tearDown()
        assert TF

    def test_query_cat_recent_with_label(self):
        name = 'kkkk'
        p_d = {'extinfo': {'def_tag_arr': name}}
        self.add_message(**p_d)

        self.uu.query_cat_recent_with_label(self.tag_id, label=name, num=300)
        qq = self.uu.get_by_uid(self.post_id)
        assert qq.extinfo == p_d['extinfo']
        self.tearDown()

    def test_query_cat_recent_no_label(self):
        self.add_message()

        qq = self.uu.query_cat_recent_no_label(self.tag_id, num=300)
        print(qq.count())
        print(qq)

        assert qq[0].uid == self.post_id
        self.tearDown()

    def test_query_total_cat_recent(self):
        name = 'kkkk'
        p_d = {'extinfo': {'def_tag_arr': name}}
        self.add_message(**p_d)
        qq = self.uu.query_total_cat_recent([self.tag_id, '9090'],
                                            label=name,
                                            num=300)
        assert qq[0].uid == self.post_id
        self.tearDown()

    def test_query_total_cat_recent_no_label(self):
        self.add_message()
        qq = self.uu.query_total_cat_recent_no_label([self.tag_id], num=300)

        assert qq[0].uid == self.post_id
        self.tearDown()

    def test_get_next_record(self):
        self.add_message()

        q = {
            'post_id': self.post_id2,
            'title': '90909090',
            'cnt_md': 'oosdfsfofsf',
            'time_create': '1996'
        }
        self.add_message(**q)
        qq = self.uu.get_next_record(self.post_id2)
        print(qq)
        assert qq.uid == self.post_id
        self.tearDown()

    def test_get_previous_record(self):
        self.add_message()
        q = {
            'post_id': self.post_id2,
            'title': '90909090',
            'cnt_md': 'oosdfsfofsf',
            'time_create': '1996'
        }
        self.add_message(**q)
        qq = self.uu.get_previous_record(self.post_id)
        assert qq.uid == self.post_id2
        self.tearDown()

    def test_get_all(self):
        ose = self.uu.get_all(kind='1')
        TF = True
        for i in ose:
            if self.post_id == i.uid:
                TF = False
        assert TF

        self.add_message()
        eee = self.uu.get_all(kind='1')
        TF = False
        for i in eee:
            if self.post_id == i.uid:
                TF = True
        self.tearDown()
        assert TF

    def test_update_jsonb(self):
        p = {'ii': 'ii00ii'}
        self.add_message()
        self.uu.update_jsonb(self.post_id, p)
        aa = self.uu.get_by_uid(self.post_id)

        assert aa.extinfo == p

        self.tearDown()

    def test_modify_meta(self):
        self.add_message()
        p_d = {
            'title': 'qqqii',
            'cnt_md': 'qwqwqw',
            'time_create': '1999',
            'time_update': '2019',
            'user_name': 'max',
            'view_count': '1',
            'logo': 'opps',
            'memo': '',
            'order': '1',
            'extinfo': {},
            'kind': '1',
            'valid': '1',
        }
        self.uu.modify_meta(self.post_id, p_d)
        aa = self.uu.get_by_uid(self.post_id)
        self.tearDown()
        assert aa.title == p_d['title']

    def test_modify_init(self):
        self.add_message()
        p_d = {'keywords': 'io'}
        self.uu.modify_init(self.post_id, p_d)
        aa = self.uu.get_by_uid(self.post_id)
        assert aa.keywords == p_d['keywords']

        self.tearDown()

    def test_get_view_count(self):

        p_d = {'view_count': 3}
        self.add_message(**p_d)
        qq = self.uu.get_view_count(self.post_id)

        assert qq == p_d['view_count']
        self.tearDown()

    def test_query_most_by_cat(self):
        self.add_message()
        a = self.uu.query_most_by_cat(catid=self.tag_id, kind='1')
        assert a[0].uid == self.post_id
        self.tearDown()

    def test_query_least_by_cat(self):
        self.add_message()
        a = self.uu.query_least_by_cat(cat_str=self.tag_id, kind='1')
        assert a[0].uid == self.post_id
        self.tearDown()

    def test_get_by_keyword(self):
        p_d = {'title': 'yyyyy'}
        self.add_message(**p_d)
        aa = self.uu.get_by_keyword(p_d['title'], kind='1')
        assert aa[0].uid == self.post_id
        self.tearDown()

    def test_query_extinfo_by_cat(self):
        oo = 'd99s9s'
        p_d = {'extinfo': {'def_cat_uid': oo}}
        self.add_message(**p_d)
        aa = self.uu.query_extinfo_by_cat(oo, kind='1')
        assert aa[0].uid == self.post_id
        self.tearDown()

    def test_query_by_tagname(self):
        oo = 'd99s9s'
        p_d = {'extinfo': {'def_tag_arr': oo}}
        self.add_message(**p_d)
        aa = self.uu.query_by_tagname(oo, kind='1')
        assert aa[0].uid == self.post_id
        self.tearDown()

    def test_query_pager_by_tag(self):
        oo = 'd99s9s'
        p_d = {'extinfo': {'def_tag_arr': oo}}
        self.add_message(**p_d)
        aa = self.uu.query_pager_by_tag(oo, kind='1')
        assert aa[0].uid == self.post_id
        self.tearDown()

    def test_add_meta(self):
        p_d = {
            'title': 'qqqii',
            'cnt_md': 'qwqwqw',
            'time_create': '1999',
            'time_update': '2019',
            'user_name': 'max',
            'view_count': '1',
            'logo': 'opps',
            'memo': '',
            'order': '1',
            'kind': '1',
            'valid': 1,
        }
        self.uu.add_meta(self.post_id, p_d)

        aa = self.uu.get_by_uid(self.post_id)
        self.tearDown()
        assert aa.title == p_d['title']

    def test_query_under_condition(self):
        oo = {'def_tag_arr': 'd99s9s'}
        p_d = {'extinfo': oo}
        self.add_message(**p_d)
        aa = self.uu.query_under_condition(oo, kind='1')
        print(aa.count())
        assert aa[0].uid == self.post_id
        self.tearDown()

    def test_addata_init(self):
        p_d = {
            'sig': self.post_id,
            'title': 'qqq4ii',
            'cnt_md': 'qwqwqw',
            'user_name': 'max',
            'view_count': '1',
            'logo': 'opps',
            'memo': '',
            'order': '1',
            'cnt_html': 'dddd',
            'kind': '2',
            'valid': 1,
        }
        print('fddfsfdsfdsfd')
        self.uu.addata_init(p_d)
        print('fddfsfdsfdsfd')

        aa = self.uu.get_by_uid(self.post_id)
        print(aa)
        assert aa.title == p_d['title']

        self.tearDown()

    def test_query_list_pager(self):
        oo = {'def_tag_arr': 'd99s9s'}
        p_d = {'extinfo': oo}
        self.add_message(**p_d)
        qq = self.uu.query_list_pager(oo, 1, kind='1')
        print(qq[0])
        assert qq[0].uid == self.post_id

        self.tearDown()

    def test_count_of_certain_kind(self):
        a = self.uu.count_of_certain_kind(1)
        self.add_message()
        b = self.uu.count_of_certain_kind(1)
        assert a + 1 <= b

        self.tearDown()

    def test_total_number(self):
        a = self.uu.total_number(1)
        self.add_message()
        b = self.uu.total_number(1)
        assert a + 1 <= b

        self.tearDown()

    def tearDown(self):
        print("function teardown")
        tt = self.uu.get_by_uid(self.uid)

        if tt:
            print('翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻翻3')
            self.uu.delete(self.uid)
        tt = MCategory.get_by_uid(self.tag_id)
        if tt:
            MCategory.delete(self.tag_id)
        tt = self.uu.get_by_uid(self.post_id)
        if tt:
            MCategory.delete(self.tag_id)

            MPost.delete(self.post_id2)
            MPost.delete(self.post_id)

            MPost2Catalog.remove_relation(self.post_id, self.tag_id)
            print('545456365635653')

        tt = MLabel.get_by_slug(self.slug)
        if tt:
            print('8888888888')
            MLabel.delete(tt.uid)
Exemple #11
0
class TestMPost():
    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.create_post(self.uid, post_data)
        new_count = self.uu.get_counts()

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

        assert tt.title == post_data['title']
        assert tt.cnt_md == tornado.escape.xhtml_unescape(post_data['cnt_md'])
        assert tt.cnt_html == tools.markdown2html(post_data['cnt_md'])
        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.create_post(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.create_post(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.create_post(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.create_post(self.uid, post_data)

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

    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.create_post(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.create_post(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_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_update_keywords(self):
        self.uu.update_misc(self.uid, keywords='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.create_post(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_misc(rec.uid, count=True)

        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)
Exemple #12
0
import config
from torcms.core.tools import timestamp
import time

html_tmpl = '''
<!doctype html><html><head><title></title></head><body>
<dl>{cnt}</dl></body></html>
'''

dt_str = '''<dt>{title}</dt><dd>{uid}</dd>
<dd><a href="{edit_link}">{edit_link}</a></dd>'''

tstr = ''

for kind in config.router_post.keys():
    posts = MPost.query_all(kind=kind, limit=20000)
    for index, post in enumerate(posts):
        the_url0 = '{site_url}/{kind_url}/{uid}'.format(
            site_url=config.SITE_CFG['site_url'],
            kind_url=config.router_post[post.kind],
            uid=post.uid)

        the_url = '{site_url}/{kind_url}/_edit/{uid}'.format(
            site_url=config.SITE_CFG['site_url'],
            kind_url=config.router_post[post.kind],
            uid=post.uid)
        uu = requests.get(the_url0)

        if uu.status_code == 200:
            # print(index)
            # print(post.uid)
Exemple #13
0
def run_export():
    all_recs = MPost.query_all(kind='9', limit=10000)
    for postinfo in all_recs:
        insert_into_db(postinfo)
Exemple #14
0
 def test_query_all(self):
     MPost.query_all()
     assert True