Ejemplo n.º 1
0
    def post(self):

        username = self.user.name
        post_id = self.request.get("post_id")
        post_id = int(post_id)
        liked = self.request.get("liked")
        # username = self.request.get("username")
        source = self.request.get("source")
        source = str(source)

        p = Blog_post.by_id(post_id)
        if username != p.owner:  # Check that user is valid and doesn't own the post
            if liked == "False":  # User clicked Unlike
                if hasattr(p, username):  # If they've previously liked...
                    delattr(p, username)  # Delete the like
                    p.like_count -= 1  # And decrement the counter
                    p.put()
                    self.redirect(source)
            else:  # User clicked Like
                if hasattr(p, username):  # If they've previously liked...
                    self.redirect("/")  # Redirect to home page
                else:
                    setattr(p, username, "liked")  # If not, add the like
                    p.like_count += 1  # And increment the counter
                    p.put()
                    self.redirect(source)
Ejemplo n.º 2
0
    def post(self, post_id):

        username = self.user.name
        comment = self.request.get("comment")
        owner = username
        post_id = self.request.get("post_id")
        post_id = int(post_id)

        q = Blog_post.by_id(post_id)
        parent = q.key()  #Comment's parent is key of associated post
        subject = q.subject

        if username:  # Check that user is valid
            if comment:  # and comment is populated
                c = Comment(parent=parent,
                            owner=owner,
                            comment=comment,
                            subject=subject)
                c.put()
                q.comment_count += 1
                q.put()
                self.redirect("/posts/{}".format(post_id))
            else:
                error = "Please add your comment."
                self.render_form(subject, comment, username, error, post_id)
        else:
            self.redirect('/')
Ejemplo n.º 3
0
    def get(self, post_id):

        if self.user:
            username = self.user.name
        else:
            username = None

        post_id_int = int(post_id)

        p = Blog_post.by_id(post_id_int)
        subject = p.subject
        content = p.content
        created = p.created
        owner = p.owner
        comment_count = p.comment_count
        like_count = p.like_count
        comment_key = p.key()
        source = self.request.url

        comments = Comment.all().ancestor(comment_key).order('-created').run()

        if username:
            liked = hasattr(p, username)
        else:
            liked = ""

        self.render_post(subject, content, created, comments, comment_count,
                         like_count, username, owner, post_id, source, liked)
Ejemplo n.º 4
0
    def get(self, post_id):

        username = self.user.name
        post_id = int(post_id)

        q = Blog_post.by_id(post_id)
        subject = q.subject
        self.render_form(subject=subject, username=username, post_id=post_id)
Ejemplo n.º 5
0
    def post(self):

        username = self.user.name  # Check that the user is valid
        post_id = self.request.get("post_id")
        post_id = int(post_id)

        a = Blog_post.by_id(post_id)

        if a.owner == username:  # Check that the user owns the post
            db.delete(a.key())
            self.redirect('/')
        else:
            self.redirect('/')
Ejemplo n.º 6
0
    def get(self, post_id, comment_id):

        username = self.user.name
        comment_id = int(comment_id)
        post_id = int(post_id)

        p = Blog_post.by_id(post_id)
        parent = p.key()  #Comment's parent is key of associated post

        c = Comment.by_id(comment_id, parent)
        subject = c.subject
        comment = c.comment
        owner = c.owner

        if c.owner == username:  # Check to ensure current user owns comment
            self.render_form(subject, comment, username, post_id)
        else:
            self.redirect("/")
Ejemplo n.º 7
0
    def get(self, post_id):

        post_id = int(post_id)
        username = self.user.name

        a = Blog_post.by_id(post_id)
        subject = a.subject
        content = a.content
        created = a.created.date()
        owner = a.owner

        if a.owner == username:  # Check to ensure current user owns post
            self.render_form(subject,
                             content,
                             created,
                             username,
                             owner,
                             post_id=post_id)
        else:
            self.redirect("/")
Ejemplo n.º 8
0
    def post(self):

        username = self.user.name  # Check that the user is valid

        post_id = self.request.get("post_id")
        post_id = int(post_id)

        comment_id = self.request.get("comment_id")
        comment_id = int(comment_id)

        p = Blog_post.by_id(post_id)
        parent = p.key()

        c = Comment.by_id(comment_id, parent)

        if c.owner == username:  # Check that the user owns the comment
            db.delete(c.key())
            p.comment_count -= 1
            p.put()
            self.redirect('/posts/{}'.format(post_id))
        else:
            self.redirect('/')
Ejemplo n.º 9
0
    def post(self, post_id, comment_id):

        username = self.user.name
        post_id_int = int(post_id)
        comment_id = int(comment_id)
        subject = self.request.get("subject")
        comment = self.request.get("comment")

        p = Blog_post.by_id(post_id_int)
        parent = p.key()

        c = Comment.by_id(comment_id, parent)
        owner = c.owner

        if username == owner:  # Check that user is valid and owns comment
            if comment:
                c.comment = comment
                c.put()
                self.redirect("/posts/{}".format(post_id))
            else:
                error = "Please add your comment."
                self.render_form(subject, comment, username, post_id, error)
        else:
            self.redirect('/')
Ejemplo n.º 10
0
    def post(self, post_id):

        post_id = int(post_id)
        username = self.user.name

        subject = self.request.get("subject")
        content = self.request.get("content")

        a = Blog_post.by_id(post_id)
        created = a.created.date()
        owner = a.owner

        if (username == owner):  # Check that user owns the post
            if subject and content:  # And that subject and content are populated
                a.subject = subject
                a.content = content
                a.put()
                self.redirect("/posts/{}".format(post_id))
            else:
                error = "Please fill in both the Subject and Content fields."
                self.render_form(subject, content, created, username, owner,
                                 error, post_id)
        else:
            self.redirect('/')