Skip to content

Commit

Permalink
Move similiar logic to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobstoehr committed Dec 19, 2019
1 parent 8fd1871 commit 9c5e9fa
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions serpy/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def _compile_fields(field_map, serializer_cls):
for name, field in field_map.items()
]

@staticmethod
def _get_list_of_fields(model_fields, fields, exclude):
if fields == '__all__':
fields = model_fields
elif not fields and exclude:
fields = [
field for field in model_fields
if field.name not in exclude
]
return fields
def __new__(cls, name, bases, attrs):
# Fields declared directly on the class.
direct_fields = {}
Expand Down Expand Up @@ -77,13 +87,7 @@ def __new__(cls, name, bases, attrs):
)
if getattr(model, "_meta", None):
# Django models
if fields == '__all__':
fields = model._meta.fields
elif not fields and exclude:
fields = [
field for field in model._meta.fields
if field.name not in meta.exclude
]
fields = cls._get_list_of_fields(model._meta.fields, fields, exclude)
direct_fields.update(
{
field.name: Field()
Expand All @@ -92,13 +96,7 @@ def __new__(cls, name, bases, attrs):
)
elif getattr(model, "__table__", None):
# SQLAlchemy model
if fields == '__all__':
fields = model._meta.fields
elif not fields and exclude:
fields = [
field for field in model.__table__.columns
if field.name not in meta.exclude
]
fields = cls._get_list_of_fields(model.__table__.columns, fields, exclude)
direct_fields.update(
{
field.name: Field()
Expand Down

0 comments on commit 9c5e9fa

Please sign in to comment.