Exemplo n.º 1
0
class Foo(Model):

    _collection = MockCollection()

    field = Field()
    required = Field(required=True)
    default = Field(default="default")
    callback = Field(get_callback=lambda x, y: "foo",
                     set_callback=lambda x, y: "bar")
    reference = ReferenceField(Ref)
    _private_field = Field()
Exemplo n.º 2
0
    def __init__(self, **kwargs):
        """ Creates an instance of the model, without saving it. """
        super(Model, self).__init__()
        # compute once
        create_fields = self._auto_create_fields
        is_new_instance = self._id_field not in kwargs
        for field, value in kwargs.items():
            if is_new_instance:
                if field in self._fields.values():
                    # Running validation, if the field exists
                    setattr(self, field, value)
                else:
                    if not create_fields:
                        raise UnknownField("Unknown field %s" % field)
                    self.add_field(field, Field())
                    setattr(self, field, value)
            else:
                self[field] = value

        for field_name in self._fields.values():
            attr = getattr(self.__class__, field_name)
            self._fields[attr.id] = field_name

            # set the default
            attr._set_default(self, field_name)
Exemplo n.º 3
0
    def __init__(self, **kwargs):
        """ Just initializes the fields. This should ONLY be called
        from .new() or the Cursor.
        """
        super(Model, self).__init__()
        is_new_instance = "_id" not in kwargs
        for field, value in kwargs.iteritems():
            private_field = field.startswith("_")
            if private_field:
                # Setting it in the dict, but leaving it alone
                self[field] = value
            elif is_new_instance:
                if field in self._fields.values():
                    # Running validation, if the field exists
                    setattr(self, field, value)
                else:
                    if not mogo.AUTO_CREATE_FIELDS:
                        raise UnknownField("Unknown field %s" % field)
                    self.add_field(field, Field())
                    setattr(self, field, value)
            else:
                self[field] = value

        for field_name in self._fields.values():
            attr = getattr(self.__class__, field_name)
            if not isinstance(attr, Field):
                continue
            self._fields[attr.id] = field_name

            # set the default
            if attr.default is not None and not self.has_key(field_name):
                self[field_name] = attr._get_default()
Exemplo n.º 4
0
class Bar(Model):

    uid = Field(unicode)

    @classmethod
    def new(cls):
        instance = super(Bar, cls).new(uid=u"testing")
        return instance
Exemplo n.º 5
0
 def test_model_add_field(self):
     """ Tests the ability to add a field. """
     class Testing(Model):
         pass
     Testing.add_field(
         "foo", Field(unicode, set_callback=lambda x, y: u"bar"))
     self.assertIsInstance(Testing.foo, Field)
     testing = Testing(foo=u"whatever")
     self.assertEqual(testing["foo"], u"bar")
     self.assertEqual(testing.foo, u"bar")