Exemple #1
0
    def test_remove_relation(self):
        self.add_message()
        MPost2Catalog.add_record(self.post_id, self.tag_id)
        MPost2Catalog.remove_relation(self.post_id, self.tag_id)
        a = MPost2Catalog.query_all()
        TF = True

        for i in a:
            if i.post_id == self.post_id:
                TF = False

        assert TF
        self.tearDown()
Exemple #2
0
def update_category(uid, postdata, kwargs):
    '''
    Update the category of the post.
    '''
    catid = kwargs['catid'] if ('catid' in kwargs and MCategory.get_by_uid(kwargs['catid'])) else None

    post_data = postdata

    current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').objects()

    new_category_arr = []
    # Used to update post2category, to keep order.
    def_cate_arr = ['gcat{0}'.format(x) for x in range(10)]

    # for old page.
    def_cate_arr.append('def_cat_uid')

    # Used to update post extinfo.
    cat_dic = {}
    for key in def_cate_arr:
        if key not in post_data:
            continue
        if post_data[key] == '' or post_data[key] == '0':
            continue
        # 有可能选重复了。保留前面的
        if post_data[key] in new_category_arr:
            continue

        new_category_arr.append(post_data[key] + ' ' * (4 - len(post_data[key])))
        cat_dic[key] = post_data[key] + ' ' * (4 - len(post_data[key]))

    if catid:
        def_cat_id = catid
    elif new_category_arr:
        def_cat_id = new_category_arr[0]
    else:
        def_cat_id = None

    if def_cat_id:
        cat_dic['def_cat_uid'] = def_cat_id
        cat_dic['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid

    MPost.update_jsonb(uid, cat_dic)

    for index, catid in enumerate(new_category_arr):
        MPost2Catalog.add_record(uid, catid, index)

    # Delete the old category if not in post requests.
    for cur_info in current_infos:
        if cur_info.tag_id not in new_category_arr:
            MPost2Catalog.remove_relation(uid, cur_info.tag_id)
Exemple #3
0
    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),
        }

        MPost.create_post(self.app_f, p_d)
        MPost2Catalog.add_record(self.app_f, self.tag_id)

        p_d = {
            'title': 'oioi',
            'cnt_md': 'oioi',
            'time_create': '1998',
            'time_update': '1999',
            'user_name': 'oioi',
            'view_count': 1,
            'logo': 'oioi',
            'memo': '',
            'order': 1,
            'keywords': '',
            'extinfo': {},
            'kind': "1",
            'valid': 1,
        }

        MPost.create_post(self.app_t, p_d)
        a = MPost.get_by_uid(self.app_t)

        MPost2Catalog.add_record(self.app_t, self.tag_id)
Exemple #4
0
    def test_get_app_relations(self, **kwargs):
        MPost.delete(self.uid)
        MPost.delete(self.uid2)
        # 添加post
        post_data = {
            'title': 'test1',
            'cnt_md': '## test',
            'user_name': 'test',
            'view_count': 1,
            'logo': ' ',
            'keywords': 'test',
            'kind': '1'
        }

        uu1 = MPost.create_post(self.uid, post_data)

        post_data2 = {
            'title': 'test2',
            'cnt_md': '## test2',
            'user_name': 'test2',
            'view_count': 1,
            'logo': ' ',
            'keywords': 'test2',
            'kind': '1'
        }

        uu2 = MPost.create_post(self.uid2, post_data2)

        # 添加category
        cat_data = {
            'name': kwargs.get('name', 'category'),
            'slug': kwargs.get('slug', 'slug1'),
            'order': kwargs.get('order', '0'),
            'kind': kwargs.get('kind1', '1'),
            'pid': kwargs.get('pid', '0000'),
        }
        MCategory.add_or_update(self.tag_id, cat_data)

        MPost2Catalog.add_record(uu1, self.tag_id)

        MRelation.add_relation(uu1, uu2)

        MRelation.get_app_relations(uu1)

        assert True
def get_meta(catid, sig, cell_cars):
    uid = sig
    meta_base = './script_helper'
    if os.path.exists(meta_base):
        pass
    else:
        return False

    pp_data = {'logo': '', 'kind': 'k'}

    pp_data['title'] = cell_cars[1]
    # pp_data['cnt_md'] = cell_cars[1]

    pp_data['user_name'] = 'admin'
    pp_data['def_cat_uid'] = catid
    pp_data['def_cat_pid'] = catid[:2] + '00'

    pp_data['extinfo'] = {
        # ad_link 不存在则使用 raw_link
        'zlink': cell_cars[3] if cell_cars[3] else cell_cars[4]
    }

    for wroot, wdirs, wfiles in os.walk(meta_base):
        for wdir in wdirs:
            if wdir.lower() == sig:
                ds_base = pathlib.Path(os.path.join(wroot, wdir))
                for uu in ds_base.iterdir():
                    if uu.name.endswith('.xlsx'):
                        meta_dic = chuli_meta(sig[2:], uu)
                    elif uu.name.endswith('.md'):
                        pp_data['cnt_md'] = open(str(uu.absolute())).read()
                    elif uu.name.startswith('thumbnail_'):
                        pp_data['logo'] = os.path.join(wroot, wdir,
                                                       uu.name).strip('.')

    kwargsa = {
        'def_cat_uid': catid,
        'cat_id': catid,
    }
    MPost.add_or_update(uid, pp_data)
    update_category(uid, pp_data, kwargsa)
    MPost2Catalog.add_record(uid, catid)
