-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement serializers, pagination, and views for stations; add …
…search functionality and enhance nearest station retrieval
- Loading branch information
1 parent
c6f7c2b
commit a25c5b8
Showing
8 changed files
with
238 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# apps/stations/pagination.py | ||
|
||
from rest_framework.pagination import PageNumberPagination | ||
from rest_framework.response import Response | ||
|
||
|
||
class StandardResultsSetPagination(PageNumberPagination): | ||
page_size = 10 | ||
page_size_query_param = 'page_size' | ||
max_page_size = 100 | ||
|
||
def get_paginated_response(self, data): | ||
try: | ||
return Response({ | ||
'count': self.page.paginator.count, | ||
'total_pages': self.page.paginator.num_pages, | ||
'current_page': self.page.number, | ||
'results': data | ||
}) | ||
except Exception as e: | ||
return Response({"error": str(e)}, status=500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from rest_framework import serializers | ||
from .models import Station, Line | ||
|
||
class LineSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Line | ||
fields = ['id', 'name'] | ||
|
||
class StationSerializer(serializers.ModelSerializer): | ||
lines = LineSerializer(many=True) # Serialize associated lines | ||
|
||
class Meta: | ||
model = Station | ||
fields = ['id', 'name', 'lines'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# apps/stations/urls.py | ||
|
||
from django.urls import path | ||
from .views import TripDetailsView, NearestStationView | ||
from .views import TripDetailsView, NearestStationView, StationListView | ||
|
||
urlpatterns = [ | ||
path('stations-list/', StationListView.as_view(), name='stations-list'), | ||
path("trip-details/<int:start_station_id>/<int:end_station_id>/", TripDetailsView.as_view(), name="trip-details"), | ||
path('nearest-station/', NearestStationView.as_view(), name='nearest-station'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
from geopy.distance import geodesic # type: ignore | ||
# apps/stations/utils/location_helpers.py | ||
|
||
from geopy.distance import geodesic # type: ignore | ||
from apps.stations.models import Station | ||
|
||
|
||
def find_nearest_station(latitude, longitude): | ||
"""Find the nearest station to the user's location.""" | ||
user_location = (latitude, longitude) | ||
stations = Station.objects.all() | ||
stations = Station.objects.exclude(latitude__isnull=True, longitude__isnull=True) | ||
|
||
if not stations.exists(): | ||
return None, None | ||
|
||
nearest_station = min( | ||
stations, | ||
key=lambda station: geodesic(user_location, (station.latitude, station.longitude)).meters, | ||
) | ||
|
||
distance = geodesic(user_location, (nearest_station.latitude, nearest_station.longitude)).meters | ||
return nearest_station, distance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.