generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
04-visits-mixins.txt
100 lines (77 loc) · 3.15 KB
/
04-visits-mixins.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Check results
=============
Code
=============
import re
import requests
from django.conf import settings
def Directions(*args, **kwargs):
"""
Posts coordinates to google API and gets json response - directions
created by following tutorial:
https://www.youtube.com/watch?v=wCn8WND-JpU&t=8s
"""
lat_a = kwargs.get("lat_a")
long_a = kwargs.get("long_a")
lat_b = kwargs.get("lat_b")
long_b = kwargs.get("long_b")
origin = f'{lat_a},{long_a}'
destination = f'{lat_b},{long_b}'
result = requests.get(
'https://maps.googleapis.com/maps/api/directions/json?',
params={
'origin': origin,
'destination': destination,
"key": settings.GOOGLE_API_KEY
})
directions = result.json()
if directions["status"] == "OK":
route = directions["routes"][0]["legs"][0]
# this is a string with full address, postcode can be extracted
origin = route["start_address"]
destination = route["end_address"]
# change km to miles and round up to 1 decimal place
distance = route["distance"]["value"]*0.000621371
distance = round(distance, 1)
# duration is displayed as a string, not number
duration = route["duration"]["text"]
return {
"origin": origin,
"destination": destination,
"distance": distance,
"duration": duration,
}
def extract_postcode(googe_places_full_addr, google_directions_full_addr):
'''
takes a string containing full adress and postcodes and returns postcode
only checks if postscode can be found in one of the possible matches,
if no postcode found it returns google_places full address
Regex from:
https://stackoverflow.com/questions/164979/regex-for-matching-uk-postcodes
'''
regex = (r'([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-'
r'hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-'
r'Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})')
matches_places = re.findall(regex, googe_places_full_addr)
matches_directions = re.findall(regex, google_directions_full_addr)
if len(matches_directions) == 0 and len(matches_places) == 0:
postcode = googe_places_full_addr
elif len(matches_places) == 0:
list_of_matches_directions = matches_directions[0]
postcode = list_of_matches_directions[1]
else:
list_of_matches_places = matches_places[0]
postcode = list_of_matches_places[1]
return postcode
def sum_all_miles(date, model, driver):
'''
takes the date of the report, filters objects in the database that match
the date and finds the distance value and adds to the list. Than summarises
the content of the list.
'''
list_of_items = model.objects.filter(
date_of_journey=date).filter(driver=driver)
sum_of_distances = 0
for item in list_of_items:
sum_of_distances = sum_of_distances + item.distance
return sum_of_distances