Exemple #1
0
 def test_to_field_date(self):
     import datetime
     date = self._today()
     typ = self._makeOne()
     formatted = date.isoformat()
     result = typ.to_field(formatted)
     expected = datetime.datetime.combine(result, datetime.time())
     tzinfo = iso8601.Utc()
     expected = expected.replace(tzinfo=tzinfo)
     self.assertEqual(result.isoformat(), expected.isoformat())
Exemple #2
0
class DateTimeField(TextField):

    default_tzinfo = iso8601.Utc()

    tmpl_display = 'ptah.form:templates/fields/datetime-display.pt'

    def serialize(self, value):
        if value is null or value is None or not value:
            return null

        if type(value) is datetime.date:  # cannot use isinstance; dt subs date
            value = datetime.datetime.combine(value, datetime.time())

        if not isinstance(value, datetime.datetime):
            raise Invalid(
                self, _('"${val}" is not a datetime object',
                        mapping={'val': value}))

        if value.tzinfo is None:
            value = value.replace(tzinfo=self.default_tzinfo)

        return value.isoformat()

    def deserialize(self, value):
        if not value:
            return null

        try:
            result = iso8601.parse_date(
                value, default_timezone=self.default_tzinfo)
        except (iso8601.ParseError, TypeError) as e:
            try:
                year, month, day = map(int, value.split('-', 2))
                result = datetime.datetime(year, month, day,
                                           tzinfo=self.default_tzinfo)
            except Exception as e:
                raise Invalid(self, _('Invalid date',
                                      mapping={'val': value, 'err': e}))
        return result
Exemple #3
0
class DateTimeField(TextField):

    default_tzinfo = iso8601.Utc()
    missing = None

    error_msg = _('"${val}" is not a datetime object')
    error_invalid_date = _('Invalid date')

    def to_form(self, value):
        if value is null or value is None or not value:
            return null

        if type(value) is datetime.date:  # cannot use isinstance; dt subs date
            value = datetime.datetime.combine(value, datetime.time())

        if not isinstance(value, datetime.datetime):
            raise Invalid(self.error_msg, self, {'val': value})

        if value.tzinfo is None:
            value = value.replace(tzinfo=self.default_tzinfo)

        return value.isoformat()

    def to_field(self, value):
        if not value:
            return null

        try:
            result = iso8601.parse_date(
                value, default_timezone=self.default_tzinfo)
        except (iso8601.ParseError, TypeError):
            try:
                year, month, day = map(int, value.split('-', 2))
                result = datetime.datetime(year, month, day,
                                           tzinfo=self.default_tzinfo)
            except Exception:
                raise Invalid(self.error_invalid_date, self)

        return result