update sync data from site dashboard and testing closing cycles

This commit is contained in:
Alberto-Audrix committed 2026-09-15 16:52:41 +07:00
1 parent 36639d8c38
commit f49e72592c
42 files changed
+1966 -688

No files matched your search

+31 -8
View File
@@ -19,7 +19,7 @@ class ActiveSiteViewSet(viewsets.ModelViewSet):
"""
List/read: scoped (director all; BUH managed GMs; GM own).
Create (claim): GM and GM Admin claim a site-registered ActiveSite by code.
Sync: GM and GM Admin only (stubs until mirror pull is implemented).
Sync: GM and GM Admin only (pulls farm pusat export into HQ mirror).
Site dashboards create/fill ActiveSite via register-from-site (API key auth).
Claiming attaches managed_by and ensures an HQ Site for the view list.
@@ -203,24 +203,47 @@ class ActiveSiteViewSet(viewsets.ModelViewSet):
{"detail": "Active site is inactive."},
status=status.HTTP_400_BAD_REQUEST,
)
if not active_site.api_base_url or not active_site.api_key:
return Response(
{"detail": "Active site is missing api_base_url or api_key."},
status=status.HTTP_400_BAD_REQUEST,
)
if not (active_site.source_site_id or "").strip():
return Response(
{"detail": "Active site is missing source_site_id."},
status=status.HTTP_400_BAD_REQUEST,
)
from apps.sync.services.mirror_pull import STATUS_OK, sync_active_site_safe
summary = sync_active_site_safe(active_site)
active_site.refresh_from_db()
http_status = (
status.HTTP_200_OK
if summary.status == STATUS_OK
else status.HTTP_502_BAD_GATEWAY
)
return Response(
{
"detail": "Active site sync not implemented yet.",
"detail": summary.message or "Sync finished.",
"summary": summary.to_dict(),
"active_site": ActiveSiteSerializer(active_site).data,
},
status=status.HTTP_501_NOT_IMPLEMENTED,
status=http_status,
)
@action(detail=False, methods=["post"], url_path="sync-all")
def sync_all(self, request):
if not can_sync(request.user):
return Response({"detail": "Sync not allowed."}, status=status.HTTP_403_FORBIDDEN)
from apps.sync.services.mirror_pull import STATUS_OK, sync_all_active_sites
sites = list(self.get_queryset().filter(is_active=True))
summaries = sync_all_active_sites(queryset=sites)
ok = sum(1 for s in summaries if s.status == STATUS_OK)
return Response(
{
"detail": "Active site sync-all not implemented yet.",
"active_site_count": len(sites),
"active_sites": ActiveSiteSerializer(sites, many=True).data,
},
status=status.HTTP_501_NOT_IMPLEMENTED,
"detail": f"Synced {ok}/{len(summaries)} active site(s).",
"active_site_count": len(summaries),
"summaries": [s.to_dict() for s in summaries],
}
)