Skip to content

Commit

Permalink
Add REST framework
Browse files Browse the repository at this point in the history
Initially read only and with simple filters
  • Loading branch information
dbaio committed Aug 16, 2020
1 parent ceba4f4 commit a8372dc
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
55 changes: 55 additions & 0 deletions ports/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2020 Danilo G. Baio <dbaio@bsd.com.br>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ports.models import Category, Port, Fallout
from rest_framework import serializers


class CategorySerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="ports:category-detail",
)
class Meta:
model = Category
fields = '__all__'


class PortSerializer(serializers.HyperlinkedModelSerializer):
categories = CategorySerializer(many=True, read_only=True)
url = serializers.HyperlinkedIdentityField(
view_name="ports:port-detail",
)
class Meta:
model = Port
fields = '__all__'


class FalloutSerializer(serializers.HyperlinkedModelSerializer):
port = PortSerializer(many=False, read_only=True)
url = serializers.HyperlinkedIdentityField(
view_name="ports:fallout-detail",
)
class Meta:
model = Fallout
fields = '__all__'

3 changes: 3 additions & 0 deletions ports/templates/ports/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<li class="nav-item {{ navbar_list }}">
<a class="nav-link" href="{% url 'ports:list' %}">Ports</a>
</li>
<li class="nav-item {{ navbar_list }}">
<a class="nav-link" href="api/"">Api</a>
</li>
<li class="nav-item {{ navbar_about }}">
<a class="nav-link" href="{% url 'ports:about' %}">About</a>
</li>
Expand Down
9 changes: 8 additions & 1 deletion ports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@

"""ports URL Configuration"""

from django.urls import path
from django.urls import include, path
from ports import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'category', views.CategoryViewSet)
router.register(r'port', views.PortViewSet)
router.register(r'fallout', views.FalloutViewSet)

# TEMPLATE TAGGING
app_name = 'ports'
Expand All @@ -36,4 +42,5 @@
path('port', views.PortListView.as_view(), name='list'),
path('port/<int:pk>/', views.PortDetailView.as_view(), name='detail'),
path('about', views.about, name='about'),
path('api/', include(router.urls)),
]
32 changes: 32 additions & 0 deletions ports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from django.views.generic import View, TemplateView, ListView, DetailView
from django.db.models import Count, Q
from ports.models import Port, Category, Fallout
from ports.serializers import CategorySerializer, PortSerializer, FalloutSerializer
from rest_framework import filters, viewsets
from datetime import date, timedelta


Expand Down Expand Up @@ -137,3 +139,33 @@ def about(request):
context_dict = {'navbar_about':'active'}
return render(request, 'ports/about.html', context_dict)


class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for Categories.
"""
search_fields = ['name']
filter_backends = (filters.SearchFilter,)
queryset = Category.objects.all().order_by('name')
serializer_class = CategorySerializer


class PortViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for Port's.
"""
search_fields = ['origin', 'maintainer']
filter_backends = (filters.SearchFilter,)
queryset = Port.objects.all().order_by('origin')
serializer_class = PortSerializer


class FalloutViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for Fallout's.
"""
search_fields = ['maintainer', 'port__origin', 'env', 'category']
filter_backends = (filters.SearchFilter,)
queryset = Fallout.objects.all().order_by('-date')
serializer_class = FalloutSerializer

6 changes: 6 additions & 0 deletions portsfallout/settings_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'debug_toolbar',
'bootstrap_pagination',
'ports',
Expand Down Expand Up @@ -154,3 +155,8 @@
INTERNAL_IPS = [
'127.0.0.1',
]

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 50
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ django
django-bootstrap-pagination
requests
scrapy
djangorestframework

0 comments on commit a8372dc

Please sign in to comment.