Exemple #6
0
    def add_post(self, **kwargs):
        p_d = {
            'title': kwargs.get('title', '一二三'),
            'cnt_md': kwargs.get('cnt_md', 'grgr'),
            'time_create': kwargs.get('time_create', '1992'),
            'time_update': kwargs.get('time_update', '1996070600'),
            'user_name': 'flower',
            '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', 'sd,as'),
            'extinfo': kwargs.get('extinfo', {}),
            'kind': kwargs.get('kind', '1'),
            'valid': kwargs.get('valid', 1),
        }
        p_id = kwargs['post_uid']

        MPost.create_post(p_id, p_d)
        MPost2Catalog.add_record(p_id, self.tag_id)
Exemple #7
0
def run_export():
    for x in yaml_infos:

        if x['uid'] in filter_arr:

            print(x['uid'])
            MPost.create_post(x['uid'], x)

            x['extinfo']['def_cat_uid'] = x['extinfo'].get('def_cat_uid', '2103')

            print(x['extinfo']['def_cat_uid'])

            if x['extinfo']['def_cat_uid'] in ['me01', 'me02', 'mf01', 'mf03']:
                # pass
                MPost2Catalog.add_record(x['uid'], x['extinfo']['def_cat_uid'])
            else:
                # pass
                MPost2Catalog.add_record(x['uid'], '2103')

        pass
