Esempio n. 1
0
async def post_user_profile(request: Request,
                            userId: int = Form(...),
                            profileId: int = Form(...)):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        users_details = requests.post(f"{BL_SERVER_URL}/users/profiles",
                                      headers={"Authorization": authorization},
                                      json={
                                          'user_id': userId,
                                          'profile_id': profileId
                                      })
        if users_details.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        user_details = requests.get(f"{BL_SERVER_URL}/users/{userId}",
                                    headers={"Authorization": authorization})
        response = templates.TemplateResponse(
            'user.html',
            context={
                'request': request,
                'data_list': (user_details.json())['data'][0]
            },
            status_code=status.HTTP_200_OK)
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 2
0
async def post_users(request: Request,
                     username: str = Form(...),
                     password: str = Form(...),
                     fullName: str = Form(...),
                     email: str = Form(...)):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        users_details = requests.post(f"{BL_SERVER_URL}/users",
                                      headers={"Authorization": authorization},
                                      json={
                                          'username': username,
                                          'password':
                                          pwd_context.encrypt(password),
                                          'full_name': fullName,
                                          'email': email
                                      })
        if users_details.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'user.html',
            context={
                'request': request,
                'data_list': (users_details.json())['data'][0]
            },
            status_code=status.HTTP_200_OK)
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 3
0
async def post_devices(request: Request,
                       name: str = Form(...),
                       description: str = Form(...),
                       limit: float = Form(...),
                       typeId: int = Form(...)):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        devices_details = requests.post(f"{BL_SERVER_URL}/devices",
                                        headers={"Authorization": authorization},
                                        json={
                                             'name': name,
                                             'description': description,
                                             'limit': limit,
                                             'type_id': typeId
                                        })
        if devices_details.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'device.html',
            context={
                'request': request,
                'data_list': (devices_details.json())['data'][0]
            },
            status_code=status.HTTP_200_OK
        )
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 4
0
async def put_settings(request: Request,
                       id: int,
                       description: str = Form(...),
                       limit: float = Form(...)
                       ):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        put_resp = requests.put(f"{BL_SERVER_URL}/devices/{id}",
                                headers={"Authorization": authorization},
                                json={
                                    'description': description,
                                    'limit': limit
                                })
        devices_details = requests.get(f"{BL_SERVER_URL}/devices", headers={"Authorization": authorization})
        if devices_details.status_code == status.HTTP_401_UNAUTHORIZED or \
                put_resp.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'devices.html',
            context={
                'request': request,
                'data_list': (devices_details.json())['data']
            },
            status_code=status.HTTP_200_OK
        )
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 5
0
async def delete_group_device(request: Request,
                              deviceId: int,
                              groupId: int):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        delete_resp = requests.delete(
            f"{BL_SERVER_URL}/devices/{deviceId}/groups/{groupId}",
            headers={"Authorization": authorization}
        )
        if delete_resp.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        group_details = requests.get(f"{BL_SERVER_URL}/groups/{groupId}", headers={"Authorization": authorization})
        response = templates.TemplateResponse(
            'group.html',
            context={
                'request': request,
                'data_list': (group_details.json())['data'][0]
            },
            status_code=status.HTTP_200_OK
        )
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 6
0
async def logout(request: Request):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if authorization:
            logout_response = requests.post(
                f"{BL_SERVER_URL}/logout",
                headers={"Authorization": authorization})
        AuthorizationUser.logout_user(request.client.host)
        return RedirectResponse(url='/home')
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 7
0
async def home(request: Request):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        user_details = requests.get(f"{BL_SERVER_URL}/",
                                    headers={"Authorization": authorization})
        if user_details.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'home.html',
            context={
                'request': request,
                'data_list': (user_details.json())['data'][0]
            },
            status_code=status.HTTP_200_OK)
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 8
0
async def real_time_values(request: Request):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        sensors_values = requests.get(f"{BL_SERVER_URL}/realTimeValues",
                                      headers={"Authorization": authorization})
        if sensors_values.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'real_time_values.html',
            context={
                'request': request,
                'data_list': (sensors_values.json())['data']
            },
            status_code=status.HTTP_200_OK)
        return response
    except Exception as e:
        return raise_exception(e, request)
Esempio n. 9
0
async def delete_profile(request: Request, id: int):
    try:
        authorization = AuthorizationUser.get_token(request.client.host)
        if not authorization:
            return RedirectResponse('/')
        delete_resp = requests.delete(f"{BL_SERVER_URL}/profiles/{id}",
                                      headers={"Authorization": authorization})
        profiles_details = requests.get(
            f"{BL_SERVER_URL}/profiles",
            headers={"Authorization": authorization})
        if delete_resp.status_code == status.HTTP_401_UNAUTHORIZED or \
                profiles_details.status_code == status.HTTP_401_UNAUTHORIZED:
            AuthorizationUser.logout_user(request.client.host)
            return RedirectResponse('/')
        response = templates.TemplateResponse(
            'profiles.html',
            context={
                'request': request,
                'data_list': (profiles_details.json())['data']
            },
            status_code=status.HTTP_200_OK)
        return response
    except Exception as e:
        return raise_exception(e, request)