Esempio n. 1
0
async def delete_user(*, user_id, request):
    if not request.__user__:
        raise APIPermissionError('User', 'Log in to manage users')
    if not request.__user__.admin == 1:
        raise APIPermissionError('User',
                                 'Only admins have access to manage accounts')
    try:
        user = await User.find(user_id)
        if not user:
            raise APIResourceNotFoundError('User', 'User record doesnt exist')
        await user.remove()
        return dict(message='Succeed')
    except Error as e:
        return dict(error='Database Error',
                    data="blogs",
                    message=str(e.args[0]))
Esempio n. 2
0
async def api_create_comment(id, request, *, content):
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('blog')
    if not content or not content.strip():
        raise APIValueError('content')
    if not request.__user__:
        raise APIPermissionError('Please sign first')
    comment = Comment(blog_id=blog.id,
                      user_id=request.__user__.id,
                      user_image=request.__user__.image,
                      content=content,
                      created_at=time.time(),
                      user_name=request.__user__.name)
    await comment.save()
    return comment
Esempio n. 3
0
async def api_add_comments(blog_id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not (content and content.strip()):
        raise APIValueError('content')
    blog = await Blog.find(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 4
0
async def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('PLEASE signin frist')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = await Blog.find(id)
    if blog is None:
        return APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 5
0
def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = yield from Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    yield from comment.save()
    return comment
Esempio n. 6
0
def api_update_blog(blog_id):
    check_admin()
    i = ctx.request.input(name='', content='')
    name = i.name.strip()
    content = i.content.strip()
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    blog = Blog.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    blog.name = name
    blog.content = content
    blog.update()
    return blog
Esempio n. 7
0
def api_update_blog(id, request, *, name, summary, content):
    check_admin(request)
    blog = yield from Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError("blog","Blog Not Found")
    if not name or not name.strip():
        raise APIValueError('name','name cannot empty')
    if not summary or not summary.strip():
        raise APIValueError('summary', 'summary cannot be empty')
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty')
    blog.name = name.strip()
    blog.summary = summary.strip()
    blog.content = content.strip()
    yield from blog.update()
    return blog
Esempio n. 8
0
async def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content', 'Content cannot be empty.')
    blog = await Blog.findByPriKey(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog', 'Blog is not found.')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 9
0
async def api_create_comment(id, request, *, content):  # 传入日志id,request,评论内容
    user = request.__user__  # 获取经过auth_factory加工的request的__user__属性
    if user is None:  # user为空说明没有登录或没有成功登录
        raise APIPermissionError('Please Signin First! | 请先登录!')
    if not content or not content.strip():  # 去掉首尾空格
        raise APIValueError('content')
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 10
0
def api_create_comment(comment_id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError("Signin, Please.")
    if not content or not content.strip():
        raise APIValueError("No content.")
    blog = yield from Blog.find(comment_id)
    if blog is None:
        raise APIResourceNotFoundError("Blog")
    comment = Comment(blog_id=blog.blog_id,
                      user_id=user.user_id,
                      user_name=name,
                      user_image=user.image,
                      content=content.strip())
    yield from comment.save()
    return comment
Esempio n. 11
0
def api_create_comment(request, *, blog_id, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = yield from Blog.find(blog_id)
    print("blog--------->" + blog.id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    if blog is not None and blog.commentState == '0':
        raise APIValueError('prohibiting', '该文章已禁止评论.')
    comment = Comment(blog_id=blog_id,
                      user_id=user.id,
                      content=content.strip())
    yield from comment.save()
    return comment
Esempio n. 12
0
def api_create_blog_comment(blog_id):
    '''
    创建评论
    '''
    blog = Blog.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    content = ctx.request.input(content='').content.strip()
    if not content:
        raise APIValueError('content')
    c = Comment(blog_id=blog_id,
                user_id=1,
                user_name='匿名',
                user_image='',
                content=content)
    c.insert()
    return dict(comment=c)
Esempio n. 13
0
async def api_delete_users(id, request):
    check_admin(request)
    id_buff = id
    user = await User.find(id)
    if user is None:
        raise APIResourceNotFoundError('Comment')
    await user.remove()
    # 给被删除的用户在评论中标记
    comments = await Comment.findAll('user_id=?', [id])
    if comments:
        for comment in comments:
            id = comment.id
            c = await Comment.find(id)
            c.user_name = c.user_name + ' (该用户已被删除)'
            await c.update()
    id = id_buff
    return dict(id=id)
Esempio n. 14
0
async def api_create_comment(*, id, content, request):
    logging.error(id)
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIPermissionError('content')
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 15
0
def api_create_blog_comment(blog_id):
    user = ctx.request.user
    if user is None:
        raise APIPermissionError('Need signin.')
    blog = Blog.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    content = ctx.request.input(content='').content.strip()
    if not content:
        raise APIValueError('content')
    c = Comment(blog_id=blog_id,
                user_id=user.id,
                user_name=user.name,
                user_image=user.image,
                content=content)
    c.insert()
    return dict(comment=c)
Esempio n. 16
0
async def api_create_comments(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissioinError('please signin first.')
    if not content or not content.strip():
        raise APIValueError('content', 'content connot be empty')
    blog = await Blog.find(id)
    if blog is None:
        #确保每一个存入的评论都有对应的blog
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=request.__user__.id,
                      user_name=request.__user__.name,
                      user_image=request.__user__.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 17
0
async def api_delete_users(id, request):
    check_admin(request)
    id_buff = id
    user = await User.find(id)
    if user is None:
        raise APIResourceNotFoundError('Comment')
    await user.remove()
    comments = await Comment.findAll('user_id=?', [id])
    if comments:
        for comment in comments:
            id = comment.id
            comment = await Comment.find(id)
            comment.user_name = comment.user_name + \
                '(the user had been deleted)'
            await comment.update()
    id = id_buff
    return dict(id=id)
Esempio n. 18
0
async def api_update_blog(blog_id, request, *, name, summary, content):
    user = request.__user__
    check_admin(user)
    blog = await Blog.find(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('blog', 'invalid blog id')
    if not name or not name.strip():
        raise APIValueError('name', 'name can not be empty')
    if not summary or not summary.strip():
        raise APIValueError('summary', 'summary can not be empty')
    if not content or not content.strip():
        raise APIValueError('content', 'content can not be empty')
    blog.name = name.strip()
    blog.summary = summary.strip()
    blog.content = content.strip()
    await blog.update()
    return blog
Esempio n. 19
0
async def create_blog_comment(id, request, *, content):
    user = request.__user__
    if not user:
        raise APIPermissionError('permission sigin first')
    if not content or not content.strip():
        raise APIValueError('comment', 'comment is empty')
    blog = await Blog.find(id)
    if not blog:
        raise APIResourceNotFoundError('not exist blog id')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    logging.info('handler comment: %s' % str(comment))
    await comment.save()
    return comment
Esempio n. 20
0
async def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content should not be empty.')
    # 查找此评论的对应的博客是否存在
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog Not Found')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 21
0
def api_create_comment(id,request,*,content):
    user=request.__user__
    #必须为登录状态
    if user is None:
        raise APIPermissionError('content')
    #评论不能为空
    if not content or not content.strip():
        raise APIValueError('content')
    #查询一下博客id是否有对应的博客
    blog=yield from Blog.find(id)
    #没有抛错
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    #构建一条评论数据
    comment=Comment(blog_id=blog.id,user_id=user.id,user_name=user.name,user_image=user.image,content=content.strip())
    #保存到评论里面
    yield from comment.save()
    return comment
Esempio n. 22
0
async def get_blog(id, request):
    blog = await Blog.find(id)  # 通过id从数据库中查找博客信息
    if not blog:
        raise APIResourceNotFoundError('Blog')  # 资源未找到
    # 从数据库查找指定blog的全部评论,按时间降序排序,即最新的排在最前
    comments = await Comment.findAll('blog_id=?', [id],
                                     orderby='created_at desc')
    # 将每条评论都转化成html格式,这里的html_content在赋值时创建
    for c in comments:
        c.html_content = text2html(c.content)
    # blog为markdown格式,将其转化成html格式,这里的html_content在赋值时创建
    blog.html_content = markdown2.markdown(blog.content)
    return {
        '__template__': 'blog.html',
        'blog': blog,
        '__user__': request.__user__,
        'comments': comments
    }
Esempio n. 23
0
def api_get_histories(user_id):
    if not user_id:
        return APIValueError('users')
    histories = History.find_by('where user_id=? order by created_at desc',
                                user_id)
    movies = []
    if histories:
        for history in histories:
            dt = datetime.fromtimestamp(history.created_at)
            movie_id = history.movie_id
            movie = Movie.get(movie_id)
            if movie:
                movie = _get_movie_details(movie)
                movie.history_day = dt.day
                movie.history_month = dt.month
            movies.append(movie)
        return dict(movies=movies)
    raise APIResourceNotFoundError('histories')
Esempio n. 24
0
async def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError("Please signin first.")
    if not content or not content.strip():
        raise APIValueError("content")
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError("Blog")
    comment = Comment(
        blog_id=blog.id,
        user_id=user.id,
        user_name=user.name,
        user_image=user.image,
        content=content.strip(),
    )
    await comment.save()
    return comment
Esempio n. 25
0
async def api_create_comment(id, request, *, name, email, website, content):
    if not name or not name.strip():
        raise APIValueError('name')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = await Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    peername = request.transport.get_extra_info('peername')
    ip = peername[0]
    comment = Comment(blog_id=blog.id,
                      user_name=name.strip(),
                      user_email=email.strip(),
                      user_website=website.strip(),
                      user_ip=ip,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 26
0
async def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = await Blog.find(id)
    blog_user = await User.find(blog.user_id)
    send_email.send_comment_email(blog_user.email, content, '/blog/%s' % id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    await comment.save()
    return comment
Esempio n. 27
0
def get_blog(id):
    blog = yield from Blog.find(id)
    logger.info('blog id is :{}'.format(id))
    if not blog:
        raise APIValueError('id', 'can not find blog id is :{}'.format(id))
    if not blog.enabled:
        raise APIResourceNotFoundError(
            'id',
            'Sorry, This articale can\'t find now, Please try it again later...'
        )

    blog.view_count += 1
    yield from blog.update()
    #comments = yield from Comment.find_all('blog_id=?', [id], order_by='created_at desc')
    comments = yield from CommentAnonymous.find_all('blog_id=?', [id],
                                                    order_by='created_at asc')

    tags = []
    if blog.tags:
        for tag_id in blog.tags.split(","):
            tag = yield from Tags.find(tag_id)
            if tag:
                tags.append({
                    "key": tag.id,
                    "value": tag.name,
                    "color": COLORS[tag.id % len(COLORS)]
                })

    blog.keywords = ",".join([x["value"] for x in tags])
    blog.tags = tags
    for c in comments:
        c.html_content = markdown2.markdown(
            c.content,
            extras=[
                'code-friendly', 'fenced-code-blocks', 'highlightjs-lang',
                'tables', 'break-on-newline'
            ]).replace("<table>", "<table class=\"ui celled table\">")
    blog.html_content = markdown2.markdown(
        blog.content,
        extras=[
            'code-friendly', 'fenced-code-blocks', 'highlightjs-lang',
            'tables', 'break-on-newline'
        ]).replace("<table>", "<table class=\"ui celled table\">")
    return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
Esempio n. 28
0
def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError("Please signin first.")
    # 验证评论内容的存在性
    if not content or not content.strip():
        raise APIValueError("content", "content cannot be empty")
    # 检查博客的存在性
    blog = yield from Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError("Blog", "No such a blog.")
    # 创建评论对象
    comment = Comment(user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      blog_id=blog.id,
                      content=content.strip())
    yield from comment.save()  # 储存评论入数据库
    return comment  # 返回评论
Esempio n. 29
0
async def api_create_comment(id, request, *, content):
	# 对某个博客发表评论
	user = request.__user__
	# 必须在登陆状态下评论
	if user is None:
		raise APIPermissionError('content')
	# 评论不能为空
	if not content or not content.strip():
		raise APIValueError('content')
	# 查询以下博客id是否有对应的博客
	blog = await Blog.find(id)
	# 没有的话抛出错误
	if blog is None:
		raise APIResourceNotFoundError('Blog')
	# 构建一条评论
	comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip())
	# 保存到评论表里
	await comment.save()
	return comment
Esempio n. 30
0
def get_about():
    about = yield from Blog.find_all(where='name=?', args=['__about__'])
    if len(about) == 0:
        raise APIResourceNotFoundError('about', 'can not find about page.')

    comments = yield from CommentAnonymous.find_all('blog_id=?', [about[0].id], order_by='created_at asc')
    for c in comments:
        c.html_content = markdown2.markdown(c.content, extras=['code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline']).replace("<table>", "<table class=\"ui celled table\">")

    about[0].html_content = markdown2.markdown(about[0].content, extras = ['code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline']).replace("<table>", "<table class=\"ui celled table\">")
    about[0].view_count += 1
    yield from about[0].update()


    return {
        '__template__': 'about.html',
        'blog': about[0],
        'comments': comments
    }