From 580ba0ec610bd70406335a27375b80b47a8f8a40 Mon Sep 17 00:00:00 2001 From: Victor Coman Date: Wed, 15 Dec 2021 11:48:09 +0100 Subject: [PATCH] agriculture: fix comments from pull request Signed-off-by: Victor Coman --- agriculture/Procfile | 2 - agriculture/agriculturecommon/settings.py | 2 +- agriculture/agriculturecommon/urls.py | 2 - agriculture/agriculturecore/drm_requests.py | 56 +++++++++--- .../agriculturecore/templates/dashboard.html | 4 +- .../agriculturecore/templates/index.html | 10 --- .../agriculturecore/templates/sidebar.html | 21 +---- agriculture/agriculturecore/views.py | 83 ++++++------------ agriculture/run_web_app.py | 24 ++--- agriculture/source.zip | Bin 695926 -> 0 bytes agriculture/static/js/dashboard.js | 22 +++-- agriculture/static/js/history.js | 22 ++++- agriculture/static/js/widgets.js | 7 +- 13 files changed, 126 insertions(+), 129 deletions(-) delete mode 100644 agriculture/Procfile delete mode 100644 agriculture/agriculturecore/templates/index.html delete mode 100644 agriculture/source.zip diff --git a/agriculture/Procfile b/agriculture/Procfile deleted file mode 100644 index e342204..0000000 --- a/agriculture/Procfile +++ /dev/null @@ -1,2 +0,0 @@ -web: gunicorn --bind :8000 --workers 3 --threads 2 agriculturecommon.wsgi:application -websocket: daphne -b :: -p 5000 agriculturecommon.asgi:application diff --git a/agriculture/agriculturecommon/settings.py b/agriculture/agriculturecommon/settings.py index a66b108..95dc2bc 100644 --- a/agriculture/agriculturecommon/settings.py +++ b/agriculture/agriculturecommon/settings.py @@ -39,7 +39,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = ['127.0.0.1', 'agriculturedemodigi-env-1.us-west-2.elasticbeanstalk.com', 'django-env5.us-west-2.elasticbeanstalk.com', '172.31.27.57', '172.31.18.186'] +ALLOWED_HOSTS = ['*'] # Application definition diff --git a/agriculture/agriculturecommon/urls.py b/agriculture/agriculturecommon/urls.py index 559965a..204d18e 100644 --- a/agriculture/agriculturecommon/urls.py +++ b/agriculture/agriculturecommon/urls.py @@ -32,8 +32,6 @@ urlpatterns = [ - # path("index.html", include("app.urls")), - path("access/", include("login.urls")), path("", include("agriculturecore.urls")), diff --git a/agriculture/agriculturecore/drm_requests.py b/agriculture/agriculturecore/drm_requests.py index bc0d69a..80896e1 100644 --- a/agriculture/agriculturecore/drm_requests.py +++ b/agriculture/agriculturecore/drm_requests.py @@ -67,6 +67,7 @@ ID_MOISTURE = "moisture" ID_CONTROLLERS = "controllers" +ID_ERROR = "error" ID_STATIONS = "stations" ID_WEATHER = "weather" ID_TANK = "tank" @@ -148,6 +149,39 @@ def get_device_cloud_session(session): base_url=user_serialized.server) +def check_ajax_request(request): + """ + Checks whether the given AJAX request is valid and the user is + authenticated. + Args: + request (:class:`.WSGIRequest`): The HTTP request. + Returns: + `None` if the request is valid, or a `JsonResponse` with the error + if it is not. + """ + if is_authenticated(request): + if not request.is_ajax or request.method != "POST": + return JsonResponse({ID_ERROR: "AJAX request must be sent using POST"}, status=400) + return None + else: + return JsonResponse({ID_ERROR: "Not authenticated"}, status=401) + + +def get_exception_response(e): + """ + Returns the JSON response with the error contained in the given exception. + + Args: + e (:class:`.Exception`): The exception. + + Returns: + A JSON response with the details of the exception. + """ + return JsonResponse({ID_ERROR: ("Error in the DRM request: {}.".format(e.response.text) + if isinstance(e, DeviceCloudHttpException) else str(e))}, + status=400) + + def send_device_request(request, target): """ Sends a Device Request to DRM to the device with the given ID. @@ -159,13 +193,12 @@ def send_device_request(request, target): Returns: A JSON with the response or the error. """ - if not request.is_ajax or request.method != "POST": - return JsonResponse({"error": "AJAX request must be sent using POST"}, - status=400) + # Check if the AJAX request is valid. + error = check_ajax_request(request) + if error is not None: + return error dc = get_device_cloud(request) - if dc is None: - return JsonResponse({"error": "Invalid credentials."}, status=400) device_id = request.POST[views.PARAM_CONTROLLER_ID] data = request.POST[PARAM_DATA] if PARAM_DATA in request.POST else None @@ -176,9 +209,7 @@ def send_device_request(request, target): return JsonResponse({"data": resp}, status=200) return JsonResponse({"valid": True}, status=200) except DeviceCloudHttpException as e: - return JsonResponse( - {"error": "Error in the DRM request: {}.".format(e.response.text)}, - status=e.response.status_code) + return get_exception_response(e) def send_request(dc, device_id, target, data=None): @@ -534,13 +565,12 @@ def get_data_points(request, stream_name): Returns: A JSON with the data points or the error. """ - if not request.is_ajax or request.method != "POST": - return JsonResponse({"error": "AJAX request must be sent using POST"}, - status=400) + # Check if the AJAX request is valid. + error = check_ajax_request(request) + if error is not None: + return error dc = get_device_cloud(request) - if dc is None: - return JsonResponse({"error": "Invalid credentials."}, status=400) device_id = request.POST[views.PARAM_CONTROLLER_ID] interval = int( diff --git a/agriculture/agriculturecore/templates/dashboard.html b/agriculture/agriculturecore/templates/dashboard.html index acd9ccb..cef1348 100644 --- a/agriculture/agriculturecore/templates/dashboard.html +++ b/agriculture/agriculturecore/templates/dashboard.html @@ -88,9 +88,7 @@
Weather station
- km/h () - - + km/h () L/m² lux diff --git a/agriculture/agriculturecore/templates/index.html b/agriculture/agriculturecore/templates/index.html deleted file mode 100644 index 9325721..0000000 --- a/agriculture/agriculturecore/templates/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Title - - - - - \ No newline at end of file diff --git a/agriculture/agriculturecore/templates/sidebar.html b/agriculture/agriculturecore/templates/sidebar.html index e03222f..49b2b85 100644 --- a/agriculture/agriculturecore/templates/sidebar.html +++ b/agriculture/agriculturecore/templates/sidebar.html @@ -21,21 +21,6 @@