Beispiel #1
0
def get_active_farms_by_id_list(
        db: Session = Depends(get_db),
        farm_id: List[int] = Query(None),
        farm_access: FarmAccess = Depends(get_farm_access),
):
    # Load all farms if the user can access all farms.
    if farm_id is None and farm_access.all_farms:
        farms = crud.farm.get_multi(db, active=True)
        return farms

    # Load all the farms the user has access to if none are provided.
    if farm_id is None and farm_access.farm_id_list is not None:
        farms = crud.farm.get_by_multi_id(
            db, farm_id_list=farm_access.farm_id_list, active=True)
        return farms

    # Load the requested farm(s) if the user has access.
    if farm_id is not None:
        for id in farm_id:
            if not farm_access.can_access_farm(id):
                raise unauthorized_exception

        farms_by_id = crud.farm.get_by_multi_id(db,
                                                farm_id_list=farm_id,
                                                active=True)

        if len(farms_by_id) > 0:
            return farms_by_id
        else:
            raise farm_not_found_exception
Beispiel #2
0
def get_farm_by_id(farm_id: int,
                   db: Session = Depends(get_db),
                   farm_access: FarmAccess = Depends(get_farm_access)):
    if not farm_access.can_access_farm(farm_id):
        raise unauthorized_exception

    farm = crud.farm.get_by_id(db, farm_id=farm_id)

    if not farm:
        raise farm_not_found_exception

    return farm
Beispiel #3
0
def get_active_farm_by_url(db: Session = Depends(get_db),
                           farm_url: str = Query(None),
                           farm_access: FarmAccess = Depends(get_farm_access)):
    farm = None
    if farm_url is not None:
        farm = crud.farm.get_by_url(db, farm_url=farm_url, active=True)

        if farm is None:
            raise farm_not_found_exception

        if not farm_access.can_access_farm(farm.id):
            raise unauthorized_exception

    return farm