move get_submodels into mapdata.utils.models
This commit is contained in:
parent
d9f7210460
commit
d5420e6d96
3 changed files with 33 additions and 34 deletions
25
src/c3nav/mapdata/utils/models.py
Normal file
25
src/c3nav/mapdata/utils/models.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
import typing
|
||||
|
||||
from celery import chain
|
||||
from django.db import models
|
||||
|
||||
|
||||
_submodels_by_model = {}
|
||||
|
||||
|
||||
def get_submodels(model: typing.Type[models.Model]) -> typing.List[typing.Type[models.Model]]:
|
||||
"""
|
||||
Get non-abstract submodels for a model including the model itself.
|
||||
Result is cached.
|
||||
"""
|
||||
try:
|
||||
return _submodels_by_model[model]
|
||||
except KeyError:
|
||||
pass
|
||||
all_models = model.__subclasses__()
|
||||
result = []
|
||||
if not model._meta.abstract:
|
||||
result.append(model)
|
||||
result.extend(chain(*(get_submodels(model) for model in all_models)))
|
||||
_submodels_by_model[model] = result
|
||||
return result
|
Loading…
Add table
Add a link
Reference in a new issue