Exemple #8
0
def update_category(uid, post_data):
    '''
    Update the category of the post.
    :param uid:  The ID of the post. Extra info would get by requests.
    '''

    # deprecated
    # catid = kwargs['catid'] if MCategory.get_by_uid(kwargs.get('catid')) else None
    # post_data = self.get_post_data()
    if 'gcat0' in post_data:
        pass
    else:
        return False

    # Used to update MPost2Category, to keep order.
    the_cats_arr = []
    # Used to update post extinfo.
    the_cats_dict = {}

    # for old page. deprecated
    # def_cate_arr.append('def_cat_uid')

    def_cate_arr = ['gcat{0}'.format(x) for x in range(10)]
    for key in def_cate_arr:
        if key not in post_data:
            continue
        if post_data[key] == '' or post_data[key] == '0':
            continue
        # 有可能选重复了。保留前面的
        if post_data[key] in the_cats_arr:
            continue

        the_cats_arr.append(post_data[key] + ' ' * (4 - len(post_data[key])))
        the_cats_dict[key] = post_data[key] + ' ' * (4 - len(post_data[key]))

    # if catid:
    #     def_cat_id = catid
    if the_cats_arr:
        def_cat_id = the_cats_arr[0]
    else:
        def_cat_id = None

    if def_cat_id:
        the_cats_dict['gcat0'] = def_cat_id
        the_cats_dict['def_cat_uid'] = def_cat_id
        the_cats_dict['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid

    # Add the category
    logger.info('Update category: {0}'.format(the_cats_arr))
    logger.info('Update category: {0}'.format(the_cats_dict))

    MPost.update_jsonb(uid, the_cats_dict)

    for index, idx_catid in enumerate(the_cats_arr):
        MPost2Catalog.add_record(uid, idx_catid, index)

    # Delete the old category if not in post requests.
    current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').objects()
    for cur_info in current_infos:
        if cur_info.tag_id not in the_cats_arr:
            MPost2Catalog.remove_relation(uid, cur_info.tag_id)
def get_meta(catid, sig, kind_sig=''):
    if kind_sig:
        pass
    else:
        return
    meta_base = './static/xx_mds'
    if os.path.exists(meta_base):
        pass
    else:
        return False

    for wroot, wdirs, wfiles in os.walk(meta_base):
        for wdir in wdirs:

            if wdir.lower().endswith(sig[1:]):
                ds_base = pathlib.Path(os.path.join(wroot, wdir))
                pp_data = {'logo': '', 'kind': kind_sig}

                for uu in ds_base.iterdir():
                    if uu.name.endswith('.md'):
                        meta_dic = chuli_meta(sig, uu)

                        if 'title' in meta_dic:
                            pass
                        else:
                            continue

                        pp_data['title'] = meta_dic['title']
                        pp_data['cnt_md'] = meta_dic['cnt_md']
                        pp_data['user_name'] = meta_dic['author']
                        pp_data['gcat0'] = catid
                        pp_data['def_cat_pid'] = catid[:2] + '00'
                        pp_data['valid'] = 1
                        # 将主要数据添加到外扩展
                        pp_data['extinfo'] = {}

                        kwargsa = {
                            'gcat0': catid,
                            'cat_id': catid,
                        }
                    elif uu.name.startswith('thumbnail_'):
                        hou = os.path.splitext(uu.name)[-1]
                        if hou in [
                                '.jpg', '.png', '.JPG', '.PNG', 'jpeg', 'JPEG'
                        ]:
                            thum_path = gen_thumb(
                                os.path.join(wroot, wdir, uu.name),
                                'd' + sig[1:])
                            pp_data['logo'] = thum_path.strip('.')

                # ToDo: 未处理完

                dataset_id = 'dn' + sig[1:]
                the_entity = fix_entity_path(dataset_id)

                post_id = sig

                postinfo = MPost.get_by_uid(post_id)

                if postinfo:
                    postinfo.extinfo.update(pp_data['extinfo'])
                    pp_data['extinfo'] = postinfo.extinfo

                # pp_data['extinfo']['tag_data_classification'] = str(dwmode)

                if the_entity:
                    pp_data['extinfo']['tag__file_download'] = the_entity
                    MPost.update_jsonb(post_id, pp_data['extinfo'])
                else:
                    # 如果未找到实体
                    pass
                    # update_category(post_id, pp_data)

                    # ws.cell(row=i, column=2).value = pp_data['title']
                # Write a copy with titles.

                MPost.add_or_update(sig, pp_data)

                update_category(sig, pp_data, kwargsa)

                update_label(sig, pp_data)

                MPost2Catalog.add_record(sig, catid)

                return pp_data['title']
 def add_P2C(self):
     MPost2Catalog.add_record(self.post_id, self.tag_id)
Exemple #11
0
def import_tables():
    inws = pathlib.Path('./database/datacn_tables')
    print(inws)
    # for y in os.listdir(inws):
    #     print(y)
    for cat_path in inws.iterdir():
        if cat_path.is_dir():
            uu_arr = cat_path.name.split('_')
            uid = uu_arr[0]
            print(uid)

            for xlsx in cat_path.iterdir():
                if xlsx.suffix.lower() == '.xlsx' and '数据指标列表' not in xlsx.name:
                    print(xlsx.name)
                    the_name = xlsx.name[:-5]
                    name_arr = the_name.split('_')
                    # ToDo: 下面强制断掉,有问题
                    post_uid = ('t' + name_arr[0]).lower()[:5]
                    if len(name_arr) > 1:
                        post_title = '_'.join(name_arr[1:])
                    else:
                        post_title = name_arr[0]

                    print(post_uid)
                    pp_data = {
                        'title': post_title,
                        'cnt_md': 'aa',
                        'user_name': 'admin',
                        'logo': 'log',
                        'def_cat_uid': uid,
                        'def_cat_pid': '5000',
                        'kind': '5',

                        'extinfo': {
                            'def_tab_path': '/'.join(str(xlsx)[:-5].split('/')[-2:]),
                        }

                    }

                    # title = title,

                    # cnt_md = tornado.escape.xhtml_escape(post_data['cnt_md'].strip()),
                    # cnt_html = tools.markdown2html(post_data['cnt_md']),
                    # uid = post_uid,
                    # time_create = post_data.get('time_create', tools.timestamp()),
                    # time_update = post_data.get('time_update', tools.timestamp()),
                    # user_name = post_data['user_name'],
                    # view_count = post_data['view_count'] if 'view_count' in post_data else 1,
                    # logo = post_data['logo'],
                    # memo = post_data['memo'] if 'memo' in post_data else '',
                    # order = post_data['order'] if 'order' in post_data else '',
                    # keywords = post_data['keywords'] if 'keywords' in post_data else '',
                    # extinfo = post_data['extinfo'] if 'extinfo' in post_data else {},
                    # kind = post_data['kind'] if 'kind' in post_data else '1',
                    # valid = post_data.get('valid', 1)
                    #
                    print('x' * 50)
                    print(post_uid)
                    print(pp_data)
                    MPost.add_or_update(post_uid, pp_data)
                    # print(xlsx.absolute())
                    # MPost.update_misc(post_uid, def_tab_path = xlsx.absolute())
                    kwargsa = {
                        'def_cat_uid': uid,
                        'cat_id': uid,
                    }
                    update_category(post_uid, pp_data, kwargsa)
                    MPost2Catalog.add_record(post_uid, uid)
Exemple #12
0
 def test_add_record(self):
     MPost2Catalog.add_record(self.post_id, self.tag_id)
     assert True