You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.4 KiB
Python

import datetime as dt
from marshmallow import Schema, fields
class PassDateTime(fields.DateTime):
"""Used to Pass Datetime deserialization """
def _deserialize(self, value, attr, data):
if isinstance(value, dt.datetime):
return value
return super()._deserialize(value, attr, data)
# TODO Write a better doc you fool
class UnixTimestamp(fields.DateTime):
"""A datetime unix timesstamp in UTC.
Example: ``1513906064.678202``
FIX THIS PART
Timezone-naive `datetime` objects are converted to
UTC (+00:00) by :meth:`Schema.dump <marshmallow.Schema.dump>`.
:meth:`Schema.load <marshmallow.Schema.load>` returns `datetime`
objects that are timezone-aware.
:param str format: Either ``"rfc"`` (for RFC822), ``"iso"`` (for ISO8601),
or a date format string. If `None`, defaults to "iso".
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
def _serialize(self, value, attr, obj):
if value is None:
return None
return value.timestamp()
class NewsSourceSchema(Schema):
id = fields.Int(dump_only=True)
created_date = UnixTimestamp(dump_only=True)
modified_date = UnixTimestamp(dump_only=True)
url = fields.URL()
source_name = fields.Str(required=True,
error_messages={'required': 'NewsSource name is a required field'})
source_type = fields.Str(required=True,
error_messages={'required': 'NewsSource type is a required field'})
# TODO add support for Categories
class Meta:
ordered = True
# TODO deserialization of timestamp to
class NewsArticleSchema(Schema):
id = fields.Int(dump_only=True)
created_date = UnixTimestamp(dump_only=True)
modified_date = UnixTimestamp(dump_only=True)
news_source_id = fields.Int(required=True, error_messages={
'required': 'A NewsArticle must include a NewsSource identified by NewsSource.id'}
)
url = fields.URL(required=True, error_messages={'required': 'A NewsArticle must include a URL'})
title = fields.Str()
authors = fields.List(fields.Str())
# published_date = fields.DateTime()
published_date = PassDateTime()
news_blob = fields.Str(required=True,
error_messages={'required': 'NewsArticle must include news content'})
# TODO add support for Tags
class Meta:
ordered = True