def post(self): user = self.check_login_status() title = self.request.get("subject") post = self.request.get("content") if not title or not post: self.render( "newpost.html", title=title, post=post, user=user, error="A post requires both a title and content." ) else: blogpost = BlogPost(title=title, post=post, author_id=user.key().id()) blogpost.put() get_latest(True) self.redirect("/blog/%s" % str(blogpost.key().id()))
def get(self): posts_query = BlogPost.query() blogposts = posts_query.fetch(15) posts_dict = [] for post in blogposts: posts_dict.append(post.to_dict()) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(posts_dict, cls=DateTimeEncoder))
def get_post(self, blog_id): perma_key = "post:%s" % blog_id query_key = "lastquery:%s" % blog_id post = memcache.get(perma_key) if not post: post = BlogPost.get_by_id(blog_id) memcache.set(perma_key, post) memcache.set(query_key, time.time()) return post
def get(self, blog_id=None): self.response.headers["Content-Type"] = "application/json" if blog_id: blog_id = int(blog_id) post = BlogPost.get_by_id(blog_id) self.write(post.to_json()) else: query = "SELECT * FROM BlogPost ORDER BY created DESC LIMIT 10" posts = db.GqlQuery(query) posts = list(posts) self.write(json.dumps([post.to_json() for post in posts]))
def get(self, post_id): post = memcache.get(post_id) lastUpdate = memcache.get('lastUpdate_'+post_id) if not post or not lastUpdate: post = BlogPost.get_by_id(int(post_id)) memcache.add(post_id,post) lastUpdate = datetime.datetime.now() memcache.add('lastUpdate_'+post_id,lastUpdate) age = int(math.ceil((datetime.datetime.now() - lastUpdate).total_seconds()-1)) ageString = 'Queried {} seconds ago'.format(age) template_values = {'blogposts': [post], 'id':post_id, 'age': ageString} self.render(self.templatename, template_values)
def get(self): posts_query = BlogPost.query() blogposts = memcache.get('blogposts') lastUpdate = memcache.get('lastUpdate') if not blogposts or not lastUpdate: blogposts = posts_query.fetch(15) memcache.add('blogposts',blogposts) lastUpdate = datetime.datetime.now() memcache.add('lastUpdate',lastUpdate) age = int(math.ceil((datetime.datetime.now() - lastUpdate).total_seconds()-1)) ageString = 'Queried {} seconds ago'.format(age) template_values = {'blogposts': blogposts, 'age': ageString} #print datetime.datetime.now() self.render(self.templatename,template_values)
def get(self, post_id): post = BlogPost.get_by_id(int(post_id)) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(post.to_dict(), cls=DateTimeEncoder))
def persist(self, subject,content): newpost = BlogPost(); newpost.subject = subject; newpost.content = content; newpost.put() return newpost.key.id()