Beispiel #1
0
def edit_shadow(request, shdw_id):
    """
    GET request: renders the edit page
    POST request: makes the shadow device update
    """
    user = User.get_instance()

    if request.POST:
        url = UM.DB_URL + 'updateShadow/{}/'.format(shdw_id)
        data = request.POST
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.post(url=url, data=data, headers=headers)

        if req.status_code == 200:
            return redirect('/profile/')
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/editShadow/{}/'.format(shdw_id)
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/edit.html')
        url = UM.DB_URL + 'getShadowById/{}/'.format(shdw_id)
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            data = json.loads(req.text)['shadow']
            context = {'data': json.loads(data), 'email': user.user_email}
            return HttpResponse(template.render(context, request))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/profile/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
def new_token(request, shdw_id):
    '''generates a new token and refresh the page'''
    user = User.get_instance()

    url = UM.DB_URL + 'generateToken/'
    headers = {'Authorization': 'Token {}'.format(user.user_token)}
    data = {'shadow_id': shdw_id, 'type': 'DEVICE'}
    req = requests.post(url=url, data=data,
                        headers=headers)  # HERE THE TOKEN IS CREATED

    if req.status_code == 200:
        tkn_id = json.loads(req.text)['token']

        url_update_shadow = UM.DB_URL + 'updateShadow/{}/'.format(shdw_id)
        data_update = {'token': tkn_id}

        req_update = requests.post(
            url=url_update_shadow, data=data_update,
            headers=headers)  # HERE WE UPDATE THE SHADOW

        if req_update.status_code == 200:
            return redirect('/viewTokens/{}/'.format(shdw_id))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req_update.status_code,
                'message': req_update.text,
                'error_name': HTTPStatus(req_update.status_code).phrase,
                'back': '/viewTokens/{}/'.format(shdw_id)
            }
            if req_update.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/error_page.html')
        context = {
            'code_error': req.status_code,
            'message': req.text,
            'error_name': HTTPStatus(req.status_code).phrase,
            'back': '/viewDevices/{}/'.format(shdw_id)
        }
        if req.status_code == 401:
            context['message'] = context['message'] + UM.REFRESH_TOKEN
            context['back'] = '/login/'

        return HttpResponse(template.render(context, request))
Beispiel #3
0
def register(request):
    """
    GET request: renders the register page
    POST request: registers the user and redirects him to user homepage
    """
    if request.POST:
        url = UM.DB_URL + 'registerUser/'
        data = request.POST
        req = requests.post(url=url, data=data)

        if req.status_code == 200:
            user_data = json.loads(req.text)

            try:
                # if an instance aleready exists it throws exception
                User(user_data['id'], user_data['token'], user_data['email'])
            except:
                user = User.get_instance()
                user.user_id = user_data['id']
                user.user_token = user_data['token']
                user.user_email = user_data['email']

            return redirect('/profile/')
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': HTTPStatus.EXPECTATION_FAILED,
                'message': UM.ALREADY_REGISTERED,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/login/'
            }
            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/register.html')
        return HttpResponse(template.render({}, request))
Beispiel #4
0
def profile(request):
    """Renders user's main page"""
    template = loader.get_template('../templates/shadows.html')
    user = User.get_instance()

    url = UM.DB_URL + 'getShadowsByUser/{}/'.format(user.user_id)
    headers = {'Authorization': 'Token {}'.format(user.user_token)}
    req = requests.get(url=url, headers=headers)

    if req.status_code == 200:
        shdw_list = json.loads(req.text)['shadows']
        context = {'shadows': [], 'email': user.user_email}
        if shdw_list:
            for shadow in shdw_list:
                json_object = json.loads(shadow)
                json_object['id'] = json_object[
                    '_id']  # because error occurrs in html if i try to do {{shadow._id}}
                context['shadows'].append(json_object)

        return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/error_page.html')
        context = {
            'code_error': req.status_code,
            'message': req.text,
            'error_name': HTTPStatus(req.status_code).phrase,
            'back': '/login/'
        }
        if req.status_code == 401:
            context['message'] = context['message'] + UM.REFRESH_TOKEN

        return HttpResponse(template.render(context, request))
Beispiel #5
0
def connectors(request):
    """Renders the page of the Iot Connectors"""
    user = User.get_instance()

    template = loader.get_template('../templates/iot_connectors.html')
    headers = {'Authorization': 'Token {}'.format(user.user_token)}
    url_connectors = UM.DB_URL + 'getAllConnectors/'
    req = requests.get(url=url_connectors, headers=headers)

    if req.status_code == 200:
        connector_list = json.loads(req.text)['images']
        context = {'connectors': [], 'email': user.user_email}

        if connector_list:
            for conn in connector_list:
                conn_json_object = json.loads(conn)
                context['connectors'].append(conn_json_object)

        return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/error_page.html')
        context = {
            'code_error': req.status_code,
            'message': req.text,
            'error_name': HTTPStatus(req.status_code).phrase,
            'back': '/login/'
        }
        if req.status_code == 401:
            context['message'] = context['message'] + UM.REFRESH_TOKEN

        return HttpResponse(template.render(context, request))
