Skip to content

Commit

Permalink
Merge pull request #273 from jenslauterbach/api-speakers
Browse files Browse the repository at this point in the history
Add Initial Speakers Endpoint
  • Loading branch information
jandd authored Nov 22, 2021
2 parents e65758a + c069110 commit 7e38e8e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions devday/devday/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

from devday.views import SendEmailView, exception_test_view
from rest_framework import routers

from speaker.api_views import SpeakerViewSet
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", SpeakerViewSet)

urlpatterns = [
url(r"^api/", include(router.urls)),
Expand Down
31 changes: 31 additions & 0 deletions devday/speaker/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from rest_framework import serializers, viewsets

from speaker.models import Speaker


class SpeakerSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.CharField(source="slug")
image = serializers.ImageField(source="public_image", use_url=True, allow_null=False, allow_empty_file=False)

class Meta:
model = Speaker
fields = ["id", "url", "name", "short_biography", "position", "image"]
lookup_field = "slug"
extra_kwargs = {
"url": {'lookup_field': 'slug'}
}

def to_representation(self, instance):
representation = super().to_representation(instance)

# TODO Jens: This probably can be solved more elegantly with a mixin.
if not representation["image"]:
representation["image"] = ""

return representation


class SpeakerViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Speaker.objects.all()
serializer_class = SpeakerSerializer
lookup_field = "slug"
15 changes: 12 additions & 3 deletions devday/talk/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
from talk.models import Talk


class SessionSerializer(serializers.ModelSerializer):
class SessionSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.CharField(source="slug")
description = serializers.CharField(source="abstract")

# TODO Jens: Replace 'event' and 'speakers' with proper nested relationships once their endpoints are done.
event = StringRelatedField()
published_speaker = StringRelatedField()
published_speakers = StringRelatedField(many=True)

class Meta:
model = Talk
fields = ["url", "title", "abstract", "published_speakers", "event"]
fields = ["id", "url", "title", "description", "published_speakers", "event"]
lookup_field = "slug"
extra_kwargs = {
"url": {'lookup_field': 'slug'}
}


class SessionViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Talk.objects.filter(published_speakers__isnull=False)
serializer_class = SessionSerializer
lookup_field = 'slug'

0 comments on commit 7e38e8e

Please sign in to comment.