def post(self): ''' Handles blog entry creation ''' self.response.headers['Content-Type'] = 'text/html' isValid = True values = {} subject = self.request.get('subject') if not subject: values['subject_error'] = 'Blog subject is required' isValid = False else: logging.info("Creating blog entry with subject: %s" % str(subject)) content = self.request.get('content') if not content: values['content_error'] = 'Blog content is required' isValid = False else: logging.info("Creating blog entry with content: %s" % str(content)) path = None if not isValid: values['subject'] = subject values['content'] = content path = os.path.join(os.path.dirname(__file__), '../templates/create_blog_entry.html') self.response.out.write(template.render(path, values)) else: blog = BlogData(subject=subject, content=content) service = BlogService(BlogDataStoreFactory()) service.save(blog) blog_id = blog.key().id() str_blog_id = str(blog_id) logging.info("Successfully posted blog entry. Redirectinr to '/unit6/%s" % str_blog_id ) self.redirect('/unit6/%s' % str_blog_id)
def get(self): ''' Handles displaying a json for all blog entries ''' self.response.headers['Content-Type'] = 'application/json' service = BlogService(BlogDataStoreFactory()) blog_json = service.create_all_json() self.response.out.write(blog_json)
def get(self): ''' Flushes the cache holding the blog and individual blog entries ''' self.response.headers['Content-Type'] = 'text/html' service = BlogService(BlogDataStoreFactory()) service.flush_blog_cache() logging.info("Blog flushed") self.redirect('/unit6')
def setUp(self): # First, create an instance of the Testbed class. self.testbed = testbed.Testbed() # Then activate the testbed, which prepares the service stubs for use. self.testbed.activate() # Next, declare which service stubs you want to use. self.testbed.init_datastore_v3_stub() self.factory = BlogDataStoreFactory() self.storage = self.factory.get_storage() self.service = BlogService(self.factory)
def get(self): ''' Handles initial get request ''' self.response.headers['Content-Type'] = 'text/html' service = BlogService(BlogDataStoreFactory()) blog_entries, last_queried = service.fetchAll() current = time.time() last_queried_all = current - last_queried values = {'blog_entries': blog_entries, "last_queried_all":last_queried_all} path = os.path.join(os.path.dirname(__file__), '../templates/blog.html') self.response.out.write(template.render(path, values))
def get(self, entry_id): ''' Handles displaying a blog entry ''' self.response.headers['Content-Type'] = 'text/html' # entry_id = self.request.get('entry_id') service = BlogService(BlogDataStoreFactory()) data = service.fetch(int(entry_id)) last_queried_time = service.get_last_queried_time(entry_id) current = time.time() last_queried = current - last_queried_time values = {'subject': data.subject, 'content':data.content, "last_queried": last_queried} path = os.path.join(os.path.dirname(__file__), '../templates/show_blog_entry.html') self.response.out.write(template.render(path, values))
def get(self): ''' Handles initial get request ''' self.response.headers['Content-Type'] = 'text/html' service = BlogService(BlogDataStoreFactory()) blog_entries, last_queried = service.fetchAll() current = time.time() last_queried_all = current - last_queried values = { 'blog_entries': blog_entries, "last_queried_all": last_queried_all } path = os.path.join(os.path.dirname(__file__), '../templates/blog.html') self.response.out.write(template.render(path, values))
class Test(unittest.TestCase): def setUp(self): # First, create an instance of the Testbed class. self.testbed = testbed.Testbed() # Then activate the testbed, which prepares the service stubs for use. self.testbed.activate() # Next, declare which service stubs you want to use. self.testbed.init_datastore_v3_stub() self.factory = BlogDataStoreFactory() self.storage = self.factory.get_storage() self.service = BlogService(self.factory) def tearDown(self): self.testbed.deactivate() def test_create_json(self): subject = 'Test Subject' content = 'Test Content' blog = BlogData(subject=subject,content=content) blog.put() blog_id = blog.key().id() # blog_data = self.service.fetch(blog_id) json_string = self.service.create_json(blog_id) self.assertTrue(('"subject": "%s"' % subject) in json_string, "Actual json string: " + str(json_string)) self.assertTrue(('"content": "%s"' % content) in json_string, "Actual json string: " + str(json_string)) def test_create_json_with_double_quotes(self): subject = 'Test"s Subject' content = 'Test"s Content' blog = BlogData(subject=subject,content=content) blog.put() blog_id = blog.key().id() # blog_data = self.service.fetch(blog_id) json_string = self.service.create_json(blog_id) quote = subject.find('"') json_subject = subject[0:quote] + '\\' + subject[quote:] quote = content.find('"') json_content = content[0:quote] + '\\' + content[quote:] self.assertTrue(('"subject": "%s"' % json_subject) in json_string, "Actual json string: " + str(json_string)) self.assertTrue(('"content": "%s"' % json_content) in json_string, "Actual json string: " + str(json_string))
def get(self, entry_id): ''' Handles displaying a blog entry ''' self.response.headers['Content-Type'] = 'text/html' # entry_id = self.request.get('entry_id') service = BlogService(BlogDataStoreFactory()) data = service.fetch(int(entry_id)) last_queried_time = service.get_last_queried_time(entry_id) current = time.time() last_queried = current - last_queried_time values = { 'subject': data.subject, 'content': data.content, "last_queried": last_queried } path = os.path.join(os.path.dirname(__file__), '../templates/show_blog_entry.html') self.response.out.write(template.render(path, values))
def post(self): ''' Handles blog entry creation ''' self.response.headers['Content-Type'] = 'text/html' isValid = True values = {} subject = self.request.get('subject') if not subject: values['subject_error'] = 'Blog subject is required' isValid = False else: logging.info("Creating blog entry with subject: %s" % str(subject)) content = self.request.get('content') if not content: values['content_error'] = 'Blog content is required' isValid = False else: logging.info("Creating blog entry with content: %s" % str(content)) path = None if not isValid: values['subject'] = subject values['content'] = content path = os.path.join(os.path.dirname(__file__), '../templates/create_blog_entry.html') self.response.out.write(template.render(path, values)) else: blog = BlogData(subject=subject, content=content) service = BlogService(BlogDataStoreFactory()) service.save(blog) blog_id = blog.key().id() str_blog_id = str(blog_id) logging.info( "Successfully posted blog entry. Redirectinr to '/unit6/%s" % str_blog_id) self.redirect('/unit6/%s' % str_blog_id)