Files
Salon/backend/apps/bookings/serializers.py
T
mohd d40bb10876 Updated PLANS.md, AGENTS.md, and arabic-localization.md to reflect the “foundations now, full translations later” approach and marked progress accordingly.
Implemented localization foundations across backend and frontend (locale settings/middleware, preferred language, i18n wiring, RTL support, minimal Arabic UI strings, Accept-Language).
Added targeted backend and frontend tests plus a risks note for pending full translation coverage.
2026-02-28 11:48:58 +03:00

67 lines
2.2 KiB
Python

from rest_framework import serializers
from django.utils.translation import gettext_lazy as _
from apps.bookings.models import Booking
from apps.salons.models import Service, StaffProfile
class BookingSerializer(serializers.ModelSerializer):
salon_name = serializers.CharField(source="salon.name", read_only=True)
service_name = serializers.CharField(source="service.name", read_only=True)
staff_name = serializers.SerializerMethodField()
class Meta:
model = Booking
fields = [
"id",
"salon",
"salon_name",
"service",
"service_name",
"staff",
"staff_name",
"start_time",
"end_time",
"status",
"price_amount",
"currency",
"notes",
"created_at",
]
read_only_fields = ["id", "salon", "status", "price_amount", "currency", "created_at"]
def get_staff_name(self, obj):
if not obj.staff:
return None
first = obj.staff.user.first_name or ""
last = obj.staff.user.last_name or ""
return (first + " " + last).strip() or obj.staff.user.email
class BookingCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Booking
fields = ["service", "staff", "start_time", "end_time", "notes"]
def validate(self, attrs):
service: Service = attrs["service"]
staff = attrs.get("staff")
if staff and staff.salon_id != service.salon_id:
raise serializers.ValidationError(_("Selected staff does not belong to this salon"))
return attrs
def create(self, validated_data):
request = self.context["request"]
service = validated_data["service"]
return Booking.objects.create(
salon=service.salon,
customer=request.user,
service=service,
staff=validated_data.get("staff"),
start_time=validated_data["start_time"],
end_time=validated_data["end_time"],
notes=validated_data.get("notes", ""),
price_amount=service.price_amount,
currency=service.currency,
)