diff --git a/bikeshop_project/bike/serializers.py b/bikeshop_project/bike/serializers.py new file mode 100644 index 0000000..c5fa9fd --- /dev/null +++ b/bikeshop_project/bike/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers + +from bike.models import Bike + + +class BikeSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Bike diff --git a/bikeshop_project/bike/urls.py b/bikeshop_project/bike/urls.py index db058ba..f9f6250 100644 --- a/bikeshop_project/bike/urls.py +++ b/bikeshop_project/bike/urls.py @@ -1,7 +1,13 @@ -from django.conf.urls import url +from django.conf.urls import url, include +from rest_framework import routers +from bike import views from .views import BikesView +router = routers.DefaultRouter() +router.register(r'bikes', views.BikeViewSet) + urlpatterns = [ + url(r'^api/', include(router.urls, namespace='api')), url(r'^$', BikesView.as_view(), name='bikes') ] diff --git a/bikeshop_project/bike/views.py b/bikeshop_project/bike/views.py index 4d978f4..6522c67 100644 --- a/bikeshop_project/bike/views.py +++ b/bikeshop_project/bike/views.py @@ -1,6 +1,10 @@ from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.views.generic import TemplateView +from rest_framework import viewsets + +from bike.models import Bike +from bike.serializers import BikeSerializer @method_decorator(login_required, name='dispatch') @@ -9,3 +13,8 @@ class BikesView(TemplateView): def get(self, request): return self.render_to_response({}) + + +class BikeViewSet(viewsets.ModelViewSet): + queryset = Bike.objects.all() + serializer_class = BikeSerializer diff --git a/bikeshop_project/bikeshop/settings/base.py b/bikeshop_project/bikeshop/settings/base.py index 3f2d2b1..186a6a6 100644 --- a/bikeshop_project/bikeshop/settings/base.py +++ b/bikeshop_project/bikeshop/settings/base.py @@ -161,3 +161,11 @@ HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' LOGIN_REDIRECT_URL = 'home' LOGIN_URL = 'login' + +REST_FRAMEWORK = { + # Use Django's standard `django.contrib.auth` permissions, + # or allow read-only access for unauthenticated users. + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ] +}