示例#1
0
async def api_image_delete(request):
    """
    删除博客API函数
    :param request: 请求
    :return:
    """
    if not is_admin(request):
        return permission_error()

    image_id = request.match_info['image_id']
    image = await Image.find(image_id)
    if not image:
        return data_error(u'非法image id')

    url = image.url
    await image.remove()

    url = url.replace(configs.domain_name, '')

    filename = '.'
    filename += url
    if os.path.exists(filename):
        os.remove(filename)

    return dict(id=image_id)
示例#2
0
async def api_blog_type_get(request):
    """
    获取指定页面的博客类别数据函数
    :param request: 请求对象
    :return: 博客类别数据
    """

    if not is_admin(request):
        return permission_error()

    page_index = 1
    qs_parser = QueryStringParser(request.query_string)
    if qs_parser.has_attr('page'):
        page_index = int(qs_parser.page)

    num = await BlogType.find_number('count(id)')
    p = Pagination(num, page_index)
    if num == 0:
        return dict(page=p, blogs=())
    types = await BlogType.find_all(order_by='level asc',
                                    limit=(p.offset, p.limit))

    # 找到每个类别对应的博客数量
    new_types = []
    for blog_type in types:
        type_name = blog_type.name
        blog_count = await Blog.find_number('count(id)', 'type=?', [type_name])
        blog_type['blog_count'] = blog_count
        new_types.append(blog_type)
    return dict(page=p, types=new_types)
示例#3
0
async def api_blog_update(request):
    """
    更新博客API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    ct = request.content_type.lower()
    if ct.startswith('application/json'):
        params = await request.json()
        if not isinstance(params, dict):
            return data_error()
    else:
        return data_error()

    blog_id = request.match_info['blog_id']

    name = None
    if 'name' in params:
        name = params['name']
    summary = None
    if 'summary' in params:
        summary = params['summary']
    content = None
    if 'content' in params:
        content = params['content']
    cover_image = None
    if 'cover_image' in params:
        cover_image = params['cover_image']
    blog_type = None
    if 'type' in params:
        blog_type = params['type']

    if not name or not name.strip():
        return data_error(u'博客名称不能为空')
    if not summary or not summary.strip():
        return data_error(u'博客摘要不能为空')
    if not content or not content.strip():
        return data_error(u'博客内容不能为空')
    if not cover_image or not cover_image.strip():
        return data_error(u'封面图片不能为空')
    if not blog_type or not blog_type.strip():
        return data_error(u'博客类型不能为空')

    blog = await Blog.find(blog_id)
    if not blog:
        return data_error(u'非法blog id')

    blog.name = name.strip()
    blog.summary = summary.strip()
    blog.content = content.strip()
    blog.cover_image = cover_image.strip()
    blog.type = blog_type.strip()
    await blog.update()
    return blog
示例#4
0
async def api_image_upload(request):
    """
    上传图片API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    ct = request.content_type.lower()
    if ct.startswith('application/json'):
        params = await request.json()
        if not isinstance(params, dict):
            return data_error()
    else:
        return data_error()

    image_name = None
    if 'name' in params:
        image_name = params['name']
    image_str = None
    if 'image' in params:
        image_str = params['image']
    if not image_name or not image_name.strip():
        return data_error(u'图片名不能为空')
    if not image_str or not image_str.strip():
        return data_error(u'图片内容不能为空')

    # 取消图片的原始名,只保留后缀名
    loc = image_name.find('.')
    if loc == -1:
        return data_error(u'图片名有误')
    image_name = image_name[loc:]

    # 先在数据库中生成一条图片的记录
    image = Image(url='xx')
    await image.save()

    # 使用图片数据的创建时间做为URL
    image_url = '/static/img/'
    image_url += str(int(image.created_at * 1000))
    image_url += image_name

    image.url = (configs.domain_name + image_url)
    await image.update()

    image_str = image_str.replace('data:image/png;base64,', '')
    image_str = image_str.replace('data:image/jpeg;base64,', '')
    image_data = base64.b64decode(image_str)

    image_path = '.'
    image_path += image_url
    file = open(image_path, 'wb')
    file.write(image_data)
    file.close()
    return image
