class TaskSerializer(serializers.ModelSerializer): contract_title = contract_serializer.ContractSerializer(read_only=True) case_title = case_serializer.CaseSerializer(read_only=True) task_object = GenericRelatedField({ Case: case_serializer.CaseSerializer(), Contract: contract_serializer.ContractSerializer() }) class Meta: model = Task fields = '__all__'
class ExpenseSerializer(serializers.ModelSerializer): expense_by = user_serializer.BaseUserSerializer(read_only=True) contract = contract_serializer.ContractSerializer(read_only=True) case = case_serializer.CaseSerializer(read_only=True) class Meta: model = Expense fields = '__all__'
class CaseSerializer(serializers.ModelSerializer): contract_type = contract_serializer.ContractSerializer(read_only=True) court_type = CourtTypeSerializer(read_only=True) case_stage = CaseStageSerializer(read_only=True) class Meta: model = Case fields = '__all__'
class InvoiceSerializer(serializers.ModelSerializer): customer_name = user_serializer.BaseUserSerializer(read_only=True) contract = contract_serializer.ContractSerializer(read_only=True) case = case_serializer.CaseSerializer(read_only=True) class Meta: model = Invoice fields = '__all__'
class AssignmentSerializer(serializers.ModelSerializer): user = user_serializer.BaseUserSerializer(read_only=True) role = user_serializer.RoleSerializer(read_only=True) assignment_object = GenericRelatedField({ Task: task_serializer.TaskSerializer(), Case: case_serializer.CaseSerializer(), Contract: contract_serializer.ContractSerializer() }) class Meta: model = Assignment fields = '__all__'
class DocumentSerializer(serializers.ModelSerializer): document_type = DocumentTypeSerializer(read_only=True) document_object = GenericRelatedField({ Task: task_serializer.TaskSerializer(), Case: case_serializer.CaseSerializer(), Contract: contract_serializer.ContractSerializer(), Client: user_serializer.ClientSerializer(), }) class Meta: model = Document fields = '__all__'
def put(self, request, contract_id): payload = json.loads(request.body) try: contract = contract_service.update_contract(contract_id, payload) serializer = contract_serializer.ContractSerializer(contract) return JsonResponse( {'contract': serializer.data}, safe=False, status=status.HTTP_200_OK ) except ObjectDoesNotExist: message = Message.objects.get(code=404) return JsonResponse( {'error': message.message_en}, safe=False, status=status.HTTP_404_NOT_FOUND ) except Exception: message = Message.objects.get(code=500) return JsonResponse( {'error': message.message_en}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR )
def get(self, request, contract_id): contract = Contract.objects.filter(id=contract_id) try: contract = contract[0] serializer = contract_serializer.ContractSerializer(contract) return JsonResponse( {'contract': serializer.data}, safe=False, status=status.HTTP_200_OK ) except ObjectDoesNotExist: message = Message.objects.get(code=404) return JsonResponse( {'error': message.message_en}, safe=False, status=status.HTTP_404_NOT_FOUND ) except Exception: message = Message.objects.get(code=500) return JsonResponse( {'error': message.message_en}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR )