Skip to content

Commit 98ae006

Browse files
committed
agriculture: fix comments from pull request
Signed-off-by: Victor Coman <victor.coman@digi.com>
1 parent be00e8b commit 98ae006

File tree

12 files changed

+131
-104
lines changed

12 files changed

+131
-104
lines changed

agriculture/Procfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

agriculture/agriculturecommon/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# SECURITY WARNING: don't run with debug turned on in production!
4040
DEBUG = True
4141

42-
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']
42+
ALLOWED_HOSTS = ['*']
4343

4444

4545
# Application definition

agriculture/agriculturecommon/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
urlpatterns = [
3434

35-
# path("index.html", include("app.urls")),
36-
3735
path("access/", include("login.urls")),
3836

3937
path("", include("agriculturecore.urls")),

agriculture/agriculturecore/drm_requests.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
ID_MOISTURE = "moisture"
6868

6969
ID_CONTROLLERS = "controllers"
70+
ID_ERROR = "error"
7071
ID_STATIONS = "stations"
7172
ID_WEATHER = "weather"
7273
ID_TANK = "tank"
@@ -148,6 +149,39 @@ def get_device_cloud_session(session):
148149
base_url=user_serialized.server)
149150

150151

152+
def check_ajax_request(request):
153+
"""
154+
Checks whether the given AJAX request is valid and the user is
155+
authenticated.
156+
Args:
157+
request (:class:`.WSGIRequest`): The HTTP request.
158+
Returns:
159+
`None` if the request is valid, or a `JsonResponse` with the error
160+
if it is not.
161+
"""
162+
if is_authenticated(request):
163+
if not request.is_ajax or request.method != "POST":
164+
return JsonResponse({ID_ERROR: "AJAX request must be sent using POST"}, status=400)
165+
return None
166+
else:
167+
return JsonResponse({ID_ERROR: "Not authenticated"}, status=401)
168+
169+
170+
def get_exception_response(e):
171+
"""
172+
Returns the JSON response with the error contained in the given exception.
173+
174+
Args:
175+
e (:class:`.Exception`): The exception.
176+
177+
Returns:
178+
A JSON response with the details of the exception.
179+
"""
180+
return JsonResponse({ID_ERROR: ("Error in the DRM request: {}.".format(e.response.text)
181+
if isinstance(e, DeviceCloudHttpException) else str(e))},
182+
status=400)
183+
184+
151185
def send_device_request(request, target):
152186
"""
153187
Sends a Device Request to DRM to the device with the given ID.
@@ -159,13 +193,12 @@ def send_device_request(request, target):
159193
Returns:
160194
A JSON with the response or the error.
161195
"""
162-
if not request.is_ajax or request.method != "POST":
163-
return JsonResponse({"error": "AJAX request must be sent using POST"},
164-
status=400)
196+
# Check if the AJAX request is valid.
197+
error = check_ajax_request(request)
198+
if error is not None:
199+
return error
165200

166201
dc = get_device_cloud(request)
167-
if dc is None:
168-
return JsonResponse({"error": "Invalid credentials."}, status=400)
169202

170203
device_id = request.POST[views.PARAM_CONTROLLER_ID]
171204
data = request.POST[PARAM_DATA] if PARAM_DATA in request.POST else None
@@ -176,9 +209,7 @@ def send_device_request(request, target):
176209
return JsonResponse({"data": resp}, status=200)
177210
return JsonResponse({"valid": True}, status=200)
178211
except DeviceCloudHttpException as e:
179-
return JsonResponse(
180-
{"error": "Error in the DRM request: {}.".format(e.response.text)},
181-
status=e.response.status_code)
212+
return get_exception_response(e)
182213

183214

184215
def send_request(dc, device_id, target, data=None):
@@ -534,13 +565,12 @@ def get_data_points(request, stream_name):
534565
Returns:
535566
A JSON with the data points or the error.
536567
"""
537-
if not request.is_ajax or request.method != "POST":
538-
return JsonResponse({"error": "AJAX request must be sent using POST"},
539-
status=400)
568+
# Check if the AJAX request is valid.
569+
error = check_ajax_request(request)
570+
if error is not None:
571+
return error
540572

541573
dc = get_device_cloud(request)
542-
if dc is None:
543-
return JsonResponse({"error": "Invalid credentials."}, status=400)
544574

545575
device_id = request.POST[views.PARAM_CONTROLLER_ID]
546576
interval = int(

agriculture/agriculturecore/templates/index.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

agriculture/agriculturecore/templates/sidebar.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@
243243
if (data["redirect"])
244244
window.location.replace(data["redirect"]);
245245
}
246-
);
246+
).fail(function(response) {
247+
processErrorResponse(response);
248+
});
247249
}
248250
</script>
249251
{% endblock %}

agriculture/agriculturecore/views.py

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
PARAM_CONTROLLER_ID = "controller_id"
2020
PARAM_FARM_NAME = "farm_name"
2121

22-
ID_ERROR = "error"
2322
ID_ERROR_TITLE = "error_title"
2423
ID_ERROR_MSG = "error_msg"
2524
ID_ERROR_GUIDE = "error_guide"
@@ -253,13 +252,10 @@ def get_smart_farms(request):
253252
:class:`.JsonResponse`: A JSON response with the list of the Smart
254253
Farms within the DRM account.
255254
"""
256-
if is_authenticated(request):
257-
if not request.is_ajax or request.method != "POST":
258-
return JsonResponse(
259-
{"error": "AJAX request must be sent using POST"},
260-
status=400)
261-
else:
262-
return redirect('/access/login')
255+
# Check if the AJAX request is valid.
256+
error = check_ajax_request(request)
257+
if error is not None:
258+
return error
263259

