Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
John James
whats-open
Commits
74779e8b
Verified
Commit
74779e8b
authored
Jul 08, 2017
by
David Haynes
🙆
Browse files
POC on filtering plugins
- plus field rename - plus docs! - plus another package
parent
88f9728d
Changes
6
Hide whitespace changes
Inline
Side-by-side
requirements/base.txt
View file @
74779e8b
...
...
@@ -8,4 +8,5 @@ setuptools==36.0.1
django-taggit==0.22.1
django-taggit-serializer==0.1.5
six==1.10.0
djangorestframework-gis==0.11.2
\ No newline at end of file
djangorestframework-gis==0.11.2
django-filter==1.0.4
whats-open/api/admin.py
View file @
74779e8b
...
...
@@ -27,7 +27,7 @@ class FacilityAdmin(admin.ModelAdmin):
# We are basically reordering things to look nicer to the user here
fieldsets
=
(
(
None
,
{
'fields'
:
(
'name'
,
'facility_category'
,
'facility_location'
,
'fields'
:
(
'
facility_
name'
,
'facility_category'
,
'facility_location'
,
'main_schedule'
,
'special_schedules'
,
'facility_product_tags'
,
'tapingo_url'
,
'owners'
),
}),
...
...
whats-open/api/models.py
View file @
74779e8b
...
...
@@ -81,7 +81,7 @@ class Facility(TimeStampedModel):
a specific purpose that can be categorized.
"""
# The name of the Facility
name
=
models
.
CharField
(
max_length
=
100
)
facility_
name
=
models
.
CharField
(
max_length
=
100
)
# Instead of id
slug
=
AutoSlugField
(
populate_from
=
'name'
,
unique
=
True
)
...
...
@@ -162,13 +162,13 @@ class Facility(TimeStampedModel):
verbose_name
=
"facility"
verbose_name_plural
=
"facilities"
# Sort by name in admin view
ordering
=
[
'name'
]
ordering
=
[
'
facility_
name'
]
def
__str__
(
self
):
"""
String representation of a Facility object.
"""
return
self
.
name
return
self
.
facility_
name
class
Schedule
(
TimeStampedModel
):
"""
...
...
whats-open/api/serializers.py
View file @
74779e8b
...
...
@@ -95,4 +95,4 @@ class FacilitySerializer(serializers.HyperlinkedModelSerializer):
# List the fields that we are serializing
fields
=
(
'id'
,
'facility_category'
,
'facility_location'
,
'facility_product_tags'
,
'tapingo_url'
,
'main_schedule'
,
'special_schedules'
,
'modified'
,
'name'
)
'special_schedules'
,
'modified'
,
'
facility_
name'
)
whats-open/api/views.py
View file @
74779e8b
...
...
@@ -17,7 +17,8 @@ from .serializers import (CategorySerializer, FacilitySerializer,
LocationSerializer
,
AlertSerializer
)
# Other Imports
from
rest_framework
import
viewsets
from
rest_framework
import
viewsets
,
filters
from
django_filters.rest_framework
import
DjangoFilterBackend
class
AlertViewSet
(
viewsets
.
ReadOnlyModelViewSet
):
"""
...
...
@@ -65,19 +66,45 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
GET /api/facilities/
Return all Facility objects. We additionally filter out stale special_schedules to reduce client side calculations.
Built-in query parameters:
GET /api/facilities/?search=
Query parameter that returns objects that match a keyword provided in the search.
GET /api/facilities/?ordering=
Query parameter that orders the returned objects based on the provided field to order by.
Additionally, you can query against any field you like:
ie.
GET /api/facilities/?facility_name=Southside
will return the Facility object that has "Southside" as its name.
Custom query parameters:
GET /api/facilities/?open_now
Query parameter that only returns open Facility objects.
GET /api/facilities/?closed_now
Query parameter that only returns closed Facility objects.
"""
# Associate a serializer with the ViewSet
serializer_class
=
FacilitySerializer
filter_backends
=
(
DjangoFilterBackend
,
filters
.
SearchFilter
,
filters
.
OrderingFilter
,
)
search_fields
=
(
'facility_name'
,)
ordering_fields
=
(
'facility_name'
,)
filter_fields
=
(
'facility_name'
,
'facility_product_tags__name'
)
def
get_queryset
(
self
):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
# Define
and handle
?open_now
# Define ?open_now
open_now
=
self
.
request
.
query_params
.
get
(
'open_now'
,
None
)
if
open_now
is
not
None
:
# Define ?closed_now
closed_now
=
self
.
request
.
query_params
.
get
(
'closed_now'
,
None
)
if
open_now
is
not
None
or
closed_now
is
not
None
:
# List of all open facilities
open_facilities
=
[]
for
facility
in
Facility
.
objects
.
all
():
...
...
@@ -86,7 +113,12 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
open_facilities
.
append
(
facility
.
pk
)
# Return all Facility objects with the primary keys located in the
# open_facilities list
return
Facility
.
objects
.
filter
(
pk__in
=
open_facilities
)
if
open_now
:
return
Facility
.
objects
.
filter
(
pk__in
=
open_facilities
)
# Return all Facility objects with the primary keys not located in
# the open_facilities list
elif
closed_now
:
return
Facility
.
objects
.
exclude
(
pk__in
=
open_facilities
)
# Default behavior
else
:
for
facility
in
Facility
.
objects
.
all
():
...
...
whats-open/settings/base.py
View file @
74779e8b
...
...
@@ -244,6 +244,7 @@ INSTALLED_APPS = (
'taggit_serializer'
,
'rest_framework'
,
'rest_framework_gis'
,
'django_filters'
,
)
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment