Skip to content

Commit

Permalink
Refactor imports to happen when methods called (fixes
Browse files Browse the repository at this point in the history
intecomm-form-validators tests failing to run)
  • Loading branch information
JonathanWillitts committed Nov 20, 2024
1 parent 6abe59f commit b64cda7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions edc_sites/modelform_mixins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Type

from django import forms
from django.contrib.sites.models import Site
from django.apps import apps as django_apps

if TYPE_CHECKING:
from django.contrib.sites.models import Site

__all__ = ["SiteModelFormMixin"]

Expand All @@ -26,12 +31,18 @@ def clean(self) -> dict:
self.validate_with_current_site()
return cleaned_data

@property
def site_model_cls(self) -> Type[Site]:
return django_apps.get_model("sites.site")

@property
def site(self) -> Site:
if related_visit := getattr(self, "related_visit", None):
return related_visit.site
return (
self.cleaned_data.get("site") or self.instance.site or Site.objects.get_current()
self.cleaned_data.get("site")
or self.instance.site
or self.site_model_cls.objects.get_current()
)

def validate_with_current_site(self) -> None:
Expand Down

0 comments on commit b64cda7

Please sign in to comment.