예제 #1
0
def get_all_farm_terms(
        request: Request,
        farm_list: List[Farm] = Depends(get_active_farms_url_or_list),
        db: Session = Depends(get_db),
):
    query_params = {**request.query_params}
    query_params.pop('farm_id', None)
    query_params.pop('farm_url', None)

    data = {}
    for farm in farm_list:
        data[farm.id] = []

        # Get a farmOS client.
        try:
            farm_client = get_farm_client(db_session=db, farm=farm)
        except ClientError:
            continue

        # Make the request.
        try:
            data[farm.id] = data[farm.id] + farm_client.term.get(
                filters=query_params)['list']
        except:
            continue

    return data
예제 #2
0
def delete_farm_term(
        tid: List[int] = Query(None),
        farm_list: List[Farm] = Depends(get_active_farms_url_or_list),
        db: Session = Depends(get_db),
):
    data = {}
    for farm in farm_list:
        data[farm.id] = []

        # Get a farmOS client.
        try:
            farm_client = get_farm_client(db_session=db, farm=farm)
        except ClientError:
            continue

        # Make the request.
        for id in tid:
            try:
                result = farm_client.term.delete(id=id)
                data[farm.id].append({id: result.json()})
            except:
                data[farm.id].append({id: "error"})
                continue

    return data
예제 #3
0
def get_all_farm_info(
        db: Session = Depends(get_db),
        farm_list: List[Farm] = Depends(get_farms_url_or_list),
        use_cached: bool = True,
):
    data = {}
    for farm in farm_list:
        data[farm.id] = {}

        if use_cached:
            data[farm.id] = farm.info
        else:
            try:
                farm_client = get_farm_client(db_session=db, farm=farm)
            except ClientError:
                continue

            try:
                info = farm_client.info()
                data[farm.id]['info'] = info

                crud.farm.update_info(db, farm=farm, info=info)
            except:
                continue

    return data
예제 #4
0
def ping_farms(
        db: Session = Depends(get_db),
        settings=Depends(get_settings),
):
    """
    Ping all active farms.
    """
    farm_list = crud.farm.get_multi(db, active=True)

    total_response = 0
    for farm in farm_list:
        try:
            farm_client = get_farm_client(db_session=db, farm=farm)
            info = farm_client.info()
            crud.farm.update_info(db, farm=farm, info=info)
            total_response += 1
        except Exception as e:
            continue

    difference = len(farm_list) - total_response
    if difference > 0 and settings.AGGREGATOR_ALERT_PING_FARMS_ERRORS:
        admin_alert_email(
            db_session=db,
            message=
            f"Pinged {total_response}/{len(farm_list)} active farms. {difference} did not respond. Check the list of farm profiles for authorization status errors."
        )

    return {"msg": f"Pinged {total_response}/{len(farm_list)} active farms."}
예제 #5
0
def authorize_farm(
        farm: Farm = Depends(get_farm_by_id),
        *,
        db: Session = Depends(get_db),
        auth_params: FarmAuthorizationParams,
):
    """
    Authorize an existing farm. Complete the OAuth Authorization Flow.
    """
    try:
        token = get_oauth_token(farm.url, auth_params)
    except Exception as e:
        error = f"Authorization flow failed: {e}"
        crud.farm.update_is_authorized(db,
                                       farm_id=farm.id,
                                       is_authorized=False,
                                       auth_error=error)
        raise HTTPException(
            status_code=400,
            detail=error,
        )

    new_token = FarmTokenCreate(farm_id=farm.id, **token.dict())

    old_token = crud.farm_token.get_farm_token(db, farm.id)
    if old_token is None:
        token = crud.farm_token.create_farm_token(db, token=new_token)
    else:
        token = crud.farm_token.update_farm_token(db,
                                                  token=old_token,
                                                  token_in=new_token)

    # Update the scope attribute of the Farm profile to the scope that was just authorized.
    crud.farm.update_scope(db, farm=farm, scope=auth_params.scope)

    # Reconnect to the farmOS server and update farm info.
    try:
        farm_client = get_farm_client(db=db, farm=farm)

        info = farm_client.info()

        crud.farm.update_info(db, farm=farm, info=info)
    except Exception as e:
        error = f"Could not connect to farm after successful Authorization Flow: {e}"
        crud.farm.update_is_authorized(db,
                                       farm_id=farm.id,
                                       is_authorized=False,
                                       auth_error=error)
        raise HTTPException(
            status_code=400,
            detail=error,
        )

    return token
예제 #6
0
def update_farm_terms(
        term: TermUpdate,
        farm_list: List[Farm] = Depends(get_active_farms_url_or_list),
        db: Session = Depends(get_db),
):
    data = {}
    for farm in farm_list:
        data[farm.id] = []

        # Get a farmOS client.
        try:
            farm_client = get_farm_client(db_session=db, farm=farm)
        except ClientError:
            continue

        # Make the request.
        try:
            data[farm.id].append(farm_client.term.send(payload=term.dict()))
        except:
            continue

    return data
예제 #7
0
def create_farm_assets(
    asset: Asset,
    farm_list: List[Farm] = Depends(get_active_farms_url_or_list),
    db: Session = Depends(get_db),
):
    data = {}
    for farm in farm_list:
        data[farm.id] = []

        # Get a farmOS client.
        try:
            farm_client = get_farm_client(db=db, farm=farm)
        except ClientError:
            continue

        # Make the request.
        try:
            data[farm.id].append(farm_client.asset.send(payload=asset.dict()))
        except:
            continue

    return data
예제 #8
0
def delete_farm_assets(
    id: int,
    farm_list: List[Farm] = Depends(get_active_farms_url_or_list),
    db: Session = Depends(get_db),
):
    data = {}
    for farm in farm_list:
        data[farm.id] = []

        # Get a farmOS client.
        try:
            farm_client = get_farm_client(db_session=db, farm=farm)
        except ClientError:
            continue

        # Make the request.
        try:
            data[farm.id].append(farm_client.asset.delete(id=id))
        except:
            continue

    return data