Exemple #1
0
    def deserialize(self, value):
        if not value:
            return null

        try:
            return self.vocabulary.getTermByToken(value).value
        except Exception:
            raise Invalid(
                self, _('"${val}" is not in vocabulary', mapping={'val':value}))
Exemple #2
0
    def deserialize(self, value):
        if value != 0 and not value:
            return null

        try:
            return self.num(value)
        except Exception:
            raise Invalid(
                self, _('"${val}" is not a number', mapping={'val':value}))
Exemple #3
0
    def serialize(self, value):
        if value is null:
            return null

        try:
            return self.vocabulary.getTerm(value).token
        except Exception:
            raise Invalid(
                self, _('"${val}" is not in vocabulary', mapping={'val':value}))
Exemple #4
0
    def deserialize(self, value):
        if not value:
            return null

        try:
            return [s.strip() for s in value.split()]
        except Exception:
            raise Invalid(self,
                          _('"${val}" is not a list',
                            mapping={'val': value}),
                          )
Exemple #5
0
    def serialize(self, value):
        if value is null or not value:
            return null

        try:
            return u'\n'.join(value)
        except Exception:
            raise Invalid(self,
                          _('"${val}" is not a list',
                            mapping={'val': value}),
                          )
Exemple #6
0
    def serialize(self, value):
        if value is null:
            return null

        try:
            return str(self.num(value))
        except Exception:
            raise Invalid(self,
                          _('"${val}" is not a number',
                            mapping={'val': value}),
                          )
Exemple #7
0
 def serialize(self, value):
     if value is null:
         return null
     try:
         res = []
         for val in value:
             res.append(self.vocabulary.getTerm(val).token)
         return res
     except (LookupError, TypeError):
         raise Invalid(
             self, _('"${val}" is not in vocabulary', mapping={'val':value}))
Exemple #8
0
 def deserialize(self, value):
     if not value:
         return null
     try:
         result = iso8601.parse_date(value)
         result = result.date()
     except (iso8601.ParseError, TypeError):
         try:
             year, month, day = map(int, value.split('-', 2))
             result = datetime.date(year, month, day)
         except Exception, e:
             raise Invalid(
                 self, _('Invalid date', mapping={'val': value, 'err': e}))
Exemple #9
0
    def serialize(self, value):
        if value is null:
            return null

        if isinstance(value, datetime.datetime):
            value = value.date()

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

        return value.isoformat()
Exemple #10
0
    def serialize(self, value):
        if value is null or value is None:
            return null

        if type(value) is datetime.date: # cant 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()