def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: postID = self.request.get('postID') post_obj = Post.get_by_id(int(postID)) like_obj = Likes.query(Likes.post == post_obj.key).get() if post_obj.user == cookie_user.key: self.write(json.dumps(({'like_count': -1}))) else: if like_obj: liked_by = like_obj.user_id for u_id in liked_by: if u_id == cookie_user.key.id(): self.write(json.dumps(({'like_count': -2}))) return like_obj.like_count += 1 liked_by.append(cookie_user.key.id()) like_obj.put() self.write( json.dumps(({ 'like_count': like_obj.like_count }))) else: like_obj = Likes(post=post_obj.key, like_count=1) like_obj.user_id.append(cookie_user.key.id()) like_obj.put() time.sleep(0.2) self.write( json.dumps(({ 'like_count': like_obj.like_count }))) else: return None
def get(self, post_id): """ get: Get Args: self: This object post_id: Post ID """ if not self.user: return self.redirect('/login') key = db.Key.from_path('Post', int(post_id), parent=blog_key()) b = db.get(key) if not b: self.render('notfound.html') return # # Check that user IDs do not match # user_id = self.user.key().id() if b.user_id == user_id: return self.redirect('/blog/%s' % str(b.key().id())) if b.is_liked_by_user(user_id) == 'Liked': return self.redirect('/blog/%s' % str(b.key().id())) l = Likes(parent=blog_key(), post_id=int(post_id), user_id=int(user_id)) l.put() self.redirect('/blog/%s' % str(b.key().id()))
def get(self): login = self.read_cookie('login') uid = None if login: uid = int(login) user = Users.get_by_id(uid) post_id = self.request.get("p") if post_id: if uid: post = Post.get_by_id(int(post_id)) if int(uid) == int(post.user.key().id()): #your own post self.redirect("/blog/" + post_id + "?e=like_own") else: likes = db.GqlQuery( "select * from Likes where user = :1 and post = :2", user.key(), post.key()) if likes.count() == 0: new_like = Likes(post=post, user=user) new_like.put() self.redirect("/blog/" + str(post.key().id())) else: self.redirect("/blog/" + post_id + "?e=like_alrd") else: self.redirect("/blog/" + post_id + "?e=like_permission") else: self.redirect("/blog")
def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: postID = self.request.get('postID') post_obj = Post.get_by_id(int(postID)) like_obj = Likes.query(Likes.post == post_obj.key).get() if post_obj.user == cookie_user.key: self.write(json.dumps(({'like_count': -1}))) else: if like_obj: liked_by = like_obj.user_id for u_id in liked_by: if u_id == cookie_user.key.id(): self.write(json.dumps(({'like_count': -2}))) return like_obj.like_count += 1 liked_by.append(cookie_user.key.id()) like_obj.put() self.write(json.dumps(({'like_count': like_obj.like_count}))) else: like_obj = Likes(post=post_obj.key, like_count=1) like_obj.user_id.append(cookie_user.key.id()) like_obj.put() time.sleep(0.2) self.write(json.dumps(({'like_count' : like_obj.like_count}))) else: return None
def add_like(self, thelike, type=u'facebook'): """ Adds the like to the current user. """ if self.current_user: logging.info("add_like: %s %s"%(str(self.current_user),str(thelike))) new_like = Likes(user=self.current_user, source=u'facebook', category=thelike[u'category'], name=thelike[u'name'], likeid=thelike[u'id']) # Check for duplicates if db.GqlQuery("SELECT __key__ FROM Likes WHERE user = :1 AND name = :2", new_like.user, new_like.name).count(1) == 0: new_like.put() # Does not exist so put it in the data store else: return
def get(self, postID): if self.session.get("username"): user = db.GqlQuery("SELECT * FROM User WHERE username='******'" % self.session.get("username")) userID = user.get().key().id() key = db.Key.from_path('Posts', int(postID)) post = db.get(key) if int(post.author) != int(userID): new_post_like = Likes(postID=int(postID), userID=int(userID), likesCount=1) new_post_like.put() self.redirect("/") else: self.redirect("/") else: self.redirect("/")
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 post(self): self.title = self.request.get("title") self.content = self.request.get("content") self.author = self.get_user() params = dict(title = self.title, content=self.content, this_user = self.author) if self.author and self.title and self.content: blogpost = BlogInfo(content = self.content, title = self.title, author = self.author) blogpost.put() likes_value = 0 dislikes_value = 0 who_rated = [] associated_post = blogpost.key().id() like_status = Likes(likes_value = likes_value, dislikes_value = dislikes_value, associated_post = associated_post, who_rated = who_rated, parent = blogpost) like_status.put() self.redirect("/blog/%s" % str(blogpost.key().id())) elif self.author and not (self.title and self.content): params.update(main.loggedin_params) params["error"] = "Please provide the content and the title" self.render("new_post.html", **params) else: params.update(main.loggedout_params) params["error"] = "Please log in to make a post" self.render("new_post.html", **params)
def post(self): loggedin_user = self.get_user_from_cookie() all_users = User.query() posts = Post.query() post_id = self.request.get('post_id') like_count = self.request.get('like_count') post = Post.get_by_id(int(post_id)) if post: new_like_object = Likes(post=post.key, like_count=int(like_count)) key = new_like_object.put() time.sleep(0.1) new_like = key.get() post_key = new_like.post post = post_key.get() self.write("Like Count: " + str(new_like.like_count) + "Post Title:" + post.Title)
def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: content = self.request.get('content') title = self.request.get('post-title') is_draft = self.request.get('draft') if is_draft == 'on': is_draft = True else: is_draft = False if title != 'Click here to give a Title!' and \ content != '<p>Start writing here...</p>': new_post = Post(title=title, content=content, user=cookie_user.key, is_draft=is_draft) new_post_key = new_post.put() time.sleep(0.3) like_obj = Likes(post=new_post_key, like_count=0) like_key = like_obj.put() time.sleep(0.1) if is_draft: self.redirect('/user/%s/drafts' % str(cookie_user.key.id())) else: self.redirect('/blog/%s' % str(new_post_key.id())) else: empty_post = "Both Title and Content needed for the Blog!" self.render('newpost.html', user=cookie_user, title=title, content=content, empty_post=empty_post) else: cookie_error = "Your session has expired please login again to continue!" self.render('login.html', cookie_error=cookie_error)
def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: content = self.request.get('content') title = self.request.get('post-title') is_draft = self.request.get('draft') if is_draft == 'on': is_draft = True else: is_draft = False if title != 'Click here to give a Title!' and \ content != '<p>Start writing here...</p>': new_post = Post( title=title, content=content, user=cookie_user.key, is_draft=is_draft ) new_post_key = new_post.put() time.sleep(0.3) like_obj = Likes(post=new_post_key, like_count=0) like_key = like_obj.put() time.sleep(0.1) if is_draft: self.redirect('/user/%s/drafts' % str(cookie_user.key.id())) else: self.redirect('/blog/%s' % str(new_post_key.id())) else: empty_post = "Both Title and Content needed for the Blog!" self.render('newpost.html', user=cookie_user, title=title, content=content, empty_post=empty_post) else: cookie_error = "Your session has expired please login again to continue!" self.render('login.html', cookie_error=cookie_error)
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))