mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-04 10:00:40 +01:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d3c099c05b
|
|||
|
903633b5a7
|
|||
|
ee775d737e
|
|||
|
8087ab5997
|
|||
|
1899747909
|
|||
|
0880bd0817
|
|||
|
74d7df2c8b
|
|||
|
c81508bbd5
|
|||
|
b4f69d8a34
|
@@ -22,6 +22,7 @@ from bookshelf.models import (
|
|||||||
Catalog,
|
Catalog,
|
||||||
Magazine,
|
Magazine,
|
||||||
MagazineIssue,
|
MagazineIssue,
|
||||||
|
TocEntry,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -58,20 +59,34 @@ class MagazineIssueDocInline(BookDocInline):
|
|||||||
model = MagazineIssueDocument
|
model = MagazineIssueDocument
|
||||||
|
|
||||||
|
|
||||||
|
class BookTocInline(admin.TabularInline):
|
||||||
|
model = TocEntry
|
||||||
|
min_num = 0
|
||||||
|
extra = 0
|
||||||
|
fields = (
|
||||||
|
"title",
|
||||||
|
"subtitle",
|
||||||
|
"authors",
|
||||||
|
"page",
|
||||||
|
"featured",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Book)
|
@admin.register(Book)
|
||||||
class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||||
inlines = (
|
inlines = (
|
||||||
|
BookTocInline,
|
||||||
BookPropertyInline,
|
BookPropertyInline,
|
||||||
BookImageInline,
|
BookImageInline,
|
||||||
BookDocInline,
|
BookDocInline,
|
||||||
)
|
)
|
||||||
list_display = (
|
list_display = (
|
||||||
"published",
|
|
||||||
"title",
|
"title",
|
||||||
"get_authors",
|
"get_authors",
|
||||||
"get_publisher",
|
"get_publisher",
|
||||||
"publication_year",
|
"publication_year",
|
||||||
"number_of_pages",
|
"number_of_pages",
|
||||||
|
"published",
|
||||||
)
|
)
|
||||||
autocomplete_fields = ("authors", "publisher", "shop")
|
autocomplete_fields = ("authors", "publisher", "shop")
|
||||||
readonly_fields = ("invoices", "creation_time", "updated_time")
|
readonly_fields = ("invoices", "creation_time", "updated_time")
|
||||||
@@ -366,6 +381,7 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
@admin.register(MagazineIssue)
|
@admin.register(MagazineIssue)
|
||||||
class MagazineIssueAdmin(SortableAdminBase, admin.ModelAdmin):
|
class MagazineIssueAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||||
inlines = (
|
inlines = (
|
||||||
|
BookTocInline,
|
||||||
BookPropertyInline,
|
BookPropertyInline,
|
||||||
BookImageInline,
|
BookImageInline,
|
||||||
MagazineIssueDocInline,
|
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):
|
def get_absolute_url(self):
|
||||||
return reverse("magazine", kwargs={"uuid": self.uuid})
|
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):
|
def website_short(self):
|
||||||
if self.website:
|
if self.website:
|
||||||
return urlparse(self.website).netloc.replace("www.", "")
|
return urlparse(self.website).netloc.replace("www.", "")
|
||||||
@@ -239,3 +248,35 @@ class MagazineIssue(BaseBook):
|
|||||||
return reverse(
|
return reverse(
|
||||||
"issue", kwargs={"uuid": self.uuid, "magazine": self.magazine.uuid}
|
"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">
|
<div class="mx-auto">
|
||||||
<nav class="nav nav-tabs d-none d-lg-flex flex-row mb-2" id="nav-tab" role="tablist">
|
<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>
|
<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 %}
|
{% 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>
|
</nav>
|
||||||
<select class="form-select d-lg-none mb-2" id="tabSelector" aria-label="Tab selector">
|
<select class="form-select d-lg-none mb-2" id="tabSelector" aria-label="Tab selector">
|
||||||
<option value="nav-summary" selected>Summary</option>
|
<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 %}
|
{% if documents %}<option value="nav-documents">Documents</option>{% endif %}
|
||||||
</select>
|
</select>
|
||||||
<div class="tab-content" id="nav-tabContent">
|
<div class="tab-content" id="nav-tabContent">
|
||||||
@@ -189,6 +191,30 @@
|
|||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</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">
|
<div class="tab-pane" id="nav-documents" role="tabpanel" aria-labelledby="nav-documents-tab">
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
@@ -4,12 +4,8 @@
|
|||||||
<div class="card shadow-sm">
|
<div class="card shadow-sm">
|
||||||
{% if d.obj_type == "magazine" %}
|
{% if d.obj_type == "magazine" %}
|
||||||
<a href="{{ d.get_absolute_url }}">
|
<a href="{{ d.get_absolute_url }}">
|
||||||
{% if d.image and d.obj_type == "magazine" %}
|
{% if d.get_cover %}
|
||||||
<img class="card-img-top" src="{{ d.image.url }}" alt="{{ d }}">
|
<img class="card-img-top" src="{{ d.get_cover.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 %}
|
|
||||||
{% else %}
|
{% else %}
|
||||||
<!-- Do not show the "Coming soon" image when running in a single card column mode (e.g. on mobile) -->
|
<!-- 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 }}">
|
<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="col">
|
||||||
<div class="card shadow-sm">
|
<div class="card shadow-sm">
|
||||||
{% if d.image.exists %}
|
<div id="card-img-container" class="position-relative">
|
||||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top" src="{{ d.image.first.image.url }}" alt="{{ d }}"></a>
|
{% if d.featured %}
|
||||||
{% else %}
|
<span class="position-absolute translate-middle top-0 start-0 m-3 text-danger">
|
||||||
<!-- Do not show the "Coming soon" image when running in a single card column mode (e.g. on mobile) -->
|
<abbr title="Featured item"><i class="bi bi-heart-fill"></i></abbr>
|
||||||
<a href="{{d.get_absolute_url}}"><img class="card-img-top d-none d-sm-block" src="{% static DEFAULT_CARD_IMAGE %}" alt="{{ d }}"></a>
|
</span>
|
||||||
{% endif %}
|
{% 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">
|
<div class="card-body">
|
||||||
<p class="card-text" style="position: relative;">
|
<p class="card-text" style="position: relative;">
|
||||||
<strong>{{ d }}</strong>
|
<strong>{{ d }}</strong>
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
|
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
|
||||||
{% if data.has_previous %}
|
{% if data.has_previous %}
|
||||||
<li class="page-item">
|
<li class="page-item">
|
||||||
<a class="page-link" href="{% url 'magazine_pagination' uuid=magazine.uuid page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
|
<a class="page-link" href="{% url 'magazine' uuid=magazine.uuid page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
|
||||||
</li>
|
</li>
|
||||||
{% else %}
|
{% else %}
|
||||||
<li class="page-item disabled">
|
<li class="page-item disabled">
|
||||||
@@ -48,13 +48,13 @@
|
|||||||
{% if i == data.paginator.ELLIPSIS %}
|
{% if i == data.paginator.ELLIPSIS %}
|
||||||
<li class="page-item"><span class="page-link">{{ i }}</span></li>
|
<li class="page-item"><span class="page-link">{{ i }}</span></li>
|
||||||
{% else %}
|
{% else %}
|
||||||
<li class="page-item"><a class="page-link" href="{% url 'magazine_pagination' uuid=magazine.uuid page=i %}#main-content">{{ i }}</a></li>
|
<li class="page-item"><a class="page-link" href="{% url 'magazine' uuid=magazine.uuid page=i %}#main-content">{{ i }}</a></li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% if data.has_next %}
|
{% if data.has_next %}
|
||||||
<li class="page-item">
|
<li class="page-item">
|
||||||
<a class="page-link" href="{% url 'magazine_pagination' uuid=magazine.uuid page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
|
<a class="page-link" href="{% url 'magazine' uuid=magazine.uuid page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
|
||||||
</li>
|
</li>
|
||||||
{% else %}
|
{% else %}
|
||||||
<li class="page-item disabled">
|
<li class="page-item disabled">
|
||||||
|
|||||||
@@ -196,6 +196,7 @@ class SearchObjects(View):
|
|||||||
Q(
|
Q(
|
||||||
Q(title__icontains=search)
|
Q(title__icontains=search)
|
||||||
| Q(description__icontains=search)
|
| Q(description__icontains=search)
|
||||||
|
| Q(toc__title__icontains=search)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
@@ -217,6 +218,7 @@ class SearchObjects(View):
|
|||||||
Q(
|
Q(
|
||||||
Q(magazine__name__icontains=search)
|
Q(magazine__name__icontains=search)
|
||||||
| Q(description__icontains=search)
|
| Q(description__icontains=search)
|
||||||
|
| Q(toc__title__icontains=search)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from ram.utils import git_suffix
|
from ram.utils import git_suffix
|
||||||
|
|
||||||
__version__ = "0.18.8"
|
__version__ = "0.19.3"
|
||||||
__version__ += git_suffix(__file__)
|
__version__ += git_suffix(__file__)
|
||||||
|
|||||||
@@ -1,22 +1,60 @@
|
|||||||
from django.contrib import admin
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
admin.site.site_header = settings.SITE_NAME
|
admin.site.site_header = settings.SITE_NAME
|
||||||
|
|
||||||
|
|
||||||
def publish(modeladmin, request, queryset):
|
def publish(modeladmin, request, queryset):
|
||||||
for obj in queryset:
|
queryset.update(published=True)
|
||||||
obj.published = True
|
cache.clear()
|
||||||
obj.save()
|
|
||||||
|
|
||||||
|
|
||||||
publish.short_description = "Publish selected items"
|
publish.short_description = "Publish selected items"
|
||||||
|
|
||||||
|
|
||||||
def unpublish(modeladmin, request, queryset):
|
def unpublish(modeladmin, request, queryset):
|
||||||
for obj in queryset:
|
queryset.update(published=False)
|
||||||
obj.published = False
|
cache.clear()
|
||||||
obj.save()
|
|
||||||
|
|
||||||
|
|
||||||
unpublish.short_description = "Unpublish selected items"
|
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 adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
||||||
|
|
||||||
from ram.admin import publish, unpublish
|
|
||||||
from ram.utils import generate_csv
|
from ram.utils import generate_csv
|
||||||
|
from ram.admin import publish, unpublish, set_featured, unset_featured
|
||||||
from repository.models import RollingStockDocument
|
from repository.models import RollingStockDocument
|
||||||
from portal.utils import get_site_conf
|
from portal.utils import get_site_conf
|
||||||
from roster.models import (
|
from roster.models import (
|
||||||
@@ -303,37 +303,4 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
|
|
||||||
download_csv.short_description = "Download selected items as CSV"
|
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]
|
actions = [publish, unpublish, set_featured, unset_featured, download_csv]
|
||||||
|
|||||||
@@ -175,7 +175,12 @@ class RollingStock(BaseModel):
|
|||||||
def clean(self, *args, **kwargs):
|
def clean(self, *args, **kwargs):
|
||||||
if self.featured:
|
if self.featured:
|
||||||
MAX = settings.FEATURED_ITEMS_MAX
|
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(
|
raise ValidationError(
|
||||||
"There are already {} featured items".format(MAX)
|
"There are already {} featured items".format(MAX)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user