Example #1
0
    def __init__(self, *args, **kwargs):
        Member.__init__(self, *args, **kwargs)

        if kwargs.get("integral", False) and not self.bidirectional:
            raise SchemaIntegrityError(
                "%s can't be declared 'integral' without setting "
                "'bidirectional' to True" % self)
Example #2
0
    def _default_validation(self, context):
        """Validation rule for schemas. Applies the validation rules defined by
        all members in the schema, propagating their errors."""

        for error in Member._default_validation(self, context):
            yield error

        accessor = (self.accessor or context.get("accessor", None)
                    or get_accessor(context.value))
        languages = context.get("languages")

        for member in self.ordered_members():
            key = member.name

            if member.translated:
                for language in (languages
                                 or accessor.languages(context.value, key)):
                    value = accessor.get(context.value, key, language=language)
                    for error in member.get_errors(value,
                                                   parent_context=context,
                                                   language=language):
                        yield error
            else:
                value = accessor.get(context.value, key, default=None)

                for error in member.get_errors(value, parent_context=context):
                    yield error
Example #3
0
    def _default_validation(self, context):
        """Validation rule for string values. Checks the L{min}, L{max} and
        L{format} constraints."""

        for error in Member._default_validation(self, context):
            yield error

        if context.value is not None:

            # Min length
            min = self.resolve_constraint(self.min, context)

            if min is not None and len(context.value) < min:
                yield MinLengthError(context, min)

            # Max length
            else:
                max = self.resolve_constraint(self.max, context)

                if max is not None and len(context.value) > max:
                    yield MaxLengthError(context, max)

            # Format
            format = self.resolve_constraint(self.format, context)

            if format is not None and not format.search(context.value):
                yield FormatError(context, format)
Example #4
0
    def _default_validation(self, context):

        for error in Member._default_validation(self, context):
            yield error

        for error in self._range_validation(context):
            yield error
Example #5
0
 def translate_value(self, value, language=None, **kwargs):
     if value:
         return translations(value, language=language, **kwargs)
     else:
         return Member.translate_value(self,
                                       value,
                                       language=language,
                                       **kwargs)
Example #6
0
    def get_possible_values(self, context=None):

        values = Member.get_possible_values(self, context=context)

        if values is None and self.schemas:
            values = set()
            for schema in self.schemas:
                values.update(self.__iter_values_from_schema(schema))

        return values
Example #7
0
    def __init__(self, *args, **kwargs):

        members = kwargs.pop("members", None)
        bases = kwargs.pop("bases", None)
        Member.__init__(self, *args, **kwargs)

        self.__bases = None
        self.bases = ListWrapper(empty_list)

        self.__members = None

        if members:
            if isinstance(members, (list, tuple)) and not self.members_order:
                self.members_order = [member.name for member in members]

            self.expand(members)

        if bases:
            self.inherit(*bases)
Example #8
0
 def translate_value(self, value, language=None, **kwargs):
     try:
         desc = []
         for item, member in zip(value, self.items):
             desc.append(member.translate_value(item))
         return ", ".join(desc)
     except:
         return Member.translate_value(self,
                                       value,
                                       language=language,
                                       **kwargs)
Example #9
0
    def produce_default(self, instance = None):

        default = Member.produce_default(self, instance)

        if default is None and self.required:
            if self.type is not None:
                default = self.type()
            else:
                default_type = self.default_type
                if default_type is not None:
                    default = default_type()

        return default
Example #10
0
    def __deepcopy__(self, memo):
        schema_copy = Member.__deepcopy__(self, memo)

        if not isinstance(schema_copy, type):
            if self.__bases:
                for base in self.__bases:
                    schema_copy.inherit(base)

        if self.__members:
            for member in self.__members.values():
                schema_copy.add_member(deepcopy(member))

        return schema_copy
Example #11
0
    def _default_validation(self, context):

        for error in Member._default_validation(self, context):
            yield error

        if context.value is not None:

            value_length = len(context.value)
            expected_length = len(self.items)

            if value_length < expected_length:
                yield MinItemsError(context, expected_length)

            elif value_length > expected_length:
                yield MaxItemsError(context, expected_length)

            for item_member, item in zip(self.items, context.value):
                for error in item_member.get_errors(item,
                                                    parent_context=context):
                    yield error
Example #12
0
 def translate_value(self, value, language=None, **kwargs):
     if self.keys and self.values and value:
         return ", ".join("%s: %s" % (self.keys.translate_value(key),
                                      self.values.translate_value(value))
                          for key, value in value.items())
     return Member.translate_value(self, value, language, **kwargs)
Example #13
0
 def __init__(self, *args, **kwargs):
     Member.__init__(self, *args, **kwargs)
     RangedMember.__init__(self)
Example #14
0
 def extract_searchable_text(self, extractor):
     Member.extract_searchable_text(self, extractor)
Example #15
0
 def produce_default(self, instance=None):
     default = Member.produce_default(self, instance)
     if default is None:
         default = self._create_default_instance()
     return default
Example #16
0
 def get_possible_values(self, context=None):
     values = Member.get_possible_values(self, context)
     if values is None:
         values = [True, False]
     return values
Example #17
0
 def __deepcopy__(self, memo):
     copy = Member.__deepcopy__(self, memo)
     copy.format = self.format
     return copy