Skip to content

Commit

Permalink
Add Thabloid API (#3686)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincevd1 authored May 21, 2024
1 parent 7796d69 commit 5346420
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions website/thabloid/api/v2/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from rest_framework import serializers

from thabloid.models.thabloid import Thabloid
from thaliawebsite.api.v2.serializers.cleaned_model_serializer import (
CleanedModelSerializer,
)
from utils.media.services import get_media_url


class ThabloidSerializer(CleanedModelSerializer):
"""API Serializer for thabloids."""

file = serializers.SerializerMethodField()
cover = serializers.SerializerMethodField()

class Meta:
"""Meta class for the serializer."""

model = Thabloid
fields = ("pk", "year", "issue", "cover", "file")

def get_cover(self, instance):
return get_media_url(instance.cover, absolute_url=True)

def get_file(self, instance):
return get_media_url(instance.file, absolute_url=True)
22 changes: 22 additions & 0 deletions website/thabloid/api/v2/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Thabloid API v2 urls."""
from django.urls import include, path

from thabloid.api.v2.views import ThabloidDetailView, ThabloidListView

app_name = "thabloid"

urlpatterns = [
path(
"thabloid/",
include(
[
path("thabloids/", ThabloidListView.as_view(), name="thabloid-list"),
path(
"thabloids/<int:pk>",
ThabloidDetailView.as_view(),
name="thabloid-detail",
),
]
),
),
]
34 changes: 34 additions & 0 deletions website/thabloid/api/v2/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope
from rest_framework import filters
from rest_framework.generics import ListAPIView, RetrieveAPIView

from thabloid.api.v2.serializers import ThabloidSerializer
from thabloid.models.thabloid import Thabloid


class ThabloidListView(ListAPIView):
"""Returns a list of all Thabloids."""

serializer_class = ThabloidSerializer

queryset = Thabloid.objects.all()

permission_classes = [
IsAuthenticatedOrTokenHasScope,
]
required_scopes = ["thabloid:read"]
filter_backends = (filters.SearchFilter,)
search_fields = ("year", "issue")


class ThabloidDetailView(RetrieveAPIView):
"""Returns details about a specific Thabloid."""

serializer_class = ThabloidSerializer

queryset = Thabloid.objects.all()

permission_classes = [
IsAuthenticatedOrTokenHasScope,
]
required_scopes = ["thabloid:read"]
1 change: 1 addition & 0 deletions website/thaliawebsite/api/v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
path("", include("pizzas.api.v2.urls")),
path("", include("pushnotifications.api.v2.urls")),
path("", include("sales.api.v2.urls")),
path("", include("thabloid.api.v2.urls")),
path(
"schema",
get_schema_view(
Expand Down
1 change: 1 addition & 0 deletions website/thaliawebsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ def show_toolbar(request):
"sales:read": "Read access to your Point of Sale orders",
"sales:order": "Place Point of Sale orders on your behalf",
"sales:admin": "Admin access to Point of Sale orders",
"thabloid:read": "Read access to thabloids",
},
}

Expand Down

0 comments on commit 5346420

Please sign in to comment.