Skip to content

Commit

Permalink
agriculture: fix comments from pull request
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Coman <[email protected]>
  • Loading branch information
victorcoman committed Dec 15, 2021
1 parent be00e8b commit 98ae006
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 104 deletions.
2 changes: 0 additions & 2 deletions agriculture/Procfile

This file was deleted.

2 changes: 1 addition & 1 deletion agriculture/agriculturecommon/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions agriculture/agriculturecommon/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

urlpatterns = [

# path("index.html", include("app.urls")),

path("access/", include("login.urls")),

path("", include("agriculturecore.urls")),
Expand Down
56 changes: 43 additions & 13 deletions agriculture/agriculturecore/drm_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
ID_MOISTURE = "moisture"

ID_CONTROLLERS = "controllers"
ID_ERROR = "error"
ID_STATIONS = "stations"
ID_WEATHER = "weather"
ID_TANK = "tank"
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 0 additions & 10 deletions agriculture/agriculturecore/templates/index.html

This file was deleted.

4 changes: 3 additions & 1 deletion agriculture/agriculturecore/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@
if (data["redirect"])
window.location.replace(data["redirect"]);
}
);
).fail(function(response) {
processErrorResponse(response);
});
}
</script>
{% endblock %}
88 changes: 33 additions & 55 deletions agriculture/agriculturecore/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
PARAM_CONTROLLER_ID = "controller_id"
PARAM_FARM_NAME = "farm_name"

ID_ERROR = "error"
ID_ERROR_TITLE = "error_title"
ID_ERROR_MSG = "error_msg"
ID_ERROR_GUIDE = "error_guide"
Expand Down Expand Up @@ -253,13 +252,10 @@ def get_smart_farms(request):
:class:`.JsonResponse`: A JSON response with the list of the Smart
Farms within the DRM account.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

smart_farms = get_farms(request)
if len(smart_farms) > 0:
Expand Down Expand Up @@ -287,13 +283,10 @@ def get_irrigation_stations(request):
Stations corresponding to the controller with the ID provided in
the request.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

# Get the controller ID from the POST request.
controller_id = request.POST[PARAM_CONTROLLER_ID]
Expand All @@ -308,7 +301,7 @@ def get_irrigation_stations(request):
ID_ERROR_MSG: NO_STATIONS_MSG,
ID_ERROR_GUIDE: SETUP_MODULES_GUIDE})
except DeviceCloudHttpException as e:
return JsonResponse({ID_ERROR: str(e)})
return get_exception_response(e)


def get_farm_status(request):
Expand All @@ -322,13 +315,10 @@ def get_farm_status(request):
Returns:
:class:`.JsonResponse`: A JSON response with the status of the farm.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

try:
# Get the controller ID from the POST request.
Expand Down Expand Up @@ -362,7 +352,7 @@ def get_farm_status(request):

return JsonResponse(farm_status, status=200)
except Exception as e:
return JsonResponse({ID_ERROR: str(e)})
return get_exception_response(e)


def set_valve(request):
Expand All @@ -376,13 +366,10 @@ def set_valve(request):
Returns:
:class:`.JsonResponse`: A JSON response with the set status.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

# Get the controller ID and irrigation station from the POST request.
data = json.loads(request.body.decode(request.encoding))
Expand All @@ -393,7 +380,7 @@ def set_valve(request):
new_value = set_valve_value(request, controller_id, station_id, value)
if new_value is not None:
return JsonResponse({"value": new_value}, status=200)
return JsonResponse({"error": "Could not set the valve."}, status=400)
return JsonResponse({ID_ERROR: "Could not set the valve."}, status=400)


def set_tank_valve(request):
Expand All @@ -407,13 +394,10 @@ def set_tank_valve(request):
Returns:
:class:`.JsonResponse`: A JSON response with the set status.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

# Get the controller ID and status of the valve from the POST request.
data = json.loads(request.body.decode(request.encoding))
Expand All @@ -423,7 +407,7 @@ def set_tank_valve(request):
new_value = set_tank_valve_value(request, controller_id, value)
if new_value is not None:
return JsonResponse({"value": new_value}, status=200)
return JsonResponse({"error": "Could not set the valve."}, status=400)
return JsonResponse({ID_ERROR: "Could not set the valve."}, status=400)


def refill_tank(request):
Expand All @@ -437,13 +421,10 @@ def refill_tank(request):
Returns:
:class:`.JsonResponse`: A JSON response with the set status.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

# Get the controller ID and level of the tank from the POST request.
data = json.loads(request.body.decode(request.encoding))
Expand All @@ -452,7 +433,7 @@ def refill_tank(request):
new_value = refill_tank_request(request, controller_id)
if new_value is not None:
return JsonResponse({"value": new_value}, status=200)
return JsonResponse({"error": "Could not set the valve."}, status=400)
return JsonResponse({ID_ERROR: "Could not set the valve."}, status=400)


def get_request_data(request):
Expand Down Expand Up @@ -605,13 +586,10 @@ def check_farm_connection_status(request):
Returns:
A JSON with the status of the farm or the error.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse(
{"error": "AJAX request must be sent using POST"},
status=400)
else:
return redirect('/access/login')
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error

# Get the controller ID and irrigation station from the POST request.
data = json.loads(request.body.decode(request.encoding))
Expand Down
24 changes: 12 additions & 12 deletions agriculture/run_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,18 @@ def main():

# If Python version is greater than 3.7, install the corresponding
# Twisted wheel so the channels module can be installed later on.
# if sys.platform == "win32" and py_minor_version > 7:
# twisted = TWISTED_64 if is_64_bits_python() else TWISTED_32
# twisted_path = os.path.join(
# FOLDER_WHEELS,
# twisted.format(py_major_version, py_minor_version))
# print("- Installing Twisted wheel (%s)... " % twisted_path, end="")
# sys.stdout.flush()
# if run_venv_python(venv_context, ['-m', 'pip', 'install',
# twisted_path], debug) != 0:
# print_error()
# sys.exit(-1)
# print_success()
if sys.platform == "win32" and py_minor_version > 7:
twisted = TWISTED_64 if is_64_bits_python() else TWISTED_32
twisted_path = os.path.join(
FOLDER_WHEELS,
twisted.format(py_major_version, py_minor_version))
print("- Installing Twisted wheel (%s)... " % twisted_path, end="")
sys.stdout.flush()
if run_venv_python(venv_context, ['-m', 'pip', 'install',
twisted_path], debug) != 0:
print_error()
sys.exit(-1)
print_success()

# Install the application requirements.
print("- Installing application requirements: ")
Expand Down
Binary file removed agriculture/source.zip
Binary file not shown.
Loading

0 comments on commit 98ae006

Please sign in to comment.