med/media/views.py

193 lines
6.0 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 12:20:17 +00:00
from http.client import NO_CONTENT
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
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
2017-06-30 01:25:07 +00:00
from django.db import transaction
2019-08-09 21:22:20 +00:00
from django.shortcuts import redirect, render
2019-08-02 12:57:53 +00:00
from django.utils import timezone
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
from reversion import revisions as reversion
2020-09-25 08:27:43 +00:00
from .models import Auteur, BD, CD, Emprunt, FutureMedia, Jeu, Manga, Revue, Roman, Vinyle
from .serializers import AuteurSerializer, BDSerializer, CDSerializer, EmpruntSerializer, FutureMediaSerializer, \
JeuSerializer, MangaSerializer, RevueSerializer, RomanSerializer, VinyleSerializer
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
2019-08-08 17:16:54 +00:00
@permission_required('media.change_emprunt')
2017-06-30 01:25:07 +00:00
def retour_emprunt(request, empruntid):
try:
emprunt_instance = Emprunt.objects.get(pk=empruntid)
except Emprunt.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
2019-08-10 08:44:17 +00:00
return redirect("admin:media_emprunt_changelist")
2017-06-30 01:25:07 +00:00
with transaction.atomic(), reversion.create_revision():
emprunt_instance.permanencier_rendu = request.user
emprunt_instance.date_rendu = timezone.now()
emprunt_instance.save()
reversion.set_user(request.user)
messages.success(request, "Retour enregistré")
2019-08-10 08:44:17 +00:00
return redirect("admin:media_emprunt_changelist")
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2019-08-08 17:16:54 +00:00
def index(request):
"""
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
"""
if request.user.is_authenticated:
2019-08-10 06:04:46 +00:00
return redirect('admin:index')
2019-08-08 17:16:54 +00:00
else:
2019-08-10 06:04:46 +00:00
return render(request, 'admin/index.html', {
'title': _('Welcome to the Mediatek database'),
})
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)
class MarkBDAsPresent(MarkMediumAsPresent):
model = BD
class MarkMangaAsPresent(MarkMediumAsPresent):
model = Manga
class MarkCDAsPresent(MarkMediumAsPresent):
model = CD
class MarkVinyleAsPresent(MarkMediumAsPresent):
model = Vinyle
class MarkRomanAsPresent(MarkMediumAsPresent):
model = Roman
class MarkRevueAsPresent(MarkMediumAsPresent):
model = Revue
class MarkFutureAsPresent(MarkMediumAsPresent):
model = FutureMedia
2019-08-17 10:12:10 +00:00
class AuteurViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows authors to be viewed or edited.
"""
queryset = Auteur.objects.all()
serializer_class = AuteurSerializer
2020-09-25 08:27:43 +00:00
class BDViewSet(viewsets.ModelViewSet):
2019-08-17 10:12:10 +00:00
"""
API endpoint that allows media to be viewed or edited.
"""
2020-05-22 16:04:41 +00:00
queryset = BD.objects.all()
2020-09-25 08:27:43 +00:00
serializer_class = BDSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn", "side_identifier"]
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"]
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
class VinyleViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = Vinyle.objects.all()
serializer_class = VinyleSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["side_identifier", "rpm"]
search_fields = ["title", "side_identifier", "authors__name"]
2020-09-25 08:27:43 +00:00
class RomanViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = Roman.objects.all()
serializer_class = RomanSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn", "side_identifier", "number_of_pages"]
search_fields = ["=isbn", "title", "subtitle", "side_identifier", "authors__name"]
2020-09-25 08:27:43 +00:00
class RevueViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = Revue.objects.all()
serializer_class = RevueSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["number", "year", "month", "day", "double"]
search_fields = ["title"]
2020-09-25 08:27:43 +00:00
class FutureMediaViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows media to be viewed or edited.
"""
queryset = FutureMedia.objects.all()
serializer_class = FutureMediaSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ["isbn"]
search_fields = ["=isbn"]
2019-08-17 10:12:10 +00:00
class EmpruntViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows borrowed items to be viewed or edited.
"""
queryset = Emprunt.objects.all()
serializer_class = EmpruntSerializer
class JeuViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows games to be viewed or edited.
"""
queryset = Jeu.objects.all()
serializer_class = JeuSerializer