示例#5
0
async def api_image_upload(request):
    """
    上传图片API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    request_data = RequestData(request)
    if not await request_data.json_load():
        return data_error(u'非法数据格式, 请使用JSON格式')

    image_name_ext = request_data.name
    image_str = request_data.image
    if not image_name_ext or not image_name_ext.strip():
        return data_error(u'图片名不能为空')
    if not image_str or not image_str.strip():
        return data_error(u'图片内容不能为空')

    # 取消图片的原始名,只保留后缀名
    loc = image_name_ext.find('.')
    if loc == -1:
        return data_error(u'图片名有误')
    image_name_ext = image_name_ext[loc:]

    # 先在数据库中生成一条图片的记录
    image = Image(url='xx')
    await image.save()

    # 使用图片数据的创建时间做为URL
    image_url = '/static/img/'

    # 使用年月创建文件夹
    dt = datetime.fromtimestamp(image.created_at)
    new_path = './static/img/%s/%s' % (dt.year, dt.month)
    os.makedirs(new_path, exist_ok=True)

    image_url += '%s/%s/%s%s%s%s' % \
                 (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
    image_url += image_name_ext

    image.url = (configs.domain_name + image_url)
    await image.update()

    image_str = image_str.replace('data:image/png;base64,', '')
    image_str = image_str.replace('data:image/jpeg;base64,', '')
    image_str = image_str.replace('data:image/gif;base64,', '')
    image_data = base64.b64decode(image_str)

    image_path = '.'
    image_path += image_url
    file = open(image_path, 'wb')
    file.write(image_data)
    file.close()
    return image
示例#6
0
async def api_blog_create(request):
    """
    创建博客API函数
    :param request: 请求
    :return:
    """
    if not is_admin(request):
        return permission_error()

    ct = request.content_type.lower()
    if ct.startswith('application/json'):
        params = await request.json()
        if not isinstance(params, dict):
            return data_error()
    else:
        return data_error()

    name = None
    if 'name' in params:
        name = params['name']
    summary = None
    if 'summary' in params:
        summary = params['summary']
    content = None
    if 'content' in params:
        content = params['content']
    cover_image = None
    if 'cover_image' in params:
        cover_image = params['cover_image']
    blog_type = None
    if 'type' in params:
        blog_type = params['type']
    if not name or not name.strip():
        return data_error(u'博客名称不能为空')
    if not summary or not summary.strip():
        return data_error(u'博客摘要不能为空')
    if not content or not content.strip():
        return data_error(u'博客内容不能为空')
    if not cover_image or not cover_image.strip():
        return data_error(u'封面图片不能为空')
    if not blog_type or not blog_type.strip():
        return data_error(u'博客类型不能为空')

    blog = Blog(user_id=request.__user__['id'],
                user_name=request.__user__['name'],
                user_image=request.__user__['image'],
                name=name.strip(),
                summary=summary.strip(),
                content=content.strip(),
                cover_image=cover_image.strip(),
                read_times=0,
                type=blog_type)
    await blog.save()
    return blog
示例#7
0
async def api_user_get(request):
    """
    WEB API: 获取用户数据API函数
    :param request: 请求对象
    :return: 用户数据字典
    """
    if not is_admin(request):
        return permission_error()

    users = await UserInfo.find_all(order_by='created_at desc')
    return dict(users=users)
示例#8
0
async def api_blog_get_one(request):
    """
    获取指定ID的博客数据函数
    :param request: 请求对象
    :return: 博客数据
    """
    if not is_admin(request):
        return permission_error()

    blog_id = request.match_info['blog_id']
    blog = await Blog.find(blog_id)
    if not blog:
        return data_error(u'非法blog id')

    return blog
示例#9
0
async def api_comment_delete(request):
    """
    删除评论API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    comment_id = request.match_info['id']

    c = await Comment.find(comment_id)
    if c is None:
        return data_error(u'非法comment id')

    await c.remove()
    return dict(id=comment_id)
示例#10
0
async def api_blog_type_delete(request):
    """
    删除博客类别API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    type_id = request.match_info['type_id']

    # 根据博客类别ID找到类别
    blog_type = await BlogType.find(type_id)
    if not blog_type:
        return data_error(u'非法type id')

    await blog_type.remove()

    return dict(id=type_id)
示例#11
0
async def api_blog_update(request):
    """
    更新博客API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    request_data = RequestData(request)
    if not await request_data.json_load():
        return data_error(u'非法数据格式, 请使用JSON格式')

    blog_id = request.match_info['blog_id']

    name = request_data.name
    summary = request_data.summary
    content = request_data.content
    cover_image = request_data.cover_image
    blog_type = request_data.type

    if not name or not name.strip():
        return data_error(u'博客名称不能为空')
    if not summary or not summary.strip():
        return data_error(u'博客摘要不能为空')
    if not content or not content.strip():
        return data_error(u'博客内容不能为空')
    if not cover_image or not cover_image.strip():
        return data_error(u'封面图片不能为空')
    if not blog_type or not blog_type.strip():
        return data_error(u'博客类型不能为空')

    blog = await Blog.find(blog_id)
    if not blog:
        return data_error(u'非法blog id')

    blog.name = name.strip()
    blog.summary = summary.strip()
    blog.content = content.strip()
    blog.cover_image = cover_image.strip()
    blog.type = blog_type.strip()
    await blog.update()
    return blog
