Add pagination to map table in user dashboard

This commit is contained in:
Yohan Boniface 2023-07-12 10:27:22 +02:00
parent 7adc409cdb
commit 6b539b9d40
2 changed files with 26 additions and 32 deletions

View file

@ -1,5 +1,6 @@
{% load umap_tags umap_tags i18n %} {% load umap_tags umap_tags i18n %}
<table class="maps"> <table class="maps">
{% if not is_ajax %}
<thead> <thead>
<tr> <tr>
<th>{% blocktrans %}Map{% endblocktrans %}</th> <th>{% blocktrans %}Map{% endblocktrans %}</th>
@ -10,6 +11,7 @@
<th>{% blocktrans %}Actions{% endblocktrans %}</th> <th>{% blocktrans %}Actions{% endblocktrans %}</th>
</tr> </tr>
</thead> </thead>
{% endif %}
<tbody> <tbody>
{% for map_inst in maps %} {% for map_inst in maps %}
<tr> <tr>
@ -31,3 +33,9 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
{% if maps.has_next %}
<div class="col wide">
<a href="?{% paginate_querystring maps.next_page_number %}"
class="button more_button neutral">{% trans "More" %}</a>
</div>
{% endif %}

View file

@ -77,7 +77,7 @@ PRIVATE_IP = re.compile(
ANONYMOUS_COOKIE_MAX_AGE = 60 * 60 * 24 * 30 # One month ANONYMOUS_COOKIE_MAX_AGE = 60 * 60 * 24 * 30 # One month
class PaginatorMixin(object): class PaginatorMixin:
per_page = 5 per_page = 5
def paginate(self, qs, per_page=None): def paginate(self, qs, per_page=None):
@ -94,6 +94,18 @@ class PaginatorMixin(object):
qs = paginator.page(paginator.num_pages) qs = paginator.page(paginator.num_pages)
return qs return qs
def get_context_data(self, **kwargs):
kwargs.update({"is_ajax": is_ajax(self.request)})
return super().get_context_data(**kwargs)
def get_template_names(self):
"""
Dispatch template according to the kind of request: ajax or normal.
"""
if is_ajax(self.request):
return [self.list_template_name]
return super().get_template_names()
class PublicMapsMixin(object): class PublicMapsMixin(object):
def get_public_maps(self): def get_public_maps(self):
@ -108,7 +120,7 @@ class PublicMapsMixin(object):
return maps return maps
class Home(TemplateView, PublicMapsMixin, PaginatorMixin): class Home(PaginatorMixin, TemplateView, PublicMapsMixin):
template_name = "umap/home.html" template_name = "umap/home.html"
list_template_name = "umap/map_list.html" list_template_name = "umap/map_list.html"
@ -141,15 +153,6 @@ class Home(TemplateView, PublicMapsMixin, PaginatorMixin):
"showcase_map": showcase_map, "showcase_map": showcase_map,
} }
def get_template_names(self):
"""
Dispatch template according to the kind of request: ajax or normal.
"""
if is_ajax(self.request):
return [self.list_template_name]
else:
return [self.template_name]
home = Home.as_view() home = Home.as_view()
@ -161,7 +164,7 @@ class About(Home):
about = About.as_view() about = About.as_view()
class UserMaps(DetailView, PaginatorMixin): class UserMaps(PaginatorMixin, DetailView):
model = User model = User
slug_url_kwarg = "identifier" slug_url_kwarg = "identifier"
slug_field = settings.USER_URL_FIELD slug_field = settings.USER_URL_FIELD
@ -189,15 +192,6 @@ class UserMaps(DetailView, PaginatorMixin):
kwargs.update({"maps": self.paginate(self.get_maps(), self.per_page)}) kwargs.update({"maps": self.paginate(self.get_maps(), self.per_page)})
return super().get_context_data(**kwargs) return super().get_context_data(**kwargs)
def get_template_names(self):
"""
Dispatch template according to the kind of request: ajax or normal.
"""
if is_ajax(self.request):
return [self.list_template_name]
else:
return super(UserMaps, self).get_template_names()
user_maps = UserMaps.as_view() user_maps = UserMaps.as_view()
@ -226,7 +220,7 @@ class SearchMixin:
return Map.objects.annotate(search=vector).filter(search=query) return Map.objects.annotate(search=vector).filter(search=query)
class Search(TemplateView, PublicMapsMixin, PaginatorMixin, SearchMixin): class Search(PaginatorMixin, TemplateView, PublicMapsMixin, SearchMixin):
template_name = "umap/search.html" template_name = "umap/search.html"
list_template_name = "umap/map_list.html" list_template_name = "umap/map_list.html"
@ -243,15 +237,6 @@ class Search(TemplateView, PublicMapsMixin, PaginatorMixin, SearchMixin):
kwargs.update({"maps": results, "count": qs_count}) kwargs.update({"maps": results, "count": qs_count})
return kwargs return kwargs
def get_template_names(self):
"""
Dispatch template according to the kind of request: ajax or normal.
"""
if is_ajax(self.request):
return [self.list_template_name]
else:
return super(Search, self).get_template_names()
@property @property
def per_page(self): def per_page(self):
return settings.UMAP_MAPS_PER_SEARCH return settings.UMAP_MAPS_PER_SEARCH
@ -260,9 +245,10 @@ class Search(TemplateView, PublicMapsMixin, PaginatorMixin, SearchMixin):
search = Search.as_view() search = Search.as_view()
class UserDashboard(DetailView, PaginatorMixin, SearchMixin): class UserDashboard(PaginatorMixin, DetailView, SearchMixin):
model = User model = User
template_name = "umap/user_dashboard.html" template_name = "umap/user_dashboard.html"
list_template_name = "umap/map_table.html"
def get_object(self): def get_object(self):
return self.get_queryset().get(pk=self.request.user.pk) return self.get_queryset().get(pk=self.request.user.pk)