コード例 #1
0
    def post(self, post_id):

        if not Auth.is_logged_in(self.request):
            self.redirect("/login")
        else:
            current_user = self.current_user

            post = Post.by_id(int(post_id))

            # verify if post exists
            if post is not None:

                # verify if this post matches user logged in
                if post.author.key().id() == current_user.key().id():

                    title = self.request.get("title")
                    text = self.request.get("content")

                    if title and text:
                        post = Post.by_id(int(post_id))
                        post.title = title
                        post.content = text
                        post.put()
                        self.redirect("/post/" + str(post.key().id()))
                    else:
                        error = "Title and Text must be provided" \
                                " to submit an article!"
                        self.render("newpost.html", error=error)

                else:  # redirect the user to the view
                    self.redirect("/post/" + str(post.key().id()))

            else:  # redirect the user to the view
                self.redirect("/not-found")
コード例 #2
0
    def post(self):

        if not Auth.is_logged_in(self.request):
            self.response.content_type = 'application/json'
            response_obj = {
                'type': 'error',
                'message': 'You must be logged in to comment on posts!'
            }
            self.response.write(json.encode(response_obj))
        else:
            current_user = self.current_user
            post = Post.by_id(int(self.request.get("pid")))
            text = self.request.get("text")
            comment_id = self.request.get("comment_id")

            if text:

                is_edit = False

                # if is a edit to comment
                if comment_id:
                    comment = Comment.by_id(int(comment_id))
                    comment.text = text
                    is_edit = True
                else:
                    comment = Comment(author=current_user,
                                      post=post,
                                      text=text)

                comment.put()

                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'success',
                    'message': 'Comment created!',
                    'editing': is_edit,
                    'comment': {
                        "id": comment.key().id(),
                        "author": current_user.username,
                        "text": text,
                        "time": comment.created.strftime("%d. %B %Y")
                    }
                }
                self.response.write(json.encode(response_obj))
            else:
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'error',
                    'message': 'Comment not created!'
                }
                self.response.write(json.encode(response_obj))
コード例 #3
0
    def get(self, post_id):

        current_user = self.current_user

        post = Post.by_id(int(post_id))

        # verify if post exists
        if post is not None:

            # verify if this post matches user logged in
            if post.author.key().id() == current_user.key().id():
                self.render("editpost.html", post=post)
            else:  # redirect the user to the view
                self.redirect("/post/" + str(post.key().id()))

        else:
            self.redirect("/not-found")
コード例 #4
0
ファイル: like.py プロジェクト: diegopettengill/multiuserblog
    def post(self):

        if not Auth.is_logged_in(self.request):
            self.response.content_type = 'application/json'
            response_obj = {
                'type': 'error',
                'message': 'You must be logged in to like posts!'
            }
            self.response.write(json.encode(response_obj))
        else:

            current_user = self.current_user
            post = Post.by_id(int(self.request.get("pid")))

            # checks if user already liked this post, if returns > 0
            user_liked = Like.check_user_liked(current_user, post)

            # checks if this post belongs to the current_user
            if post.author.key().id == current_user.key().id:
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'error',
                    'message': 'You can`t like you own posts :('
                }
                self.response.write(json.encode(response_obj))
            else:
                if user_liked > 0:
                    Like.unlike(current_user, post)
                    self.response.content_type = 'application/json'
                    response_obj = {
                        'type': 'success',
                        'message': 'Post unliked!',
                        'action': 'unlike'
                    }
                    self.response.write(json.encode(response_obj))
                else:
                    like = Like(author=current_user, post=post)
                    like.put()

                    self.response.content_type = 'application/json'
                    response_obj = {
                        'type': 'success',
                        'message': 'Post liked!',
                        'action': 'like'
                    }
                    self.response.write(json.encode(response_obj))
コード例 #5
0
    def get(self, post_id):

        current_user = self.current_user

        post = Post.by_id(int(post_id))

        if post is not None:

            # verify if this post matches user logged in
            if post.author.key().id() == current_user.key().id():
                post.delete()
                self.redirect("/")
            else:  # redirect the user to the view
                self.redirect("/post/" + str(post.key().id()))

        else:
            self.redirect("/not-found")