first commit
This commit is contained in:
commit
b54624be96
226 files changed
+108840
No files matched your search
@@ -0,0 +1,43 @@
|
||||
"""Link HQ Site rows to CitySite registry entries (Pengaturan view list)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from apps.farms.models import Site
|
||||
from apps.sync.models import CitySite
|
||||
|
||||
|
||||
def ensure_hq_site_for_city(city: CitySite) -> Site:
|
||||
"""
|
||||
Ensure the city registry entry has an HQ Site for farm visibility.
|
||||
|
||||
Prefers an existing linked site, then an unlinked site owned by the
|
||||
registrar with a matching name (so seeded Sukawarna attaches when
|
||||
registered), otherwise creates a new Site.
|
||||
"""
|
||||
existing = Site.objects.filter(city_site=city).first()
|
||||
if existing:
|
||||
return existing
|
||||
|
||||
owner = city.registered_by
|
||||
if owner is None:
|
||||
raise ValueError("CitySite.registered_by is required to create an HQ site.")
|
||||
|
||||
site_name = (city.name or city.code or "Site")[:30]
|
||||
orphan = (
|
||||
Site.objects.filter(user=owner, city_site__isnull=True, site_name__iexact=site_name)
|
||||
.order_by("pk")
|
||||
.first()
|
||||
)
|
||||
if orphan is None and city.code:
|
||||
# e.g. code "sukawarna" ↔ site_name "Sukawarna"
|
||||
orphan = (
|
||||
Site.objects.filter(user=owner, city_site__isnull=True, site_name__iexact=city.code)
|
||||
.order_by("pk")
|
||||
.first()
|
||||
)
|
||||
if orphan is not None:
|
||||
orphan.city_site = city
|
||||
orphan.save(update_fields=["city_site", "updated_at"])
|
||||
return orphan
|
||||
|
||||
return Site.objects.create(site_name=site_name, user=owner, city_site=city)
|
||||
Reference in new issue
Block a user