Skip to content

Commit

Permalink
update class_attrs gen implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed May 31, 2017
1 parent f1bfe59 commit d3382d5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions april/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def __new__(cls, name, bases, attrs):
_fields.extend(tmp_fields)

# save fields metainfo in klass._fields
class_attrs = {}
for key, ftype in attrs.items():
if inspect.isclass(ftype):
_fields.append((key, ftype))
else:
class_attrs[key] = ftype

_fields = list(set(_fields))

Expand All @@ -36,12 +39,7 @@ def __new__(cls, name, bases, attrs):
if hasattr(base, '_optional_fields'):
_optional_fields.extend(base._optional_fields)

# pop field from attributes
for key, ftype in _fields:
if key in attrs: # if key is not inherited from parent class
attrs.pop(key)

klass = type.__new__(cls, name, bases, attrs)
klass = type.__new__(cls, name, bases, class_attrs)
klass._fields = _fields
klass._optional_fields = _optional_fields
return klass
Expand All @@ -63,7 +61,6 @@ class UserModel(Model):
user = UserModel(name='xxx')
"""
def __init__(self, **kwargs):
super().__init__()

self.validate(kwargs)

Expand All @@ -73,6 +70,10 @@ def __init__(self, **kwargs):
value = self.deserialize_field(key, value)
self.__dict__[key] = value

# set all optional_fields value as None
for field in self._optional_fields:
self.__dict__[field] = None

self._data = kwargs

@classmethod
Expand Down

0 comments on commit d3382d5

Please sign in to comment.