Split the API functions out into their own app

This commit is contained in:
Anna Sidwell 2019-08-19 23:14:57 +02:00
parent fc7eccb06c
commit b7faf8a4a4
4 changed files with 92 additions and 84 deletions

0
apps/api/__init__.py Normal file
View File

18
apps/api/urls.py Normal file
View File

@ -0,0 +1,18 @@
from django.conf.urls import include, url
from django.urls import reverse
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from rest_framework_gis import serializers as gis_serializers
from apps.files.models import File
from apps.map.models import CaseStudy, PointOfInterest
from . import views
apirouter = routers.DefaultRouter()
apirouter.register(r'case-studies', views.CaseStudyViewSet)
apirouter.register(r'points-of-interest', views.PointOfInterestViewSet)
urlpatterns = apirouter.urls

73
apps/api/views.py Normal file
View File

@ -0,0 +1,73 @@
from django.conf.urls import include, url
from django.urls import reverse
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from rest_framework_gis import serializers as gis_serializers
from apps.files.models import File
from apps.map.models import CaseStudy, PointOfInterest
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class FileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = File
fields = ('file',)
class CaseStudySerializer(gis_serializers.GeoFeatureModelSerializer):
sector_of_economy = serializers.CharField(source='get_sector_of_economy_display')
country_name = serializers.CharField(source='get_country_display')
positive_or_negative_display = serializers.CharField(source='get_positive_or_negative_display')
images = FileSerializer(many=True)
class Meta:
model = CaseStudy
geo_field = "location"
fields = (
'country',
'country_name',
'entry_name',
'images',
'location',
'positive_or_negative',
'positive_or_negative_display',
'sector_of_economy',
'slug'
)
class CaseStudyViewSet(viewsets.ModelViewSet):
queryset = CaseStudy.objects.approved()
serializer_class = CaseStudySerializer
class PointOfInterestSerializer(gis_serializers.GeoFeatureModelSerializer):
class Meta:
model = PointOfInterest
geo_field = "location"
fields = (
'title',
'synopsis',
'link',
'slug'
)
class PointOfInterestViewSet(viewsets.ModelViewSet):
queryset = PointOfInterest.objects.approved()
serializer_class = PointOfInterestSerializer

View File

@ -1,101 +1,18 @@
"""ojusomap URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.urls import reverse
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from rest_framework_gis import serializers as gis_serializers
from apps.files.models import File
from apps.map.models import CaseStudy, PointOfInterest
from .views import LanguageDropdownView
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class FileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = File
fields = ('file',)
class CaseStudySerializer(gis_serializers.GeoFeatureModelSerializer):
sector_of_economy = serializers.CharField(source='get_sector_of_economy_display')
country_name = serializers.CharField(source='get_country_display')
positive_or_negative_display = serializers.CharField(source='get_positive_or_negative_display')
images = FileSerializer(many=True)
class Meta:
model = CaseStudy
geo_field = "location"
fields = (
'country',
'country_name',
'entry_name',
'images',
'location',
'positive_or_negative',
'positive_or_negative_display',
'sector_of_economy',
'slug'
)
class CaseStudyViewSet(viewsets.ModelViewSet):
queryset = CaseStudy.objects.approved()
serializer_class = CaseStudySerializer
class PointOfInterestSerializer(gis_serializers.GeoFeatureModelSerializer):
class Meta:
model = PointOfInterest
geo_field = "location"
fields = (
'title',
'synopsis',
'link',
'slug'
)
class PointOfInterestViewSet(viewsets.ModelViewSet):
queryset = PointOfInterest.objects.approved()
serializer_class = PointOfInterestSerializer
apirouter = routers.DefaultRouter()
#apirouter.register(r'users', UserViewSet)
apirouter.register(r'case-studies', CaseStudyViewSet)
apirouter.register(r'points-of-interest', PointOfInterestViewSet)
urlpatterns = [
url(r'api/', include(apirouter.urls)),
url(r'^api/', include("apps.api.urls")),
url(r'^admin/', admin.site.urls),
url(r'^avatar/', include('avatar.urls')),
url(r'^cas/', include('cas_server.urls', namespace='cas_server')),