Skip to content

Commit

Permalink
Merge pull request #195 from leolivier:filter-troves-by-category
Browse files Browse the repository at this point in the history
Filter-troves-by-category
  • Loading branch information
leolivier authored Dec 31, 2024
2 parents 81785af + d19953c commit 82b6eac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:

- name: Test django (skip redis needing tests)
run: |
python ./manage.py test --parallel --exclude-tag needs-redis
python ./manage.py test --exclude-tag needs-redis
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
6 changes: 4 additions & 2 deletions cousinsmatter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ def get_page_data(self, page_num, group_by=None):
return page

@staticmethod
def get_page(request, object_list, page_num, reverse_link, compute_link=None, default_page_size=100, group_by=None):
def get_page(request, object_list, page_num, reverse_link=None, compute_link=None, default_page_size=100, group_by=None):
page_size = int(request.GET["page_size"]) if "page_size" in request.GET else default_page_size

ptor = Paginator(object_list, page_size, reverse_link=reverse_link, compute_link=compute_link)
page_num = page_num or ptor.num_pages
if page_num > ptor.num_pages:
raise PageOutOfBounds(ptor._get_link(ptor.num_pages) + '?' + urlencode({'page_size': page_size}))
url = ptor._get_link(ptor.num_pages)
url += ('&' if '?' in url else '?') + urlencode({'page_size': page_size})
raise PageOutOfBounds(url)
return ptor.get_page_data(page_num, group_by=group_by)


Expand Down
23 changes: 23 additions & 0 deletions troves/templates/troves/trove_cave.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
<a class="button ml-6" href="{%url 'troves:create'%}" aria-label="{{create_label}}" title="{{create_label}}">
{%icon "new-treasure" %} <span class="is-hidden-mobile ml-3">{{create_label}}</span>
</a>
<div class="ml-5">
<form method="get" name="select_category">
<div class="control">
<label for="category">{%trans "Filter Treasures" %}:</label>
<select name="category"
value="{{request.GET.category|default:'all'}}">
<option value="all">{%trans "All" %}</option>
{% for category in trove_categories %}
<option value="{{ category.0 }}">{{ category.1 }}</option>
{% endfor %}
</select>
{# Submit "select category" form automatically #}
<script>
$(document).ready(function() {
$('select[name="category"]').val('{{request.GET.category|default:"all"}}');
$('select[name="category"]').change(function() {
$('form[name="select_category"]').submit()
})
});
</script>
</div>
</form>
</div>
</div>
<div class="panel-block is-flex is-align-items-center is-justify-content-center">
{%paginate page%}
Expand Down
13 changes: 10 additions & 3 deletions troves/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from urllib.parse import urlencode
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
Expand All @@ -12,14 +13,20 @@

@login_required
def trove_cave(request, page=1):
treasures = Trove.objects.all().order_by('category', 'id')
category = request.GET.get('category')
if category and category in dict(Trove.CATEGORY_CHOICES).keys():
treasures = Trove.objects.filter(category=category).order_by('id')
def compute_link(idx): return reverse('troves:page', args=[idx]) + '?' + urlencode({'category': category})
else:
treasures = Trove.objects.all().order_by('category', 'id')
def compute_link(idx): return reverse('troves:page', args=[idx])
try:
trove_page = Paginator.get_page(request, object_list=treasures,
page_num=page,
reverse_link='troves:page',
compute_link=compute_link,
default_page_size=settings.DEFAULT_TROVE_PAGE_SIZE,
group_by='category')
return render(request, "troves/trove_cave.html", {"page": trove_page})
return render(request, "troves/trove_cave.html", {"page": trove_page, 'trove_categories': Trove.CATEGORY_CHOICES})
except PageOutOfBounds as exc:
return redirect(exc.redirect_to)

Expand Down

0 comments on commit 82b6eac

Please sign in to comment.