Skip to content

Commit

Permalink
fix: save generated models and enums to prevent errors in openapi spe…
Browse files Browse the repository at this point in the history
…c generation
  • Loading branch information
mstingl committed Apr 19, 2024
1 parent 45855f8 commit cefd9c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
43 changes: 33 additions & 10 deletions djfapi/routing/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
TDjangoModel = TypeVar('TDjangoModel', bound=models.Model)


_list_models = {}
_generated_enums = {}


class DjangoRouterSchema(RouterSchema):
__router: APIRouter = None

Expand All @@ -66,9 +70,12 @@ def __init__(self, **data: Any) -> None:
self._create_router()

def _init_list(self):
self.list = create_model(
f"{self.get.__qualname__}List", __module__=self.get.__module__, items=(List[self.get_referenced], ...)
)
if not self.get in _list_models:
_list_models[self.get] = create_model(
f"{self.get.__qualname__}List", __module__=self.get.__module__, items=(List[self.get_referenced], ...)
)

self.list = _list_models[self.get]

@property
def name_singular(self) -> str:
Expand Down Expand Up @@ -117,7 +124,12 @@ def _get_model_fields(model, prefix='', recursion_tree=None):
recursion_tree=[*recursion_tree, model],
)

return Enum(f'{self.model.__name__}Fields', {field: ref for field, ref in _get_model_fields(self.model)})
if (self.model, 'Fields') not in _generated_enums:
_generated_enums[self.model, 'Fields'] = Enum(
f'{self.model.__name__}Fields', {field: ref for field, ref in _get_model_fields(self.model)}
)

return _generated_enums[self.model, 'Fields']

@cached_property
def order_fields(self):
Expand All @@ -130,7 +142,12 @@ def order_fields(self):
fields.append(name)
fields.append('-' + name)

return Enum(f'{self.model.__name__}OrderFields', {field: field for field in fields})
if (self.model, 'OrderFields') not in _generated_enums:
_generated_enums[self.model, 'OrderFields'] = Enum(
f'{self.model.__name__}OrderFields', {field: field for field in fields}
)

return _generated_enums[self.model, 'OrderFields']

@cached_property
def get_referenced(self):
Expand Down Expand Up @@ -177,7 +194,10 @@ def aggregated_fields(self) -> Enum:
)
}
)
return Enum(f'{self.model.__name__}AggregateFields', fields)
if (self.model, 'AggregateFields') not in _generated_enums:
_generated_enums[self.model, 'AggregateFields'] = Enum(f'{self.model.__name__}AggregateFields', fields)

return _generated_enums[self.model, 'AggregateFields']

@cached_property
def get_aggregate_group_by(self) -> Enum:
Expand All @@ -196,10 +216,13 @@ def generate_group_by_fields():
for field_name, _field_type in self._get_field_variations(field.value):
yield field_name

return Enum(
f'{self.model.__name__}GroupByFields',
{field_name: field_name for field_name in generate_group_by_fields()},
)
if (self.model, 'GroupByFields') not in _generated_enums:
_generated_enums[self.model, 'GroupByFields'] = Enum(
f'{self.model.__name__}GroupByFields',
{field_name: field_name for field_name in generate_group_by_fields()},
)

return _generated_enums[self.model, 'GroupByFields']

@property
def router(self) -> APIRouter:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = djfapi
version = 0.0.72b12
version = 0.0.72b13
author = Manuel Stingl
author_email = [email protected]
description = Utilities for use with FastAPI and django
Expand Down

0 comments on commit cefd9c2

Please sign in to comment.