
from .fields import Field
from .views import DataView

from django.utils.six import with_metaclass


class MetaView(type):

    def __new__(mcs, name, bases, attrs):
        meta = attrs.get('Meta', None)

        try:
            model = meta.model
        except AttributeError:
            if name != 'ModelDataView':
                raise
        else:
            include = getattr(meta, 'fields', None)
            exclude = getattr(meta, 'exclude', [])

            # XXX Does the top base have all fields?

            for model_field in model._meta.fields:
                if model_field.name in attrs:
                    continue
                if model_field.name in exclude:
                    continue
                if include != '__all__' and model_field.name not in include:
                    continue

                # XXX Magic for field types
                attrs[model_field.name] = Field(model_field.name)

        attrs['_meta'] = meta

        return super(MetaView, mcs).__new__(mcs, name, bases, attrs)


class ModelDataView(with_metaclass(MetaView, DataView)):

    pass


from django.db.models.fields import NOT_PROVIDED
from django.utils.six import with_metaclass

from . import filters
from .fields import Field
from .views import DataView


# Map of ModelField name -> list of filters
FIELD_FILTERS = {
    'DateField': [filters.DateFilter],
    'TimeField': [filters.TimeFilter],
    'DateTimeField': [filters.DateTimeFilter],
}


class Options(object):
    def __init__(self, meta):
        self.model = getattr(meta, 'model', None)
        self.fields = getattr(meta, 'fields', [])
        self.exclude = getattr(meta, 'exclude', [])
        self.required = getattr(meta, 'required', {})


class MetaView(type):

    def __new__(mcs, name, bases, attrs):
        meta = Options(attrs.get('Meta', None))

        if meta.model is None:
            if name != 'ModelDataView':
                raise ValueError('model not defined on class Meta')
        else:
            # XXX Does the top base have all fields?

            for model_field in meta.model._meta.fields:
                if model_field.name in attrs:
                    continue
                if model_field.name in meta.exclude:
                    continue
                if meta.fields != '__all__' and model_field.name not in meta.fields:
                    continue

                # XXX Magic for field types
                kwargs = {}
                kwargs['default'] = model_field.default
                kwargs['required'] = any([
                    not model_field.blank,
                    model_field.default is not NOT_PROVIDED,
                ])
                kwargs['filters'] = FIELD_FILTERS.get(model_field.__class__.__name__, [])
                attrs[model_field.name] = Field(model_field.name, **kwargs)

        attrs['_meta'] = meta

        return super(MetaView, mcs).__new__(mcs, name, bases, attrs)


class ModelDataView(with_metaclass(MetaView, DataView)):

    pass

Add Options class Add field filters lists Start proper model field introspection
Add Options class
Add field filters lists
Start proper model field introspection

  
+ from django.db.models.fields import NOT_PROVIDED
+ from django.utils.six import with_metaclass
+ 
+ from . import filters
  from .fields import Field
  from .views import DataView
  
- from django.utils.six import with_metaclass
+ 
+ # Map of ModelField name -> list of filters
+ FIELD_FILTERS = {
+     'DateField': [filters.DateFilter],
+     'TimeField': [filters.TimeFilter],
+     'DateTimeField': [filters.DateTimeFilter],
+ }
+ 
+ 
+ class Options(object):
+     def __init__(self, meta):
+         self.model = getattr(meta, 'model', None)
+         self.fields = getattr(meta, 'fields', [])
+         self.exclude = getattr(meta, 'exclude', [])
+         self.required = getattr(meta, 'required', {})
  
  
  class MetaView(type):
  
      def __new__(mcs, name, bases, attrs):
-         meta = attrs.get('Meta', None)
+         meta = Options(attrs.get('Meta', None))
  
+         if meta.model is None:
-         try:
-             model = meta.model
-         except AttributeError:
              if name != 'ModelDataView':
-                 raise
+                 raise ValueError('model not defined on class Meta')
          else:
