def validate(self, data): request = self.context.get('request') if self.context.get('request').user.is_donor: raise serializers.ValidationError( {"detail": "Only Seekers can upload results!"}) if not self.context.get('request').user.seeker_profile.is_valid: raise serializers.ValidationError({ "detail": "Only validated Seekers can make upload results, please wait for validation!" }) if not get_or_none(DonorProfile, id=request.data.get('donor')): raise serializers.ValidationError( {"detail": "This donor does not exist!"}) if not get_or_none(OfferedTest, id=request.data.get('test_id')): raise serializers.ValidationError( {"detail": "This offered test does not exist!"}) if not DonorProfile.objects.get( id=request.data.get('donor')) in OfferedTest.objects.get( id=request.data.get('test_id')).donors_who_bought.all(): raise serializers.ValidationError( {"detail": "The selected Donor never bought this test!"}) duplicate = TestResult.objects.filter( donor=request.data.get('donor'), offered_test=request.data.get('test_id')).exists() if duplicate: raise serializers.ValidationError( 'This result already exists, please update instead of creating a new result for this particular test' ) return data
def create(self, request, *args, **kwargs): code = request.data.get('code') code_array = code.split(".") unique_br_code = int(code_array[0]) unique_donor_id = int(code_array[1]) if not get_or_none(BloodRequest, unique_request_id=unique_br_code): return Response( {"detail": "This Blood request test does not exist"}, status=status.HTTP_400_BAD_REQUEST) if not get_or_none(DonorProfile, unique_donor_id=unique_donor_id): return Response({"detail": "This donor does not exist"}, status=status.HTTP_400_BAD_REQUEST) target_donor = get_or_none(DonorProfile, unique_donor_id=unique_donor_id) target_blood_request = get_or_none(BloodRequest, unique_request_id=unique_br_code) if target_donor in target_blood_request.applicants.all(): return Response( { 'donor': f'{target_donor.first_name} {target_donor.last_name}', 'blood_type': f'{target_donor.blood_group}', 'birthday': f'{target_donor.birthday}', 'institution': f'{target_blood_request.seeker.name}', 'type': f'Blood Donation' }, status=status.HTTP_200_OK) else: return Response({"detail": "Invalid Code"}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): target_seeker = get_or_none(SeekerProfile, id=self.kwargs['seeker_id']) if target_seeker: target_seeker.is_valid = not target_seeker.is_valid target_seeker.save() return Response(self.get_serializer(target_seeker).data) else: return Response({"detail": "This Seeker Does not exist"}, status=status.HTTP_400_BAD_REQUEST)
def validate(self, data): if not get_or_none(Registration, email=data.get('email')): raise serializers.ValidationError( {"detail": "Your email is invalid!"}) if data.get('code') != Registration.objects.get( email=data.get('email')).code: raise serializers.ValidationError( {"detail": "Your validation code is incorrect!"}) if data.get('password') != data.get('password_repeat'): raise serializers.ValidationError( {"detail": "the passwords do not match!"}) return data
def create(self, request, *args, **kwargs): offered_test_id = self.request.data.get('test_id') if offered_test_id: if get_or_none(OfferedTest, id=int(offered_test_id)): target_offered_test = OfferedTest.objects.get( id=offered_test_id) customers = target_offered_test.donors_who_bought.all() serializer = self.get_serializer(customers, many=True) return Response(serializer.data) else: return Response({"detail": "Not found"}, status=status.HTTP_400_BAD_REQUEST) else: return Response({"detail": "You need to provide a test_id"}, status=status.HTTP_400_BAD_REQUEST)