Skip to content

Commit 43d9741

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

File tree

13 files changed

+125
-128
lines changed

13 files changed

+125
-128
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/dashboard.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ <h6 class="card-title">Weather station</h6>
8888
<td><span class="digi-icon-color fas fa-sun fa-2x"></span></td>
8989
</tr>
9090
<tr>
91-
<td><span id="wind_speed"><i class="fas fa-circle-notch fa-spin"></i></span> km/h (<span id="wind_direction"><i class="fas fa-circle-notch fa-spin"></i></span>)
92-
93-
</td>
91+
<td><span id="wind_speed"><i class="fas fa-circle-notch fa-spin"></i></span> km/h (<span id="wind_direction"><i class="fas fa-circle-notch fa-spin"></i></span>)</td>
9492
<td><span id="rain_acc"><i class="fas fa-circle-notch fa-spin"></i></span> L/m²</td>
9593
<td><span id="luminosity"><i class="fas fa-circle-notch fa-spin"></i></span> lux</td>
9694
</tr>

agriculture/agriculturecore/templates/index.html

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

agriculture/agriculturecore/templates/sidebar.html

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,6 @@
2121
</button>
2222
<div class="dropdown-menu shadow-sm simulation-controls-container" aria-labelledby="simulationMenuButton">
2323
<p>Simulation control</p>
24-
<!-- <div class="card">-->
25-
<!-- <p class="widget-title">Weather condition</p>-->
26-
<!-- <p class="widget-desc">Control the weather condition of the farm. You can change it to see different temperature and moisture values.</p>-->
27-
<!-- <div class="widget-button-container">-->
28-
<!-- <button id="weather-sunny" class="btn widget-button weather-button" type="button" value="0">-->
29-
<!-- <span id="weather-sunny-logo" class="icon-widget weather-icon fas fa-sun fa-2x" title="Change weather to sunny"></span>-->
30-
<!-- </button>-->
31-
<!-- <button id="weather-cloudy" class="btn widget-button weather-button" type="button" value="1">-->
32-
<!-- <span id="weather-cloudy-logo" class="icon-widget weather-icon fas fa-cloud fa-2x" title="Change weather to cloudy"></span>-->
33-
<!-- </button>-->
34-
<!-- <button id="weather-rainy" class="btn widget-button weather-button" type="button" value="2">-->
35-
<!-- <span id="weather-rainy-logo" class="icon-widget weather-icon fas fa-cloud-rain fa-2x" title="Change weather to rainy"></span>-->
36-
<!-- </button>-->
37-
<!-- </div>-->
38-
<!-- </div>-->
3924
<div class="card">
4025
<p class="widget-title">Time</p>
4126
<p class="widget-desc">Control how the time elapses in the farm. You can change it to see the irrigation events faster.</p>
@@ -188,14 +173,12 @@
188173
});
189174

190175
// Add 'click' callbacks to the weather and time buttons.
191-
// $(".weather-button").click(setWeatherCondition);
192176
$(".time-button").click(setTimeFactor);
193177

194178
// Check the farm connection status.
195179
checkFarmConnectionStatus();
196180

197181
// Get the time factor from the irrigation controller.
198-
// getWeatherCondition();
199182
getTimeFactor();
200183

201184
// Check farm connectivity in 5 secs.
@@ -243,7 +226,9 @@
243226
if (data["redirect"])
244227
window.location.replace(data["redirect"]);
245228
}
246-
);
229+
).fail(function(response) {
230+
processErrorResponse(response);
231+
});
247232
}
248233
</script>
249234
{% endblock %}

agriculture/agriculturecore/views.py

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
from django.shortcuts import redirect
1616
from django.template.response import TemplateResponse
17+
1718
from agriculturecore.drm_requests import *
1819

1920
PARAM_CONTROLLER_ID = "controller_id"
2021
PARAM_FARM_NAME = "farm_name"
2122

22-
ID_ERROR = "error"
2323
ID_ERROR_TITLE = "error_title"
2424
ID_ERROR_MSG = "error_msg"
2525
ID_ERROR_GUIDE = "error_guide"
@@ -253,13 +253,8 @@ def get_smart_farms(request):
253253
:class:`.JsonResponse`: A JSON response with the list of the Smart
254254
Farms within the DRM account.
255255
"""
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')
256+
# Check if the AJAX request is valid.
257+
check_error(request)
263258

264259
smart_farms = get_farms(request)
265260
if len(smart_farms) > 0:
@@ -287,13 +282,8 @@ def get_irrigation_stations(request):
287282
Stations corresponding to the controller with the ID provided in
288283
the request.
289284
"""
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')
285+
# Check if the AJAX request is valid.
286+
check_error(request)
297287

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

313303

314304
def get_farm_status(request):
@@ -322,13 +312,8 @@ def get_farm_status(request):
322312
Returns:
323313
:class:`.JsonResponse`: A JSON response with the status of the farm.
324314
"""
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')
315+
# Check if the AJAX request is valid.
316+
check_error(request)
332317

333318
try:
334319
# Get the controller ID from the POST request.
@@ -362,7 +347,7 @@ def get_farm_status(request):
362347

363348
return JsonResponse(farm_status, status=200)
364349
except Exception as e:
365-
return JsonResponse({ID_ERROR: str(e)})
350+
return get_exception_response(e)
366351

367352

368353
def set_valve(request):
@@ -376,13 +361,8 @@ def set_valve(request):
376361
Returns:
377362
:class:`.JsonResponse`: A JSON response with the set status.
378363
"""
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')
364+
# Check if the AJAX request is valid.
365+
check_error(request)
386366

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

398378

399379
def set_tank_valve(request):
@@ -407,13 +387,8 @@ def set_tank_valve(request):
407387
Returns:
408388
:class:`.JsonResponse`: A JSON response with the set status.
409389
"""
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')
390+
# Check if the AJAX request is valid.
391+
check_error(request)
417392

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

428403

429404
def refill_tank(request):
@@ -437,13 +412,8 @@ def refill_tank(request):
437412
Returns:
438413
:class:`.JsonResponse`: A JSON response with the set status.
439414
"""
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')
415+
# Check if the AJAX request is valid.
416+
check_error(request)
447417

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

457427

458428
def get_request_data(request):
@@ -605,13 +575,8 @@ def check_farm_connection_status(request):
605575
Returns:
606576
A JSON with the status of the farm or the error.
607577
"""
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')
578+
# Check if the AJAX request is valid.
579+
check_error(request)
615580

616581
# Get the controller ID and irrigation station from the POST request.
617582
data = json.loads(request.body.decode(request.encoding))
@@ -651,3 +616,9 @@ def request_has_params(request):
651616
`True` if the request has the required parameters, `False` otherwise.
652617
"""
653618
return request_has_id(request) and request_has_name(request)
619+
620+
621+
def check_error(request):
622+
error = check_ajax_request(request)
623+
if error is not None:
624+
return error

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)