Skip to content

Commit

Permalink
Provide speaker and session API
Browse files Browse the repository at this point in the history
addresses devdaydresden#219, devdaydresden#220 and devdaydresden#221 but needs more customizing
  • Loading branch information
jandd committed Mar 11, 2020
1 parent b7d34cb commit b769f95
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
11 changes: 4 additions & 7 deletions devday/devday/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions devday/devday/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

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

admin.autodiscover()

router = routers.DefaultRouter()
router.register(r"sessions", SessionViewSet)
router.register(r"speakers", PublishedSpeakerViewSet, basename="speaker")

urlpatterns = [
url(r"^api/", include(router.urls)),
Expand Down
40 changes: 40 additions & 0 deletions devday/speaker/api_views.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 19 additions & 7 deletions devday/talk/api_views.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b769f95

Please sign in to comment.