def post(self, post_id): self.p = BlogInfo.get_by_id(int(post_id)) self.post_comments = Comment.all().ancestor(self.p).fetch(limit=10) self.like_status = Likes.all().ancestor(self.p).get() self.comment_author = self.get_user() self.comment_text = self.request.get("comment") params = dict(p = self.p, post_comments = self.post_comments, like_status = self.like_status, this_user = self.comment_author) if self.comment_text and self.comment_author: comment = Comment(comment_text = self.comment_text, comment_author = self.comment_author, associated_post = int(post_id), like_status = self.like_status, parent=self.p) comment.put() self.redirect("/blog/%s" % post_id) elif self.comment_author and not self.comment_text: params.update(main.loggedin_params) params["no_comment"] = "Please enter text" self.render("permalink.html", **params) else: params.update(main.loggedout_params) params["cant_comment"] = "Please log in to comment" self.render("permalink.html", **params)
def get(self, post_id, is_liked): self.p = BlogInfo.get_by_id(int(post_id)) self.this_user = self.get_user() self.like_status = Likes.all().ancestor(self.p).get() self.post_comments = Comment.all().ancestor(self.p).fetch(limit=10) params = dict(p = self.p, this_user = self.this_user, like_status = self.like_status, post_comments = self.post_comments) if self.this_user in self.like_status.who_rated: params["like_error"] = "You've already rated this post" params.update(main.loggedin_params) self.render("rate_error.html", **params) elif self.this_user and self.p.author != self.this_user: is_liked = int(is_liked) if is_liked == 0: self.like_status.likes_value += 1 self.like_status.who_rated += [self.this_user] self.like_status.put() self.redirect("/blog/%s" % post_id) else: self.like_status.dislikes_value += 1 self.like_status.who_rated += [self.this_user] self.like_status.put() self.redirect("/blog/%s" % post_id) elif self.p.author == self.this_user: params["like_error"] = "Not allowed to like and dislike own posts" params.update(main.loggedin_params) self.render("rate_error.html", **params) else: params["cant_like"] = "Please login to like and dislike posts" params.update(main.loggedout_params) self.render("rate_error.html", **params)
def post(self, id): # only logged in users may like/unlike posts if not self.user: return self.redirect('/login') post = Post.get_by_id(int(id)) user_id = self.user.key().id() # users may not like their own posts if user_id == post.creator: self.write('You are not allowed to like your own post') else: l = Likes.all().ancestor(post).filter('user ='******'/%s' % id)
def get(self, id): post = Post.get_by_id(int(id)) comments = Comment.all().ancestor(post) # make sure the post exists if not post: self.error(404) return # only logged in users may like/unlike posts if self.user: user_id = self.user.key().id() # a user may only like/unlike the post once if Likes.all().ancestor(post).filter('user ='******'unlike' else: vote = 'like' self.render('post.html', post=post, vote=vote, comments=comments) else: self.render('post.html', post=post, comments=comments)
def get(self, post_id): self.p = BlogInfo.get_by_id(int(post_id)) self.post_comments = Comment.all().ancestor(self.p).fetch(limit=10) self.like_status = Likes.all().ancestor(self.p).get() self.this_user = self.get_user() params = dict(p = self.p, post_comments = self.post_comments, like_status = self.like_status, this_user = self.this_user) if self.this_user: params.update(main.loggedin_params) self.render("permalink.html", **params) else: params.update(main.loggedout_params) self.render("permalink.html", **params) if not self.p: self.error(404) return
def get(self): user_id = None # AUTHENTICATE check for valid cookie user_id = auth(self.request.cookies.get('user_id')) if user_id: # get the logged in user try: u2 = Users.get_by_id(int(user_id)) except: pass if u2: # get the username NOT the id user_name = u2.userName postId = self.request.get("postId") error = self.request.get("error") q = None # querie BlogEntry for the blog entity filtered by title try: q = BlogEntry.get_by_id(int(postId)) title = q.title body = q.body created = q.created last_mod = q.last_mod author_id = q.author_id k = q.key() except: pass if q: # get the user who is the author of the blogpost try: u = Users.get_by_id(int(author_id)) except: pass if u: # get the authors username NOT id author = u.userName comments = None # get all comments for this blogpost try: comments = Comments.all().ancestor(k) except: pass # if there are no comments.... if not comments: commentError = "No comments" else: commentError = "" else: error = "Could not access the blog post." self.redirect("/welcome?error=%s" % error) # count likes in the database for display, all relevant likes are\ # ancestors of the blog post with title count = 0 likes = None try: likes = Likes.all().ancestor(k) except: pass if likes: # iterate through all likes for this post to count them for like in likes: if like.user_id: count += 1 self.render("focus.html", postId=postId, title=title, body=body, created=created, last_mod=last_mod, author=author, comments=comments, error=error, count=count, commentError=commentError, user_name=user_name) else: self.redirct("/login")
def post(self): def render_focus(title, body, created, last_mod, author, comments, count, error, postId, user_name): self.render("focus.html", title=title, body=body, created=created, last_mod=last_mod, author=author, comments=comments, count=count, error=error, postId=postId, user_name=user_name) postId = self.request.get("postId") user_id = None # AUTHENTICATE check for valid cookie user_id = auth(self.request.cookies.get('user_id')) # get the logged in user to get username if user_id: try: u = Users.get_by_id(int(user_id)) except: pass if u: user_name = u.userName # query for the usual blogPost entity properties try: q = BlogEntry.get_by_id(int(postId)) if q: title = q.title body = q.body created = q.created last_mod = q.last_mod author = u.userName author_id = q.author_id # k is the key to the blog post. k = q.key() except: pass try: # get all comments for for the blogpost - that means get # all comments who are ancestors of K (the blogpost). comments = Comments.all().ancestor(k) except: pass # check if user has already liked this post # all Likes have a user_id property which corresponds to the # User who LIKED the post, so query Likes filtered by user_id # to get ALL the likes for this user try: z = Likes.all().filter("user_id =", user_id) except: pass # then get the ONE (if theres is one) that is an ancestor # of the blog post if z: try: alreadyLiked = z.ancestor(k).get() except: pass # set flag default = go flag = "go" # if there are ZERO likes in the db, you'll get an error bc # the query gets nothing. To prevent the error, use try/except try: if alreadyLiked.user_id: flag = "nogo" except: pass # initialize the counter count = 0 # If the logged in user is the author then error if user_id == author_id: # repaint page likes = Likes.all().ancestor(k) count = 0 for like in likes: try: if like.user_id: count += 1 except: pass error = "You can't like your own posts." render_focus(title, body, created, last_mod, author, comments, count, error, postId, user_name) else: # if the logged in user has already liked this post then error if flag == "nogo": error = "Stop it....You already liked this post." likes = Likes.all().ancestor(k) count = 0 if likes: for like in likes: if like.user_id: count += 1 # repaint page render_focus(title, body, created, last_mod, author, comments, count, error, postId, user_name) else: # if tests are passed....record the LIKE; # record the userIDKEY in LIKES as a CHILD of the BLOGPOST # increment the like so it updates the display (not the db) # record like in the db - user_id is the only property and # it's ancestor is the blogpost k. try: l = Likes(parent=k, user_id=user_id) except: pass if l: l.put() error = "The Like was recorded." # count ALL the existing LIKES to update display \ # on VIEW post # filter by ancestor bc ALL likes are recorded as an \ # ancestor of ONE blogPost likes = Likes.all().ancestor(k) count = 0 if likes: for like in likes: if like.user_id: count += 1 # repaint page render_focus(title, body, created, last_mod, author, comments, count, error, postId, user_name) else: error = "Please signup and login to like a post." # if you are not logged in, you must sign up and login. self.redirect("/focus?postId=%s&error=%s" % (postId, error))