def get(self, post_id): # get post content, likes and comments from id passed in the url. post = db.get(db.Key.from_path('Post', int(post_id))) likes = Like.all().filter('post_id =', post_id).count() comments = Comment.all().filter('post_id = ', post_id) # check if logged in and get username. username = self.check_login(self.request.cookies.get('user_id')) # detect if user already liked the post. already_like = Like.all().filter('username='******'post_id=', post_id).get() # if post does not exist based id, show alert. if not post: self.render('alerts.html', alert='Post does not exist', path='/blog') # show the form. else: self.render('post-page.html', post=post, likes=likes, already_like=already_like, username=username, comments=comments)
def get(self, post_id): if self.good_cookie_exists(): # delete that SPECIFIC like from DB # MUST match both user and post_id h = self.request.cookies.get('user') user = h.split("|")[0] post_id_s = str(post_id) # make sure current user doesn't own the post # user post_id to query Post post_key = db.Key.from_path('Post', int(post_id)) if db.get(post_key) is not None: owns = self.user_owns_post(post_key) if owns: self.redirect("/") return else: likes = Like.all().filter("user = "******"post_id = ", # NOQA post_id_s) # NOQA key = likes.get().key() key_go = db.get(key) # verify the like exists if not key_go: self.redirect("/") return else: key_go.delete() self.render("successlike.html", loul="unliked") else: self.redirect("/") return else: self.redirect("/login") return
def get(self, post_id): if self.good_cookie_exists(): # put the user id and post_id into the Like entity # go to success page, which will have link to home h = self.request.cookies.get('user') curusor = h.split("|")[0] # make sure user doesn't own post post_key = db.Key.from_path( 'Post', int(post_id)) # returning something, but what??? if db.get(post_key) is not None: # if post exists, continue owns = self.user_owns_post(post_key) if owns: # if user owns the post, end self.redirect("/") return else: # otherwise post exists and user is not owner likes = Like.all().filter("post_id = ", str(post_id)).filter( "user = "******"/") # UI blocks multiple liking as well return else: # new Like entity l = Like(post_id=post_id, user=curusor) l.put() # commit entry self.render("successlike.html", loul="liked") else: self.redirect("/") return else: self.redirect("/login") return
def delete_post(cls, post_id, user_id): error_msg = None posts = Post.all().order('-created').fetch(limit=10) updated_post_list = posts post = Post.get_by_id(post_id) if user_id == post.created_by: # delete comments of the post comments = Comment.all().filter('post_id =', post_id).fetch(limit=10) for c in comments: Comment.delete(c) # delete likes of the post likes = Like.all().filter('post_id =', post_id).fetch(limit=10) for l in likes: Like.delete(l) # delete the post updated_post_list = cls.update_list_on_delete( object_list=posts, object_id=post_id ) Post.delete(post) else: error_msg = 'You can delete only your own posts' return updated_post_list, error_msg
def post(self): post_id = int(self.request.get('post_id')) user_id = int(self.request.get('user_id')) post_username = self.request.get('username') message = '' u = self.current_user() if u: # check if user is not liking own post if u.username != post_username: # check if post was already liked likes = Like.all().filter('post_id =', post_id).filter( 'username ='******'/view?p=%s&u=%s&#likes' % (post_id, user_id)) else: message = 'You can only like a post once' else: message = 'You cannot like your own post' self.redirect('/view?p=%s&u=%s&m=%s&#likes' % (post_id, user_id, message)) else: self.redirect('/login')
def get(self, post_id): # check if logged in and get username. username = self.check_login(self.request.cookies.get('user_id')) if not username: self.redirect('/login') else: # get the post. post = db.get(db.Key.from_path('Post', int(post_id))) # if post belongs to user, show alert. if post.username == username: self.render('alerts.html', alert='You cannot like your own post', path='/%s' % post_id) else: already_like = Like.all().filter('username='******'post_id=', post_id).get() # if user hasn't liked the post, create like. if not already_like: l = Like(username=username, post_id=post_id) l.put() self.render('alerts.html', alert='Like added to post', path='/%s' % post_id) # if user has already liked the post, show alert. else: self.render('alerts.html', alert='You can only like a post once', path='/%s' % post_id)
def get(self, slug): user = User.is_logged(self) query = Koch.all().filter( 'slug =', slug).fetch(1) if len( query ): koch = query[0]; alreadylike = False alreadyfollow = False likesusers = [] koch.views += 1 koch.put() author = koch.author avatar = helpers.get_gravatar( author.email, 90 ) author_recipes_count = Koch.all().filter('author =', author).count(); for like in Like.all().filter( 'koch =', koch ): if like.user.fb_profile_url: lavatar = "https://graph.facebook.com/%s/picture" % (like.user.nickname) elif not like.user.usegravatar and like.user.avatar: lavatar = "/avatar/?user_id=%s" %(like.user.key()) else: lavatar = helpers.get_gravatar( like.user.email, 90 ) likesusers.append({ 'nickname' : like.user.nickname, 'avatar' : lavatar }) if user: alreadylike = Like.alreadylike( koch, user ) alreadyfollow = Friendship.alreadyfollow( user, author ) is_owner = True if user.key() == author.key() else False if author.fb_profile_url: avatar = "https://graph.facebook.com/%s/picture" % (author.nickname) elif not author.usegravatar and author.avatar: avatar = "/avatar/?user_id=%s" %(author.key()) else: avatar = helpers.get_gravatar( author.email, 90 ) last_kochs = Koch.all().filter('author =', author).order('-created').fetch(5); last_from_all = Koch.get_random() current = "explore" humanlikes = intcomma( int( koch.likes) ) self.response.out.write(template.render('templates/details_koch.html', locals())) else: self.error(404)
def post(self, post_id, post): # Find users like on the post likes = Like.all() likes.filter("post_key =", post.key()) likes.filter("user_key =", self.user.key()) # Delete all likes by user on the post if likes: for like in likes: like.delete() self.redirect('/post/%s' % str(post.key().id())) else: self.redirect('/post/%s' % str(post.key().id()))
def like_update(cls, post_id, user_id): error_msg = '' post = Post.get_by_id(post_id) current_like = Like.all().filter('post_id =', post_id).filter('user_id =', int(user_id)).get() if user_id != post.created_by: if not current_like: # if there is no record, adding a record with value=True l = Like(user_id=int(user_id), post_id=int(post_id), like_value=True) l.put() return l, error_msg current_like.like_value = not current_like.like_value current_like.put() else: error_msg = 'You cannot like your own post' return current_like, error_msg
def get_post_bundle(cls, post_id, user=None): user_id = int(user.key().id()) if user else None post = Post.get_by_id(post_id) comments = Comment.all().filter('post_id =', post_id).order('-created').fetch(limit=10) like = Like.all().filter('post_id =', post_id).filter('user_id =', user_id).get() if user else None # username dictionary, to show usernames instead of IDs user_ids = set([c.created_by for c in comments]) user_ids.add(post.created_by) if user_id: user_ids.add(user_id) user_objects = User.get_by_id(user_ids) username_dict = {u.key().id(): u.username for u in user_objects} return PostBundle(post, comments, like, username_dict)
def get(self, post_id): # check if logged in and get username. username = self.check_login(self.request.cookies.get('user_id')) if not username: self.redirect('/login') # get like for given post and given user. else: l = Like.all().filter('username='******'post_id=', post_id).get() if not l: self.render('alerts.html', alert='You have not liked this post', path='/%s' % post_id) else: l.delete() self.render('alerts.html', alert='Like removed from post', path='/%s' % post_id)
def render_front(self, creator): posts = Post.all().order('-created') # posts = db.GqlQuery("select * from Post order by created desc # limit 10") # look for likes in Like entity for the current user (creator) likes = Like.all().filter("user = ", creator) dict = [] if likes: # iterate through and make dictionary from response for l in likes: dict.append(int(l.post_id)) # turn dict into a set userlikes = set(dict) if posts: self.render('index.html', posts=posts, creator=creator, userlikes=userlikes) else: self.render('noposts.html')
def post(self): user = User.is_logged(self) if not user: self.response.out.write( simplejson.dumps({'status':'error', 'message':'In order to vote you must sign in.'}) ) return key = self.request.get('key') koch = db.get(key) vote = Like.all().filter('koch =', koch).filter('user ='******'status' : 'success', 'message' : 'down', 'votes' : votes } ) ) return self.response.out.write( simplejson.dumps({'status':'success', 'message':'An error occurred please contact the developer'}) )
def post(self): user = User.is_logged(self) if not user: self.response.out.write( simplejson.dumps({'status':'error', 'message':'In order to vote you must sign in.'}) ) return key = self.request.get('key') koch = db.get(key) vote = Like.all().filter('koch =', koch).filter('user ='******'status':'error', 'message':'Are you cheating?'}) ) return votes = Like.up( koch, user ) self.response.out.write( simplejson.dumps( { 'status' : 'success', 'message' : 'up', 'votes' : votes } ) )
def get(self, post_id): post = Post.get_by_id(int(post_id)) current_user = self.get_current_user() is_user_like = False if current_user: like = Like.all() \ .filter('user = '******'post = ', post.key()) \ .get() if like: is_user_like = True comments = post.comments.order('-created') comments = json.dumps(list(map(lambda cmt: map_comment(cmt), comments))) self.render('post_detail.html', post=post, is_user_like=is_user_like, comments=comments)
def post(self, post_id, post): # If user made post, don't allow them to like it if self.user.key().id() == post.user_key.key().id(): return self.redirect('/post/%s' % str(post.key().id())) # Retrieve likes by the user for the post likes = Like.all() likes.filter("post_key =", post.key()) likes.filter("user_key =", self.user.key()) # Check if user already liked the post liked = likes.count() > 0 # If user hasn't liked, create like else redirect to post page if not liked: like = Like(parent=blog_key(), post_key=post.key(), user_key=self.user.key()) like.put() self.redirect('/post/%s' % str(post.key().id())) else: self.redirect('/post/%s' % str(post.key().id()))