264260
smart_farms = get_farms(request)
265261
if len(smart_farms) > 0:
@@ -287,13 +283,10 @@ def get_irrigation_stations(request):
287283
Stations corresponding to the controller with the ID provided in
288284
the request.
289285
"""
290-
if is_authenticated(request):
291-
if not request.is_ajax or request.method != "POST":
292-
return JsonResponse(
293-
{"error": "AJAX request must be sent using POST"},
294-
status=400)
295-
else:
296-
return redirect('/access/login')
286+
# Check if the AJAX request is valid.
287+
error = check_ajax_request(request)
288+
if error is not None:
289+
return error
297290

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

313306

314307
def get_farm_status(request):
@@ -322,13 +315,10 @@ def get_farm_status(request):
322315
Returns:
323316
:class:`.JsonResponse`: A JSON response with the status of the farm.
324317
"""
325-
if is_authenticated(request):
326-
if not request.is_ajax or request.method != "POST":
327-
return JsonResponse(
328-
{"error": "AJAX request must be sent using POST"},
329-
status=400)
330-
else:
331-
return redirect('/access/login')
318+
# Check if the AJAX request is valid.
319+
error = check_ajax_request(request)
320+
if error is not None:
321+
return error
332322

333323
try:
334324
# Get the controller ID from the POST request.
@@ -362,7 +352,7 @@ def get_farm_status(request):
362352

363353
return JsonResponse(farm_status, status=200)
364354
except Exception as e:
365-
return JsonResponse({ID_ERROR: str(e)})
355+
return get_exception_response(e)
366356

367357

368358
def set_valve(request):
@@ -376,13 +366,10 @@ def set_valve(request):
376366
Returns:
377367
:class:`.JsonResponse`: A JSON response with the set status.
378368
"""
379-
if is_authenticated(request):
380-
if not request.is_ajax or request.method != "POST":
381-
return JsonResponse(
382-
{"error": "AJAX request must be sent using POST"},
383-
status=400)
384-
else:
385-
return redirect('/access/login')
369+
# Check if the AJAX request is valid.
370+
error = check_ajax_request(request)
371+
if error is not None:
372+
return error
386373

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

398385

399386
def set_tank_valve(request):
@@ -407,13 +394,10 @@ def set_tank_valve(request):
407394
Returns:
408395
:class:`.JsonResponse`: A JSON response with the set status.
409396
"""
410-
if is_authenticated(request):
411-
if not request.is_ajax or request.method != "POST":
412-
return JsonResponse(
413-
{"error": "AJAX request must be sent using POST"},
414-
status=400)
415-
else:
416-
return redirect('/access/login')
397+
# Check if the AJAX request is valid.
398+
error = check_ajax_request(request)
399+
if error is not None:
400+
return error
417401

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

428412

429413
def refill_tank(request):
@@ -437,13 +421,10 @@ def refill_tank(request):
437421
Returns:
438422
:class:`.JsonResponse`: A JSON response with the set status.
439423
"""
440-
if is_authenticated(request):
441-
if not request.is_ajax or request.method != "POST":
442-
return JsonResponse(
443-
{"error": "AJAX request must be sent using POST"},
444-
status=400)
445-
else:
446-
return redirect('/access/login')
424+
# Check if the AJAX request is valid.
425+
error = check_ajax_request(request)
426+
if error is not None:
427+
return error
447428

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

457438

458439
def get_request_data(request):
@@ -605,13 +586,10 @@ def check_farm_connection_status(request):
605586
Returns:
606587
A JSON with the status of the farm or the error.
607588
"""
608-
if is_authenticated(request):
609-
if not request.is_ajax or request.method != "POST":
610-
return JsonResponse(
611-
{"error": "AJAX request must be sent using POST"},
612-
status=400)
613-
else:
614-
return redirect('/access/login')
589+
# Check if the AJAX request is valid.
590+
error = check_ajax_request(request)
591+
if error is not None:
592+
return error
615593

616594
# Get the controller ID and irrigation station from the POST request.
617595
data = json.loads(request.body.decode(request.encoding))

agriculture/run_web_app.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,18 @@ def main():
292292

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

308308
# Install the application requirements.
309309
print("- Installing application requirements: ")

agriculture/source.zip

-680 KB
Binary file not shown.

0 commit comments

Comments
 (0)