From d235532e346a8ee315b62067df015afbb813c91b Mon Sep 17 00:00:00 2001 From: Michael Brusegard <56915010+michaelbrusegard@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:58:30 +0200 Subject: [PATCH] feat: added serializers to watchlist and news --- .gitignore | 1 + news/event_urls.py | 26 ++++++++++++------- news/serializers/article.py | 22 ++++++++++++++++ news/serializers/event.py | 22 ++++++++++++++++ news/serializers/event_registration.py | 9 +++++++ news/serializers/upload.py | 9 +++++++ news/urls.py | 18 ++++++++----- news/views/article.py | 14 ++++++++++ news/views/event.py | 14 ++++++++++ news/{views.py => views/template_views.py} | 4 +-- news/views/upload.py | 9 +++++++ watchlist/serializers/shift_slot.py | 9 +++++++ watchlist/urls.py | 17 ++++++++---- watchlist/views/shift_slot.py | 9 +++++++ .../{views.py => views/template_views.py} | 4 +-- website/urls.py | 6 +++++ 16 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 news/serializers/article.py create mode 100644 news/serializers/event.py create mode 100644 news/serializers/event_registration.py create mode 100644 news/serializers/upload.py create mode 100644 news/views/article.py create mode 100644 news/views/event.py rename news/{views.py => views/template_views.py} (99%) create mode 100644 news/views/upload.py create mode 100644 watchlist/serializers/shift_slot.py create mode 100644 watchlist/views/shift_slot.py rename watchlist/{views.py => views/template_views.py} (96%) diff --git a/.gitignore b/.gitignore index 8bd4f593d..472a28121 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ profilepictures/ # For Rope Project settings .ropeproject +.python-version env/ venv/ diff --git a/news/event_urls.py b/news/event_urls.py index 2e5aee321..b8dd418a5 100644 --- a/news/event_urls.py +++ b/news/event_urls.py @@ -1,18 +1,26 @@ from django.urls import path -from . import views from .ical import HSEventFeed, HSEventSingleFeed +from .views import template_views app_name = "events" urlpatterns = [ - path("", views.EventListView.as_view(), name="all"), - path("/", views.EventView.as_view(), name="details"), - path("/edit/", views.EventUpdateView.as_view(), name="edit"), - path("/attended/", views.EventAttendeeEditView.as_view(), name="attended"), - path("/skills/", views.EventAttendeeSkillsView.as_view(), name="skills"), - path("new", views.EventCreateView.as_view(), name="new"), - path("/delete/", views.EventDeleteView.as_view(), name="delete"), - path("/register/", views.register_on_event, name="register"), + path("", template_views.EventListView.as_view(), name="all"), + path("/", template_views.EventView.as_view(), name="details"), + path("/edit/", template_views.EventUpdateView.as_view(), name="edit"), + path( + "/attended/", + template_views.EventAttendeeEditView.as_view(), + name="attended", + ), + path( + "/skills/", + template_views.EventAttendeeSkillsView.as_view(), + name="skills", + ), + path("new", template_views.EventCreateView.as_view(), name="new"), + path("/delete/", template_views.EventDeleteView.as_view(), name="delete"), + path("/register/", template_views.register_on_event, name="register"), path("feed.ics", HSEventFeed()), path("/feed.ics", HSEventSingleFeed()), ] diff --git a/news/serializers/article.py b/news/serializers/article.py new file mode 100644 index 000000000..6c58bbb63 --- /dev/null +++ b/news/serializers/article.py @@ -0,0 +1,22 @@ +from rest_framework import serializers + +from news.models import Article + + +class ArticleListSerializer(serializers.ModelSerializer): + class Meta: + model = Article + fields = ( + "id", + "title", + "ingress_content", + "internal", + "pub_date", + "thumbnail", + ) + + +class ArticleRetrieveSerializer(ArticleListSerializer): + class Meta: + model = Article + fields = "__all__" diff --git a/news/serializers/event.py b/news/serializers/event.py new file mode 100644 index 000000000..e139da706 --- /dev/null +++ b/news/serializers/event.py @@ -0,0 +1,22 @@ +from rest_framework import serializers + +from news.models import Event + + +class EventListSerializer(serializers.ModelSerializer): + class Meta: + model = Event + fields = fields = ( + "id", + "title", + "ingress_content", + "internal", + "pub_date", + "thumbnail", + ) + + +class EventRetrieveSerializer(EventListSerializer): + class Meta: + model = Event + fields = "__all__" diff --git a/news/serializers/event_registration.py b/news/serializers/event_registration.py new file mode 100644 index 000000000..ecedbca0b --- /dev/null +++ b/news/serializers/event_registration.py @@ -0,0 +1,9 @@ +from rest_framework import serializers + +from news.models import EventRegistration + + +class EventRegistrationSerializer(serializers.ModelSerializer): + class Meta: + model = EventRegistration + fields = "__all__" diff --git a/news/serializers/upload.py b/news/serializers/upload.py new file mode 100644 index 000000000..9502b2ada --- /dev/null +++ b/news/serializers/upload.py @@ -0,0 +1,9 @@ +from rest_framework import serializers + +from news.models import Upload + + +class UploadSerializer(serializers.ModelSerializer): + class Meta: + model = Upload + fields = "__all__" diff --git a/news/urls.py b/news/urls.py index cab70f5a0..701f33c88 100644 --- a/news/urls.py +++ b/news/urls.py @@ -1,12 +1,18 @@ from django.conf.urls import url -from . import views +from .views import template_views app_name = "news" urlpatterns = [ - url(r"^$", views.ArticleListView.as_view(), name="all"), - url(r"^(?P[0-9]+)/$", views.ArticleView.as_view(), name="details"), - url(r"^(?P[0-9]+)/edit", views.ArticleUpdateView.as_view(), name="edit"), - url(r"^new", views.ArticleCreateView.as_view(), name="new"), - url(r"^(?P[0-9]+)/delete", views.ArticleDeleteView.as_view(), name="delete"), + url(r"^$", template_views.ArticleListView.as_view(), name="all"), + url(r"^(?P[0-9]+)/$", template_views.ArticleView.as_view(), name="details"), + url( + r"^(?P[0-9]+)/edit", template_views.ArticleUpdateView.as_view(), name="edit" + ), + url(r"^new", template_views.ArticleCreateView.as_view(), name="new"), + url( + r"^(?P[0-9]+)/delete", + template_views.ArticleDeleteView.as_view(), + name="delete", + ), ] diff --git a/news/views/article.py b/news/views/article.py new file mode 100644 index 000000000..9a7b8a796 --- /dev/null +++ b/news/views/article.py @@ -0,0 +1,14 @@ +from rest_framework import viewsets + +from news.models import Article +from news.serializers.article import ArticleListSerializer, ArticleRetrieveSerializer + + +class ArticleViewSet(viewsets.ModelViewSet): + queryset = Article.objects.all() + serializer_class = ArticleListSerializer + + def get_serializer_class(self): + if self.action == "list": + return ArticleListSerializer + return ArticleRetrieveSerializer diff --git a/news/views/event.py b/news/views/event.py new file mode 100644 index 000000000..4a9bb163d --- /dev/null +++ b/news/views/event.py @@ -0,0 +1,14 @@ +from rest_framework import viewsets + +from news.models import Event +from news.serializers.event import EventListSerializer, EventRetrieveSerializer + + +class EventViewSet(viewsets.ModelViewSet): + queryset = Event.objects.all() + serializer_class = EventListSerializer + + def get_serializer_class(self): + if self.action == "list": + return EventListSerializer + return EventRetrieveSerializer diff --git a/news/views.py b/news/views/template_views.py similarity index 99% rename from news/views.py rename to news/views/template_views.py index 9e1115a2b..3b51a5d5e 100644 --- a/news/views.py +++ b/news/views/template_views.py @@ -23,14 +23,14 @@ from committees.models import Committee -from .forms import ( +from ..forms import ( ArticleForm, EventForm, eventAttendeeFormSet, eventSkillFormSet, uploadformset, ) -from .models import Article, Event, EventRegistration +from ..models import Article, Event, EventRegistration class EventView(DetailView): diff --git a/news/views/upload.py b/news/views/upload.py new file mode 100644 index 000000000..395b03885 --- /dev/null +++ b/news/views/upload.py @@ -0,0 +1,9 @@ +from rest_framework import viewsets + +from news.models import Upload +from news.serializers.upload import UploadSerializer + + +class UploadViewSet(viewsets.ModelViewSet): + queryset = Upload.objects.all() + serializer_class = UploadSerializer diff --git a/watchlist/serializers/shift_slot.py b/watchlist/serializers/shift_slot.py new file mode 100644 index 000000000..ce3f30be6 --- /dev/null +++ b/watchlist/serializers/shift_slot.py @@ -0,0 +1,9 @@ +from rest_framework import serializers + +from watchlist.models import ShiftSlot + + +class ShiftSlotSerializer(serializers.ModelSerializer): + class Meta: + model = ShiftSlot + fields = "__all__" diff --git a/watchlist/urls.py b/watchlist/urls.py index 92086ead1..2c63658b8 100644 --- a/watchlist/urls.py +++ b/watchlist/urls.py @@ -1,16 +1,23 @@ from django.urls import path -from . import views +from website.urls import api_router + +from .views import shift_slot, template_views app_name = "watchlist" +api_router.register("shiftslots", shift_slot.ShiftSlotViewSet) urlpatterns = [ - path("", views.watchlistView.as_view(), name="vaktliste"), - path("register/", views.WatchListRegisterView.as_view(), name="register"), + path("", template_views.watchlistView.as_view(), name="vaktliste"), + path( + "register/", + template_views.WatchListRegisterView.as_view(), + name="register", + ), path( "unregister/", - views.WatchListUnregisterView.as_view(), + template_views.WatchListUnregisterView.as_view(), name="unregister", ), - path("reset", views.WatchListResetView.as_view(), name="reset"), + path("reset", template_views.WatchListResetView.as_view(), name="reset"), ] diff --git a/watchlist/views/shift_slot.py b/watchlist/views/shift_slot.py new file mode 100644 index 000000000..d3e0478cd --- /dev/null +++ b/watchlist/views/shift_slot.py @@ -0,0 +1,9 @@ +from rest_framework import viewsets + +from watchlist.models import ShiftSlot +from watchlist.serializers.shift_slot import ShiftSlotSerializer + + +class ShiftSlotViewSet(viewsets.ModelViewSet): + queryset = ShiftSlot.objects.all() + serializer_class = ShiftSlotSerializer diff --git a/watchlist/views.py b/watchlist/views/template_views.py similarity index 96% rename from watchlist/views.py rename to watchlist/views/template_views.py index c3dfa23db..0da3a97a6 100644 --- a/watchlist/views.py +++ b/watchlist/views/template_views.py @@ -6,8 +6,8 @@ from userprofile.models import Skill -from .models import ShiftSlot -from .utils import get_shift_weekview_columns, get_shift_weekview_rows +from ..models import ShiftSlot +from ..utils import get_shift_weekview_columns, get_shift_weekview_rows class watchlistView(TemplateView): diff --git a/website/urls.py b/website/urls.py index b0f6658bf..92860fde9 100644 --- a/website/urls.py +++ b/website/urls.py @@ -9,6 +9,7 @@ from rest_framework import routers from inventory.views.item import InventoryListAPIView +from news.views import article, event, upload from search.views import SearchAPIView, SearchView from userprofile.views import ( MembersAPIView, @@ -17,6 +18,7 @@ TermsOfServiceCreateView, TermsOfServiceView, ) +from watchlist.views import shift_slot from website.views import ( AboutView, AcceptTosRedirectView, @@ -34,6 +36,10 @@ # Add rest framework urls api_router = routers.DefaultRouter() +api_router.register("upload", upload.UploadViewSet) +api_router.register("event", event.EventViewSet) +api_router.register("article", article.ArticleViewSet) +api_router.register("shiftslot", shift_slot.ShiftSlotViewSet) urlpatterns = [ path("", IndexView.as_view(), name="index"),