Exemplo n.º 1
0
    def post(self):
        with self.api.pgsql.transaction() as tx:
            form = self.get_form()
            posted = form.to_dict()

            post_id = posted.get("id", 0)
            print(posted)
            if not post_id:
                log.error("Missing post id")
                self.abort(404)

            post = posts_model.get_post_by_id(tx, int(post_id))
            if not post:
                log.error("Editing inexistent post")
                self.abort(404)

            tags = posted.get("tags", "")
            tags = list(set(
                filter(lambda t: t,
                       map(lambda w: w.strip(), tags.split(",")))
            ))
            print("TAGS", tags)
            posted["tags"] = tags
            post.update(posted)

            cache = EditorCache(self.api, self.session.id, post_id)
            if posted.get("preview", None):
                cache.store(post)
                print("PREVIEW", cache.load())
            print("UPDATED", post)
            tx.connection.commit()
            return self.redirect("post.details", post_id=post_id, slug=post.get("slug", ""))
Exemplo n.º 2
0
Arquivo: comments.py Projeto: mcptr/ki
    def get(self, comment_id):
        post = None
        with self.api.pgsql.transaction() as tx:
            saved = comments_model.get(tx, comment_id)
            if not saved:
                self.abort(404)

            is_same_user = (flask.g.user.id == saved.user_id)
            is_admin = flask.g.user.is_admin
            is_moderator = flask.g.user.is_moderator

            if not (is_admin or is_moderator or is_same_user):
                self.abort(403)

            comments_model.delete(tx, comment_id)
            post = posts_model.get_post_by_id(tx, saved.post_id)
            tx.connection.commit()

        if post:
            return self.redirect("post.details",
                                 post_id=post.get("id", 0),
                                 slug=post.get("slug", ""),
                                 _anchor="comment-%d" % (comment_id))
        else:
            return self.redirect("posts.main")
Exemplo n.º 3
0
Arquivo: comments.py Projeto: mcptr/ki
    def post(self, **kwargs):
        f = self.get_form()
        post_id = int(f.get("post_id", None) or 0)
        comment_id = int(f.get("comment_id", None) or 0)
        parent_id = (int(f.get("parent_id", 0)) or None)
        content = str(f.get("content", None) or "")

        if not post_id:
            self.abort(402)

        u = flask.g.user
        with self.api.pgsql.transaction() as tx:
            post_record = posts_model.get_post_by_id(tx, post_id)

            if not post_record:
                self.abort(404)

            saved_comment_id = None
            if comment_id:
                orig = comments_model.get(tx, comment_id)
                if not orig:
                    self.abort(404)

                is_same_user = (u.id == orig.user_id)
                is_admin = u.is_admin
                is_moderator = u.is_moderator

                if not (is_same_user or u.is_admin or u.is_moderator):
                    self.abort(403)

                saved_comment_id = comments_model.update(
                    tx, comment_id, content)
            else:
                saved_comment_id = comments_model.create(
                    tx,
                    post_record["id"],
                    content,
                    user_id=flask.g.user.id,
                    parent_id=parent_id)

            tx.connection.commit()
            anchor = None
            if saved_comment_id:
                anchor = "comment-%d" % (saved_comment_id
                                         if saved_comment_id else None)
            else:
                anchor = "comment-%d" % parent_id if parent_id else None

            return self.redirect(
                "post.details",
                post_id=post_record["id"],
                slug=post_record["slug"],
                _anchor=anchor,
            )
Exemplo n.º 4
0
    def get(self, post_id, slug=None, **kwargs):
        post = None
        comments = []
        with self.api.pgsql.transaction() as tx:
            post = posts_model.get_post_by_id(tx, int(post_id))
            comments = comments_model.get_comments_tree(tx, post_id)

        return self.mk_response(
            template=self.template,
            post=post,
            comments=comments,
            reply_to_id=self.get_argument("reply_to_id", None, int),
            edit_comment_id=self.get_argument("edit_comment_id", None, int),
        )
Exemplo n.º 5
0
    def get(self, post_id=None):
        if not post_id:
            self.abort(404)

        with self.api.pgsql.transaction() as tx:
            post = posts_model.get_post_by_id(tx, post_id)
            if not post:
                self.abort(404)

            cache = EditorCache(self.api, self.session.id, post_id)
            cached = cache.load()
            print("CACHED", cached)
            post.update(cached)
            # FIXME: AUTHORIZE
            # assert_authorized("post.edit", flask.g.user, orig_post)
            # original_post.update(cached)

            tx.connection.commit()

            return self.mk_response(
                template=self.template,
                post=post,
            )