More code refactoring, reduce template duplications

This commit is contained in:
2024-12-22 21:32:22 +01:00
parent cbf6c942b9
commit d55bce6e78
14 changed files with 41 additions and 242 deletions

View File

@@ -180,7 +180,7 @@
<li><a class="dropdown-item" href="{% url 'manufacturers' category='model' %}">Manufacturer</a></li>
<li><hr class="dropdown-divider"></li>
<li class="ps-2 text-secondary">Prototype</li>
<li><a class="dropdown-item" href="{% url 'types' %}">Type</a></li>
<li><a class="dropdown-item" href="{% url 'rolling_stock_types' %}">Type</a></li>
<li><a class="dropdown-item" href="{% url 'companies' %}">Company</a></li>
<li><a class="dropdown-item" href="{% url 'manufacturers' category='real' %}">Manufacturer</a></li>
</ul>

View File

@@ -136,7 +136,6 @@
</div>
</div>
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
FIXME: {{ type }}
{% if request.user.is_staff %}<a class="btn btn-sm btn-outline-danger" href="{% dynamic_admin_url 'bookshelf' type book.pk %}">Edit</a>{% endif %}
</div>
</div>

View File

@@ -6,7 +6,7 @@
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3">
{% block cards %}
{% for d in data %}
{% if d.type == "rolling_stock" %}
{% if d.type == "roster" %}
{% include "cards/roster.html" %}
{% elif d.type == "company" %}
{% include "cards/company.html" %}

View File

