示例#1
0
文件: views.py 项目: Scrub000/website
def edit(slug: str):
    try:
        blog = queries.get_blog(slug=slug)
    except exceptions.DoesNotExist:
        flask.abort(status=404)
    flask_bouncer.ensure(action=flask_bouncer.EDIT, subject=blog)
    form = forms.Edit(obj=blog)
    form.categories.query = category_models.Category.query.all()
    if form.validate_on_submit():
        try:
            operations.update_blog(
                blog=blog,
                title=form.title.data,
                description=form.description.data,
                body=bleach.clean(text=form.body.data,
                                  **constants.BLEACH_KWARGS),
                categories=form.categories.data,
                published=form.published.data,
                comment=form.comment.data,
            )
        except exceptions.UnableToUpdate:
            flask.flash(message="Unable to update blog.", category="error")
            return flask.redirect(
                location=flask.url_for(endpoint="blogs.edit", slug=blog.slug))
        else:
            flask.flash(message="Blog updated.", category="success")
            return flask.redirect(location=flask.url_for(
                endpoint="blogs.display", slug=blog.slug))
    context = {"title": f"Edit {blog.title}", "form": form}
    return flask.render_template(template_name_or_list="blogs/edit.html",
                                 **context)
示例#2
0
文件: views.py 项目: Scrub000/website
 def put(self, id: int):
     blog = queries.get_blog(id=id)
     flask_bouncer.ensure(action=flask_bouncer.EDIT, subject=blog)
     payload = request.get_json(force=True)
     data = self.serializer.load(data=payload)
     operations.update_blog(blog=blog, **data)
     return self.serializer.dump(obj=blog), http.HTTPStatus.ACCEPTED
示例#3
0
文件: views.py 项目: Scrub000/website
def display(slug: str):
    try:
        blog = queries.get_blog(slug=slug)
    except exceptions.DoesNotExist:
        flask.abort(status=404)
    flask_bouncer.ensure(action=flask_bouncer.READ, subject=blog)
    context = {"title": blog.title, "blog": blog}
    return flask.render_template(template_name_or_list="blogs/display.html",
                                 **context)
示例#4
0
文件: views.py 项目: Scrub000/website
 def get(
     self, blog_id: Optional[int] = None, parent_id: Optional[int] = None
 ):
     kwargs = dict()
     if blog_id:
         kwargs["blog"] = blog_queries.get_blog(id=blog_id)
     elif parent_id:
         kwargs["parent"] = queries.get_comment(id=parent_id)
     comments = queries.get_comments(**kwargs)
     return (
         self.serializer.dump(obj=comments, many=True),
         http.HTTPStatus.OK,
     )
示例#5
0
文件: views.py 项目: Scrub000/website
 def post(
     self, blog_id: Optional[int] = None, parent_id: Optional[int] = None
 ):
     flask_bouncer.ensure(
         action=flask_bouncer.CREATE, subject=models.Comment
     )
     payload = request.get_json(force=True)
     data = self.serializer.load(data=payload)
     data["author"] = flask_login.current_user
     if blog_id:
         data["blog"] = blog_queries.get_blog(id=blog_id)
     elif parent_id:
         data["parent"] = queries.get_comment(id=parent_id)
     comment = operations.create_comment(**data)
     return self.serializer.dump(obj=comment), http.HTTPStatus.CREATED
示例#6
0
文件: views.py 项目: Scrub000/website
def delete(slug: str):
    try:
        blog = queries.get_blog(slug=slug)
    except exceptions.DoesNotExist:
        flask.abort(status=404)
    flask_bouncer.ensure(action=flask_bouncer.DELETE, subject=blog)
    form = forms.Delete()
    if form.validate_on_submit():
        try:
            operations.delete_blog(blog=blog)
        except exceptions.UnableToDelete:
            flask.flash(message="Unable to delete blog.", category="error")
            return flask.redirect(location=flask.url_for(
                endpoint="blogs.display", slug=blog.slug))
        else:
            flask.flash(message="Blog deleted.", category="success")
            return flask.redirect(location=flask.url_for(
                endpoint="main.landing"))
    context = {"title": f"Delete {blog.title}", "form": form}
    return flask.render_template(template_name_or_list="blogs/delete.html",
                                 **context)
示例#7
0
文件: views.py 项目: Scrub000/website
 def delete(self, id: int):
     blog = queries.get_blog(id=id)
     flask_bouncer.ensure(action=flask_bouncer.DELETE, subject=blog)
     blog.delete()
     return dict(), http.HTTPStatus.NO_CONTENT
示例#8
0
文件: views.py 项目: Scrub000/website
 def get(self, id: int):
     blog = queries.get_blog(id=id)
     return self.serializer.dump(obj=blog), http.HTTPStatus.OK