def post(self): user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): username = l[0] subject = self.request.get("subject") content = self.request.get("content") if not subject or not content: error_msg = "Both Subject and Content are required." self.render("newpost.html", username=username, error_msg=error_msg, content=content, subject=subject) else: #Add a new blog post to data store and publish the permalink with /blog/[NUMBER] where NUMBER is obj.key().id() bp = BlogPosts(subject=subject, content=content, author=username) #, permalink = "/blog/") # bpkey = bp.put() # bp = db.get(bpkey) # # bp.permalink = "/blog/"+str(bpkey.id()) bp.put() self.redirect("/blog/" + str(bp.key().id())) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')
def post(self): logging.info(self.request.body) data = json.loads(self.request.body) user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): # Get the comment from data store c = Comments.get_by_id(long(data['commentid'])) blogid = c.blogid # and the blogpost itself bp = BlogPosts.get_by_id(long(blogid)) commentsnum = self.deleteCommentInDB(c, bp) self.response.out.write( json.dumps(({ 'blogid': str(blogid), 'commentsnum': commentsnum }))) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog') else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')
def post(self): logging.info(self.request.body) data = json.loads(self.request.body) # need blog to increment number of posts user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): logging.info(user_id + " " + l[0]) bp = BlogPosts.get_by_id(long(data['blogid'])) # new Comment to add to data store newcomment = Comments(blogid=str(data['blogid']), comment=data['comment'], commenter=data['username']) added, count, commentid = self.insertCommentAndInc( bp, newcomment) addedstr = datetime.strftime(added, '%b %d, %Y %I:%M %p') self.response.out.write( json.dumps(({ 'added': str(addedstr), 'comments': count, "commentid": commentid }))) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog') else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')
def get(self, bpid=None): username = "" user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): username = l[0] if bpid: bp = BlogPosts.get_by_id(long(bpid)) blogposts = list() blogposts.append(bp) # self.response.out.write(blogposts) else: blogposts = db.GqlQuery( "select * from BlogPosts order by published desc") likes = [] if username and len(username) > 0: # get all the posts that this user has liked userlikes = db.GqlQuery("select * from Likes where username = :1", username) for userlike in userlikes: likes.append(str(userlike.blogid)) logging.info("likes: " + str(likes)) #self.response.headers.add_header('set-cookie', str('user_id=%s' % make_cookie_val(username))) self.render("bloghome.html", username=username, blogposts=blogposts, likedposts=likes)
def post(self, blogid): logging.info("post del") user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): # Get the comments from data store cq = db.GqlQuery("select * from Comments where blogid = :1", blogid) c = cq.get() # and the likes lq = db.GqlQuery("select * from Likes where blogid = :1", blogid) l = lq.get() # and the blogpost itself bp = BlogPosts.get_by_id(long(blogid)) if self.deletePostInDB(c, l, bp): self.response.out.write(json.dumps(({ 'delete': "success" }))) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog') else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')
def get(self, bpid): username = "" user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): username = l[0] bp = BlogPosts.get_by_id(long(bpid)) likedposts = [] if username and len(username) > 0: #self.response.headers.add_header('set-cookie', str('user_id=%s' % make_cookie_val(username))) gql = db.GqlQuery("select * from Likes where blogid = :1 and username = :2" , bpid, username) if gql.fetch(1): likedposts.append(bpid) # Get all comments for this blog post comments = db.GqlQuery("Select * from Comments where blogid = :1 order by added desc", bpid) self.render("blogpost.html", username = username, blogpost = bp, likedposts = likedposts, comments = comments)
def post(self): logging.info(self.request.body) data = json.loads(self.request.body) # Need blog ID to update # of likes # Need username so you don't like a post more than once # Like or unlike flag user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): bp = BlogPosts.get_by_id(long(data['blogid'])) if (data['todo'] == "like"): # Create a row in Likes for this user and blog combo like = Likes(blogid=data['blogid'], username=data['username']) # Add it to datastore and inc like count count = self.updateDB(bp, data['blogid'], data['username'], 1, like=like) #self.response.out.write(json.dumps(({'likes': bp.likes}))) if (data['todo'] == "unlike"): # Delete the entry for this user and blog combo from Likes gqd = db.GqlQuery( "select * from Likes where blogid = :1 and username = :2", data['blogid'], data['username']) deletelikes = gqd.get() count = self.updateDB(bp, data['blogid'], data['username'], -1, deletelikes=deletelikes) self.response.out.write(json.dumps(({'likes': count}))) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog') else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')
def get(self, bpid): username = "" user_id = self.request.cookies.get("user_id") if user_id and len(user_id) > 0: l = user_id.split("|") if (user_id == Utilities.make_cookie_val(l[0])): username = l[0] #self.response.headers.add_header('set-cookie', str('user_id=%s' % make_cookie_val(username))) bp = BlogPosts.get_by_id(long(bpid)) self.render("editpost.html", username=username, subject=bp.subject, contents=bp.content, blogid=str(bpid)) else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog') else: self.response.headers.add_header('set-cookie', str('user_id=;Path=/')) self.redirect('/blog')