Add source_site_id to ActiveSite model and implement unique constraint. Update serializers and views for site registration and claiming. Enhance tests for site registration and claiming functionality.
This commit is contained in:
1 parent
521f36d22d
commit
61f954a3bd
12 files changed
+684
-172
No files matched your search
@@ -1,8 +1,16 @@
|
||||
from django.utils.text import slugify
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.sync.models import ActiveSite
|
||||
|
||||
|
||||
def _normalize_code(value: str) -> str:
|
||||
code = slugify(str(value or "").strip())[:64]
|
||||
if not code:
|
||||
raise serializers.ValidationError("Kode tidak valid.")
|
||||
return code
|
||||
|
||||
|
||||
class ActiveSiteSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(source="pk", read_only=True)
|
||||
# Never echo the outbound site API key back to the browser by default.
|
||||
@@ -17,6 +25,7 @@ class ActiveSiteSerializer(serializers.ModelSerializer):
|
||||
"id",
|
||||
"code",
|
||||
"name",
|
||||
"source_site_id",
|
||||
"api_base_url",
|
||||
"has_api_key",
|
||||
"managed_by",
|
||||
@@ -30,6 +39,7 @@ class ActiveSiteSerializer(serializers.ModelSerializer):
|
||||
]
|
||||
read_only_fields = [
|
||||
"id",
|
||||
"source_site_id",
|
||||
"has_api_key",
|
||||
"managed_by",
|
||||
"managed_by_name",
|
||||
@@ -58,3 +68,43 @@ class ActiveSiteWriteSerializer(serializers.ModelSerializer):
|
||||
"is_active",
|
||||
]
|
||||
extra_kwargs = {"api_key": {"write_only": True, "required": False}}
|
||||
|
||||
|
||||
class ActiveSiteClaimSerializer(serializers.Serializer):
|
||||
"""GM claims an ActiveSite that the site dashboard already registered."""
|
||||
|
||||
code = serializers.CharField(max_length=64)
|
||||
|
||||
def validate_code(self, value: str) -> str:
|
||||
return _normalize_code(value)
|
||||
|
||||
|
||||
class SiteRegisterSerializer(serializers.Serializer):
|
||||
"""Payload from a site dashboard registering into the ActiveSite registry."""
|
||||
|
||||
code = serializers.CharField(max_length=64)
|
||||
site_id = serializers.SlugField(max_length=64)
|
||||
name = serializers.CharField(max_length=120)
|
||||
api_base_url = serializers.URLField(max_length=255)
|
||||
api_key = serializers.CharField(max_length=255)
|
||||
|
||||
def validate_code(self, value: str) -> str:
|
||||
return _normalize_code(value)
|
||||
|
||||
def validate_site_id(self, value: str) -> str:
|
||||
site_id = str(value or "").strip()[:64]
|
||||
if not site_id:
|
||||
raise serializers.ValidationError("site_id wajib diisi.")
|
||||
return site_id
|
||||
|
||||
def validate_name(self, value: str) -> str:
|
||||
name = str(value or "").strip()
|
||||
if not name:
|
||||
raise serializers.ValidationError("Nama wajib diisi.")
|
||||
return name[:120]
|
||||
|
||||
def validate_api_key(self, value: str) -> str:
|
||||
key = str(value or "").strip()
|
||||
if not key:
|
||||
raise serializers.ValidationError("api_key wajib diisi.")
|
||||
return key
|
||||
Reference in new issue
Block a user