def has_object_permission(self, request, view, obj): """Check that user can use these method on that object. When user try to modify object, first has_permission is run and THEN object permission Here is the check on the paternity """ # Read permissions are allowed to any request, # so we'll always allow GET, HEAD or OPTIONS requests. if request.method in permissions.SAFE_METHODS: if request.user.is_authenticated: # hospital = get_user_hospital(request.user) # profile = get_user_profile(request.user) # if is_valid_hospital(hospital) and profile.is_medical: # return True return True elif request.method in ["PUT", "PATCH"]: if request.user.is_authenticated: user = get_user_profile(request.user) if obj.current_unit_stay is not None: return obj.current_unit_stay.bed.unit.reanimation_service in user.authorized_reanimation_services.all() # hospital = get_user_hospital(request.user) # profile = get_user_profile(request.user) # if is_valid_hospital(hospital) and profile.is_medical: # return obj.hospital == hospital return False
def create(self, validated_data): user = self.context.get('request').user if user.is_authenticated: # hospital = get_user_hospital(user) # if hospital is None: # raise serializers.ValidationError('Unknown hospital') # validated_data['hospital'] = hospital user = get_user_profile(user) assigned_caregivers = validated_data.pop("assigned_caregivers", None) bed_id = validated_data.pop("bed", None) start_date = validated_data.pop("stay_start_date", None) validated_data.pop("stay_id", None) patient = Patient(**validated_data) if bed_id: bed = Bed.objects.filter(id=bed_id).first() if bed is None: raise serializers.ValidationError('Bed id (' + bed_id + ') was not found') rea = bed.unit.reanimation_service if bed.unit else None if rea is None: # should never happen raise serializers.ValidationError( f'Bed id ({bed_id}) does not belong to a Reanimation service' ) if rea not in user.authorized_reanimation_services.all(): raise serializers.ValidationError( f'The bed is in Reanimation {bed_id}, and the user can\'t manage it' ) if bed.is_unusable: raise serializers.ValidationError( f'The bed {bed.unit_index} in Reanimation {bed_id} is not usable' ) if get_current_unit_stay(bed): raise serializers.ValidationError( f'The bed {bed.unit_index} in Reanimation {bed_id} is already occupied' ) patient.current_reanimation_service = rea patient.save() patient.assigned_caregivers.set(assigned_caregivers) UnitStay.objects.create(created_by=user, patient=patient, bed=bed, start_date=start_date) else: patient.save() patient.assigned_caregivers.set(assigned_caregivers) return patient else: raise PermissionDenied
def get_queryset(self): patients = Patient.objects.all() # hospital = get_user_hospital(self.request.user) # patients = patients.filter(hospital=hospital) user = get_user_profile(self.request.user) patients = patients.filter(current_reanimation_service__in=user. authorized_reanimation_services.all()) code = self.request.query_params.get('code', None) if code is not None: patients = patients.filter(inclusion_nb=code) return patients
def create(self, validated_data): user = self.context.get('request').user user = get_user_profile(user) id_patient = validated_data.pop("id_patient", None) if id_patient is None: raise serializers.ValidationError( 'Vous devez fournir id_patient dans les données') patient = Patient.objects.filter(id=id_patient).first() if patient is None: raise serializers.ValidationError( f'Patien ({id_patient}) introuvable') if patient.current_unit_stay is None: raise serializers.ValidationError( f'Patient {id_patient} pas actuellement en réanimation.') try: rea = patient.current_unit_stay.bed.unit.reanimation_service except: # should not happen raise serializers.ValidationError( 'Le patient est en réanimation, mais ' 'le service de réa est introuvable') if rea not in user.authorized_reanimation_services.all(): raise serializers.ValidationError( f'Le patient est en réanimation, dans le service {rea}. ' f'Vous n\'avez pas accès à ce service') matching_measure = StatusMeasure.objects.filter(patient=patient)\ .filter(status_type=validated_data["status_type"])\ .filter(created_date=validated_data["created_date"])\ .filter(reanimation_service=rea)\ .first() if matching_measure: matching_measure.value = validated_data["value"] matching_measure.created_by = user matching_measure.save() return matching_measure else: validated_data["reanimation_service"] = rea validated_data["patient"] = patient validated_data["created_by"] = user measure = StatusMeasure(**validated_data) measure.save() return measure
def has_object_permission(self, request, view, obj): # Read permissions are allowed to any request, # so we'll always allow GET, HEAD or OPTIONS requests. if request.method in permissions.SAFE_METHODS: if request.user.is_authenticated: # hospital = get_user_hospital(request.user) # profile = get_user_profile(request.user) # if is_valid_hospital(hospital) and profile.is_medical: # return True user = get_user_profile(request.user) return obj.bed.unit.reanimation_service in \ user.authorized_reanimation_services.all() elif request.method in ["PUT", "PATCH"]: # if request.user.is_authenticated: # hospital = get_user_hospital(request.user) # profile = get_user_profile(request.user) # if is_valid_hospital(hospital) and profile.is_medical: # return obj.patient.hospital == hospital user = get_user_profile(request.user) return obj.bed.unit.reanimation_service in \ user.authorized_reanimation_services.all() return False
def beds(request): profile = get_user_profile(request.user) link = {'main': "", "2": ""} # js = {"version": os.getenv("JS_VERSION")} js = {"main": os.getenv("JS_MAIN_PATIENT"), "2": os.getenv("JS_2_PATIENT")} # hospital = get_user_hospital(request.user) return render( request, 'web/beds.html', { "link": link, "js": js, # "hospital": hospital, "profile": profile })
def get_queryset(self): user = get_user_profile(self.request.user) measures = StatusMeasure.objects.all() measures.filter( reanimation_service__in=user.authorized_reanimation_services.all()) patient_id = self.request.query_params.get('patient_id', None) if patient_id is not None: patient = Patient.objects.filter(id=patient_id).first() if patient is None: raise exceptions.NotFound( f"Patient with id ${patient_id} was not found") measures = measures.filter(patient__id=patient_id) return measures
def get_queryset(self): access_code = self.request.query_params.get("reanimation_service_code", None) if access_code is not None: query_set = ReanimationService.objects.filter( access_code=access_code) rea = query_set.first() if rea is None: return query_set user = get_user_profile(self.request.user) user.authorized_reanimation_services.add(rea) user.save() reas = query_set else: reas = get_user_reas(self.request.user) return reas
def has_object_permission(self, request, view, obj): """Check that user can use these method on that object. When user try to modify object, first has_permission is run and THEN object permission Here is the check on the paternity """ if request.method in permissions.SAFE_METHODS: if request.user.is_authenticated: # hospital = get_user_hospital(request.user) # profile = get_user_profile(request.user) # if is_valid_hospital(hospital) and profile.is_medical: # return True return True elif request.method in ["PUT", "PATCH"]: if request.user.is_authenticated: user = get_user_profile(request.user) return user == obj return False