@@ -1,40 +0,0 @@
{% extends "cards.html" %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
{% if data.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'companies_pagination' page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-left"></i></span>
</li>
{% endif %}
{% for i in page_range %}
{% if data.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == data.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'companies_pagination' page=i %}#main-content">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if data.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'companies_pagination' page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-right"></i></span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

View File

@@ -1,40 +0,0 @@
{% extends "cards.html" %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
{% if data.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'consists_pagination' page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-left"></i></span>
</li>
{% endif %}
{% for i in page_range %}
{% if data.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == data.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'consists_pagination' page=i %}#main-content">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if data.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'consists_pagination' page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-right"></i></span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

View File

@@ -1,4 +1,4 @@
{% extends "roster.html" %}
{% extends "pagination.html" %}
{% block header %}
<div class="text-muted">{{ site_conf.about | safe }}</div>

View File

@@ -1,41 +0,0 @@
{% extends "cards.html" %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
{% if data.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'roster_pagination' page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-left"></i></span>
</li>
{% endif %}
{% for i in page_range %}
{% if data.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == data.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'roster_pagination' page=i %}#main-content">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if data.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'roster_pagination' page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-right"></i></span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

View File

@@ -1,40 +0,0 @@
{% extends "cards.html" %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
{% if data.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'scales_pagination' page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-left"></i></span>
</li>
{% endif %}
{% for i in page_range %}
{% if data.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == data.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'scales_pagination' page=i %}#main-content">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if data.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'scales_pagination' page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-right"></i></span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

View File

@@ -1,40 +0,0 @@
{% extends "cards.html" %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap justify-content-center mt-4 mb-0">
{% if data.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'types_pagination' page=data.previous_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-left"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-left"></i></span>
</li>
{% endif %}
{% for i in page_range %}
{% if data.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == data.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'types_pagination' page=i %}#main-content">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if data.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'types_pagination' page=data.next_page_number %}#main-content" tabindex="-1"><i class="bi bi-chevron-right"></i></a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="bi bi-chevron-right"></i></span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

View File

@@ -15,5 +15,7 @@ def dynamic_admin_url(app_name, model_name, object_id=None):
@register.simple_tag
def dynamic_pagination(model_name, page):
return reverse(f'{model_name}s_pagination', args=[page])
def dynamic_pagination(reverse_name, page):
if reverse_name.endswith('y'):
return reverse(f'{reverse_name[:-1]}ies_pagination', args=[page])
return reverse(f'{reverse_name}s_pagination', args=[page])

View File

@@ -25,7 +25,7 @@ urlpatterns = [
path(
"roster/page/<int:page>",
GetRoster.as_view(),
name="roster_pagination"
name="rosters_pagination"
),
path(
"page/<str:flatpage>",
@@ -34,12 +34,12 @@ urlpatterns = [
),
path(
"consists",
Consists.as_view(template="consists.html"),
Consists.as_view(),
name="consists"
),
path(
"consists/page/<int:page>",
Consists.as_view(template="consists.html"),
Consists.as_view(),
name="consists_pagination"
),
path("consist/<uuid:uuid>", GetConsist.as_view(), name="consist"),
@@ -50,52 +50,52 @@ urlpatterns = [
),
path(
"companies",
Companies.as_view(template="companies.html"),
Companies.as_view(),
name="companies"
),
path(
"companies/page/<int:page>",
Companies.as_view(template="companies.html"),
Companies.as_view(),
name="companies_pagination",
),
path(
"manufacturers/<str:category>",
Manufacturers.as_view(template="manufacturers.html"),
Manufacturers.as_view(template="pagination_manufacturers.html"),
name="manufacturers"
),
path(
"manufacturers/<str:category>/page/<int:page>",
Manufacturers.as_view(template="manufacturers.html"),
Manufacturers.as_view(template="pagination_manufacturers.html"),
name="manufacturers_pagination",
),
path(
"scales",
Scales.as_view(template="scales.html"),
Scales.as_view(),
name="scales"
),
path(
"scales/page/<int:page>",
Scales.as_view(template="scales.html"),
Scales.as_view(),
name="scales_pagination"
),
path(
"types",
Types.as_view(template="types.html"),
name="types"
Types.as_view(),
name="rolling_stock_types"
),
path(
"types/page/<int:page>",
Types.as_view(template="types.html"),
name="types_pagination"
Types.as_view(),
name="rolling_stock_types_pagination"
),
path(
"bookshelf/books",
Books.as_view(template="bookshelf/books.html"),
Books.as_view(),
name="books"
),
path(
"bookshelf/books/page/<int:page>",
Books.as_view(template="bookshelf/books.html"),
Books.as_view(),
name="books_pagination"
),
path(
@@ -105,12 +105,12 @@ urlpatterns = [
),
path(
"bookshelf/catalogs",
Catalogs.as_view(template="bookshelf/books.html"),
Catalogs.as_view(),
name="catalogs"
),
path(
"bookshelf/catalogs/page/<int:page>",
Catalogs.as_view(template="bookshelf/books.html"),
Catalogs.as_view(),
name="catalogs_pagination"
),
path(

View File

@@ -62,8 +62,8 @@ class Render404(View):
class GetData(View):
title = "Home"
template = "roster.html"
item_type = "rolling_stock"
template = "pagination.html"
item_type = "roster"
filter = Q() # empty filter by default
def get_data(self, request):
@@ -78,8 +78,7 @@ class GetData(View):
for item in self.get_data(request):
data.append({"type": self.item_type, "item": item})
paginator = Paginator(data, 3)
# paginator = Paginator(data, get_items_per_page())
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
page_range = paginator.get_elided_page_range(
data.number, on_each_side=1, on_ends=1
@@ -99,8 +98,8 @@ class GetData(View):
class GetRoster(GetData):
title = "Roster"
item_type = "rolling_stock"
title = "The Roster"
item_type = "roster"
def get_data(self, request):
return RollingStock.objects.get_published(request.user).order_by(
@@ -153,14 +152,14 @@ class SearchObjects(View):
# FIXME see if it makes sense to filter calatogs and books by scale
# and manufacturer as well
data = []
rolling_stock = (
roster = (
RollingStock.objects.get_published(request.user)
.filter(query)
.distinct()
.order_by(*get_order_by_field())
)
for item in rolling_stock:
data.append({"type": "rolling_stock", "item": item})
for item in roster:
data.append({"type": "roster", "item": item})
if _filter is None:
consists = (
@@ -247,7 +246,7 @@ class GetManufacturerItem(View):
)
if search != "all":
rolling_stock = get_list_or_404(
roster = get_list_or_404(
RollingStock.objects.get_published(request.user).order_by(
*get_order_by_field()
),
@@ -260,10 +259,10 @@ class GetManufacturerItem(View):
manufacturer,
# all returned records must have the same `item_number``;
# just pick it up the first result, otherwise `search`
rolling_stock[0].item_number if rolling_stock else search,
roster.first.item_number if roster else search,
)
else:
rolling_stock = (
roster = (
RollingStock.objects.get_published(request.user)
.order_by(*get_order_by_field())
.filter(
@@ -274,8 +273,8 @@ class GetManufacturerItem(View):
title = "Manufacturer: {0}".format(manufacturer)
data = []
for item in rolling_stock:
data.append({"type": "rolling_stock", "item": item})
for item in roster:
data.append({"type": "roster", "item": item})
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
@@ -318,7 +317,7 @@ class GetObjectsFiltered(View):
else:
raise Http404
rolling_stock = (
roster = (
RollingStock.objects.get_published(request.user)
.filter(query)
.distinct()
@@ -326,8 +325,8 @@ class GetObjectsFiltered(View):
)
data = []
for item in rolling_stock:
data.append({"type": "rolling_stock", "item": item})
for item in roster:
data.append({"type": "roster", "item": item})
try: # Execute only if query_2nd is defined
consists = (
@@ -452,7 +451,7 @@ class GetConsist(View):
raise Http404
data = [
{
"type": "rolling_stock",
"type": "roster",
"item": RollingStock.objects.get_published(request.user).get(
uuid=r.rolling_stock_id
),