async def upload_file(history_id: str, file: UploadFile = File(...)):
    """
    upload file to google cloud storage
    :param history_id: id of history in the database
    :param file: file to uploading
    :return:
    """
    if history_id in HistoriesCollection.get_ids():
        if not Repository.get_history_by_id(history_id).get('file_name'):
            with open(file.filename, "wb") as buffer:
                shutil.copyfileobj(file.file, buffer)

            new_filename = str(
                uuid.uuid4()) + '.' + file.filename.split('.')[-1]
            os.renames(file.filename, new_filename)
            shutil.move(new_filename, "app/disease_storage/")

            file_uploader = FileUploader()
            file_uploader.upload_file('app/disease_storage/' + new_filename,
                                      new_filename)

            os.remove('app/disease_storage/' + new_filename)

            Repository.update_history(history_id, {'file_name': new_filename})

            return {'description': 'Success add file', 'result': True}
        else:
            return {'description': 'File already added', 'result': False}

    return {'description': 'Can\'n found history by this id', 'result': False}
Beispiel #2
0
def register(user: RegisterScheme):
    # TODO: Unique email
    try:
        registered_user = Repository.add_user({
            "email":
            user.email,
            "password":
            UsersCollection.get_password_hash(user.password1),
            "name":
            user.name,
            "surname":
            user.surname,
            "patronymic":
            user.patronymic,
            "phone_number":
            user.phone_number,
            "gender":
            user.gender,
            "address":
            user.address,
            "profession":
            user.profession,
            "birthday":
            UsersCollection.date_to_datetime(user.birthday),
            "role":
            'patient'
        })
    except ValidationError as e:
        return {"result": False, "msg": e}

    if registered_user:
        if registered_user['role'] == 'patient':
            try:
                Repository.add_patient({
                    "user_id":
                    str(registered_user['_id']),
                    "address":
                    registered_user['address'],
                    "profession":
                    registered_user['profession']
                })
            except ValidationError as e:
                return {"result": False, "msg": e}
        return {
            "result": True,
            'user_id': str(registered_user['_id']),
            'email': registered_user['email'],
            'role': registered_user['role'],
            'profession': registered_user['profession'],
            'name': registered_user['name'],
            'surname': registered_user['surname'],
            'patronymic': registered_user['patronymic'],
            'address': registered_user['address'],
            'gender': registered_user['gender'],
            'phone_number': registered_user['phone_number'],
            'birthday': registered_user['birthday']
        }
    return {"result": False, "msg": "Invalid credentials"}
async def add_history(history: DiseaseHistoryScheme):
    """
    Add to database new disease history for user
    :param history: dict of data to add to database
    :return:
    """
    Repository.add_history(history)

    return {'description': 'Success add', 'result': True}
Beispiel #4
0
async def get_hospital_profile(hospital_id: str):
    """
    Get data for hospital profile
    """
    hospital_profile = Repository.get_hospital_by_id(hospital_id)

    return {'data': hospital_profile, 'result': bool(hospital_profile)}
Beispiel #5
0
async def get_all_hospitals():
    """
    Get all hospitals from database
    :return: list of hospitals
    """
    list_hospitals = Repository.get_all_hospitals()

    return {'data': list_hospitals, 'result': bool(list_hospitals)}
async def delete_history(history_id: str):
    """
    Delete history with id - history_id
    :param history_id: id of history in the database
    :return:
    """
    result, status = Repository.delete_history(history_id)

    return {'description': result, 'result': status}
async def get_history(history_id: str):
    """
    Get history of some patient with some id of history
    :param history_id: id of history in the database
    :return: histories with history_id
    """
    history = Repository.get_history_by_id(history_id)

    return {'data': history, 'result': bool(history)}
async def get_disease_histories(patient_id: str):
    """
    Get list of disease histories of some user
    :param patient_id:
    :return: list of histtories
    """
    histories_patient = Repository.get_histories_by_patient_id(patient_id)

    return {'data': histories_patient, 'result': bool(histories_patient)}
Beispiel #9
0
async def get_all_doctors():
    """
    Get all doctors from database

    :return: list of doctors
    """
    list_doctors = Repository.get_all_doctors()

    return {'data': list_doctors, 'result': bool(list_doctors)}
Beispiel #10
0
async def get_all_patient():
    """
    Get all patients from database

    :return: list of patients
    """
    list_patients = Repository.get_all_patients()

    return {'data': list_patients, 'result': bool(list_patients)}
async def update_history(history_id: str, history: DiseaseHistoryScheme):
    """
    Update history with id - history_id
    :param history_id: id of history in the database
    :param history: dict of data to add to database
    :return:
    """
    result, status = Repository.update_history(history_id, history)

    return {'description': result, 'result': status}
Beispiel #12
0
async def get_doctor_profile(user_id: str):
    """
    Get data for doctor's profile

    :param user_id: Id of doctor in database
    :return: doctor data
    """
    doctor_profile = Repository.get_doctor_by_id(user_id)

    return {'data': doctor_profile, 'result': bool(doctor_profile)}
Beispiel #13
0
async def get_patient_profile(user_id: str):
    """
    Get data for patients profile

    :param user_id: Id of user in database
    :return: patient data
    """
    patient_profile = Repository.get_patient_by_id(user_id)

    return {'data': patient_profile, 'result': bool(patient_profile)}
Beispiel #14
0
async def get_hospitals_doctors(hospital_id: str):
    """
    Get doctors of hospital with hospital_id
    :param hospital_id: hospital's id
    :return: list of doctors
    """
    list_doctors = Repository.get_doctors_by_hospital_id(hospital_id)

    return {'data': list_doctors, 'result': bool(list_doctors)}
    
async def download_file(history_id: str):
    if history_id in HistoriesCollection.get_ids():
        filename = Repository.get_history_by_id(history_id).get('file_name')

        if filename not in os.listdir(CLEARED_DIR):
            file_uploader = FileUploader()

            if filename in file_uploader.list_blobs():
                file_uploader.download_file(filename)
            else:
                return {'description': "File not found", 'result': True}

            shutil.move(filename, "app/disease_storage/")

        return FileResponse('app/disease_storage/' + filename,
                            media_type='application/octet-stream',
                            filename=filename)

    return {'description': 'Can\'n found history by this id', 'result': False}