Skip to content

Commit

Permalink
fix: Avoid circular import by creating another app
Browse files Browse the repository at this point in the history
  • Loading branch information
EnriqueSoria committed Nov 1, 2023
1 parent f6008b4 commit bb9b005
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
15 changes: 0 additions & 15 deletions django_lifecycle/apps.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import inspect
from typing import Iterable, Type, Union

from django.apps import AppConfig, apps
from django.core.checks import Error, register, Tags
from typing import Iterable
from typing import Type
from typing import Union

from django.apps import AppConfig
from django.apps import apps
from django.core.checks import Error
from django.core.checks import Tags
from django.core.checks import register
from django.db import models


Expand All @@ -23,17 +28,29 @@ def model_has_hooked_methods(model: Type[models.Model]) -> bool:
return False


def model_has_lifecycle_mixin(model: Type[models.Model]) -> bool:
from django_lifecycle.mixins import LifecycleModelMixin

return issubclass(model, LifecycleModelMixin)


def check_models_with_hooked_methods_inherit_from_lifecycle(
app_configs: Union[Iterable[AppConfig], None] = None, **kwargs
):
from django_lifecycle import LifecycleModelMixin

for model in get_models_to_check(app_configs):
if model_has_hooked_methods(model) and not issubclass(
model, LifecycleModelMixin
):
if model_has_hooked_methods(model) and not model_has_lifecycle_mixin(model):
yield Error(
"Model has hooked methods but it doesn't inherit from LifecycleModelMixin",
id="django_lifecycle.E001",
obj=model,
)


class ChecksConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "django_lifecycle_checks"

def ready(self) -> None:
super().ready()

register(check_models_with_hooked_methods_inherit_from_lifecycle, Tags.models)
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class YourModel(LifecycleModelMixin, models.Model):
```python
INSTALLED_APPS = [
# ...
'django_lifecycle',
"django_lifecycle_checks",
# ...
]
```
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django_lifecycle_checks",
]

MIDDLEWARE = [
Expand Down

0 comments on commit bb9b005

Please sign in to comment.