-             include = getattr(meta, 'fields', None)
-             exclude = getattr(meta, 'exclude', [])
- 
              # XXX Does the top base have all fields?
  
-             for model_field in model._meta.fields:
+             for model_field in meta.model._meta.fields:
                  if model_field.name in attrs:
                      continue
-                 if model_field.name in exclude:
+                 if model_field.name in meta.exclude:
                      continue
-                 if include != '__all__' and model_field.name not in include:
+                 if meta.fields != '__all__' and model_field.name not in meta.fields:
                      continue
  
                  # XXX Magic for field types
+                 kwargs = {}
+                 kwargs['default'] = model_field.default
+                 kwargs['required'] = any([
+                     not model_field.blank,
+                     model_field.default is not NOT_PROVIDED,
+                 ])
+                 kwargs['filters'] = FIELD_FILTERS.get(model_field.__class__.__name__, [])
-                 attrs[model_field.name] = Field(model_field.name)
+                 attrs[model_field.name] = Field(model_field.name, **kwargs)
  
          attrs['_meta'] = meta
  
          return super(MetaView, mcs).__new__(mcs, name, bases, attrs)
  
  
  class ModelDataView(with_metaclass(MetaView, DataView)):
  
      pass
  
Add Options class Add field filters lists Start proper model field introspection
## Code Before:

from .fields import Field
from .views import DataView

from django.utils.six import with_metaclass


class MetaView(type):

    def __new__(mcs, name, bases, attrs):
        meta = attrs.get('Meta', None)

        try:
            model = meta.model
        except AttributeError:
            if name != 'ModelDataView':
                raise
        else:
            include = getattr(meta, 'fields', None)
            exclude = getattr(meta, 'exclude', [])

            # XXX Does the top base have all fields?

            for model_field in model._meta.fields:
                if model_field.name in attrs:
                    continue
                if model_field.name in exclude:
                    continue
                if include != '__all__' and model_field.name not in include:
                    continue

                # XXX Magic for field types
                attrs[model_field.name] = Field(model_field.name)

        attrs['_meta'] = meta

        return super(MetaView, mcs).__new__(mcs, name, bases, attrs)


class ModelDataView(with_metaclass(MetaView, DataView)):

    pass

## Instruction:
Add Options class Add field filters lists Start proper model field introspection
## Code After:

from django.db.models.fields import NOT_PROVIDED
from django.utils.six import with_metaclass

from . import filters
from .fields import Field
from .views import DataView


# Map of ModelField name -> list of filters
FIELD_FILTERS = {
    'DateField': [filters.DateFilter],
    'TimeField': [filters.TimeFilter],
    'DateTimeField': [filters.DateTimeFilter],
}


class Options(object):
    def __init__(self, meta):
        self.model = getattr(meta, 'model', None)
        self.fields = getattr(meta, 'fields', [])
        self.exclude = getattr(meta, 'exclude', [])
        self.required = getattr(meta, 'required', {})


class MetaView(type):

    def __new__(mcs, name, bases, attrs):
        meta = Options(attrs.get('Meta', None))

        if meta.model is None:
            if name != 'ModelDataView':
                raise ValueError('model not defined on class Meta')
        else:
            # XXX Does the top base have all fields?

            for model_field in meta.model._meta.fields:
                if model_field.name in attrs:
                    continue
                if model_field.name in meta.exclude:
                    continue
                if meta.fields != '__all__' and model_field.name not in meta.fields:
                    continue

                # XXX Magic for field types
                kwargs = {}
                kwargs['default'] = model_field.default
                kwargs['required'] = any([
                    not model_field.blank,
                    model_field.default is not NOT_PROVIDED,
                ])
                kwargs['filters'] = FIELD_FILTERS.get(model_field.__class__.__name__, [])
                attrs[model_field.name] = Field(model_field.name, **kwargs)

        attrs['_meta'] = meta

        return super(MetaView, mcs).__new__(mcs, name, bases, attrs)


class ModelDataView(with_metaclass(MetaView, DataView)):

    pass
