def test_serializer_create_invalid_phone(): patient = { "name": "teste", "email": "*****@*****.**", "cpf": "00000000000", "phone": "00000", "birth_date": "2020-09-11", "address": address } serializer = PatientSerializer(data=patient) assert not serializer.is_valid()
def test_serializer_create_same_cpf(): patient = { "name": "teste", "email": "*****@*****.**", "cpf": "00000000000", "phone": "00000000", "birth_date": "2020-09-11", "address": address } baker.make(Patient, cpf="00000000000") serializer = PatientSerializer(data=patient) assert not serializer.is_valid()
def patient_create_v2(request): data = request.data serializer = PatientSerializer(data=data) if serializer.is_valid(): logger.info('Start create patient without selenium') success, error = create_patient_v2(serializer.validated_data) if success: logger.info('Success create patient') return Response(status=status.HTTP_200_OK) else: return Response(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class CustomUserSerializer(serializers.ModelSerializer): child_sitter = SitterSerializer(read_only=True) child_patient = PatientSerializer(read_only=True) class Meta: model = CustomUser fields = ('id', 'username', 'password', 'email', 'first_name', 'last_name', 'is_sitter', 'is_patient', 'child_sitter', 'child_patient')
def custom_signup(self, request, user): user_type = self.validated_data.get('user_type', '') serializer = None if user_type == DOCTOR_USER: data = self.get_cleaned_data() data['user'] = user.id serializer = DoctorSerializer(data=data) else: data = self.get_cleaned_data() data['user'] = user.id serializer = PatientSerializer(data=data) serializer.is_valid(raise_exception=True) serializer.save()
class CaseSerializer(serializers.ModelSerializer): id = serializers.IntegerField() confirmed_date = serializers.DateField() origin = serializers.CharField() patient = PatientSerializer() virus = VirusSerializer() case_locations = CaseLocationOutputSerializer(many=True) class Meta: model = Case fields = ("id", "confirmed_date", "origin", "patient", "virus", "case_locations")
def test_serializer_create(): patient = { "name": "teste", "email": "*****@*****.**", "cpf": "00000000000", "phone": "00000000", "birth_date": "2020-09-11", "address": address } serializer = PatientSerializer(data=patient) serializer.is_valid(raise_exception=True) serializer.save()
class AppointmentSerializer(serializers.ModelSerializer): ''' Create Diagnosise include data{ } ''' patient_id = serializers.CharField(max_length=255) doctor_id = serializers.CharField(max_length=255) patient = PatientSerializer(read_only=True) doctor = DoctorSerializer(read_only=True) class Meta: model = Appointment fields = [ "id", "patient_id", 'patient', "doctor_id", 'doctor', "appointment_date" ] read_only = ['id']
class PaymentSerializer(serializers.ModelSerializer): ''' Create Diagnosise include data{ } ''' patient_id = serializers.CharField(max_length=255) doctor_id = serializers.CharField(max_length=255) patient = PatientSerializer(read_only=True) doctor = DoctorSerializer(read_only=True) class Meta: model = Payment fields = [ "id", 'patient_id', "patient", "doctor", 'doctor_id', "amount", 'created_at', 'updated_at', 'check_out' ] read_only = ['id'] depth = 1
class DiagnosticianSerializer(serializers.ModelSerializer): ''' Create Diagnosise include data{ } ''' patient_id = serializers.CharField(max_length=255) treatment_id = serializers.CharField(max_length=255) patient = PatientSerializer(read_only=True) treatment = SickSerializer(read_only=True) class Meta: model = Diagnostician fields = [ "id", 'patient_id', "patient", "symptom", 'treatment_id', "treatment", 'created_at', 'updated_at' ] read_only = ['id'] depth = 1
class DoctorAppointmentSerializer(serializers.HyperlinkedModelSerializer): patient = PatientSerializer(read_only=True) patient_id = serializers.IntegerField() doctor = DoctorSerializer(read_only=True) doctor_id = serializers.IntegerField() doc_image = Base64ImageField(allow_null=True, max_length=None, use_url=True, required=False) doc_file = Base64ImageField(allow_null=True, max_length=None, use_url=True, required=False) class Meta: model = DoctorAppointment fields = [ 'id', 'name', 'mobile', 'problem', 'age', 'gender', 'description', 'doctor', 'doctor_id', 'patient', 'patient_id', 'advice', 'doc_image', 'doc_file', 'created_at' ]
class CreateCaseRecordSerializer(serializers.Serializer): patient = PatientSerializer() dateOfConfirm = serializers.DateField() localOrImported = serializers.CharField(max_length=20) virus = serializers.CharField(max_length=20) def create(self, validated_data): dateOfConfirm = validated_data['dateOfConfirm'] localOrImported = validated_data['localOrImported'] virusName = validated_data['virus'] virusObj = Virus.objects.get(name=virusName) patient_serializer = self.fields['patient'] patient = patient_serializer.create(validated_data['patient']) case_record = CaseRecord.objects.create( patient=patient, virus=virusObj, dateOfConfirm=dateOfConfirm, localOrImported=localOrImported) return case_record
class UserSerializer(serializers.ModelSerializer): patient = PatientSerializer() doctor = DoctorSerializer() user_type = serializers.SerializerMethodField() phone_no = serializers.SerializerMethodField() name = serializers.SerializerMethodField() class Meta: model = User fields = ('phone_no', 'username', 'name', 'email', 'doctor', 'patient', 'user_type') def get_user_type(self, obj): try: patient = obj.patient return PATIENT_USER except: return DOCTOR_USER def get_phone_no(self, obj): return obj.username def get_name(self, obj): return obj.first_name + obj.last_name
class CaseRecordSerializer(serializers.ModelSerializer): patient = PatientSerializer() virus = VirusSerializer() class Meta: model = CaseRecord fields = ('dateOfConfirm', 'localOrImported', 'patient', 'virus') def create(self, validated_data): patient_serializer = self.fields['patient'] virus_serializer = self.fields['virus'] dateOfConfirm = validated_data['dateOfConfirm'] localOrImported = validated_data['localOrImported'] patient = patient_serializer.create(validated_data['patient']) virus = virus_serializer.create(validated_data['virus']) case_record = CaseRecord.objects.create( patient=patient, virus=virus, dateOfConfirm=dateOfConfirm, localOrImported=localOrImported) return case_record
def post(self, request): serializer = PatientSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class PatientVisitSerializer(serializers.ModelSerializer): ''' Create Diagnosise include data{ } ''' doctor = serializers.CharField(required=False) patient = PatientSerializer(many=False) treatment = serializers.CharField(required=False, allow_null=True, allow_blank=True) class Meta: model = PatientVisit fields = [ "id", "doctor", "patient", 'treatment', "charges", 'created_at', 'updated_at' ] read_only_fields = ['id'] depth = 1 ordering = ['created_at'] # lookup_field = 'patient_id' def create(self, validated_data): # validate doctor rule = Rule.objects.get(pk=1) limited_patient = rule.rule_parameters['limited'] today = localtime(now()).replace(hour=0, minute=0, second=0, microsecond=0) nextday = localtime(today + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) total_visitor = PatientVisit.objects.filter(created_at__lte=nextday, created_at__gte=today) total_count = 0 for i in total_visitor: total_count += 1 if (total_count > limited_patient): raise serializers.ValidationError( detail="The patient higher than per day") doctor = Profile.objects.filter( pk=validated_data.pop('doctor')).first() if not doctor: raise serializers.ValidationError(detail="Doctor not exist") # validate patient patient_param = validated_data.pop('patient') patient = Patient.objects.filter( identity_card=patient_param['identity_card']).first() if not patient: patient = Patient.objects.create(**patient_param) # validate treatment treatment = Sick.objects.filter( name=validated_data.pop('treatment')).first() patient_visit = PatientVisit.objects.create(**validated_data, doctor=doctor, patient=patient, treatment=treatment) return patient_visit
def get(self, request): patients = Patient.objects.all() serializer = PatientSerializer(patients, many=True) response_data = {'patients': serializer.data} return JsonResponse(response_data, safe=False)
def validate_patient(self, value): patient = PatientSerializer(data=value) patient.is_valid(raise_exception=True) return value