You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
|
|
from rest_framework import serializers
|
|
|
|
from apps.transaction.models import TransactionParticipant, ParticipantInfo, TransactionReceipt
|
|
from apps.course.serializers import CourseDetailSerializer
|
|
from utils import FileFieldSerializer
|
|
|
|
|
|
|
|
|
|
class ParticipantInfoSerializer(serializers.ModelSerializer):
|
|
phone_number = serializers.CharField(max_length=30)
|
|
|
|
class Meta:
|
|
model = ParticipantInfo
|
|
fields = ['fullname', 'email', 'phone_number', 'gender', 'birthdate']
|
|
|
|
def validate_phone_number(self, value):
|
|
return value
|
|
|
|
|
|
class TransactionParticipantSerializer(serializers.ModelSerializer):
|
|
participant_infos = ParticipantInfoSerializer(many=True)
|
|
|
|
class Meta:
|
|
model = TransactionParticipant
|
|
fields = ['participant_infos']
|
|
|
|
|
|
def create(self, validated_data):
|
|
participant_infos_data = validated_data.pop('participant_infos', [])
|
|
transaction_participant = TransactionParticipant.objects.create(**validated_data)
|
|
|
|
for participant_info_data in participant_infos_data:
|
|
ParticipantInfo.objects.create(transaction_participant=transaction_participant, **participant_info_data)
|
|
|
|
return transaction_participant
|
|
|
|
|
|
|
|
class TransactionListSerializer(serializers.ModelSerializer):
|
|
course = serializers.SerializerMethodField()
|
|
receipts = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = TransactionParticipant
|
|
fields = ['id', 'course', 'status', 'price', 'receipts', 'created_at', 'updated_at']
|
|
|
|
def get_course(self, obj):
|
|
return CourseDetailSerializer(obj.course, context=self.context).data
|
|
|
|
def get_receipts(self, obj):
|
|
receipts = obj.receipts.all()
|
|
return TransactionReceiptSerializer(receipts, many=True, context=self.context).data
|
|
|
|
|
|
class TransactionReceiptSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for uploading payment receipts
|
|
Uses FileFieldSerializer to handle pre-uploaded files from /upload-tmp-media/
|
|
"""
|
|
file = FileFieldSerializer()
|
|
|
|
class Meta:
|
|
model = TransactionReceipt
|
|
fields = ['id', 'file', 'description', 'uploaded_at']
|
|
read_only_fields = ['id', 'uploaded_at']
|
|
|
|
|
|
class UploadReceiptsSerializer(serializers.Serializer):
|
|
"""
|
|
Serializer for uploading multiple receipt files for a transaction.
|
|
Files should be pre-uploaded using /upload-tmp-media/ endpoint,
|
|
then their URLs should be sent here.
|
|
"""
|
|
files = serializers.ListField(
|
|
child=FileFieldSerializer(),
|
|
allow_empty=False,
|
|
max_length=10,
|
|
help_text="List of file URLs (max 10 files) - files should be pre-uploaded via /upload-tmp-media/"
|
|
)
|
|
description = serializers.CharField(
|
|
required=False,
|
|
allow_blank=True,
|
|
max_length=1000,
|
|
help_text="Optional description for the receipts"
|
|
)
|
|
|
|
def validate_files(self, files):
|
|
"""
|
|
Validate uploaded file URLs
|
|
"""
|
|
if len(files) > 10:
|
|
raise serializers.ValidationError("You can upload a maximum of 10 files.")
|
|
|
|
return files
|