Пример #1
0
    def __init__(self, retrieved_fields=None, loading_from_db=False, **kwargs):
        ''' :param retrieved_fields: The names of the fields returned when loading \
                a partial object.  This argument should not be explicitly set \
                by subclasses
            :param \*\*kwargs:  The values for all of the fields in the document. \
                Any additional fields will raise a :class:`~mongoalchemy.document.ExtraValueException` and \
                any missing (but required) fields will raise a :class:`~mongoalchemy.document.MissingValueException`. \
                Both types of exceptions are subclasses of :class:`~mongoalchemy.document.DocumentException`.
        '''
        self.partial = retrieved_fields is not None
        self.retrieved_fields = self.__normalize(retrieved_fields)

        # Mapping from attribute names to values.
        self._values = {}
        self.__extra_fields = {}

        cls = self.__class__

        # Process the fields on the object
        fields = self.get_fields()
        for name, field in fields.items():
            # print name
            if self.partial and field.db_field not in self.retrieved_fields:
                self._values[name] = Value(field, self, retrieved=False)
            elif name in kwargs:
                field = getattr(cls, name)
                value = kwargs[name]
                self._values[name] = Value(field,
                                           self,
                                           from_db=loading_from_db)
                field.set_value(self, value)
            elif field.auto:
                self._values[name] = Value(field, self, from_db=False)
            else:
                self._values[name] = Value(field, self, from_db=False)

        # Process any extra fields
        for k in kwargs:
            if k not in fields:
                if self.config_extra_fields == 'ignore':
                    self.__extra_fields[k] = kwargs[k]
                else:
                    raise ExtraValueException(k)

        self.__extra_fields_orig = dict(self.__extra_fields)

        # Validate defult sort
        if self.config_default_sort:
            for (name, direction) in self.config_default_sort:
                try:
                    resolve_name(type(self), name)
                    dirs = (1, -1, pymongo.ASCENDING, pymongo.DESCENDING)
                    if direction not in dirs:
                        m = 'Bad sort direction on %s: %s' % (name, direction)
                        raise BadFieldSpecification(m)
                except FieldNotFoundException:
                    raise BadFieldSpecification("Could not resolve field %s in"
                                                " config_default_sort" % name)
Пример #2
0
 def type(self):
     if not isinstance(self.__type, str) and issubclass(self.__type, Document):
         return self.__type
     if self.parent.config_namespace == None:
         raise BadFieldSpecification('Document namespace is None.  Strings are not allowed for DocumentFields')
     type = document_type_registry[self.parent.config_namespace].get(self.__type)
     if type == None or not issubclass(type, Document):
         raise BadFieldSpecification('No type found for %s.  Maybe it has not been imported yet and is not registered?' % self.__type)
     return type
Пример #3
0
    def __init__(self, key_type, value_type, **kwargs):
        ''' :param key_type: the Field type to use for the keys
            :param value_type: the Field type to use for the values
        '''
        super(DictField, self).__init__(**kwargs)

        if not isinstance(key_type, Field):
            raise BadFieldSpecification("KVField key type is not a field!")
        if not isinstance(value_type, Field):
            raise BadFieldSpecification("KVField value type is not a field!")
        self.key_type = key_type
        self.key_type._name = 'k'

        self.value_type = value_type
        self.value_type._name = 'v'
Пример #4
0
 def __init__(self, value_type, **kwargs):
     ''' :param value_type: the Field type to use for the values
     '''
     super(DictField, self).__init__(**kwargs)
     self.value_type = value_type
     if not isinstance(value_type, Field):
         raise BadFieldSpecification("DictField value type is not a field!")
Пример #5
0
 def __init__(self,
              item_type,
              min_capacity=None,
              max_capacity=None,
              **kwargs):
     ''' :param item_type: :class:`Field` instance used for validation and (un)wrapping
         :param min_capacity: minimum number of items contained in values
         :param max_capacity: maximum number of items contained in values
     '''
     super(SequenceField, self).__init__(**kwargs)
     self.item_type = item_type
     self.min = min_capacity
     self.max = max_capacity
     if not isinstance(item_type, Field):
         raise BadFieldSpecification("List item_type is not a field!")