Пример #1
0
    def test_get_image(self):
        """
		Test how to generate a single
		random image
		"""

        with self.app.test_request_context():
            image = ImagePlaceholder().get_image([Tag('Informatique')])
            print(image)

        assert '.jpg' in image
Пример #2
0
 def create_or_update_tags(self, offer):
     """
     Avoid recreating tags
     """
     generated_tags = Classifier().predict_category(offer)
     for i in range(len(generated_tags)):
         cached_tag = self.find_tag_by_title(generated_tags[i])
         if cached_tag is not None:
             generated_tags[i] = cached_tag
         else:
             generated_tags[i] = Tag(generated_tags[i])
     return generated_tags
Пример #3
0
def addtag(message):
    """
    添加标签
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    new_tag = request.values.get('tag')
    tag = Tag(new_tag)
    result = Tag.save(tag)
    if result is None:
        return jsonify(
            response.return_message(data={
                'token':
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.BAD_REQUEST.value,
                                code=Code.BAD_REQUEST.value))
Пример #4
0
def tags():
    """
    获取所有标签
    :return:
    """
    tags = Tag.get_all()
    if tags:
        data_tag = []
        for tag in tags:
            sin_tag = {"id": tag.id, "tag": tag.tag}
            data_tag.append(sin_tag)
        app.logger.info("request result: {}".format(tags))
        return jsonify(
            response.return_message({'tags': data_tag}, Message.SUCCESS.value,
                                    Code.SUCCESS.value))
    return jsonify(
        response.return_message(None, Message.SUCCESS.value,
                                Code.SUCCESS.value))
Пример #5
0
def get_by_tag(message):
    """
    模糊查询tag
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    tag_value = request.values.get('tag')
    tags = Tag.get_by_tag(tag_value)
    tags_value = [tag.tag for tag in tags]
    return jsonify(
        response.return_message(data={
            'tags':
            tags_value,
            'token':
            Verificate.encode_auth_token(
                message['data']['id'], message['data']['last_login']).decode()
        },
                                msg=Message.SUCCESS.value,
                                code=Code.SUCCESS.value))
Пример #6
0
def gettags(message):
    """
    获取标签
    :param message:
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    tags = Tag.get_all()
    tags_value = [tag.tag for tag in tags]
    return jsonify(
        response.return_message(data={
            'tags':
            tags_value,
            'token':
            Verificate.encode_auth_token(
                message['data']['id'], message['data']['last_login']).decode()
        },
                                msg=Message.SUCCESS.value,
                                code=Code.SUCCESS.value))
Пример #7
0
def put_publish(message):
    """
    修改文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    results = request.values.to_dict()
    if Article.get_by_id(results['article_id']) is None:
        return jsonify(
            response.return_message(data={
                "token":
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.ARTICLE_NOT_EXISTS.value,
                                    code=Code.NOT_FOUND.value))
    article = Article(results['title'], results['desc'], results['content'],
                      datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                      results['back_img'])
    article.id = results['article_id']
    # 将前台传来的字符串,转换成列表,再转换成元组,然后通过标签查询id
    tag_ids = Tag.get_id_by_tag(tuple(eval(results['tags'])))

    if Article.update(article, tag_ids) is None:
        return jsonify(
            response.return_message(data={
                "token":
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.BAD_REQUEST.value,
                                code=Code.BAD_REQUEST.value))
Пример #8
0
def get_tag_articles(message):
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    tag_id = message['data']['tag_id']
    page_no = message['data']['page_no']
    app.logger.info("request params - tag_id: {}, page_no: {}".format(
        tag_id, page_no))
    articles = Tag.get_tag_by_id(tag_id, page_no)
    article_list = []
    if articles:
        for item in articles.items:
            article_list.append({
                'id':
                item.id,
                'title':
                item.title,
                'desc':
                item.desc,
                'content':
                item.content,
                'publish_time':
                item.date_publish,
                'back_url':
                item.back_img.replace('.webp', '.tiny.webp')
                if item.back_img else None
            })
        app.logger.info("request result: {}".format(article_list))
        return jsonify(
            response.return_message(
                {
                    'total': articles.total,
                    'articles': article_list
                }, Message.SUCCESS.value, Code.SUCCESS.value))
    return jsonify(
        response.return_message(None, Message.BAD_REQUEST.value,
                                Code.BAD_REQUEST.value))