Пример #1
0
    def deserialize(cls, json_string):
        """Returns a Collection domain object decoded from a JSON string.

        Args:
            json_string: str. A JSON-encoded utf-8 string that can be
                decoded into a dictionary representing a Collection. Only call
                on strings that were created using serialize().

        Returns:
            Collection. The corresponding Collection domain object.
        """
        collection_dict = json.loads(json_string.decode('utf-8'))

        created_on = (
            utils.convert_string_to_naive_datetime_object(
                collection_dict['created_on'])
            if 'created_on' in collection_dict else None)
        last_updated = (
            utils.convert_string_to_naive_datetime_object(
                collection_dict['last_updated'])
            if 'last_updated' in collection_dict else None)
        collection = cls.from_dict(
            collection_dict,
            collection_version=collection_dict['version'],
            collection_created_on=created_on,
            collection_last_updated=last_updated)

        return collection
Пример #2
0
    def from_dict(cls, blog_post_dict):
        """Returns a blog post domain object from a dictionary.

        Args:
            blog_post_dict: dict. The dictionary representation of blog post
                object.

        Returns:
            BlogPost. The corresponding blog post domain object.
        """

        last_updated = (
            utils.convert_string_to_naive_datetime_object(
                blog_post_dict['last_updated'])
            if blog_post_dict['last_updated'] else None)
        published_on = (
            utils.convert_string_to_naive_datetime_object(
                blog_post_dict['published_on'])
            if blog_post_dict['published_on'] else None)
        blog_post = cls(
            blog_post_dict['id'], blog_post_dict['author_id'],
            blog_post_dict['title'], blog_post_dict['content'],
            blog_post_dict['url_fragment'], blog_post_dict['tags'],
            blog_post_dict['thumbnail_filename'],
            last_updated,
            published_on)

        return blog_post
Пример #3
0
def update_blog_models_author_and_published_on_date(
        blog_post_id, author_id, date):
    """Updates blog post model with the author id and published on
    date provided.

    Args:
        blog_post_id: str. The ID of the blog post which has to be updated.
        author_id: str. User ID of the author.
        date: str. The date of publishing the blog post.
    """
    blog_post = get_blog_post_by_id(blog_post_id, True)
    blog_post_rights = get_blog_post_rights(
        blog_post_id, strict=True)

    blog_post.author_id = author_id
    supported_date_string = date + ', 00:00:00:00'
    blog_post.published_on = utils.convert_string_to_naive_datetime_object(
        supported_date_string)
    blog_post.validate(strict=True)

    blog_post_summary = compute_summary_of_blog_post(blog_post)
    _save_blog_post_summary(blog_post_summary)

    blog_post_model = blog_models.BlogPostModel.get(
        blog_post.id, strict=True)
    blog_post_model.author_id = blog_post.author_id
    blog_post_model.published_on = blog_post.published_on
    blog_post_model.update_timestamps()
    blog_post_model.put()

    blog_post_rights.editor_ids.append(blog_post.author_id)
    save_blog_post_rights(blog_post_rights)
Пример #4
0
 def test_string_to_datetime_conversion_returns_correct_datetime(self):
     # type: () -> None
     time_string = '12/01/2016, 01:02:03:000000'
     initial_time = datetime.datetime(2016, 12, 1, 1, 2, 3)
     self.assertEqual(
         utils.convert_string_to_naive_datetime_object(time_string),
         initial_time)
Пример #5
0
 def test_conversion_between_string_and_naive_datetime_object(self):
     """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)