Beispiel #6
0
def delete_shadow(request, shdw_id):
    '''Deletes a shadow device'''
    user = User.get_instance()

    # first we delete it's associated devices
    url_devices = UM.DB_URL + 'getShadowDevices/{}/'.format(shdw_id)
    headers = {'Authorization': 'Token {}'.format(user.user_token)}
    req = requests.get(url=url_devices, headers=headers)

    devices_ids = []
    if req.status_code == 200:
        devices_ids = [
            json.loads(dev)['_id'] for dev in json.loads(req.text)['devices']
        ]

    url_delete_device = UM.DB_URL + 'deletePhysicalDevice/{}/'

    for dev_id in devices_ids:
        url_delete_device.format(dev_id)
        requests.get(url=url_delete_device, headers=headers)

    url = UM.DB_URL + 'deleteShadow/{}/'.format(shdw_id)
    req = requests.get(url=url, headers=headers)

    if req.status_code == 200:
        return redirect('/profile/')
    else:
        template = loader.get_template('../templates/error_page.html')
        context = {
            'code_error': req.status_code,
            'message': req.text,
            'error_name': HTTPStatus(req.status_code).phrase,
            'back': '/profile/'
        }
        if req.status_code == 401:
            context['message'] = context['message'] + UM.REFRESH_TOKEN
            context['back'] = '/login/'

        return HttpResponse(template.render(context, request))
Beispiel #7
0
def new_shadow(request):
    """
    GET request: render the page for a new shadow device
    POST request: create the new shadow device
    """
    user = User.get_instance()

    if request.POST:
        url = UM.DB_URL + 'createShadow/'
        data = request.POST
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.post(url=url, data=data, headers=headers)

        if req.status_code == 200:
            shadow = json.loads(req.text)['shadow']
            shadow_id = json.loads(shadow)['_id']  # this will go to the user
            url2 = UM.DB_URL + 'updateUser/{}/'.format(user.user_id)
            data2 = {'shadow': shadow_id}
            requests.post(url=url2, data=data2,
                          headers=headers)  # WE UPDATE HERE THE USER

            return redirect('/profile/')
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/newShadow/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/new_shadow.html')
        return HttpResponse(
            template.render({'email': user.user_email}, request))
Beispiel #8
0
def shadow_resources(request, shdw_id):
    """Renders the page that contains all the resources of a shadow device"""
    user = User.get_instance()

    template = loader.get_template('../templates/resources.html')

    url = UM.DB_URL + 'getShadowResources/{}/'.format(shdw_id)
    headers = {'Authorization': 'Token {}'.format(user.user_token)}
    req = requests.get(url=url, headers=headers)

    if req.status_code == 200:
        resources_list = json.loads(req.text)['resources']
        context = {'resources': {}, 'email': user.user_email, 'shadow_id': shdw_id}

        if resources_list:
            json_object_list = [json.loads(x) for x in resources_list]
            frequency = {}

            for resource in json_object_list:
                if resource['type'] in frequency:
                    frequency[resource['type']] += 1
                else:
                    frequency[resource['type']] = 1

            context['resources'] = frequency

        return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/error_page.html')
        context = {'code_error': req.status_code,
                   'message': req.text,
                   'error_name': HTTPStatus(req.status_code).phrase,
                   'back': '/profile/'
                   }
        if req.status_code == 401:
            context['message'] = context['message'] + UM.REFRESH_TOKEN
            context['back'] = '/login/'

        return HttpResponse(template.render(context, request))
Beispiel #9
0
def new_connector(request):
    """
    GET request: renders the page for a new connector
    POST request: Stores the connector and redirects to the connectors view
    """
    user = User.get_instance()

    if request.POST:
        url = UM.DB_URL + 'storeType/'
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        data = {}

        data['image'] = request.POST['inputImage']
        data['type'] = request.POST['inputType']

        req = requests.post(url=url, data=data, headers=headers)

        if req.status_code == HTTPStatus.OK or req.status_code == HTTPStatus.NOT_MODIFIED:
            return redirect('/connectors/')
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/newConnector/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('../templates/new_connector.html')
        return HttpResponse(
            template.render({'email': user.user_email}, request))
Beispiel #10
0
 def setStatusCode(self, code: int):
     self.__status = HTTPStatus(code)
