def test_valid_uuid_is_valid(self): """ Tests if the method validate_uuid validates a correct uuid """ try: Utils.validate_uuid(UtilsDataRepository.get_valid_uuid()) except ValidationError: self.fail('There should be no validation error.')
def is_valid_patient(id_patient): """ Checks if the specified patient exists :param id_patient: ID of patient to be checked """ Utils.validate_uuid(id_patient) if not Patient.objects.filter(id=id_patient).exists(): raise ValidationError("The patient is not valid!")
def is_valid_physiotherapist(id_physiotherapist): """ Checks if the specified physiotherapist exists :param id_physiotherapist: ID of physiotherapist to be checked """ Utils.validate_uuid(id_physiotherapist) if not Physiotherapist.objects.filter(id=id_physiotherapist).exists(): raise ValidationError("The physiotherapist is not valid!")
def is_valid_person(id_person): """ Checks if the specified person exists :param id_person: ID of person to be checked """ Utils.validate_uuid(id_person) if not Person.objects.filter(id=id_person).exists(): raise ValidationError("The person is not valid!")
def is_valid_goniometry(goniometry_id): """ Checks if the specified goniometry exists :param goniometry_id: if of the goniometry """ Utils.validate_uuid(goniometry_id) if not Goniometry.objects.filter(id=goniometry_id).exists(): raise ValidationError("The goniometry is not valid!")
def is_valid_muscle_test(muscle_test_id): """ Checks if the specified muscle_test exists :param muscle_test_id: if of the muscle_test """ Utils.validate_uuid(muscle_test_id) if not MuscleTest.objects.filter(id=muscle_test_id).exists(): raise ValidationError("The muscle test is not valid!")
def is_valid_perimetry(perimetry_id): """ Checks if the specified perimetry exists :param perimetry_id: if of the perimetry """ Utils.validate_uuid(perimetry_id) if not Perimetry.objects.filter(id=perimetry_id).exists(): raise ValidationError("The perimetry is not valid!")
def is_valid_state(id_state): """ Checks if the specified state exists :param id_state: ID of state to be checked """ Utils.validate_uuid(id_state) if not State.objects.filter(id=id_state).exists(): raise ValidationError("The state is not valid!")
def is_valid_treatment(treatment_id): """ Checks if the specified treatment exists :param treatment_id: if of the treatment """ Utils.validate_uuid(treatment_id) if not Treatment.objects.filter(id=treatment_id).exists(): raise ValidationError("The treatment is not valid!")
def is_valid_credential(id_credential): """ Checks if the specified credential exists :param id_credential: ID of credential to be checked """ Utils.validate_uuid(id_credential) if not Credential.objects.filter(id=id_credential).exists(): raise ValidationError("The credential " + id_credential + " is not valid!")
def is_valid_address(id_address): """ Checks if the specified address exists :param id_address: ID of address to be checked """ Utils.validate_uuid(id_address) if not Address.objects.filter(id=id_address).exists(): raise ValidationError("The address " + id_address + " is not valid!")
def is_valid_administrator(id_administrator): """ Checks if the specified administrator exists :param id_administrator: ID of administrator to be checked """ Utils.validate_uuid(id_administrator) if not Administrator.objects.filter(id=id_administrator).exists(): raise ValidationError("The administrator " + id_administrator + " is not valid!")
def is_valid_body_zone(id_body_zone): """ Checks if the specified body zone exists :param id_body_zone: ID of body zone to be checked """ Utils.validate_uuid(id_body_zone) if not BodyZone.objects.filter(id=id_body_zone).exists(): raise ValidationError("The body zone " + id_body_zone + " is not valid!")
def is_valid_treatment_cycle(id_treatment_cycle, id_patient): """ Checks if the specified treatment cycle belongs to the patient :param id_treatment_cycle: ID of the treatment cycle :param id_patient: Patient where the cycle should belong to """ Utils.validate_uuid(id_treatment_cycle) Utils.validate_uuid(id_patient) if not TreatmentCycle.objects.filter(id=id_treatment_cycle, patient_id=id_patient).exists(): raise ValidationError("The treatment cycle is not valid!")
def get_administrator(id_administrator): """ Returns the administrator with a given id :param id_administrator: ID of administrator :return: Administrator found or none """ Utils.validate_uuid(id_administrator) try: admin = Administrator.objects.get(id=id_administrator) return AdministratorSerializer(admin).data except ObjectDoesNotExist: return None
def get_physiotherapist(id_physiotherapist): """ Returns the physiotherapist with a given id :param id_physiotherapist: ID of physiotherapist :return: Physiotherapist found or none """ Utils.validate_uuid(id_physiotherapist) try: physiotherapist = Physiotherapist.objects.get( id=id_physiotherapist) return PhysiotherapistSerializer(physiotherapist).data except ObjectDoesNotExist: return None
def generate_tokens(user_id, email, is_admin, token_type): ''' Method that generates access and refresh tokens :param user_id: administrator or physiotherapist id :param email: user email :param is_admin: flag that tells if user is admin or not :param token_type: token type :return: JWT access or refresh token ''' if token_type == ACCESS_TOKEN_TYPE: ttl = ACCESS_TOKEN_EXPIRY_TIME elif token_type == REFRESH_TOKEN_TYPE: ttl = REFRESH_TOKEN_EXPIRY_TIME token_content = { "id": user_id, "email": email, "is_admin": is_admin, "token_type": token_type, 'exp': Utils.get_expire_time(ttl) } return jwt.encode(token_content, JWT_SECRET_KEY).decode('utf8')
def generate_response(admin, physio): ''' Method that generates json response for login and token refresh requests :param admin: Adminstrator object or None :param physio: Physiotherapist object or None :return: json response with access token, refresh token and user information ''' if admin: user = admin is_admin = True elif physio: user = physio is_admin = False else: raise PermissionDenied("Account not found!") if not user['state']['name'] == "active": raise PermissionDenied("The account is not active!") return { "access": { "token": AuthenticationService.generate_tokens(user['id'], user['person']['email'], is_admin, ACCESS_TOKEN_TYPE), "expiredTime": Utils.get_expire_time(ACCESS_TOKEN_EXPIRY_TIME) }, "refresh": { "token": AuthenticationService.generate_tokens(user['id'], user['person']['email'], is_admin, REFRESH_TOKEN_TYPE), "expiredTime": Utils.get_expire_time(REFRESH_TOKEN_EXPIRY_TIME) }, "is_admin": is_admin, "id": user['id'], "first_name": user['person']['first_name'], "last_name": user['person']['last_name'], "email": user['person']['email'] }
def validate_muscle_test_info_request(id_patient, id_treatment_cycle, id_treatment, id_muscle_test): """ Validates the treatment information received in the request body :param id_patient: Id of the patient received :param id_treatment_cycle: Id of the treatment cycle received :param id_treatment: Id of the treatment received :param id_muscle_test: Id of the muscle test received """ Utils.validate_uuid(id_patient) Utils.validate_uuid(id_treatment_cycle) Utils.validate_uuid(id_treatment) Utils.validate_uuid(id_muscle_test)
def validate_goniometry_info_request(id_patient, id_treatment_cycle, id_treatment, id_goniometry): """ Validates the goniometry information received in the request :param id_patient: Id of the patient received :param id_treatment_cycle: Id of the treatment cycle received :param id_treatment: Id of the treatment received :param id_goniometry: Id of the goniometry received """ Utils.validate_uuid(id_patient) Utils.validate_uuid(id_treatment_cycle) Utils.validate_uuid(id_treatment) Utils.validate_uuid(id_goniometry)
def recover_password(email): """ Method that recovers the password of a given credential :param email: email of the credential to be changed """ try: credential = Credential.objects.get(email=email) except ObjectDoesNotExist: raise ValidationError(f"No account found with the email {email}!") password = Utils.generate_random_password() credential.password = make_password(password) credential.save() # TODO: Send email with new "password" print(f"NEW PASSWORD: {password}")
def add_credential(email): """ Method create a new credential :param password: State name :return: Credential created id """ password = Utils.generate_random_password() new_credential = Credential.objects.create( password=make_password(password), email=email) new_credential.save() # TODO: Send email with "password" print(f"PASSWORD: {password}") return new_credential.id