# HG changeset patch # User durandn # Date 1480413038 -3600 # Node ID d00d1089b2c8792e046a5fddbe99858cf5b18499 # Parent c3ff8c5b28e6ff8499dfa3d113e9c3cc925c24b7 adapted collection_home pagination to higher page count diff -r c3ff8c5b28e6 -r d00d1089b2c8 src/iconolab/templates/iconolab/collection_home.html --- a/src/iconolab/templates/iconolab/collection_home.html Mon Nov 28 14:34:07 2016 +0100 +++ b/src/iconolab/templates/iconolab/collection_home.html Tue Nov 29 10:50:38 2016 +0100 @@ -40,46 +40,7 @@
{% include "partials/image_annotations_list.html" with annotation_list=recent_list %} {% if recent_list %} - - {% if recent_list.has_previous or recent_list.has_next %} - - {% endif %} + {% include "partials/collection_home_pagination_links.html" with pagination_data=recent_pagination_data list_identifier="recent" %} {% endif %}
@@ -87,46 +48,7 @@
{% include "partials/image_annotations_list.html" with annotation_list=revised_list %} {% if revised_list %} - - {% if revised_list.has_previous or revised_list.has_next %} - - {% endif %} + {% include "partials/collection_home_pagination_links.html" with pagination_data=revised_pagination_data list_identifier="revised" %} {% endif %}
@@ -134,54 +56,14 @@
{% include "partials/image_annotations_list.html" with annotation_list=contributions_list %} {% if contributions_list %} - - {% if contributions_list.has_previous or contributions_list.has_next %} - - {% endif %} + {% include "partials/collection_home_pagination_links.html" with pagination_data=contributions_pagination_data list_identifier="contributions" %} {% endif %}
-
{% endblock %} diff -r c3ff8c5b28e6 -r d00d1089b2c8 src/iconolab/templates/partials/collection_home_pagination_links.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/templates/partials/collection_home_pagination_links.html Tue Nov 29 10:50:38 2016 +0100 @@ -0,0 +1,55 @@ + +{% if pagination_data.list.has_previous or pagination_data.list.has_next %} + +{% endif %} \ No newline at end of file diff -r c3ff8c5b28e6 -r d00d1089b2c8 src/iconolab/views/objects.py --- a/src/iconolab/views/objects.py Mon Nov 28 14:34:07 2016 +0100 +++ b/src/iconolab/views/objects.py Tue Nov 29 10:50:38 2016 +0100 @@ -65,7 +65,34 @@ except (ValueError, AnnotationRevision.DoesNotExist): return False, RedirectView.as_view(url=reverse('404error')) return True, objects_tuple - + + def get_pagination_data(self, list_to_paginate, page, perpage, adjacent_pages_count, perpage_range=[5, 10, 25, 100], trailing_qarg=""): + """ + Takes a queryset or a list and returns a dict with pagination data + """ + pagination_data = {} + pagination_data["page"] = page + pagination_data["perpage"] = perpage + pagination_data["perpage_range"] = perpage_range + pagination_data["trailing_qarg"] = trailing_qarg + paginator = Paginator(list_to_paginate, perpage) + try: + pagination_data["list"] = paginator.page(page) + except PageNotAnInteger: + pagination_data["list"] = paginator.page(1) + except EmptyPage: + pagination_data["list"] = paginator.page(paginator.num_pages) + pagination_data["page_range"] = [ + n for n in \ + range(page - adjacent_pages_count, page + adjacent_pages_count + 1) \ + if n > 0 and n <= paginator.num_pages + ] + pagination_data["show_first"] = page - adjacent_pages_count > 1 + pagination_data["ellipsis_first"] = pagination_data["show_first"] and (page - adjacent_pages_count != 2) + pagination_data["show_last"] = page + adjacent_pages_count < paginator.num_pages + pagination_data["ellipsis_last"] = pagination_data["show_last"] and (page + adjacent_pages_count != paginator.num_pages - 1) + return pagination_data + class CollectionHomepageView(View, ContextMixin, IconolabObjectView): def get(self, request, *args, **kwargs): success, result = self.check_kwargs(kwargs) @@ -119,50 +146,63 @@ active_list = 'items' context["active_list"] = active_list + + # Pagination values + adjacent_pages_count = 2 + # Paginated objects list - context["items_page"] = items_page - context["items_perpage"] = items_per_page - items_paginator = Paginator(collection.items.order_by("metadatas__inventory_number").all(), items_per_page) - try: - context["items_list"] = items_paginator.page(items_page) - except PageNotAnInteger: - context["items_list"] = items_paginator.page(1) - except EmptyPage: - context["items_list"] = items_paginator.page(items_paginator.num_pages) + items_list = collection.items.order_by("metadatas__inventory_number").all() + context["items_pagination_data"] = self.get_pagination_data( + items_list, + items_page, + items_per_page, + adjacent_pages_count, + perpage_range=[6, 12, 48, 192], + trailing_qarg="&recent_page="+str(recent_page) + +"&recent_perpage="+str(recent_per_page) + +"&revised_page="+str(revised_page) + +"&revised_perpage="+str(revised_per_page) + +"&contributions_page="+str(contributions_page) + +"&contributions_perpage="+str(contributions_per_page) + ) # Paginated recent annotations list - context["recent_page"] = recent_page - context["recent_perpage"] = recent_per_page recent_annotations = Annotation.objects.filter(image__item__collection__name=collection.name).prefetch_related( 'current_revision', 'stats' ).order_by('-current_revision__created') - recent_paginator = Paginator(recent_annotations, recent_per_page) - try: - context["recent_list"] = recent_paginator.page(recent_page) - except PageNotAnInteger: - context["recent_list"] = recent_paginator.page(1) - except EmptyPage: - context["recent_list"] = recent_paginator.page(recent_paginator.num_pages) + context["recent_pagination_data"] = self.get_pagination_data( + recent_annotations, + recent_page, + recent_per_page, + adjacent_pages_count, + trailing_qarg="&items_page="+str(items_page) + +"&items_perpage="+str(items_per_page) + +"&revised_page="+str(revised_page) + +"&revised_perpage="+str(revised_per_page) + +"&contributions_page="+str(contributions_page) + +"&contributions_perpage="+str(contributions_per_page) + ) # Paginated revised annotations list - context["revised_page"] = revised_page - context["revised_perpage"] = revised_per_page revised_annotations = Annotation.objects.filter(image__item__collection__name=collection.name).prefetch_related( 'current_revision', 'stats' ).annotate(revision_count=Count('revisions')).order_by('-revision_count') - revised_paginator = Paginator(revised_annotations, revised_per_page) - try: - context["revised_list"] = revised_paginator.page(revised_page) - except PageNotAnInteger: - context["revised_list"] = revised_paginator.page(1) - except EmptyPage: - context["revised_list"] = revised_paginator.page(revised_paginator.num_pages) + context["revised_pagination_data"] = self.get_pagination_data( + revised_annotations, + revised_page, + revised_per_page, + adjacent_pages_count, + trailing_qarg="&items_page="+str(items_page) + +"&items_perpage="+str(items_per_page) + +"&recent_page="+str(recent_page) + +"&recent_perpage="+str(recent_per_page) + +"&contributions_page="+str(contributions_page) + +"&contributions_perpage="+str(contributions_per_page) + ) # Paginated contribution calls annotation list - context["contributions_page"] = contributions_page - context["contributions_perpage"] = contributions_per_page contrib_calls_annotations_ids = list(set(MetaCategoryInfo.objects.filter( metacategory__collection__name=collection.name, metacategory__triggers_notifications=MetaCategory.CONTRIBUTORS @@ -170,13 +210,18 @@ collection_annotations = Annotation.objects.filter(id__in=contrib_calls_annotations_ids).all() collection_ann_dict = dict([(str(annotation.id), annotation) for annotation in collection_annotations]) contributions_annotations = [collection_ann_dict[id] for id in contrib_calls_annotations_ids] - contributions_paginator = Paginator(contributions_annotations, contributions_per_page) - try: - context["contributions_list"] = contributions_paginator.page(contributions_page) - except PageNotAnInteger: - context["contributions_list"] = contributions_paginator.page(1) - except EmptyPage: - context["contributions_list"] = contributions_paginator.page(contributions_paginator.num_pages) + context["contributions_pagination_data"] = self.get_pagination_data( + contributions_annotations, + contributions_page, + contributions_per_page, + adjacent_pages_count, + trailing_qarg="&items_page="+str(items_page) + +"&items_perpage="+str(items_per_page) + +"&recent_page="+str(recent_page) + +"&recent_perpage="+str(recent_per_page) + +"&revised_page="+str(revised_page) + +"&revised_perpage="+str(revised_per_page) + ) return render(request, 'iconolab/collection_home.html', context)