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)
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})
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
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))
def validate(self, data): values = [] for val in data: if self.stub: return gettext("Property cannot be written")
def description(self): return gettext(self._description)
def label(self): return gettext(self._label)
def plural(self): return gettext()
def to_dict(self): data = {"label": gettext(self.label)} return data