Add shop field (from properties) (#48)

* Add shop field (from properties)

* Update template
This commit is contained in:
2025-01-27 00:34:44 +01:00
committed by GitHub
parent 1a8f2aace8
commit d16e00d66b
16 changed files with 191 additions and 9 deletions

View File

@@ -59,7 +59,7 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
"number_of_pages",
"published",
)
autocomplete_fields = ("authors", "publisher")
autocomplete_fields = ("authors", "publisher", "shop")
readonly_fields = ("creation_time", "updated_time")
search_fields = ("title", "publisher__name", "authors__last_name")
list_filter = ("publisher__name", "authors")
@@ -86,6 +86,7 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
"Purchase data",
{
"fields": (
"shop",
"purchase_date",
"price",
)
@@ -133,6 +134,7 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
"Publication Year",
"Description",
"Tags",
"Shop",
"Purchase Date",
"Price ({})".format(get_site_conf().currency),
"Notes",
@@ -158,6 +160,7 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
settings.CSV_SEPARATOR_ALT.join(
t.name for t in obj.tags.all()
),
obj.shop,
obj.purchase_date,
obj.price,
html.unescape(strip_tags(obj.notes)),
@@ -274,6 +277,7 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
"Description",
"Tags",
"Purchase Date",
"Shop",
"Price ({})".format(get_site_conf().currency),
"Notes",
"Properties",
@@ -299,6 +303,7 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
settings.CSV_SEPARATOR_ALT.join(
t.name for t in obj.tags.all()
),
obj.shop,
obj.purchase_date,
obj.price,
html.unescape(strip_tags(obj.notes)),

View File

@@ -0,0 +1,46 @@
# Generated by Django 5.1.4 on 2025-01-26 14:32
import django.db.models.deletion
from django.db import migrations, models
def shop_from_property(apps, schema_editor):
basebook = apps.get_model("bookshelf", "BaseBook")
shop_model = apps.get_model("metadata", "Shop")
for row in basebook.objects.all():
property = row.property.filter(
property__name__icontains="shop"
).first()
if property:
shop, created = shop_model.objects.get_or_create(
name=property.value,
defaults={"on_line": False}
)
row.shop = shop
row.save()
class Migration(migrations.Migration):
dependencies = [
("bookshelf", "0021_basebookdocument_creation_time_and_more"),
("metadata", "0023_shop"),
]
operations = [
migrations.AddField(
model_name="basebook",
name="shop",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="metadata.shop",
),
),
migrations.RunPython(
shop_from_property,
reverse_code=migrations.RunPython.noop
),
]

View File

@@ -7,10 +7,9 @@ from django_countries.fields import CountryField
from tinymce import models as tinymce
from metadata.models import Tag
from ram.utils import DeduplicatedStorage
from ram.models import BaseModel, Image, Document, PropertyInstance
from metadata.models import Scale, Manufacturer
from metadata.models import Scale, Manufacturer, Shop, Tag
class Publisher(models.Model):
@@ -50,6 +49,9 @@ class BaseBook(BaseModel):
number_of_pages = models.SmallIntegerField(null=True, blank=True)
publication_year = models.SmallIntegerField(null=True, blank=True)
description = tinymce.HTMLField(blank=True)
shop = models.ForeignKey(
Shop, on_delete=models.CASCADE, null=True, blank=True
)
price = models.DecimalField(
max_digits=10,
decimal_places=2,

View File

@@ -26,7 +26,7 @@ class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
exclude = ("purchase_date", "price",)
exclude = ("shop", "purchase_date", "price",)
read_only_fields = ("creation_time", "updated_time")