Add Catalogs views, but still need to fix templates (use books for now)

This commit is contained in:
2024-11-29 23:43:36 +01:00
parent 1a3b30ace3
commit 83444266cb
6 changed files with 58 additions and 7 deletions

View File

@@ -139,4 +139,4 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
@admin.display(description="Scales")
def get_scales(self, obj):
return ", ".join(s.scale for s in obj.scales.all())
return "/".join(s.scale for s in obj.scales.all())

View File

@@ -54,9 +54,6 @@ class BaseBook(BaseModel):
Tag, related_name="bookshelf", blank=True
)
def get_absolute_url(self):
return reverse("book", kwargs={"uuid": self.uuid})
def delete(self, *args, **kwargs):
shutil.rmtree(
os.path.join(
@@ -110,6 +107,9 @@ class Book(BaseBook):
def publisher_name(self):
return self.publisher.name
def get_absolute_url(self):
return reverse("book", kwargs={"uuid": self.uuid})
class Catalog(BaseBook):
manufacturer = models.ForeignKey(
@@ -127,3 +127,6 @@ class Catalog(BaseBook):
def __str__(self):
scales = "/".join([s.scale for s in self.scales.all()])
return "%s %s %s" % (self.manufacturer.name, self.years, scales)
def get_absolute_url(self):
return reverse("catalog", kwargs={"uuid": self.uuid})

View File

@@ -5,6 +5,7 @@
</a>
<ul class="dropdown-menu" aria-labelledby="bookshelfDropdownMenuLink">
<li><a class="dropdown-item" href="{% url 'books' %}">Books</a></li>
<li><a class="dropdown-item" href="{% url 'catalogs' %}">Catalogs</a></li>
</ul>
</li>
{% endif %}

View File

@@ -1,13 +1,15 @@
from django import template
from portal.models import Flatpage
from bookshelf.models import Book
from bookshelf.models import Book, Catalog
register = template.Library()
@register.inclusion_tag('bookshelf/bookshelf_menu.html')
def show_bookshelf_menu():
return {"bookshelf_menu": Book.objects.exists()}
return {
"bookshelf_menu": (Book.objects.exists() or Catalog.objects.exists())
}
@register.inclusion_tag('flatpages/flatpages_menu.html')

View File

@@ -15,6 +15,8 @@ from portal.views import (
Types,
Books,
GetBook,
Catalogs,
GetCatalog,
SearchObjects,
)
@@ -98,6 +100,21 @@ urlpatterns = [
name="books_pagination"
),
path("bookshelf/book/<uuid:uuid>", GetBook.as_view(), name="book"),
path(
"bookshelf/catalogs",
Catalogs.as_view(template="bookshelf/books.html"),
name="catalogs"
),
path(
"bookshelf/catalogs/page/<int:page>",
Catalogs.as_view(template="bookshelf/books.html"),
name="catalogs_pagination"
),
path(
"bookshelf/catalog/<uuid:uuid>",
GetCatalog.as_view(),
name="catalog"
),
path(
"search",
SearchObjects.as_view(http_method_names=["post"]),

View File

@@ -15,7 +15,7 @@ from portal.utils import get_site_conf
from portal.models import Flatpage
from roster.models import RollingStock
from consist.models import Consist
from bookshelf.models import Book
from bookshelf.models import Book, Catalog
from metadata.models import (
Company,
Manufacturer,
@@ -536,6 +536,34 @@ class GetBook(View):
)
class Catalogs(GetData):
title = "Catalogs"
item_type = "book"
def get_data(self, request):
return Catalog.objects.get_published(request.user).all()
class GetCatalog(View):
def get(self, request, uuid):
try:
catalog = Catalog.objects.get_published(request.user).get(uuid=uuid)
except ObjectDoesNotExist:
raise Http404
catalog_properties = catalog.property.get_public(request.user)
return render(
request,
"bookshelf/book.html",
{
"title": catalog,
"catalog_properties": catalog_properties,
"book": catalog,
},
)
class GetFlatpage(View):
def get(self, request, flatpage):
try: