Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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('/')
Esempio 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)
Esempio n. 5
0
    def post(self):
        username = self.user.name
        subject = self.request.get("subject")
        content = self.request.get("content")
        owner = username
        parent = blog_key()

        if subject and content and username:  # Both fields are populated
            # and user is valid
            p = Blog_post(parent=parent,
                          owner=owner,
                          subject=subject,
                          content=content)
            p.put()
            post_id = p.key().id()
            self.redirect("/posts/{}".format(post_id))
        else:
            error = "Please fill in both the Subject and Content fields."
            self.render_form(subject, content, username, error)
Esempio n. 6
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('/')
Esempio n. 7
0
    def render_blog(self, blog_posts="", username="", source=""):

        if self.user:
            username = self.user.name  # check if user is logged in and get name

        source = self.request.url  # Posted to the Like handler so it can redirect back

        blog_posts = Blog_post.all().ancestor(
            blog_key()).order('-created').run()

        self.render("home.html",
                    blog_posts=blog_posts,
                    username=username,
                    source=source)
Esempio n. 8
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("/")
Esempio n. 9
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("/")
Esempio n. 10
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('/')
Esempio n. 11
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('/')
Esempio n. 12
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('/')
Esempio n. 13
0
from blog_post import Blog_post

blog1 = Blog_post("John Doe", "Lorem Ipsum", "Lorem ipsum dolor sit amet.",
                  "2000.05.04.")

blog2 = Blog_post(
    "Tim Urban", "Wait but why",
    "A popular long-form, stick-figure-illustrated blog about almost everything.",
    "2010.10.10.")

blog3 = Blog_post(
    "William Turton", "One Engineer Is Trying to Get IBM to Reckon With Trump",
    "Daniel Hanley, a cybersecurity engineer at IBM, doesn’t want to be the center of attention. When I asked to take his picture outside one of IBM’s New York City offices, he told me that he wasn’t really into the whole organizer profile thing.",
    "2017.03.28.")

print(blog1.authorName)
print(blog2.title)
Esempio n. 14
0
 def render_posts(self, blog_posts="", username=""):
     username = self.user.name
     blog_posts = Blog_post.all().filter("owner =",
                                         username).order('-created').run()
     self.render("my_posts.html", blog_posts=blog_posts, username=username)