import persistent
import indexable
import exceptions


class Model(persistent.Persistent):
    """
    Data model to easily create indexable persistent objects.
    If an object declares a property from indexable, this property is indexed.
    *All* other properties are stored but unindexed

    Example:
        >>> class Page(Model):
        ...     title = indexable.Field()
        ...     text = indexable.Text()

        ... page = Page(title="Hello", text="World", extra=12345)
    """

    def __init__(self, **kw):
        indexed_fields = set(filter(lambda key:
                not key.startswith("_") and
                isinstance(getattr(self.__class__, key), indexable.Indexable),
            self.__class__.__dict__.keys()))

        required_fields = set(filter(lambda key: getattr(self.__class__, key).default is None, indexed_fields))

        missed_fields = required_fields.difference(kw)
        if missed_fields:
            raise exceptions.ModelException("You should provide fields: " + ", ".join(map(str, missed_fields)))

@@ -518,16 +518,56 @@
  **kw):%0A
+        # This set will go to metaclass%0A
         
@@ -771,24 +771,64 @@
 _.keys()))%0A%0A
+        # This set will go to metaclass%0A
         requ
@@ -924,24 +924,159 @@
 d_fields))%0A%0A
+        # Indexed fields which have default values%0A        default_fields = indexed_fields.difference(required_fields).difference(kw)%0A%0A
         miss
@@ -1252,12 +1252,208 @@
 d_fields)))%0A
+%0A        for field in default_fields:%0A            setattr(self, field, getattr(self.__class__, field).default)%0A%0A        for field, value in kw.iteritems():%0A            setattr(self, field, value)%0A

Set model instance attributes based on fields