update link registration site to executive dashboard
This commit is contained in:
1 parent
1e6f6b3b57
commit
d0d1ae7d24
26 files changed
+1044
-55
No files matched your search
@@ -2,7 +2,7 @@ from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.dateparse import parse_date
|
||||
from rest_framework import permissions, status, viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.decorators import action, api_view, permission_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
import secrets
|
||||
@@ -11,6 +11,7 @@ from apps.accounts.models import User
|
||||
from apps.farms.models import Cycle, Flock, Kandang, Site
|
||||
from apps.farms.serializers import CycleSerializer, FlockSerializer, KandangSerializer, SiteSerializer
|
||||
from apps.farms.services.pusat_close import PusatCloseError, notify_pusat_close_request
|
||||
from apps.farms.services.pusat_register import PusatRegisterError, register_site_to_pusat
|
||||
from apps.operations.services.feed_sacks import (
|
||||
cleanup_opening_balance_feed_rows,
|
||||
recompute_feed_sack_in_totals,
|
||||
@@ -84,6 +85,20 @@ def _restore_pending_close(
|
||||
)
|
||||
|
||||
|
||||
class IsActiveStaff(permissions.BasePermission):
|
||||
"""Staff with status=active only (not superadmin)."""
|
||||
|
||||
message = "Hanya staf aktif yang dapat mendaftar ke pusat."
|
||||
|
||||
def has_permission(self, request, view):
|
||||
user = request.user
|
||||
return bool(
|
||||
user
|
||||
and user.is_authenticated
|
||||
and getattr(user, "status", None) == User.STATUS_ACTIVE
|
||||
)
|
||||
|
||||
|
||||
class IsSuperAdminForWrite(permissions.BasePermission):
|
||||
"""Reads for any authenticated user; mutations only for superadmin.
|
||||
|
||||
@@ -99,6 +114,72 @@ class IsSuperAdminForWrite(permissions.BasePermission):
|
||||
return bool(request.user.is_superuser or request.user.status == User.STATUS_SUPERADMIN)
|
||||
|
||||
|
||||
def _pusat_registration_payload(site: Site) -> dict:
|
||||
return {
|
||||
"site_id": site.pk,
|
||||
"site_name": site.site_name,
|
||||
"code": site.pusat_code,
|
||||
"active_site_id": site.pusat_active_site_id,
|
||||
"registered_at": site.pusat_registered_at.isoformat()
|
||||
if site.pusat_registered_at
|
||||
else None,
|
||||
}
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@permission_classes([permissions.IsAuthenticated])
|
||||
def pusat_registration_status(request):
|
||||
"""Return pusat registration status for sites (optional site_id filter)."""
|
||||
site_id = request.query_params.get("site_id")
|
||||
qs = Site.objects.exclude(pusat_active_site_id__isnull=True).exclude(pusat_code="")
|
||||
if site_id:
|
||||
qs = qs.filter(pk=site_id)
|
||||
return Response([_pusat_registration_payload(row) for row in qs])
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
@permission_classes([IsActiveStaff])
|
||||
def pusat_register(request):
|
||||
"""Staff registers the selected farm site to executive ActiveSite registry."""
|
||||
code = request.data.get("code")
|
||||
site_id = request.data.get("site_id")
|
||||
try:
|
||||
site = Site.objects.get(pk=site_id)
|
||||
except (Site.DoesNotExist, TypeError, ValueError):
|
||||
return Response(
|
||||
{"detail": "Pilih site yang akan didaftarkan."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
try:
|
||||
site = register_site_to_pusat(site=site, code=str(code or ""))
|
||||
except PusatRegisterError as exc:
|
||||
return Response({"detail": exc.message}, status=status.HTTP_400_BAD_REQUEST)
|
||||
return Response(_pusat_registration_payload(site), status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
@permission_classes([IsActiveStaff])
|
||||
def pusat_change_code(request):
|
||||
"""Staff changes the pusat registration code for an already-registered site."""
|
||||
code = request.data.get("code")
|
||||
site_id = request.data.get("site_id")
|
||||
try:
|
||||
site = Site.objects.get(pk=site_id)
|
||||
except (Site.DoesNotExist, TypeError, ValueError):
|
||||
return Response(
|
||||
{"detail": "Pilih site yang akan diubah kodenya."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
try:
|
||||
site = register_site_to_pusat(
|
||||
site=site, code=str(code or ""), allow_code_change=True
|
||||
)
|
||||
except PusatRegisterError as exc:
|
||||
return Response({"detail": exc.message}, status=status.HTTP_400_BAD_REQUEST)
|
||||
return Response(_pusat_registration_payload(site), status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
|
||||
class SiteViewSet(viewsets.ModelViewSet):
|
||||
queryset = Site.objects.select_related("user").all()
|
||||
serializer_class = SiteSerializer
|
||||
|
||||
Reference in new issue
Block a user