Пример #1
0
    def add(self, prop, values, cleaned=False, quiet=False, fuzzy=False):
        prop_name = self._prop_name(prop, quiet=quiet)
        if prop_name is None:
            return
        prop = self.schema.properties[prop_name]

        if prop.stub:
            if quiet:
                msg = gettext("Stub property (%s): %s")
                raise InvalidData(msg % (self.schema, prop))

        for value in value_list(values):
            if not cleaned:
                value = prop.type.clean(value, proxy=self, fuzzy=fuzzy)
            if value is None:
                continue
            if prop.type == registry.entity and value == self.id:
                msg = gettext("Self-relationship (%s): %s")
                raise InvalidData(msg % (self.schema, prop))

            # Limit the maximum size of any particular field to avoid overloading upstream systems
            value_size = len(value)
            if prop.type.max_size is not None:
                if self._size + value_size > prop.type.max_size:
                    continue

            self._size += value_size
            self._properties.setdefault(prop_name, set())
            self._properties[prop_name].add(value)
Пример #2
0
    def validate(self, data):
        """
        Validate a dataset against the given schema.  This will also drop keys which are not present as properties
        """
        errors = {}
        properties = ensure_dict(data.get("properties"))
        for name, prop in self.properties.items():
            values = ensure_list(prop)
            error = prop.validate(values)
            if error is None and not len(values):
                if prop.name in self.required:
                    error = gettext("Required")
            if error is not None:
                errors[name] = error

        if len(errors):
            msg = gettext("Entity validation failed")
            raise InvalidData(msg, errors={"properties": errors})
Пример #3
0
 def __init__(self, model, data, key_prefix=None, cleaned=True):
     data = dict(data)
     properties = data.pop("properties", {})
     if not cleaned:
         properties = ensure_dict(properties)
     self.schema = model.get(data.pop("schema", None))
     if self.schema is None:
         raise InvalidData(gettext("No schema for entity"))
     self.key_prefix = key_prefix
Пример #4
0
 def _prop_name(self, prop, quiet=False):
     if prop in self.schema.properties:
         return prop
     try:
         if prop.name in self.schema.properties:
             return prop.name
     except AttributeError:
         pass
     if quiet:
         return
     msg = gettext("Unknown property (%s): %s")
     raise InvalidData(msg % (self.schema, prop))
Пример #5
0
 def validate(self, data):
     values = []
     for val in data:
         if self.stub:
             return gettext("Property cannot be written")
Пример #6
0
 def description(self):
     return gettext(self._description)
Пример #7
0
 def label(self):
     return gettext(self._label)
Пример #8
0
 def plural(self):
     return gettext()
Пример #9
0
 def to_dict(self):
     data = {"label": gettext(self.label)}
     return data