Exemplo n.º 1
0
def get_editor_article(message):
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    params = request.values.to_dict()
    start_time = params['beginTime']
    end_time = params['endTime']
    page_no = params['pageNo']
    result = Article.get_all_by_date(start_time, end_time, page_no)
    articles = []
    for item in result.items:
        tags = [tag.tag for tag in item.tags.all()]
        article = {
            "id": item.id,
            "title": item.title,
            "tags": tags,
            "datetime": item.date_publish.strftime("%Y-%m-%d")
        }
        articles.append(article)
    return jsonify(
        response.return_message(data={
            "articles":
            articles,
            "token":
            Verificate.encode_auth_token(
                message['data']['id'], message['data']['last_login']).decode(),
            "total":
            result.total
        },
                                msg=Message.SUCCESS.value,
                                code=Code.SUCCESS.value))
Exemplo n.º 2
0
def get_publish(message):
    """
    查询文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    article_id = request.values.get("article_id")
    article = Article.get_by_id(article_id)
    if article:
        tags = [tag.tag for tag in article.tags.all()]
        result = {
            "id": article.id,
            "title": article.title,
            "tags": tags,
            "desc": article.desc,
            "content": article.content
        }
        return jsonify(
            response.return_message(data={
                "article":
                result,
                "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))
Exemplo n.º 3
0
def upload_image(message):
    """
    图片上传到腾讯cos
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    base64_str = base64.b64encode(request.files['file'].read()).decode('utf-8')
    filename = "files.webp"
    CommonUtil.handle_img(base64_str, filename)
    result = CommonUtil.upload_img(filename)
    if result is not None:
        return jsonify(
            response.return_message(data={
                'image_url':
                result,
                'token':
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.UPLOAD_SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.UPLOAD_FAILED,
                                code=Code.BAD_REQUEST.value))
Exemplo n.º 4
0
def change_info(message):
    """
    修改个人信息
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    params = request.values.to_dict()
    passwords = set_password(params['password'])
    sys_user = SysUser(params['username'], passwords, params['email'],
                       params['avatar'])
    result = SysUser.update(sys_user)
    if result is None:
        # 为空说明存入数据库没有报错
        sys_user = SysUser.get_info(params['username'])
        token = Verificate.encode_auth_token(
            sys_user.id, sys_user.last_login.strftime("%Y-%m-%d %H:%M:%S"))
        data = {
            'info': {
                'username': sys_user.username,
                'password': sys_user.password,
                'email': sys_user.email,
                'avatar': sys_user.avatar
            },
            'token': token.decode()
        }
        return jsonify(
            response.return_message(data, Message.SUCCESS.value,
                                    Code.SUCCESS.value))
    else:
        return jsonify(
            response.return_message(None, Message.BAD_REQUEST.value,
                                    Code.BAD_REQUEST.value))
Exemplo n.º 5
0
def upload(message):
    """
    上传图片
    :return: 返回图片链接
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    base64_str = request.values.get('img')
    username = request.values.get('username')
    CommonUtil.handle_img(base64_str, 'avatar.webp')
    result = CommonUtil.upload_img('avatar.webp')
    if result is not None:
        result_avatar = SysUser.update_avatar(username, result)
        if result_avatar is None:
            return jsonify(
                response.return_message(data={
                    'image_url':
                    result,
                    'token':
                    Verificate.encode_auth_token(
                        message['data']['id'],
                        message['data']['last_login']).decode()
                },
                                        msg=Message.UPLOAD_SUCCESS.value,
                                        code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.UPLOAD_FAILED.value,
                                code=Code.BAD_REQUEST.value))
Exemplo n.º 6
0
def login():
    """
    管理员登录
    :return: 基本信息以及token
    """
    username = request.values.get('username')
    password = request.values.get('password')
    return Verificate.authenticate(username, password)
Exemplo n.º 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))
Exemplo n.º 8
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))
Exemplo n.º 9
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))
Exemplo n.º 10
0
def info_page(message):
    """
    用户信息页面
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    username = request.values.get('username')
    sys_user = SysUser.get_info(username)
    token = Verificate.encode_auth_token(
        sys_user.id, sys_user.last_login.strftime("%Y-%m-%d %H:%M:%S"))
    data = {
        'info': {
            'username': sys_user.username,
            'password': sys_user.password,
            'email': sys_user.email,
            'avatar': sys_user.avatar
        },
        'token': token.decode()
    }
    return jsonify(
        response.return_message(data, Message.SUCCESS.value,
                                Code.SUCCESS.value))
Exemplo n.º 11
0
def delete_publish(message):
    """
    删除文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    article_id = request.values.get('article_id')
    result = Article.delete(article_id)
    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.DELETE_SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.DELETE_FAILED.value,
                                code=Code.BAD_REQUEST.value))
Exemplo n.º 12
0
def upload_image(message):
    """
    图片上传到腾讯cos
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    base64_str = base64.b64encode(request.files['file'].read()).decode('utf-8')
    if request.values.get('back_img'):
        img = CommonUtil.handle_img(base64_str, 'files',
                                    Constant.IS_BACK_IMG.value)
    else:
        img = CommonUtil.handle_img(base64_str, 'files')
    remote_name = str(int(time.time()))
    CommonUtil.upload_img('files.jpg', remote_name, '.jpg')
    result = CommonUtil.upload_img('files.webp', remote_name)
    if request.values.get('back_img'):
        remote_name = remote_name + '.tiny'
        CommonUtil.tiny_img_thumb(img, 'tiny_files')
        CommonUtil.upload_img('tiny_files.jpg', remote_name, '.jpg')
        CommonUtil.upload_img('tiny_files.webp', remote_name)
    if result is not None:
        return jsonify(
            response.return_message(data={
                'image_url':
                result,
                'token':
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.UPLOAD_SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.UPLOAD_FAILED,
                                code=Code.BAD_REQUEST.value))
Exemplo n.º 13
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))