Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor action links for easier template customization and Implement pagination functionality #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/neapolitan/templates/neapolitan/object_list.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{% extends "base.html" %}
{% extends 'base.html' %}
{% load neapolitan %}

{% block content %}

<div class="sm:flex sm:items-center">
<div class="sm:flex sm:items-center">
<h1 class="sm:flex-auto text-base font-semibold leading-6 text-gray-900">{{ object_verbose_name_plural|capfirst }}</h1>
{% if create_view_url %}
<div class="mt-4 sm:ml-16 sm:mt-0 sm:flex-none">
<a class="block rounded-md bg-indigo-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
href="{{ create_view_url }}">Add a new {{ object_verbose_name }}</a>
</div>
<div class="mt-4 sm:ml-16 sm:mt-0 sm:flex-none">
<a class="block rounded-md bg-indigo-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" href="{{ create_view_url }}">Add a new {{ object_verbose_name }}</a>
</div>
{% endif %}
</div>
</div>

{% if object_list %}
{% if object_list %}
{% object_list object_list view %}
{% else %}
{% if is_paginated %}
{% pagination page_obj %}
{% endif %}
{% else %}
<p class="mt-8">There are no {{ object_verbose_name_plural }}. Create one now?</p>
{% endif %}

<hr>
{% endif %}

<hr class="border-t border-gray-300 my-6">
{% endblock %}
6 changes: 4 additions & 2 deletions src/neapolitan/templates/neapolitan/partial/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
{% endif %}
">{{ field }}</td>
{% endfor %}
<td class="py-3.5 px-3 text-right text-sm font-medium [&_a]:text-indigo-600 [&_a:hover]:text-indigo-900">
{{ object.actions }}
<td class="py-3.5 px-3 space-x-3 text-right text-sm font-medium [&_a]:text-indigo-600 [&_a:hover]:text-indigo-900">
{% for action in object.actions %}
<a href="{{ action.URL }}">{{ action.Name }}</a>
{% endfor %}
</td>
</tr>
{% endfor %}
Expand Down
31 changes: 31 additions & 0 deletions src/neapolitan/templates/neapolitan/partial/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<nav aria-label="Page navigation" class="mt-4">
<ul class="flex justify-center space-x-2">
{% if page_obj.has_previous %}
<li>
<a class="px-3 py-2 bg-white border border-gray-300 rounded-md text-sm text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
href="?page={{ page_obj.previous_page_number }}">Previous</a>
</li>
{% endif %}

{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<li>
<a class="px-3 py-2 bg-indigo-600 border border-indigo-600 rounded-md text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
href="#">{{ num }}</a>
</li>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<li>
<a class="px-3 py-2 bg-white border border-gray-300 rounded-md text-sm text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
href="?page={{ num }}">{{ num }}</a>
</li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li>
<a class="px-3 py-2 bg-white border border-gray-300 rounded-md text-sm text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
href="?page={{ page_obj.next_page_number }}">Next</a>
</li>
{% endif %}
</ul>
</nav>
21 changes: 18 additions & 3 deletions src/neapolitan/templatetags/neapolitan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

def action_links(view, object):
actions = [
(url, name)
{"Name": name, "URL": url}
for url, name in [
(Role.DETAIL.maybe_reverse(view, object), "View"),
(Role.UPDATE.maybe_reverse(view, object), "Edit"),
(Role.DELETE.maybe_reverse(view, object), "Delete"),
] if url is not None
]
links = [f"<a href='{url}'>{anchor_text}</a>" for url, anchor_text in actions]
return mark_safe(" | ".join(links))
return actions


@register.inclusion_tag("neapolitan/partial/detail.html")
Expand Down Expand Up @@ -69,3 +68,19 @@ def object_list(objects, view):
"headers": headers,
"object_list": object_list,
}


@register.inclusion_tag("neapolitan/partial/pagination.html")
def pagination(page_obj):
"""
Renders pagination controls for the given page object.

Inclusion tag usage::

{% pagination page_obj %}

Template: ``neapolitan/partial/pagination.html`` - Will render the pagination controls.
"""
return {
"page_obj": page_obj,
}
Loading