示例#12
0
async def api_blog_delete(request):
    """
    删除博客API函数
    :param request: 请求对象
    :return:
    """
    if not is_admin(request):
        return permission_error()

    blog_id = request.match_info['blog_id']

    # 根据博客ID找到博客详细内容
    blog = await Blog.find(blog_id)
    if not blog:
        return data_error(u'非法blog id')

    await blog.remove()

    return dict(id=blog_id)
示例#13
0
async def api_comment_get(request):
    """
    获取评论API函数
    :param request: 请求对象
    :return: 评论数据
    """
    if not is_admin(request):
        return permission_error()

    page_index = 1
    qs_parser = QueryStringParser(request.query_string)
    if qs_parser.has_attr('page'):
        page_index = int(qs_parser.page)

    num = await Comment.find_number('count(id)')
    p = Pagination(num, page_index)
    if num == 0:
        return dict(page=p, comments=())
    comments = await Comment.find_all(order_by='created_at desc',
                                      limit=(p.offset, p.limit))
    return dict(page=p, comments=comments)
示例#14
0
async def api_image_get(request):
    """
    获取图像API函数
    :param request: 请求对象
    :return: 图像数据
    """
    if not is_admin(request):
        return permission_error()

    page_index = 1
    qs_parser = QueryStringParser(request.query_string)
    if qs_parser.has_attr('page'):
        page_index = int(qs_parser.page)

    num = await Image.find_number('count(id)')
    p = Pagination(num, page_index, page_size=6)
    if num == 0:
        return dict(page=p, images=())
    images = await Image.find_all(order_by='created_at desc',
                                  limit=(p.offset, p.limit))
    return dict(page=p, images=images)
示例#15
0
async def api_user_get(request):
    """
    WEB API: 获取用户数据API函数
    :param request: 请求对象
    :return: 用户数据字典
    """
    if not is_admin(request):
        return permission_error()

    page_index = 1
    qs_parser = QueryStringParser(request.query_string)
    if qs_parser.has_attr('page'):
        page_index = int(qs_parser.page)

    num = await UserInfo.find_number('count(id)')
    p = Pagination(num, page_index)
    if num == 0:
        return dict(page=p, users=())
    users = await UserInfo.find_all(order_by='created_at desc',
                                    limit=(p.offset, p.limit))
    return dict(page=p, users=users)
示例#16
0
async def api_blog_create(request):
    """
    创建博客API函数
    :param request: 请求
    :return:
    """
    if not is_admin(request):
        return permission_error()

    request_data = RequestData(request)
    if not await request_data.json_load():
        return data_error(u'非法数据格式, 请使用JSON格式')

    name = request_data.name
    summary = request_data.summary
    content = request_data.content
    cover_image = request_data.cover_image
    blog_type = request_data.type
    if not name or not name.strip():
        return data_error(u'博客名称不能为空')
    if not summary or not summary.strip():
        return data_error(u'博客摘要不能为空')
    if not content or not content.strip():
        return data_error(u'博客内容不能为空')
    if not cover_image or not cover_image.strip():
        return data_error(u'封面图片不能为空')
    if not blog_type or not blog_type.strip():
        return data_error(u'博客类型不能为空')

    blog = Blog(user_id=request.__user__['id'],
                user_name=request.__user__['name'],
                user_image=request.__user__['image'],
                name=name.strip(),
                summary=summary.strip(),
                content=content.strip(),
                cover_image=cover_image.strip(),
                read_times=0,
                type=blog_type)
    await blog.save()
    return blog
示例#17
0
async def api_blog_get(request):
    """
    获取指定页面的博客数据函数
    :param request: 请求对象
    :return: 博客数据
    """

    if not is_admin(request):
        return permission_error()

    page_index = 1
    str_dict = parse_query_string(request.query_string)
    if 'page' in str_dict:
        page_index = int(str_dict['page'])

    num = await Blog.find_number('count(id)')
    p = Pagination(num, page_index)
    if num == 0:
        return dict(page=p, blogs=())
    blogs = await Blog.find_all(order_by='created_at desc',
                                limit=(p.offset, p.limit))
    return dict(page=p, blogs=blogs)
示例#18
0
async def api_blog_type_create(request):
    """
    创建博客类别API函数
    :param request: 请求对象
    :return: 博客类别数据
    """
    if not is_admin(request):
        return permission_error()

    request_data = RequestData(request)
    if not await request_data.json_load():
        return data_error(u'非法数据格式, 请使用JSON格式')

    name = request_data.name
    level = request_data.level
    if not name or not name.strip():
        return data_error(u'类别名称不能为空')
    if not level or not level.strip():
        return data_error(u'类别优先级不能为空')

    blog_type = BlogType(name=name.strip(), level=int(level.strip()))
    await blog_type.save()
    return blog_type