mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-03 17:40:39 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d3c099c05b
|
|||
|
903633b5a7
|
|||
|
ee775d737e
|
|||
|
8087ab5997
|
|||
|
1899747909
|
|||
|
0880bd0817
|
|||
|
74d7df2c8b
|
|||
|
c81508bbd5
|
@@ -22,6 +22,7 @@ from bookshelf.models import (
|
||||
Catalog,
|
||||
Magazine,
|
||||
MagazineIssue,
|
||||
TocEntry,
|
||||
)
|
||||
|
||||
|
||||
@@ -58,20 +59,34 @@ class MagazineIssueDocInline(BookDocInline):
|
||||
model = MagazineIssueDocument
|
||||
|
||||
|
||||
class BookTocInline(admin.TabularInline):
|
||||
model = TocEntry
|
||||
min_num = 0
|
||||
extra = 0
|
||||
fields = (
|
||||
"title",
|
||||
"subtitle",
|
||||
"authors",
|
||||
"page",
|
||||
"featured",
|
||||
)
|
||||
|
||||
|
||||
@admin.register(Book)
|
||||
class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
inlines = (
|
||||
BookTocInline,
|
||||
BookPropertyInline,
|
||||
BookImageInline,
|
||||
BookDocInline,
|
||||
)
|
||||
list_display = (
|
||||
"published",
|
||||
"title",
|
||||
"get_authors",
|
||||
"get_publisher",
|
||||
"publication_year",
|
||||
"number_of_pages",
|
||||
"published",
|
||||
)
|
||||
autocomplete_fields = ("authors", "publisher", "shop")
|
||||
readonly_fields = ("invoices", "creation_time", "updated_time")
|
||||
@@ -366,6 +381,7 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
@admin.register(MagazineIssue)
|
||||
class MagazineIssueAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
inlines = (
|
||||
BookTocInline,
|
||||
BookPropertyInline,
|
||||
BookImageInline,
|
||||
MagazineIssueDocInline,
|
||||
|
||||
53
ram/bookshelf/migrations/0030_tocentry.py
Normal file
53
ram/bookshelf/migrations/0030_tocentry.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# Generated by Django 6.0 on 2025-12-29 11:02
|
||||
|
||||
import django.db.models.deletion
|
||||
import tinymce.models
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("bookshelf", "0029_alter_catalog_manufacturer_alter_catalog_scales"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="TocEntry",
|
||||
fields=[
|
||||
(
|
||||
"uuid",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("description", tinymce.models.HTMLField(blank=True)),
|
||||
("notes", tinymce.models.HTMLField(blank=True)),
|
||||
("creation_time", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_time", models.DateTimeField(auto_now=True)),
|
||||
("published", models.BooleanField(default=True)),
|
||||
("title", models.CharField(max_length=200)),
|
||||
("subtitle", models.CharField(blank=True, max_length=200)),
|
||||
("authors", models.CharField(blank=True, max_length=256)),
|
||||
("page", models.SmallIntegerField()),
|
||||
("featured", models.BooleanField(default=False)),
|
||||
(
|
||||
"book",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="toc",
|
||||
to="bookshelf.basebook",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Table of Contents Entry",
|
||||
"verbose_name_plural": "Table of Contents Entries",
|
||||
"ordering": ["page"],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -191,6 +191,15 @@ class Magazine(BaseModel):
|
||||
def get_absolute_url(self):
|
||||
return reverse("magazine", kwargs={"uuid": self.uuid})
|
||||
|
||||
def get_cover(self):
|
||||
if self.image:
|
||||
return self.image
|
||||
else:
|
||||
cover_issue = self.issue.filter(published=True).first()
|
||||
if cover_issue and cover_issue.image.exists():
|
||||
return cover_issue.image.first().image
|
||||
return None
|
||||
|
||||
def website_short(self):
|
||||
if self.website:
|
||||
return urlparse(self.website).netloc.replace("www.", "")
|
||||
@@ -239,3 +248,35 @@ class MagazineIssue(BaseBook):
|
||||
return reverse(
|
||||
"issue", kwargs={"uuid": self.uuid, "magazine": self.magazine.uuid}
|
||||
)
|
||||
|
||||
|
||||
class TocEntry(BaseModel):
|
||||
book = models.ForeignKey(
|
||||
BaseBook, on_delete=models.CASCADE, related_name="toc"
|
||||
)
|
||||
title = models.CharField(max_length=200)
|
||||
subtitle = models.CharField(max_length=200, blank=True)
|
||||
authors = models.CharField(max_length=256, blank=True)
|
||||
page = models.SmallIntegerField()
|
||||
featured = models.BooleanField(
|
||||
default=False,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ["page"]
|
||||
verbose_name = "Table of Contents Entry"
|
||||
verbose_name_plural = "Table of Contents Entries"
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title} (p. {self.page})"
|
||||
|
||||
def clean(self):
|
||||
if self.page < 1:
|
||||
raise ValidationError("Page number is invalid.")
|
||||
try:
|
||||
if self.page > self.book.number_of_pages:
|
||||
raise ValidationError(
|
||||
"Page number exceeds the publication's number of pages."
|
||||
)
|
||||
except TypeError:
|
||||
pass # number_of_pages is None
|
||||
|
||||
@@ -49,10 +49,12 @@
|
||||
<div class="mx-auto">
|
||||
<nav class="nav nav-tabs d-none d-lg-flex flex-row mb-2" id="nav-tab" role="tablist">
|
||||
<button class="nav-link active" id="nav-summary-tab" data-bs-toggle="tab" data-bs-target="#nav-summary" type="button" role="tab" aria-controls="nav-summary" aria-selected="true">Summary</button>
|
||||
{% if data.toc.all %}<button class="nav-link" id="nav-toc-tab" data-bs-toggle="tab" data-bs-target="#nav-toc" type="button" role="tab" aria-controls="nav-toc" aria-selected="true">Table of contents</button>{% endif %}
|
||||
{% if documents %}<button class="nav-link" id="nav-documents-tab" data-bs-toggle="tab" data-bs-target="#nav-documents" type="button" role="tab" aria-controls="nav-documents" aria-selected="false">Documents</button>{% endif %}
|
||||
</nav>
|
||||
<select class="form-select d-lg-none mb-2" id="tabSelector" aria-label="Tab selector">
|
||||
<option value="nav-summary" selected>Summary</option>
|
||||
{% if data.toc.all %}<option value="nav-toc">Table of contents</option>{% endif %}
|
||||
{% if documents %}<option value="nav-documents">Documents</option>{% endif %}
|
||||
</select>
|
||||
<div class="tab-content" id="nav-tabContent">
|
||||
@@ -189,6 +191,30 @@
|
||||
</table>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="tab-pane" id="nav-toc" role="tabpanel" aria-labelledby="nav-toc-tab">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="row">Title</th>
|
||||
<th scope="row">Subtitle</th>
|
||||
<th scope="row">Authors</th>
|
||||
<th scope="row">Page</th>
|
||||
<th scope="row"><abbr title="Featured article"><i class="bi bi-star-fill"></i></abbr></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="table-group-divider">
|
||||
{% for toc in data.toc.all %}
|
||||
<tr>
|
||||
<td class="w-33">{{ toc.title }}</td>
|
||||
<td class="w-33">{{ toc.subtitle }}</td>
|
||||
<td>{{ toc.authors }}</td>
|
||||
<td>{{ toc.page }}</td>
|
||||
<td>{% if toc.featured %}<abbr title="Featured article"><i class="bi bi-star-fill text-warning"></i></abbr>{% endif %}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="tab-pane" id="nav-documents" role="tabpanel" aria-labelledby="nav-documents-tab">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
||||
@@ -4,12 +4,8 @@
|
||||
<div class="card shadow-sm">
|
||||
{% if d.obj_type == "magazine" %}
|
||||
<a href="{{ d.get_absolute_url }}">
|
||||
{% if d.image and d.obj_type == "magazine" %}
|
||||
<img class="card-img-top" src="{{ d.image.url }}" alt="{{ d }}">
|
||||
{% elif d.issue.first.image.exists %}
|
||||
{% with d.issue.first as i %}
|
||||
<img class="card-img-top" src="{{ i.image.first.image.url }}" alt="{{ d }}">
|
||||
{% endwith %}
|
||||
{% if d.get_cover %}
|
||||
<img class="card-img-top" src="{{ d.get_cover.url }}" alt="{{ d }}">
|
||||
{% else %}
|
||||
<!-- Do not show the "Coming soon" image when running in a single card column mode (e.g. on mobile) -->
|
||||
<img class="card-img-top d-none d-sm-block" src="{% static DEFAULT_CARD_IMAGE %}" alt="{{ d }}">
|
||||
|
||||
@@ -3,12 +3,19 @@
|
||||
|
||||
<div class="col">
|
||||
<div class="card shadow-sm">
|
||||
{% if d.image.exists %}
|
||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top" src="{{ d.image.first.image.url }}" alt="{{ d }}"></a>
|
||||
{% else %}
|
||||
<!-- Do not show the "Coming soon" image when running in a single card column mode (e.g. on mobile) -->
|
||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top d-none d-sm-block" src="{% static DEFAULT_CARD_IMAGE %}" alt="{{ d }}"></a>
|
||||
{% endif %}
|
||||
<div id="card-img-container" class="position-relative">
|
||||
{% if d.featured %}
|
||||
<span class="position-absolute translate-middle top-0 start-0 m-3 text-danger">
|
||||
<abbr title="Featured item"><i class="bi bi-heart-fill"></i></abbr>
|
||||
</span>
|
||||
{% endif %}
|
||||
{% if d.image.exists %}
|
||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top" src="{{ d.image.first.image.url }}" alt="{{ d }}"></a>
|
||||
{% else %}
|
||||
<!-- Do not show the "Coming soon" image when running in a single card column mode (e.g. on mobile) -->
|
||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top d-none d-sm-block" src="{% static DEFAULT_CARD_IMAGE %}" alt="{{ d }}"></a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text" style="position: relative;">
|
||||
<strong>{{ d }}</strong>
|
||||
|
||||
@@ -196,6 +196,7 @@ class SearchObjects(View):
|
||||
Q(
|
||||
Q(title__icontains=search)
|
||||
| Q(description__icontains=search)
|
||||
| Q(toc__title__icontains=search)
|
||||
)
|
||||
)
|
||||
.distinct()
|
||||
@@ -217,6 +218,7 @@ class SearchObjects(View):
|
||||
Q(
|
||||
Q(magazine__name__icontains=search)
|
||||
| Q(description__icontains=search)
|
||||
| Q(toc__title__icontains=search)
|
||||
)
|
||||
)
|
||||
.distinct()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from ram.utils import git_suffix
|
||||
|
||||
__version__ = "0.18.9"
|
||||
__version__ = "0.19.3"
|
||||
__version__ += git_suffix(__file__)
|
||||
|
||||
@@ -1,22 +1,60 @@
|
||||
from django.contrib import admin
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.core.cache import cache
|
||||
|
||||
admin.site.site_header = settings.SITE_NAME
|
||||
|
||||
|
||||
def publish(modeladmin, request, queryset):
|
||||
for obj in queryset:
|
||||
obj.published = True
|
||||
obj.save()
|
||||
queryset.update(published=True)
|
||||
cache.clear()
|
||||
|
||||
|
||||
publish.short_description = "Publish selected items"
|
||||
|
||||
|
||||
def unpublish(modeladmin, request, queryset):
|
||||
for obj in queryset:
|
||||
obj.published = False
|
||||
obj.save()
|
||||
queryset.update(published=False)
|
||||
cache.clear()
|
||||
|
||||
|
||||
unpublish.short_description = "Unpublish selected items"
|
||||
|
||||
|
||||
def set_featured(modeladmin, request, queryset):
|
||||
count = queryset.count()
|
||||
if count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"You can only mark up to {} items as featured.".format(
|
||||
settings.FEATURED_ITEMS_MAX
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
featured = modeladmin.model.objects.filter(featured=True).count()
|
||||
if featured + count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"There are already {} featured items. You can only mark {} more items as featured.".format( # noqa: E501
|
||||
featured,
|
||||
settings.FEATURED_ITEMS_MAX - featured,
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
queryset.update(featured=True)
|
||||
cache.clear()
|
||||
|
||||
|
||||
set_featured.short_description = "Mark selected items as featured"
|
||||
|
||||
|
||||
def unset_featured(modeladmin, request, queryset):
|
||||
queryset.update(featured=False)
|
||||
cache.clear()
|
||||
|
||||
|
||||
unset_featured.short_description = (
|
||||
"Unmark selected items as featured"
|
||||
)
|
||||
|
||||
@@ -6,8 +6,8 @@ from django.utils.html import format_html, format_html_join, strip_tags
|
||||
|
||||
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
||||
|
||||
from ram.admin import publish, unpublish
|
||||
from ram.utils import generate_csv
|
||||
from ram.admin import publish, unpublish, set_featured, unset_featured
|
||||
from repository.models import RollingStockDocument
|
||||
from portal.utils import get_site_conf
|
||||
from roster.models import (
|
||||
@@ -303,37 +303,4 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
|
||||
download_csv.short_description = "Download selected items as CSV"
|
||||
|
||||
def set_featured(modeladmin, request, queryset):
|
||||
count = queryset.count()
|
||||
if count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"You can only mark up to {} items as featured.".format(
|
||||
settings.FEATURED_ITEMS_MAX
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
featured = RollingStock.objects.filter(featured=True).count()
|
||||
if featured + count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"There are already {} featured items. You can only mark {} more items as featured.".format( # noqa: E501
|
||||
featured,
|
||||
settings.FEATURED_ITEMS_MAX - featured,
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
queryset.update(featured=True)
|
||||
|
||||
set_featured.short_description = "Mark selected rolling stock as featured"
|
||||
|
||||
def unset_featured(modeladmin, request, queryset):
|
||||
queryset.update(featured=False)
|
||||
|
||||
unset_featured.short_description = (
|
||||
"Unmark selected rolling stock as featured"
|
||||
)
|
||||
|
||||
actions = [publish, unpublish, set_featured, unset_featured, download_csv]
|
||||
|
||||
@@ -175,7 +175,12 @@ class RollingStock(BaseModel):
|
||||
def clean(self, *args, **kwargs):
|
||||
if self.featured:
|
||||
MAX = settings.FEATURED_ITEMS_MAX
|
||||
if RollingStock.objects.filter(featured=True).count() > MAX - 1:
|
||||
featured_count = (
|
||||
RollingStock.objects.filter(featured=True)
|
||||
.exclude(uuid=self.uuid)
|
||||
.count()
|
||||
)
|
||||
if featured_count > MAX - 1:
|
||||
raise ValidationError(
|
||||
"There are already {} featured items".format(MAX)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user