Skip to content

Commit

Permalink
Add is_active to ServiceSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyunar committed Jan 29, 2025
1 parent 827f608 commit 99455ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ServiceURLSerializer(serializers.Serializer):

class ServiceSerializer(serializers.Serializer):
name = serializers.CharField()
is_active = serializers.BooleanField(required=False)
website = serializers.URLField(required=False, allow_null=True)
rating = serializers.DecimalField(max_digits=3, decimal_places=2, required=False)
slug = serializers.SlugField()
Expand Down
22 changes: 18 additions & 4 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create(self, request, *args, **kwargs):
skipped_services = []

for service_data in data:
created, instance = self.import_service(service_data)
created, instance, detail = self.import_service(service_data)
service_info = {
"name": service_data["name"],
"url": request.build_absolute_uri(
Expand All @@ -53,6 +53,7 @@ def create(self, request, *args, **kwargs):
kwargs={"slug": instance.slug},
)
),
"detail": detail,
}
if created:
created_services.append(service_info)
Expand All @@ -72,8 +73,9 @@ def create(self, request, *args, **kwargs):
status=response_status,
)

def import_service(self, data) -> Tuple[bool, Service]:
def import_service(self, data) -> Tuple[bool, Service, str]:
name = data.get("name")
is_active = data.get("is_active", True)
website = data.get("website")
rating = data.get("rating")
slug = data.get("slug")
Expand All @@ -82,7 +84,18 @@ def import_service(self, data) -> Tuple[bool, Service]:

# Check if the service already exists
if Service.objects.filter(name=name).exists():
return False, Service.objects.get(name=name)
return (
False,
Service.objects.get(name=name),
"Service with the same name already exists.",
)

if Service.objects.filter(slug=slug).exists():
return (
False,
Service.objects.get(slug=slug),
"Service with the same slug already exists.",
)

# Create the Service
service = Service.objects.create(
Expand All @@ -91,6 +104,7 @@ def import_service(self, data) -> Tuple[bool, Service]:
rating=rating,
slug=slug,
icon_class=icon_class,
is_active=is_active,
)

# Download and save the image
Expand All @@ -107,7 +121,7 @@ def import_service(self, data) -> Tuple[bool, Service]:
for info_data in data.get("service_infos", []):
self.add_service_info(service, info_data)

return True, service
return True, service, "Service imported successfully."

def add_service_info(self, service, info_data):
description = info_data.get("description")
Expand Down

0 comments on commit 99455ac

Please sign in to comment.