Fix pagination on matmat, don't allow empty matmat search and add htmx pagination

This commit is contained in:
2025-12-17 00:05:18 +01:00
parent f24e39ccb7
commit a2243e5e9a
6 changed files with 52 additions and 10 deletions

View File

@@ -16,13 +16,14 @@
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from typing import Any
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from core.models import User
@@ -46,3 +47,8 @@ class SearchForm(forms.ModelForm):
self.fields[key].required = False
if key not in initial:
self.fields[key].initial = None
def clean(self):
data = self.cleaned_data
if all(data[key] in self.fields[key].empty_values for key in self.fields):
raise ValidationError(_("Empty search"))

View File

@@ -1,6 +1,11 @@
{% extends "core/base.jinja" %}
{% from "core/macros.jinja" import user_mini_profile, paginate_jinja %}
{% from "core/macros.jinja" import user_mini_profile, paginate_jinja with context %}
{% if is_fragment %}
{% extends "core/base_fragment.jinja" %}
{% else %}
{% extends "core/base.jinja" %}
{% endif %}
{% from "core/macros.jinja" import user_mini_profile, paginate_htmx with context %}
{% block title %}
{% trans %}Search user{% endtrans %}
@@ -20,7 +25,7 @@
{% endfor %}
</div>
{% if page_obj.has_other_pages() %}
{{ paginate_jinja(page_obj, paginator) }}
{{ paginate_htmx(page_obj, paginator) }}
{% endif %}
<hr>
{% endif %}

View File

@@ -33,3 +33,27 @@ class TestMatmatronch(TestCase):
)
assert response.status_code == 200
assert list(response.context_data["object_list"]) == [self.users[2]]
def test_empty_search(self):
self.client.force_login(subscriber_user.make())
response = self.client.get(reverse("matmat:search"))
assert response.status_code == 200
assert list(response.context_data["object_list"]) == []
assert not response.context_data["form"].is_valid()
response = self.client.get(
reverse(
"matmat:search",
query={
"promo": "",
"role": "",
"department": "",
"semester": "",
"date_of_birth": "",
},
)
)
assert response.status_code == 200
assert list(response.context_data["object_list"]) == []
assert not response.context_data["form"].is_valid()
assert "Empty search" in response.context_data["form"].non_field_errors()

View File

@@ -16,7 +16,7 @@
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
@@ -28,10 +28,11 @@ from django.views.generic.edit import FormMixin
from core.auth.mixins import FormerSubscriberMixin
from core.models import User, UserQuerySet
from core.schemas import UserFilterSchema
from core.views.mixins import AllowFragment
from matmat.forms import SearchForm
class MatmatronchView(FormerSubscriberMixin, FormMixin, ListView):
class MatmatronchView(AllowFragment, FormerSubscriberMixin, FormMixin, ListView):
model = User
paginate_by = 20
template_name = "matmat/search_form.jinja"