def serialize(self): """Returns the object serialized as a JSON string. Returns: str. JSON-encoded str encoding all of the information composing the object. """ collection_dict = self.to_dict() # The only reason we add the version parameter separately is that our # yaml encoding/decoding of this object does not handle the version # parameter. # NOTE: If this changes in the future (i.e the version parameter is # added as part of the yaml representation of this object), all YAML # files must add a version parameter to their files with the correct # version of this object. The line below must then be moved to # to_dict(). collection_dict['version'] = self.version if self.created_on: collection_dict['created_on'] = ( utils.convert_naive_datetime_to_string(self.created_on)) if self.last_updated: collection_dict['last_updated'] = ( utils.convert_naive_datetime_to_string(self.last_updated)) return json.dumps(collection_dict)
def test_conversion_between_string_and_naive_datetime_object(self) -> None: """Tests to make sure converting a naive datetime object to a string and back doesn't alter the naive datetime object data. """ now = datetime.datetime.utcnow() self.assertEqual( utils.convert_string_to_naive_datetime_object( utils.convert_naive_datetime_to_string(now)), now)
def to_dict(self): """Returns a dict representing this blog post summary domain object. Returns: dict. A dict, mapping all fields of blog post instance. """ published_on = utils.convert_naive_datetime_to_string( self.published_on) if self.published_on else None last_updated = utils.convert_naive_datetime_to_string( self.last_updated) if self.last_updated else None return { 'id': self.id, 'author_id': self.author_id, 'title': self.title, 'summary': self.summary, 'thumbnail_filename': self.thumbnail_filename, 'tags': self.tags, 'url_fragment': self.url_fragment, 'published_on': published_on, 'last_updated': last_updated }
def test_datetime_conversion_to_string_returns_correct_format( self) -> None: initial_time = datetime.datetime(2016, 12, 1, 1, 2, 3) self.assertEqual(utils.convert_naive_datetime_to_string(initial_time), '12/01/2016, 01:02:03:000000')
def test_get_blog_post_editor_page_data(self): # Checks blog editor can access blog post editor. self.login(self.BLOG_EDITOR_EMAIL) json_response = self.get_json( '%s/%s' % (feconf.BLOG_EDITOR_DATA_URL_PREFIX, self.blog_post.id), ) self.assertEqual(self.BLOG_EDITOR_USERNAME, json_response['username']) expected_blog_post_dict = { 'id': u'%s' % self.blog_post.id, 'author_username': self.BLOG_EDITOR_USERNAME, 'title': '', 'content': '', 'tags': [], 'thumbnail_filename': None, 'url_fragment': '', 'published_on': None, 'last_updated': u'%s' % utils.convert_naive_datetime_to_string(self.blog_post.last_updated) } self.assertEqual(expected_blog_post_dict, json_response['blog_post_dict']) self.assertEqual(10, json_response['max_no_of_tags']) self.logout() # Checks blog admin can access blog post editor for a given blog post. self.login(self.BLOG_ADMIN_EMAIL) json_response = self.get_json( '%s/%s' % (feconf.BLOG_EDITOR_DATA_URL_PREFIX, self.blog_post.id), ) self.assertEqual(self.BLOG_EDITOR_USERNAME, json_response['username']) expected_blog_post_dict = { 'id': u'%s' % self.blog_post.id, 'author_username': self.BLOG_EDITOR_USERNAME, 'title': '', 'content': '', 'tags': [], 'thumbnail_filename': None, 'url_fragment': '', 'published_on': None, 'last_updated': u'%s' % utils.convert_naive_datetime_to_string(self.blog_post.last_updated) } self.assertEqual(expected_blog_post_dict, json_response['blog_post_dict']) self.assertEqual(10, json_response['max_no_of_tags']) self.logout() # Checks non blog-admins and non-editors can not access blog editor. self.login(self.user_email) json_response = self.get_json( '%s/%s' % (feconf.BLOG_EDITOR_DATA_URL_PREFIX, self.blog_post.id), expected_status_int=401) self.logout() self.set_curriculum_admins([self.username]) self.login(self.user_email) json_response = self.get_json( '%s/%s' % (feconf.BLOG_EDITOR_DATA_URL_PREFIX, self.blog_post.id), expected_status_int=401) self.logout()