From 054865cd41d909f59f9a702bf98e26b24449dba7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 22 May 2020 18:04:41 +0200 Subject: [PATCH] Ajout des CD et des vinyles --- media/admin.py | 17 +++++- media/forms.py | 4 +- .../management/commands/split_media_types.py | 10 ++- media/models.py | 61 +++++++++++++++---- media/serializers.py | 4 +- media/tests/test_templates.py | 6 +- media/views.py | 4 +- 7 files changed, 82 insertions(+), 24 deletions(-) diff --git a/media/admin.py b/media/admin.py index f4e2d56..bd92c6b 100644 --- a/media/admin.py +++ b/media/admin.py @@ -9,7 +9,7 @@ from reversion.admin import VersionAdmin from med.admin import admin_site from .forms import MediaAdminForm -from .models import Auteur, Emprunt, FutureMedia, Jeu, Manga, Media +from .models import Auteur, BD, CD, Emprunt, FutureMedia, Jeu, Manga, Vinyle class AuteurAdmin(VersionAdmin): @@ -76,6 +76,17 @@ class FutureMediaAdmin(VersionAdmin): extra_context=extra_context) +class CDAdmin(VersionAdmin): + list_display = ('title', 'authors_list', 'side_identifier',) + search_fields = ('title', 'authors__name', 'side_identifier',) + autocomplete_fields = ('authors',) + + def authors_list(self, obj): + return ", ".join([a.name for a in obj.authors.all()]) + + authors_list.short_description = _('authors') + + class EmpruntAdmin(VersionAdmin): list_display = ('media', 'user', 'date_emprunt', 'date_rendu', 'permanencier_emprunt', 'permanencier_rendu_custom') @@ -120,8 +131,10 @@ class JeuAdmin(VersionAdmin): admin_site.register(Auteur, AuteurAdmin) -admin_site.register(Media, MediaAdmin) +admin_site.register(BD, MediaAdmin) admin_site.register(Manga, MediaAdmin) +admin_site.register(CD, CDAdmin) +admin_site.register(Vinyle, CDAdmin) admin_site.register(FutureMedia, FutureMediaAdmin) admin_site.register(Emprunt, EmpruntAdmin) admin_site.register(Jeu, JeuAdmin) diff --git a/media/forms.py b/media/forms.py index f368974..48eff85 100644 --- a/media/forms.py +++ b/media/forms.py @@ -10,7 +10,7 @@ import urllib.request from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ -from .models import Auteur, Media +from .models import Auteur, BD from .scraper import BedetequeScraper @@ -263,5 +263,5 @@ class MediaAdminForm(ModelForm): self.add_error(name, e) class Meta: - model = Media + model = BD fields = '__all__' diff --git a/media/management/commands/split_media_types.py b/media/management/commands/split_media_types.py index b5d7d3c..823009a 100644 --- a/media/management/commands/split_media_types.py +++ b/media/management/commands/split_media_types.py @@ -1,7 +1,9 @@ +from time import sleep + from django.core.management import BaseCommand from media.forms import MediaAdminForm -from media.models import Manga, Media +from media.models import BD, Manga class Command(BaseCommand): @@ -13,7 +15,11 @@ class Command(BaseCommand): def handle(self, *args, **options): converted = 0 - for media in Media.objects.all(): + for media in BD.objects.all(): + if media.pk < 3400: + continue + # We sleep 5 seconds to avoid a ban from Bedetheque + sleep(5) self.stdout.write(str(media)) form = MediaAdminForm(instance=media, data={"isbn": media.isbn, "_isbn": True, }) diff --git a/media/models.py b/media/models.py index 60f7c2f..c7b830a 100644 --- a/media/models.py +++ b/media/models.py @@ -30,7 +30,7 @@ class Auteur(models.Model): ordering = ['name'] -class Media(models.Model): +class BD(models.Model): isbn = ISBNField( _('ISBN'), help_text=_('You may be able to scan it from a bar code.'), @@ -86,8 +86,8 @@ class Media(models.Model): return self.title class Meta: - verbose_name = _("medium") - verbose_name_plural = _("media") + verbose_name = _("BD") + verbose_name_plural = _("BDs") ordering = ['title', 'subtitle'] @@ -140,16 +140,55 @@ class Manga(models.Model): null=True, ) + +class Vinyle(models.Model): + title = models.CharField( + verbose_name=_('title'), + max_length=255, + ) + + side_identifier = models.CharField( + verbose_name=_('side identifier'), + max_length=255, + ) + + authors = models.ManyToManyField( + 'Auteur', + verbose_name=_('authors'), + ) + def __str__(self): - if self.subtitle: - return "{} : {}".format(self.title, self.subtitle) - else: - return self.title + return self.title class Meta: - verbose_name = _("manga") - verbose_name_plural = _("mangas") - ordering = ['title', 'subtitle'] + verbose_name = _("vinyle") + verbose_name_plural = _("vinyles") + ordering = ['title'] + + +class CD(models.Model): + title = models.CharField( + verbose_name=_('title'), + max_length=255, + ) + + side_identifier = models.CharField( + verbose_name=_('side identifier'), + max_length=255, + ) + + authors = models.ManyToManyField( + 'Auteur', + verbose_name=_('authors'), + ) + + def __str__(self): + return self.title + + class Meta: + verbose_name = _("CD") + verbose_name_plural = _("CDs") + ordering = ['title'] class FutureMedia(models.Model): @@ -171,7 +210,7 @@ class FutureMedia(models.Model): class Emprunt(models.Model): media = models.ForeignKey( - 'Media', + 'BD', on_delete=models.PROTECT, ) user = models.ForeignKey( diff --git a/media/serializers.py b/media/serializers.py index d16547c..bbdce2e 100644 --- a/media/serializers.py +++ b/media/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import Auteur, Emprunt, Jeu, Media +from .models import Auteur, BD, Emprunt, Jeu class AuteurSerializer(serializers.HyperlinkedModelSerializer): @@ -11,7 +11,7 @@ class AuteurSerializer(serializers.HyperlinkedModelSerializer): class MediaSerializer(serializers.HyperlinkedModelSerializer): class Meta: - model = Media + model = BD fields = ['url', 'isbn', 'title', 'subtitle', 'external_url', 'side_identifier', 'authors', 'number_of_pages', 'publish_date'] diff --git a/media/tests/test_templates.py b/media/tests/test_templates.py index 0a1a150..4c2ffbd 100644 --- a/media/tests/test_templates.py +++ b/media/tests/test_templates.py @@ -4,7 +4,7 @@ from django.test import TestCase from django.urls import reverse -from media.models import Auteur, Media +from media.models import Auteur, BD from users.models import User """ @@ -25,12 +25,12 @@ class TemplateTests(TestCase): self.dummy_author = Auteur.objects.create(name="Test author") # Create media - self.dummy_media1 = Media.objects.create( + self.dummy_media1 = BD.objects.create( title="Test media", side_identifier="T M", ) self.dummy_media1.authors.add(self.dummy_author) - self.dummy_media2 = Media.objects.create( + self.dummy_media2 = BD.objects.create( title="Test media bis", side_identifier="T M 2", external_url="https://example.com/", diff --git a/media/views.py b/media/views.py index 3ed0b45..e6ac52f 100644 --- a/media/views.py +++ b/media/views.py @@ -11,7 +11,7 @@ from django.utils.translation import gettext_lazy as _ from rest_framework import viewsets from reversion import revisions as reversion -from .models import Auteur, Emprunt, Jeu, Media +from .models import Auteur, BD, Emprunt, Jeu from .serializers import AuteurSerializer, EmpruntSerializer, \ JeuSerializer, MediaSerializer @@ -57,7 +57,7 @@ class MediaViewSet(viewsets.ModelViewSet): """ API endpoint that allows media to be viewed or edited. """ - queryset = Media.objects.all() + queryset = BD.objects.all() serializer_class = MediaSerializer