diff --git a/devday/devday/settings/base.py b/devday/devday/settings/base.py index acea5dd0..b6995038 100644 --- a/devday/devday/settings/base.py +++ b/devday/devday/settings/base.py @@ -15,11 +15,11 @@ import mimetypes import os +import requests + from django.core.exceptions import ImproperlyConfigured from django.utils.translation import ugettext_lazy as _ -import requests - def gettext(s): return s @@ -249,11 +249,8 @@ def get_variable_cascade(var_name, type=str, default_value=None): "DEFAULT_AUTHENTICATION_CLASSES": [ "rest_framework.authentication.BasicAuthentication", "rest_framework.authentication.SessionAuthentication", - ] -} - -REST_FRAMEWORK = { - "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"] + ], + "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"], } ROOT_URLCONF = "devday.urls" diff --git a/devday/devday/urls.py b/devday/devday/urls.py index 76d5a31e..958f021c 100644 --- a/devday/devday/urls.py +++ b/devday/devday/urls.py @@ -9,6 +9,7 @@ from devday.views import SendEmailView, exception_test_view from rest_framework import routers +from speaker.api_views import PublishedSpeakerViewSet from talk.api_views import SessionViewSet from twitterfeed.views import TwitterwallView @@ -16,6 +17,7 @@ router = routers.DefaultRouter() router.register(r"sessions", SessionViewSet) +router.register(r"speakers", PublishedSpeakerViewSet, basename="speaker") urlpatterns = [ url(r"^api/", include(router.urls)), diff --git a/devday/speaker/api_views.py b/devday/speaker/api_views.py new file mode 100644 index 00000000..62ddc21a --- /dev/null +++ b/devday/speaker/api_views.py @@ -0,0 +1,40 @@ +from rest_framework import serializers, viewsets +from rest_framework.relations import HyperlinkedRelatedField, SlugRelatedField +from speaker.models import PublishedSpeaker + + +class PublishedSpeakerSerializer(serializers.ModelSerializer): + event = SlugRelatedField(slug_field="slug", read_only=True) + talk_set = HyperlinkedRelatedField( + many=True, view_name="talk-detail", read_only=True, lookup_field="slug" + ) + + class Meta: + model = PublishedSpeaker + fields = [ + "url", + "name", + "twitter_handle", + "short_biography", + "slug", + "event", + "portrait", + "thumbnail", + "talk_set", + ] + extra_kwargs = {"url": {"view_name": "speaker-detail", "lookup_field": "slug"}} + + +class PublishedSpeakerViewSet(viewsets.ReadOnlyModelViewSet): + serializer_class = PublishedSpeakerSerializer + lookup_field = "slug" + + def get_queryset(self): + qs = PublishedSpeaker.objects.order_by("name") + event = self.request.query_params.get("event", None) + speaker = self.request.query_params.get("speaker", None) + if event: + qs = qs.filter(event__slug=event) + if speaker: + qs = qs.filter(slug=speaker) + return qs diff --git a/devday/talk/api_views.py b/devday/talk/api_views.py index d7a49c67..060ea2c5 100644 --- a/devday/talk/api_views.py +++ b/devday/talk/api_views.py @@ -1,18 +1,30 @@ -from rest_framework import serializers, viewsets -from rest_framework.relations import StringRelatedField - +from rest_framework import filters, serializers, viewsets +from rest_framework.relations import HyperlinkedRelatedField, SlugRelatedField from talk.models import Talk class SessionSerializer(serializers.ModelSerializer): - event = StringRelatedField() - published_speaker = StringRelatedField() + event = SlugRelatedField(slug_field="slug", read_only=True) + published_speaker = HyperlinkedRelatedField( + read_only=True, view_name="speaker-detail", lookup_field="slug" + ) class Meta: model = Talk - fields = ['url', 'title', 'abstract', 'published_speaker', 'event'] + fields = ["url", "title", "abstract", "published_speaker", "event"] + extra_kwargs = {"url": {"lookup_field": "slug"}} class SessionViewSet(viewsets.ReadOnlyModelViewSet): - queryset = Talk.objects.filter(published_speaker__isnull=False) + queryset = Talk.objects.filter(published_speaker__isnull=False).order_by( + "-event__start_time", "title", "published_speaker__name" + ) serializer_class = SessionSerializer + lookup_field = "slug" + + def get_queryset(self): + qs = super().get_queryset() + event_slug = self.request.query_params.get("event", None) + if event_slug: + qs = qs.filter(event__slug=event_slug) + return qs