mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Extend the bookshelf implementation
This commit is contained in:
@@ -1,6 +1,15 @@
|
||||
from django.contrib import admin
|
||||
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
||||
|
||||
from bookshelf.models import BookProperty, Book, Author, Publisher
|
||||
from bookshelf.models import BookProperty, BookImage, Book, Author, Publisher
|
||||
|
||||
|
||||
class BookImageInline(SortableInlineAdminMixin, admin.TabularInline):
|
||||
model = BookImage
|
||||
min_num = 0
|
||||
extra = 0
|
||||
readonly_fields = ("image_thumbnail",)
|
||||
classes = ["collapse"]
|
||||
|
||||
|
||||
class BookPropertyInline(admin.TabularInline):
|
||||
@@ -10,8 +19,8 @@ class BookPropertyInline(admin.TabularInline):
|
||||
|
||||
|
||||
@admin.register(Book)
|
||||
class BookAdmin(admin.ModelAdmin):
|
||||
inlines = (BookPropertyInline,)
|
||||
class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
inlines = (BookImageInline, BookPropertyInline,)
|
||||
list_display = (
|
||||
"title",
|
||||
"get_authors",
|
||||
@@ -19,8 +28,8 @@ class BookAdmin(admin.ModelAdmin):
|
||||
"publication_year",
|
||||
"numbers_of_pages"
|
||||
)
|
||||
search_fields = ("title",)
|
||||
list_filter = ("publisher__name",)
|
||||
search_fields = ("title", "publisher__name", "authors__last_name")
|
||||
list_filter = ("publisher__name", "authors")
|
||||
|
||||
@admin.display(description="Publisher")
|
||||
def get_publisher(self, obj):
|
||||
|
51
ram/bookshelf/migrations/0003_bookimage.py
Normal file
51
ram/bookshelf/migrations/0003_bookimage.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# Generated by Django 4.2.5 on 2023-10-02 10:36
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import ram.utils
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("bookshelf", "0002_book_language_book_numbers_of_pages_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="BookImage",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("order", models.PositiveIntegerField(default=0)),
|
||||
(
|
||||
"image",
|
||||
models.ImageField(
|
||||
blank=True,
|
||||
null=True,
|
||||
storage=ram.utils.DeduplicatedStorage,
|
||||
upload_to="images/books/",
|
||||
),
|
||||
),
|
||||
(
|
||||
"book",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="image",
|
||||
to="bookshelf.book",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"ordering": ["order"],
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
]
|
@@ -5,10 +5,9 @@ from django_countries.fields import CountryField
|
||||
|
||||
from ckeditor_uploader.fields import RichTextUploadingField
|
||||
|
||||
from metadata.models import (
|
||||
Property,
|
||||
Tag,
|
||||
)
|
||||
from metadata.models import Tag
|
||||
from ram.utils import DeduplicatedStorage
|
||||
from ram.models import Image, PropertyInstance
|
||||
|
||||
|
||||
class Publisher(models.Model):
|
||||
@@ -62,7 +61,19 @@ class Book(models.Model):
|
||||
# return reverse("rolling_stock", kwargs={"uuid": self.uuid})
|
||||
|
||||
|
||||
class BookProperty(models.Model):
|
||||
class BookImage(Image):
|
||||
book = models.ForeignKey(
|
||||
Book, on_delete=models.CASCADE, related_name="image"
|
||||
)
|
||||
image = models.ImageField(
|
||||
upload_to="images/books/", # FIXME, find a better way to replace this
|
||||
storage=DeduplicatedStorage,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
|
||||
|
||||
class BookProperty(PropertyInstance):
|
||||
book = models.ForeignKey(
|
||||
Book,
|
||||
on_delete=models.CASCADE,
|
||||
@@ -70,11 +81,3 @@ class BookProperty(models.Model):
|
||||
blank=False,
|
||||
related_name="property",
|
||||
)
|
||||
property = models.ForeignKey(Property, on_delete=models.CASCADE)
|
||||
value = models.CharField(max_length=256)
|
||||
|
||||
def __str__(self):
|
||||
return self.property.name
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Properties"
|
||||
|
26
ram/bookshelf/serializers.py
Normal file
26
ram/bookshelf/serializers.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from rest_framework import serializers
|
||||
from bookshelf.models import Book, Author, Publisher
|
||||
from metadata.serializers import TagSerializer
|
||||
|
||||
|
||||
class AuthorSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Author
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class PublisherSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Publisher
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
authors = AuthorSerializer(many=True)
|
||||
publisher = PublisherSerializer()
|
||||
tags = TagSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = Book
|
||||
fields = "__all__"
|
||||
read_only_fields = ("creation_time", "updated_time")
|
7
ram/bookshelf/urls.py
Normal file
7
ram/bookshelf/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
from bookshelf.views import BookList, BookGet
|
||||
|
||||
urlpatterns = [
|
||||
path("book/list", BookList.as_view()),
|
||||
path("book/get/<str:uuid>", BookGet.as_view()),
|
||||
]
|
@@ -1,3 +1,18 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||
from rest_framework.schemas.openapi import AutoSchema
|
||||
|
||||
# Create your views here.
|
||||
from bookshelf.models import Book
|
||||
from bookshelf.serializers import BookSerializer
|
||||
|
||||
|
||||
class BookList(ListAPIView):
|
||||
queryset = Book.objects.all()
|
||||
serializer_class = BookSerializer
|
||||
|
||||
|
||||
class BookGet(RetrieveAPIView):
|
||||
queryset = Book.objects.all()
|
||||
serializer_class = BookSerializer
|
||||
lookup_field = "uuid"
|
||||
|
||||
schema = AutoSchema(operation_id_base="retrieveBookByUUID")
|
||||
|
Reference in New Issue
Block a user