コード例 #1
0
ファイル: blog.py プロジェクト: ruishi/udacity-webdev
 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
コード例 #2
0
ファイル: blog.py プロジェクト: ruishi/udacity-webdev
 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]))
コード例 #3
0
 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)
コード例 #4
0
 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))