Beispiel #11
0
def devices(request, shdw_id):
    """
    GET request: renders the physical devices page
    POST request: It's a request to delete one physical device
    """

    user = User.get_instance()

    if request.POST:  # REQUEST TO DELETE DEVICE
        url = UM.DB_URL + 'deletePhysicalDevice/{}/'.format(
            request.POST['device_id'])
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            return redirect('/viewDevices/{}/'.format(shdw_id))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/viewDevices/{}/'.format(shdw_id)
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))

    else:  # GET - RENDER THE TEMPLATE WITH PHYSICAL DEVICES

        template = loader.get_template('../templates/physical_devices.html')

        url = UM.DB_URL + 'getShadowDevices/{}/'.format(shdw_id)
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            devices_list = json.loads(req.text)['devices']
            context = {
                'devices': [],
                'shadow_id': shdw_id,
                'email': user.user_email
            }
            if devices_list:
                for device in devices_list:
                    json_object = json.loads(device)

                    # CHECK THIS AGAIN
                    url_token = UM.DB_URL + 'getTokenById/{}/'.format(
                        json_object['token'])
                    res_tok = requests.get(url=url_token, headers=headers)
                    token = json.loads(res_tok.text)['token']
                    json_object[
                        'token'] = token  # we replace token id with token value

                    json_object['id'] = json_object['_id']

                    url_status = UM.DB_URL + 'getDeviceStatus/{}/'.format(
                        json_object['_id'])
                    req_status = requests.get(url=url_status, headers=headers)
                    json_object['STATUS'] = json.loads(
                        req_status.text)['status']

                    context['devices'].append(json_object)

            return HttpResponse(template.render(context, request))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/profile/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
Beispiel #12
0
def dev_resources(request, dev_id):
    """
    GET request: renders the page with all the resources of a device
    POST request: It's a request to delete a specific resource
    """

    user = User.get_instance()

    if request.POST:  # REQUEST TO DELETE A RESOURCE
        url = UM.DB_URL + 'deleteResource/{}/'.format(request.POST['resource_id'])  # must be done in database
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            return redirect('/viewDeviceResources/{}/'.format(dev_id))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {'code_error': req.status_code,
                       'message': req.text,
                       'error_name': HTTPStatus(req.status_code).phrase,
                       'back': '/viewDeviceResources/{}/'.format(dev_id)
                       }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
    else:  # GET - RENDER THE TEMPLATE WITH DEVICE RESOURCES

        template = loader.get_template('../templates/resources.html')

        url = UM.DB_URL + 'getDeviceResources/{}/'.format(dev_id)
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            resources = json.loads(req.text)
            context = {'resources': {}, 'email': user.user_email, 'device_id': dev_id}
            if resources:
                for ep, resources_list in resources.items():
                    for res in resources_list:
                        json_object = json.loads(res)
                        json_object['id'] = json_object['_id']

                        url_status = UM.DB_URL + 'getResourceStatus/{}/'.format(json_object['_id'])
                        req_status = requests.get(url=url_status, headers=headers)
                        json_object['STATUS'] = json.loads(req_status.text)['status']

                        if ep in context['resources']:
                            context["resources"][ep].append(json_object)
                        else:
                            context["resources"][ep] = [json_object]

            return HttpResponse(template.render(context, request))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {'code_error': req.status_code,
                       'message': req.text,
                       'error_name': HTTPStatus(req.status_code).phrase,
                       'back': '/profile/'
                       }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))
def tokens(request, shdw_id):
    """
    GET request: renders the token page
    POST request: revokes a specific token
    """
    user = User.get_instance()

    if not request.POST:
        template = loader.get_template('../templates/shadow_tokens.html')

        url = UM.DB_URL + 'getShadowTokens/{}/'.format(shdw_id)
        headers = {'Authorization': 'Token {}'.format(user.user_token)}
        req = requests.get(url=url, headers=headers)

        if req.status_code == 200:
            tkn_list = json.loads(req.text)['tokens']
            context = {'tokens': [], 'email': user.user_email}

            if tkn_list:
                for tkn in tkn_list:
                    json_object = json.loads(tkn)
                    if json_object["revoked"]:
                        json_object['status'] = "REVOKED"
                    else:
                        json_object['status'] = "VALID"

                    context['tokens'].append(json_object)

            context['shadow'] = shdw_id
            return HttpResponse(template.render(context, request))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/profile/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))

    else:  # it's a post (to revoke a token)
        url = UM.DB_URL + 'revokeToken/'
        token = request.POST['token']
        headers = {'Authorization': 'Token {}'.format(token)}
        req = requests.get(url=url,
                           headers=headers)  # HERE THE TOKEN IS REVOKED
        if req.status_code == 200:
            return redirect('/viewDevices/{}/'.format(shdw_id))
        else:
            template = loader.get_template('../templates/error_page.html')
            context = {
                'code_error': req.status_code,
                'message': req.text,
                'error_name': HTTPStatus(req.status_code).phrase,
                'back': '/login/'
            }
            if req.status_code == 401:
                context['message'] = context['message'] + UM.REFRESH_TOKEN
                context['back'] = '/login/'

            return HttpResponse(template.render(context, request))