mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Complete the implementation of document repository and add invoices
This commit is contained in:
@@ -60,10 +60,11 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
"published",
|
"published",
|
||||||
)
|
)
|
||||||
autocomplete_fields = ("authors", "publisher", "shop")
|
autocomplete_fields = ("authors", "publisher", "shop")
|
||||||
readonly_fields = ("creation_time", "updated_time")
|
readonly_fields = ("invoices", "creation_time", "updated_time")
|
||||||
search_fields = ("title", "publisher__name", "authors__last_name")
|
search_fields = ("title", "publisher__name", "authors__last_name")
|
||||||
list_filter = ("publisher__name", "authors")
|
list_filter = ("publisher__name", "authors")
|
||||||
|
|
||||||
|
def get_fieldsets(self, request, obj=None):
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(
|
(
|
||||||
None,
|
None,
|
||||||
@@ -107,6 +108,9 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if obj and obj.invoice.count() > 0:
|
||||||
|
fieldsets[1][1]["fields"] += ("invoices",)
|
||||||
|
return fieldsets
|
||||||
|
|
||||||
def get_form(self, request, obj=None, **kwargs):
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
form = super().get_form(request, obj, **kwargs)
|
form = super().get_form(request, obj, **kwargs)
|
||||||
@@ -115,6 +119,14 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
)
|
)
|
||||||
return form
|
return form
|
||||||
|
|
||||||
|
@admin.display(description="Invoices")
|
||||||
|
def invoices(self, obj):
|
||||||
|
html = "<br>".join(
|
||||||
|
"<a href=\"{}\" target=\"_blank\">{}</a>".format(
|
||||||
|
i.file.url, i
|
||||||
|
) for i in obj.invoice.all())
|
||||||
|
return format_html(html)
|
||||||
|
|
||||||
@admin.display(description="Publisher")
|
@admin.display(description="Publisher")
|
||||||
def get_publisher(self, obj):
|
def get_publisher(self, obj):
|
||||||
return obj.publisher.name
|
return obj.publisher.name
|
||||||
@@ -214,6 +226,7 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
search_fields = ("manufacturer__name", "years", "scales__scale")
|
search_fields = ("manufacturer__name", "years", "scales__scale")
|
||||||
list_filter = ("manufacturer__name", "publication_year", "scales__scale")
|
list_filter = ("manufacturer__name", "publication_year", "scales__scale")
|
||||||
|
|
||||||
|
def get_fieldsets(self, request, obj=None):
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(
|
(
|
||||||
None,
|
None,
|
||||||
@@ -256,6 +269,9 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if obj and obj.invoice.count() > 0:
|
||||||
|
fieldsets[1][1]["fields"] += ("invoices",)
|
||||||
|
return fieldsets
|
||||||
|
|
||||||
def get_form(self, request, obj=None, **kwargs):
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
form = super().get_form(request, obj, **kwargs)
|
form = super().get_form(request, obj, **kwargs)
|
||||||
|
@@ -7,6 +7,7 @@ class Migration(migrations.Migration):
|
|||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("bookshelf", "0022_basebook_shop"),
|
("bookshelf", "0022_basebook_shop"),
|
||||||
|
("repository", "0001_initial"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
@@ -7,6 +7,7 @@ class Migration(migrations.Migration):
|
|||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("metadata", "0023_shop"),
|
("metadata", "0023_shop"),
|
||||||
|
("repository", "0001_initial"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
@@ -29,10 +29,6 @@ class Document(models.Model):
|
|||||||
upload_to="files/",
|
upload_to="files/",
|
||||||
storage=DeduplicatedStorage(),
|
storage=DeduplicatedStorage(),
|
||||||
)
|
)
|
||||||
private = models.BooleanField(
|
|
||||||
default=False,
|
|
||||||
help_text="Document will be visible only to logged users",
|
|
||||||
)
|
|
||||||
creation_time = models.DateTimeField(auto_now_add=True)
|
creation_time = models.DateTimeField(auto_now_add=True)
|
||||||
updated_time = models.DateTimeField(auto_now=True)
|
updated_time = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
@@ -61,8 +57,17 @@ class Document(models.Model):
|
|||||||
'<a href="{0}" target="_blank">Link</a>'.format(self.file.url)
|
'<a href="{0}" target="_blank">Link</a>'.format(self.file.url)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PrivateDocument(Document):
|
||||||
|
private = models.BooleanField(
|
||||||
|
default=False,
|
||||||
|
help_text="Document will be visible only to logged users",
|
||||||
|
)
|
||||||
objects = PublicManager()
|
objects = PublicManager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
class Image(models.Model):
|
class Image(models.Model):
|
||||||
order = models.PositiveIntegerField(default=0, blank=False, null=False)
|
order = models.PositiveIntegerField(default=0, blank=False, null=False)
|
||||||
|
@@ -3,7 +3,8 @@ from django.contrib import admin
|
|||||||
from ram.admin import publish, unpublish
|
from ram.admin import publish, unpublish
|
||||||
from repository.models import (
|
from repository.models import (
|
||||||
GenericDocument,
|
GenericDocument,
|
||||||
BaseBookDocument,
|
InvoiceDocument,
|
||||||
|
# BaseBookDocument,
|
||||||
DecoderDocument,
|
DecoderDocument,
|
||||||
RollingStockDocument
|
RollingStockDocument
|
||||||
)
|
)
|
||||||
@@ -54,6 +55,51 @@ class GenericDocumentAdmin(admin.ModelAdmin):
|
|||||||
actions = [publish, unpublish]
|
actions = [publish, unpublish]
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(InvoiceDocument)
|
||||||
|
class InvoiceDocumentAdmin(admin.ModelAdmin):
|
||||||
|
readonly_fields = ("size", "creation_time", "updated_time")
|
||||||
|
list_display = (
|
||||||
|
"__str__",
|
||||||
|
"description",
|
||||||
|
"size",
|
||||||
|
"download",
|
||||||
|
)
|
||||||
|
search_fields = (
|
||||||
|
"description",
|
||||||
|
"file",
|
||||||
|
)
|
||||||
|
autocomplete_fields = ("rolling_stock", "book", "catalog")
|
||||||
|
fieldsets = (
|
||||||
|
(
|
||||||
|
None,
|
||||||
|
{
|
||||||
|
"fields": (
|
||||||
|
"description",
|
||||||
|
"rolling_stock",
|
||||||
|
"book",
|
||||||
|
"catalog",
|
||||||
|
"file",
|
||||||
|
"size",
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Notes",
|
||||||
|
{"classes": ("collapse",), "fields": ("notes",)},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Audit",
|
||||||
|
{
|
||||||
|
"classes": ("collapse",),
|
||||||
|
"fields": (
|
||||||
|
"creation_time",
|
||||||
|
"updated_time",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# @admin.register(BaseBookDocument)
|
# @admin.register(BaseBookDocument)
|
||||||
# class BookDocumentAdmin(admin.ModelAdmin):
|
# class BookDocumentAdmin(admin.ModelAdmin):
|
||||||
# readonly_fields = ("size",)
|
# readonly_fields = ("size",)
|
||||||
|
@@ -66,8 +66,6 @@ def migrate_book(apps, schema_editor):
|
|||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
initial = True
|
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("bookshelf", "0022_basebook_shop"),
|
("bookshelf", "0022_basebook_shop"),
|
||||||
("metadata", "0023_shop"),
|
("metadata", "0023_shop"),
|
||||||
|
@@ -0,0 +1,66 @@
|
|||||||
|
# Generated by Django 5.1.4 on 2025-02-09 15:16
|
||||||
|
|
||||||
|
import ram.utils
|
||||||
|
import tinymce.models
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("bookshelf", "0023_delete_basebookdocument"),
|
||||||
|
("repository", "0002_alter_decoderdocument_options_and_more"),
|
||||||
|
("roster", "0036_delete_rollingstockdocument"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name="basebookdocument",
|
||||||
|
options={"verbose_name_plural": "Bookshelf Documents"},
|
||||||
|
),
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name="genericdocument",
|
||||||
|
options={"verbose_name_plural": "Generic documents"},
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="InvoiceDocument",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("description", models.CharField(blank=True, max_length=128)),
|
||||||
|
(
|
||||||
|
"file",
|
||||||
|
models.FileField(
|
||||||
|
storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("creation_time", models.DateTimeField(auto_now_add=True)),
|
||||||
|
("updated_time", models.DateTimeField(auto_now=True)),
|
||||||
|
("private", models.BooleanField(default=True, editable=False)),
|
||||||
|
("notes", tinymce.models.HTMLField(blank=True)),
|
||||||
|
(
|
||||||
|
"book",
|
||||||
|
models.ManyToManyField(
|
||||||
|
blank=True, related_name="invoice", to="bookshelf.basebook"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"rolling_stock",
|
||||||
|
models.ManyToManyField(
|
||||||
|
blank=True, related_name="invoice", to="roster.rollingstock"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "Invoice",
|
||||||
|
"verbose_name_plural": "Invoices",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
@@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 5.1.4 on 2025-02-09 17:42
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("bookshelf", "0023_delete_basebookdocument"),
|
||||||
|
("repository", "0003_alter_basebookdocument_options_and_more"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="invoicedocument",
|
||||||
|
name="catalog",
|
||||||
|
field=models.ManyToManyField(
|
||||||
|
blank=True, related_name="invoice", to="bookshelf.catalog"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="invoicedocument",
|
||||||
|
name="book",
|
||||||
|
field=models.ManyToManyField(
|
||||||
|
blank=True, related_name="invoice", to="bookshelf.book"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
@@ -1,30 +1,43 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.contenttypes.models import ContentType
|
|
||||||
from django.contrib.contenttypes import fields
|
|
||||||
|
|
||||||
from tinymce import models as tinymce
|
from tinymce import models as tinymce
|
||||||
|
|
||||||
from ram.models import Document
|
from ram.models import PrivateDocument
|
||||||
from metadata.models import Decoder, Tag
|
from metadata.models import Decoder, Tag
|
||||||
from roster.models import RollingStock
|
from roster.models import RollingStock
|
||||||
from bookshelf.models import BaseBook
|
from bookshelf.models import Book, Catalog, BaseBook
|
||||||
|
|
||||||
|
|
||||||
class GenericDocument(Document):
|
class GenericDocument(PrivateDocument):
|
||||||
notes = tinymce.HTMLField(blank=True)
|
notes = tinymce.HTMLField(blank=True)
|
||||||
tags = models.ManyToManyField(Tag, blank=True, related_name="document")
|
tags = models.ManyToManyField(Tag, blank=True, related_name="document")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name_plural = "Generic Documents"
|
verbose_name_plural = "Generic documents"
|
||||||
|
|
||||||
|
|
||||||
class InvoiceDocument(Document):
|
class InvoiceDocument(PrivateDocument):
|
||||||
content_type = models.ForeignKey(ContentType)
|
private = models.BooleanField(default=True, editable=False)
|
||||||
object_id = models.PositiveIntegerField()
|
rolling_stock = models.ManyToManyField(
|
||||||
content_object = fields.GenericForeignKey("content_type", "object_id")
|
RollingStock, related_name="invoice",
|
||||||
|
blank=True
|
||||||
|
)
|
||||||
|
book = models.ManyToManyField(
|
||||||
|
Book, related_name="invoice",
|
||||||
|
blank=True
|
||||||
|
)
|
||||||
|
catalog = models.ManyToManyField(
|
||||||
|
Catalog, related_name="invoice",
|
||||||
|
blank=True
|
||||||
|
)
|
||||||
|
notes = tinymce.HTMLField(blank=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = "Invoice"
|
||||||
|
verbose_name_plural = "Invoices"
|
||||||
|
|
||||||
|
|
||||||
class DecoderDocument(Document):
|
class DecoderDocument(PrivateDocument):
|
||||||
decoder = models.ForeignKey(
|
decoder = models.ForeignKey(
|
||||||
Decoder, on_delete=models.CASCADE, related_name="document"
|
Decoder, on_delete=models.CASCADE, related_name="document"
|
||||||
)
|
)
|
||||||
@@ -38,13 +51,13 @@ class DecoderDocument(Document):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class BaseBookDocument(Document):
|
class BaseBookDocument(PrivateDocument):
|
||||||
book = models.ForeignKey(
|
book = models.ForeignKey(
|
||||||
BaseBook, on_delete=models.CASCADE, related_name="document"
|
BaseBook, on_delete=models.CASCADE, related_name="document"
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name_plural = "Documents"
|
verbose_name_plural = "Bookshelf Documents"
|
||||||
constraints = [
|
constraints = [
|
||||||
models.UniqueConstraint(
|
models.UniqueConstraint(
|
||||||
fields=["book", "file"],
|
fields=["book", "file"],
|
||||||
@@ -53,7 +66,7 @@ class BaseBookDocument(Document):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class RollingStockDocument(Document):
|
class RollingStockDocument(PrivateDocument):
|
||||||
rolling_stock = models.ForeignKey(
|
rolling_stock = models.ForeignKey(
|
||||||
RollingStock, on_delete=models.CASCADE, related_name="document"
|
RollingStock, on_delete=models.CASCADE, related_name="document"
|
||||||
)
|
)
|
||||||
|
@@ -8,8 +8,8 @@ from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
|||||||
|
|
||||||
from ram.admin import publish, unpublish
|
from ram.admin import publish, unpublish
|
||||||
from ram.utils import generate_csv
|
from ram.utils import generate_csv
|
||||||
from portal.utils import get_site_conf
|
|
||||||
from repository.models import RollingStockDocument
|
from repository.models import RollingStockDocument
|
||||||
|
from portal.utils import get_site_conf
|
||||||
from roster.models import (
|
from roster.models import (
|
||||||
RollingClass,
|
RollingClass,
|
||||||
RollingClassProperty,
|
RollingClassProperty,
|
||||||
@@ -118,7 +118,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
RollingStockJournalInline,
|
RollingStockJournalInline,
|
||||||
)
|
)
|
||||||
autocomplete_fields = ("rolling_class", "shop")
|
autocomplete_fields = ("rolling_class", "shop")
|
||||||
readonly_fields = ("preview", "creation_time", "updated_time")
|
readonly_fields = ("preview", "invoices", "creation_time", "updated_time")
|
||||||
list_display = (
|
list_display = (
|
||||||
"__str__",
|
"__str__",
|
||||||
"address",
|
"address",
|
||||||
@@ -152,6 +152,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
'<img src="{}" /> {}'.format(obj.country.flag, obj.country)
|
'<img src="{}" /> {}'.format(obj.country.flag, obj.country)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_fieldsets(self, request, obj=None):
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(
|
(
|
||||||
None,
|
None,
|
||||||
@@ -207,6 +208,9 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if obj and obj.invoice.count() > 0:
|
||||||
|
fieldsets[2][1]["fields"] += ("invoices",)
|
||||||
|
return fieldsets
|
||||||
|
|
||||||
def get_form(self, request, obj=None, **kwargs):
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
form = super().get_form(request, obj, **kwargs)
|
form = super().get_form(request, obj, **kwargs)
|
||||||
@@ -215,6 +219,14 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
)
|
)
|
||||||
return form
|
return form
|
||||||
|
|
||||||
|
@admin.display(description="Invoices")
|
||||||
|
def invoices(self, obj):
|
||||||
|
html = "<br>".join(
|
||||||
|
"<a href=\"{}\" target=\"_blank\">{}</a>".format(
|
||||||
|
i.file.url, i
|
||||||
|
) for i in obj.invoice.all())
|
||||||
|
return format_html(html)
|
||||||
|
|
||||||
def download_csv(modeladmin, request, queryset):
|
def download_csv(modeladmin, request, queryset):
|
||||||
header = [
|
header = [
|
||||||
"Name",
|
"Name",
|
||||||
|
@@ -7,6 +7,7 @@ class Migration(migrations.Migration):
|
|||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("roster", "0035_alter_rollingstock_shop"),
|
("roster", "0035_alter_rollingstock_shop"),
|
||||||
|
("repository", "0001_initial"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
@@ -5,14 +5,12 @@ from django.db import models
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.contrib.contenttypes import fields
|
|
||||||
|
|
||||||
from tinymce import models as tinymce
|
from tinymce import models as tinymce
|
||||||
|
|
||||||
from ram.models import BaseModel, Image, PropertyInstance
|
from ram.models import BaseModel, Image, PropertyInstance
|
||||||
from ram.utils import DeduplicatedStorage, slugify
|
from ram.utils import DeduplicatedStorage, slugify
|
||||||
from ram.managers import PublicManager
|
from ram.managers import PublicManager
|
||||||
from repository.models import InvoiceDocument
|
|
||||||
from metadata.models import (
|
from metadata.models import (
|
||||||
Scale,
|
Scale,
|
||||||
Manufacturer,
|
Manufacturer,
|
||||||
@@ -115,9 +113,6 @@ class RollingStock(BaseModel):
|
|||||||
null=True,
|
null=True,
|
||||||
blank=True,
|
blank=True,
|
||||||
)
|
)
|
||||||
invoice = fields.GenericRelation(
|
|
||||||
app.get_model("repository", "InvoiceDocument"),
|
|
||||||
InvoiceDocument)
|
|
||||||
tags = models.ManyToManyField(
|
tags = models.ManyToManyField(
|
||||||
Tag, related_name="rolling_stock", blank=True
|
Tag, related_name="rolling_stock", blank=True
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user