Skip to content

Commit 2aecd3c

Browse files
Merge pull request #42 from metallicgloss/validation-improvement
Validation improvements.
2 parents c9aa812 + b903ba8 commit 2aecd3c

File tree

5 files changed

+77
-19
lines changed

5 files changed

+77
-19
lines changed

underground_route_planner/models/route_planner.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ def check_route_speed_factor_should_apply(current_time_in_minutes: int, train_li
8181
remaining_stations = self._station_handler.get_all_station_names()
8282

8383
# Move the starting station to the front of the list
84-
remaining_stations.remove(starting_station_name)
84+
try:
85+
# If error occurs when attempting to remove the starting station, invalid station name passed
86+
# Likely manual request or bypassing attempt of frontend validation.
87+
remaining_stations.remove(starting_station_name)
88+
except:
89+
return {
90+
"response": "Invalid Start Station"
91+
}
92+
8593
remaining_stations.insert(0, starting_station_name)
8694

8795
# Set starting point time to 0
@@ -187,7 +195,14 @@ def check_route_speed_factor_should_apply(current_time_in_minutes: int, train_li
187195
# Set end time for route calculation
188196
calculation_end_time = time.time()
189197

190-
return self._generate_route_structure(starting_station_name, destination_station_name, calculation_end_time - calculation_start_time)
198+
try:
199+
# If error occurs when generating route structure, most common problem caused by invalid destination station name.
200+
# Likely manual request or bypassing attempt of frontend validation.
201+
return self._generate_route_structure(starting_station_name, destination_station_name, calculation_end_time - calculation_start_time)
202+
except:
203+
return {
204+
"response": "Invalid Route Request"
205+
}
191206

192207
# ----------------------------------------------------------------------- #
193208
# 1.4 Generate Route Structure #

underground_route_planner/static/css/styles.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ footer {
209209
background: var(--secondary-blue);
210210
}
211211

212+
.homepage-selection .selection-box .search-train-graphic-box {
213+
overflow: hidden;
214+
margin-top: -25px;
215+
padding-top: 28px;
216+
}
217+
212218
/*
213219
-------------------------------------------------
214220
------------- Typeahead Styles -------------

underground_route_planner/static/js/route-planner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ $('#selection-submit-button').click(function () {
132132
// Create styled map with center in the middle of London
133133
const map = new google.maps.Map(document.getElementById("map-object"), {
134134
center: {
135-
lat: 51.5074,
136-
lng: -0.1278
135+
lat: response['raw_data']['route_locations'][0]['latitude'],
136+
lng: response['raw_data']['route_locations'][0]['longitude']
137137
},
138138
zoom: 12,
139139
disableDefaultUI: true,

underground_route_planner/templates/licences.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ <h2 class="subpage-block-subheading">Licences of Open Source software used for t
1313

1414
<div class="container">
1515
<div class="license-list">
16+
<div class="row">
17+
<div class="col-lg-12">
18+
<h2>Django</h2>
19+
Version: 1.0<br>
20+
URL: https://github.com/django/django/<br>
21+
Copyright: &copy; 2013-2020 Django Software Foundation and other contributors. (https://github.com/django/django/graphs/contributors)</p>
22+
</div>
23+
</div>
1624
<div class="row">
1725
<div class="col-lg-12">
1826
<h2>Bootstrap</h2>

underground_route_planner/views.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from django.shortcuts import render
23
from django.http import JsonResponse
34
from django.apps import apps
@@ -36,13 +37,33 @@ def station_search(request):
3637

3738

3839
def route_search(request):
40+
if any(key not in request.GET for key in ("origin_location", "destination_location", "start_time")):
41+
# Return invalid message
42+
return JsonResponse(
43+
{
44+
'response': 'Missing Required Parameter.'
45+
},
46+
safe=True
47+
)
48+
3949
for value in request.GET.items():
50+
# If time input, check formatting.
51+
if(value[0] == "start_time"):
52+
if(not re.search("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", value[1])):
53+
# Return invalid message
54+
return JsonResponse(
55+
{
56+
'response': 'Invalid Time Format'
57+
},
58+
safe=True
59+
)
60+
4061
# Simple check to ensure parameter is string and within length limits.
41-
if((not isinstance(value[0], str)) and (3 < len(value[0]) < 25)):
62+
if((not isinstance(value[1], str)) or (3 > len(value[1])) or (25 < len(value[1])) or (value[1] is None) or (value[1] == "")):
4263
# Return invalid message
4364
return JsonResponse(
4465
{
45-
'response': 'invalid'
66+
'response': 'Invalid or Missing Data'
4667
},
4768
safe=True
4869
)
@@ -57,16 +78,24 @@ def route_search(request):
5778
(int(start_time[0]) * 60) + int(start_time[1])
5879
)
5980

60-
# Generate html formatted data.
61-
html_data = HTMLFormatter(route_data).format_route()
62-
63-
# Return formatted data to the frontend.
64-
return JsonResponse(
65-
{
66-
'raw_data': route_data,
67-
'route_table': html_data['route_table'],
68-
'route_summary': html_data['route_summary'],
69-
'route_travel_time': html_data['route_travel_time']
70-
},
71-
safe=True
72-
)
81+
if('response' not in route_data):
82+
# Generate html formatted data.
83+
html_data = HTMLFormatter(route_data).format_route()
84+
85+
# Return formatted data to the frontend.
86+
return JsonResponse(
87+
{
88+
'raw_data': route_data,
89+
'route_table': html_data['route_table'],
90+
'route_summary': html_data['route_summary'],
91+
'route_travel_time': html_data['route_travel_time']
92+
},
93+
safe=True
94+
)
95+
96+
else:
97+
# Return formatted data to the frontend.
98+
return JsonResponse(
99+
route_data,
100+
safe=True
101+
)

0 commit comments

Comments
 (0)