Skip to content

Commit 842d551

Browse files
author
Bobur Yusupov
committed
Price Function added
1 parent 3624271 commit 842d551

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ django-allauth==0.54.0
1111
django-cors-headers==4.1.0
1212
djangorestframework==3.14.0
1313
drf-spectacular==0.26.3
14+
geographiclib==2.0
15+
geopy==2.3.0
1416
idna==3.4
1517
inflection==0.5.1
1618
jsonschema==4.17.3
@@ -26,6 +28,7 @@ PyYAML==6.0
2628
requests==2.31.0
2729
requests-oauthlib==1.3.1
2830
sqlparse==0.4.4
31+
typing_extensions==4.7.0
2932
tzdata==2023.3
3033
uritemplate==4.1.1
3134
urllib3==2.0.3

taxi/functions/__init__.py

Whitespace-only changes.

taxi/functions/measure_length.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from geopy import (
2+
geocoders,
3+
distance,
4+
)
5+
6+
geolocator = geocoders.Nominatim(user_agent="Taxi")
7+
8+
class Distance:
9+
"""Returns distance between to cities."""
10+
def __init__(self, first_city, second_city):
11+
self.first_city = first_city
12+
self.second_city = second_city
13+
14+
def city_coordinates(self, city_name):
15+
location = geolocator.geocode(city_name)
16+
coordinates = (location.latitude, location.longitude)
17+
18+
return coordinates
19+
20+
def distance(self, first_city, second_city):
21+
first_city = self.city_coordinates(first_city)
22+
second_city = self.city_coordinates(second_city)
23+
24+
distance_between_cities = distance.distance(first_city, second_city).kilometers
25+
26+
return distance_between_cities
27+
28+
def price(self):
29+
distance1 = self.distance(self.first_city, self.second_city)
30+
31+
oil_price = 4
32+
price = distance1 / 10 * oil_price + 100 + 100
33+
34+
return int(price)

taxi/models/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from django.db import models
77
from django.conf import settings
88

9+
from taxi.functions.measure_length import Distance
10+
911
class OrderTaxi(models.Model):
1012
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
1113
user = models.ForeignKey(
@@ -15,5 +17,10 @@ class OrderTaxi(models.Model):
1517
from_location = models.CharField(max_length=254)
1618
to_location = models.CharField(max_length=254)
1719

20+
@property
21+
def price(self):
22+
price = Distance(self.from_location, self.to_location)
23+
return price.price
24+
1825
def __str__(self):
19-
return f"{self.from_location}-{self.to_location}"
26+
return f"{self.from_location}-{self.to_location}"

taxi/serializers/taxi_order_serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class OrderTaxiSerializer(ModelSerializer):
1010
"""OrderTaxi Serializer."""
1111
class Meta:
1212
model = OrderTaxi
13-
fields = ['id', 'from_location', 'to_location']
13+
fields = ['id', 'from_location', 'to_location', 'price']
1414
read_only_fields = ['id']
1515

1616
def create_taxi_order(self, validated_data):

0 commit comments

Comments
 (0)