med/media/views.py

176 lines
5.2 KiB
Python
Raw Normal View History

2019-08-02 12:57:53 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2020-09-25 08:27:43 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2020-09-25 12:20:17 +00:00
from django.http import HttpResponse
2020-09-25 08:27:43 +00:00
from django_filters.rest_framework import DjangoFilterBackend
2021-10-23 17:00:45 +00:00
from django.shortcuts import redirect
2019-08-10 06:04:46 +00:00
from django.utils.translation import gettext_lazy as _
2020-09-25 12:20:17 +00:00
from django.views.generic import TemplateView, DetailView
2019-08-17 10:12:10 +00:00
from rest_framework import viewsets
2020-09-25 08:27:43 +00:00
from rest_framework.filters import SearchFilter
2017-06-30 01:25:07 +00:00
2021-11-14 13:26:41 +00:00
from .models import Author, Borrow, CD, Comic, FutureMedium, Game, Manga,\
2021-10-23 16:31:03 +00:00
Novel, Review, Vinyl
2021-11-14 13:26:41 +00:00
from .serializers import AuthorSerializer, BorrowSerializer, ComicSerializer, \
CDSerializer, FutureMediumSerializer, GameSerializer, MangaSerializer, \
NovelSerializer, ReviewSerializer, VinylSerializer
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2021-10-23 17:00:45 +00:00
class IndexView(TemplateView):
2019-08-08 17:16:54 +00:00
"""
2019-08-10 06:04:46 +00:00
Home page which redirect to admin when logged in
2019-08-08 17:16:54 +00:00
"""
2021-10-23 17:00:45 +00:00
extra_context = {'title': _('Welcome to the Mediatek database')}
template_name = 'admin/index.html'
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('admin:index')
return super().dispatch(request, *args, **kwargs)
2019-08-17 10:12:10 +00:00
2020-09-25 08:27:43 +00:00
class FindMediumView(LoginRequiredMixin, TemplateView):
template_name = "media/find_medium.html"
2020-09-25 12:20:17 +00:00
class MarkMediumAsPresent(LoginRequiredMixin, DetailView):
def dispatch(self, request, *args, **kwargs):
object = self.get_object()
object.present = not request.GET.get("absent", False)
object.save()
return HttpResponse("", content_type=204)
2021-10-23 16:31:03 +00:00
class MarkComicAsPresent(MarkMediumAsPresent):
model = Comic
2020-09-25 12:20:17 +00:00
class MarkMangaAsPresent(MarkMediumAsPresent):
model = Manga
class MarkCDAsPresent(MarkMediumAsPresent):
model = CD
2021-10-23 16:31:03 +00:00
class MarkVinylAsPresent(MarkMediumAsPresent):
model = Vinyl
2020-09-25 12:20:17 +00:00
2021-10-26 13:16:00 +00:00
class MarkNovelAsPresent(MarkMediumAsPresent):
2021-10-23 16:31:03 +00:00
model = Novel
2020-09-25 12:20:17 +00:00
2021-10-26 13:16:00 +00:00
class MarkReviewAsPresent(MarkMediumAsPresent):
2021-10-23 16:31:03 +00:00
model = Review
2020-09-25 12:20:17 +00:00
class MarkFutureAsPresent(MarkMediumAsPresent):
2021-10-23 16:31:03 +00:00
model = FutureMedium
2020-09-25 12:20:17 +00:00
2021-10-23 16:31:03 +00:00
class AuthorViewSet(viewsets.ModelViewSet):
2019-08-17 10:12:10 +00:00
"""
API endpoint that allows authors to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Author.objects.all()
serializer_class = AuthorSerializer
2019-08-17 10:12:10 +00:00
2021-10-23 16:31:03 +00:00
class ComicViewSet(viewsets.ModelViewSet):
2019-08-17 10:12:10 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Comic.objects.all()
serializer_class = ComicSerializer
2020-09-25 08:27:43 +00:00
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn", "side_identifier"]
2020-12-28 22:12:27 +00:00
search_fields = ["=isbn", "title", "subtitle", "side_identifier",
"authors__name"]
2020-09-25 08:27:43 +00:00
class MangaViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = Manga.objects.all()
serializer_class = MangaSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn", "side_identifier"]
2020-12-28 22:12:27 +00:00
search_fields = ["=isbn", "title", "subtitle", "side_identifier",
"authors__name"]
2020-09-25 08:27:43 +00:00
class CDViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = CD.objects.all()
serializer_class = CDSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["side_identifier"]
search_fields = ["title", "side_identifier", "authors__name"]
2020-09-25 08:27:43 +00:00
2021-10-23 16:31:03 +00:00
class VinylViewSet(viewsets.ModelViewSet):
2020-09-25 08:27:43 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Vinyl.objects.all()
serializer_class = VinylSerializer
2020-09-25 08:27:43 +00:00
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["side_identifier", "rpm"]
search_fields = ["title", "side_identifier", "authors__name"]
2020-09-25 08:27:43 +00:00
2021-10-23 16:31:03 +00:00
class NovelViewSet(viewsets.ModelViewSet):
2020-09-25 08:27:43 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Novel.objects.all()
serializer_class = NovelSerializer
2020-09-25 08:27:43 +00:00
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn", "side_identifier", "number_of_pages"]
2020-12-28 22:12:27 +00:00
search_fields = ["=isbn", "title", "subtitle", "side_identifier",
"authors__name"]
2020-09-25 08:27:43 +00:00
2021-10-23 16:31:03 +00:00
class ReviewViewSet(viewsets.ModelViewSet):
2020-09-25 08:27:43 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Review.objects.all()
serializer_class = ReviewSerializer
2020-09-25 08:27:43 +00:00
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["number", "year", "month", "day", "double"]
search_fields = ["title"]
2020-09-25 08:27:43 +00:00
2021-10-23 16:31:03 +00:00
class FutureMediumViewSet(viewsets.ModelViewSet):
2020-09-25 08:27:43 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = FutureMedium.objects.all()
serializer_class = FutureMediumSerializer
2020-09-25 08:27:43 +00:00
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn"]
search_fields = ["=isbn"]
2019-08-17 10:12:10 +00:00
2021-11-14 13:26:41 +00:00
class BorrowViewSet(viewsets.ModelViewSet):
2019-08-17 10:12:10 +00:00
"""
API endpoint that allows borrowed items to be viewed or edited.
"""
2021-11-14 13:26:41 +00:00
queryset = Borrow.objects.all()
serializer_class = BorrowSerializer
2019-08-17 10:12:10 +00:00
2021-10-23 16:31:03 +00:00
class GameViewSet(viewsets.ModelViewSet):
2019-08-17 10:12:10 +00:00
"""
API endpoint that allows games to be viewed or edited.
"""
2021-10-23 16:31:03 +00:00
queryset = Game.objects.all()
serializer_class = GameSerializer