Skip to content

Commit

Permalink
Merge pull request #18 from awaisdar001/get-distinc-records
Browse files Browse the repository at this point in the history
Get Distinct records from the API
  • Loading branch information
awaisdar001 committed Oct 10, 2020
2 parents 435d4ab + 8b1f666 commit 36b5ff5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-trips
version = 0.2.7.4
version = 0.2.7.5
description = A Django Rest API for fetching and creating trips and their schedules.
long_description = file: README.md
url = https://github.com/awaisdar001/django-trips
Expand Down
51 changes: 29 additions & 22 deletions trips/django_trips/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import django_filters
from django_trips.models import Trip
import django_filters as filters
from django_trips.models import Trip, Location
from django.db.models import Q


class TripFilter(django_filters.FilterSet):
name = django_filters.CharFilter(field_name='name', lookup_expr=('icontains'))
duration = django_filters.NumberFilter(field_name='duration', lookup_expr=('gte'))
price = django_filters.NumberFilter(field_name='trip_schedule__price', method='filter_price')
destination = django_filters.CharFilter(field_name='destination', method='filter_price')
class TripFilter(filters.FilterSet):
name = filters.CharFilter(field_name='name', lookup_expr=('icontains'))
destination = filters.BaseInFilter(field_name="destination__slug")
duration_from = filters.NumberFilter(field_name='duration', lookup_expr=('gte'))
duration_to = filters.NumberFilter(field_name='duration', lookup_expr=('lte'))
price_from = filters.NumberFilter(
label="Price From",
field_name='trip_schedule__price',
method='filter_from'
)
price_to = filters.NumberFilter(
label='Price To',
field_name='trip_schedule__price',
method='filter_to'
)

from_date = django_filters.DateFilter(
label="Trip From Date",
date_from = filters.DateFilter(
label="Trip Date From",
field_name='trip_schedule__date_from',
method='filter_from_date',
method='filter_from',
)
to_date = django_filters.DateFilter(
label="Trip To Date",
date_to = filters.DateFilter(
label="Trip Date To",
field_name='trip_schedule__date_from',
method='filter_to_date',
method='filter_to',
)

def filter_from_date(self, queryset, name, value):
def filter_from(self, queryset, name, value):
lookup = '__'.join([name, 'gte'])
return queryset.filter(**{lookup: value})
return queryset.filter(**{lookup: value}).distinct()

def filter_to_date(self, queryset, name, value):
def filter_to(self, queryset, name, value):
lookup = '__'.join([name, 'lte'])
return queryset.filter(**{lookup: value})

def filter_price(self, queryset, name, value):
lookup = '__'.join([name, 'gte'])
return queryset.filter(**{lookup: value})
return queryset.filter(**{lookup: value}).distinct()

class Meta:
model = Trip
fields = ('name', 'price')
fields = ('destination',)
34 changes: 19 additions & 15 deletions trips/django_trips/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,35 @@ class TripListCreateAPIView(generics.ListCreateAPIView):
Examples:
POST: Creates a new trip
/api/trips/
data = {
"name":"3 days trip to Islamabad",
"description": "This is the description for trip: 66",
"locations": [1,2],
"facilities": [1,2],
"destination": 1,
"created_by": 1,
"host": 1,
}
GET: Return all trips (paginated, 10per page.)
/api/trips/
GET: Search Trips that contains specific name.
/api/trips/?name=Islamabad
Other Options.
| Keyword | |
| name | Find trips that contains specific name. |
| price | Find trips that contains price greater than or equal |
| duration | Find trips of duration greater than or equal |
| from_date | Find trips that are scheduled greater than or specified date |
| to_date | Find trips that are scheduled less than or equal to specified date |
| Keyword | |
| name "" | Find trips that contains specific name. |
| | name=Trip to lahore OR name=chitral |
| destination[] | Filter trips with specific destinations. |
| | e.g. destination=islamabad,lahore |
| price_from (str) | Find trips that has price greater than or equal to the given amount |
| price_to (str) | Find trips that has price less than or equal to the given amount |
| duration_from (int) | Find trips having duration greater than or equal to the given number |
| duration_to (int) | Find trips having duration less than or equal to the given number |
| date_from (date) | Find trips that are scheduled greater than or specified date |
| date_to (date) | Find trips that are scheduled less than or equal to specified date |
Examples:
/api/trips/?
destination=islamabad%2Clahore%2Cfairy+meadows&name=trip
&duration_from=1&duration_to=15
&price_from=500&price_to=8000
&date_from=2020-10-21&date_to=2020-11-11
"""

authentication_classes = [SessionAuthentication, BasicAuthentication]
pagination_class = TripResponsePagination
permission_classes = [IsAuthenticated]
filter_backends = [DjangoFilterBackend]
filter_class = TripFilter
Expand Down

0 comments on commit 36b5ff5

Please sign in to comment.