move get_submodels into mapdata.utils.models

This commit is contained in:
Laura Klünder 2017-06-22 18:48:23 +02:00
parent d9f7210460
commit d5420e6d96
3 changed files with 33 additions and 34 deletions

View file

@ -1,25 +1,16 @@
from typing import List
from django.apps import AppConfig
from django.core.exceptions import FieldDoesNotExist
from django.db import models
from django.utils.translation import ugettext_lazy as _
from c3nav.mapdata.utils.models import get_submodels
class MapdataConfig(AppConfig):
name = 'c3nav.mapdata'
def _get_submodels(self, cls: type) -> List[models.Model]:
submodels = []
for subcls in cls.__subclasses__():
if issubclass(subcls, models.Model) and not subcls._meta.abstract:
submodels.append(subcls)
submodels.extend(self._get_submodels(subcls))
return submodels
def ready(self):
from c3nav.mapdata.models.geometry.base import GeometryMixin, GEOMETRY_MODELS
for cls in self._get_submodels(GeometryMixin):
for cls in get_submodels(GeometryMixin):
GEOMETRY_MODELS[cls.__name__] = cls
try:
cls._meta.get_field('geometry')
@ -27,10 +18,10 @@ class MapdataConfig(AppConfig):
raise TypeError(_('Model %s has GeometryMixin as base class but has no geometry field.') % cls)
from c3nav.mapdata.models.locations import Location, LOCATION_MODELS
LOCATION_MODELS.extend(self._get_submodels(Location))
LOCATION_MODELS.extend(get_submodels(Location))
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin, LEVEL_MODELS
LEVEL_MODELS.extend(self._get_submodels(LevelGeometryMixin))
LEVEL_MODELS.extend(get_submodels(LevelGeometryMixin))
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin, SPACE_MODELS
SPACE_MODELS.extend(self._get_submodels(SpaceGeometryMixin))
SPACE_MODELS.extend(get_submodels(SpaceGeometryMixin))