Paginates lists view

This commit is contained in:
Mouse Reeve 2021-01-31 17:34:06 -08:00
parent 6e0d258c97
commit d7c32cc314
3 changed files with 26 additions and 6 deletions

View file

@ -46,6 +46,10 @@ class List(OrderedCollectionMixin, BookWyrmModel):
''' list of books for this shelf, overrides OrderedCollectionMixin ''' ''' list of books for this shelf, overrides OrderedCollectionMixin '''
return self.books.all().order_by('listitem') return self.books.all().order_by('listitem')
class Meta:
''' default sorting '''
ordering = ('-updated_date',)
class ListItem(ActivitypubMixin, BookWyrmModel): class ListItem(ActivitypubMixin, BookWyrmModel):
''' ok ''' ''' ok '''

View file

@ -4,8 +4,7 @@
<header class="block"> <header class="block">
<h1 class="title">Lists</h1> <h1 class="title">Lists</h1>
</header> </header>
{% if request.user.is_authenticated and not lists.has_previous %}
{% if request.user.is_authenticated %}
<section class="block content"> <section class="block content">
<header class="columns"> <header class="columns">
<div class="column"> <div class="column">
@ -27,18 +26,26 @@
</form> </form>
{% if request.user.list_set.exists %} {% if request.user.list_set.exists %}
{% include 'lists/list_items.html' with lists=request.user.list_set.all %} {% include 'lists/list_items.html' with lists=request.user.list_set.all|slice:4 %}
{% endif %}
{% if request.user.list_set.count > 4 %}
<a href="{{ request.user.local_path }}/lists">See all {{ request.user.list_set.count}} lists</a>
{% endif %} {% endif %}
</section> </section>
{% endif %} {% endif %}
{% if lists.exists %}
{% if lists %}
<section class="block content"> <section class="block content">
<h2 class="title">Recent Lists</h2> <h2 class="title">Recent Lists</h2>
{% if request.user.list_set.exists %} {% if request.user.list_set.exists %}
{% include 'lists/list_items.html' with lists=lists %} {% include 'lists/list_items.html' with lists=lists %}
{% endif %} {% endif %}
</section> </section>
<div>
{% include 'snippets/pagination.html' with page=lists path=path %}
</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View file

@ -1,5 +1,6 @@
''' book list views''' ''' book list views'''
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Q from django.db.models import Q
from django.http import HttpResponseNotFound, HttpResponseBadRequest from django.http import HttpResponseNotFound, HttpResponseBadRequest
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -19,15 +20,23 @@ class Lists(View):
''' book list page ''' ''' book list page '''
def get(self, request): def get(self, request):
''' display a book list ''' ''' display a book list '''
try:
page = int(request.GET.get('page', 1))
except ValueError:
page = 1
user = request.user if request.user.is_authenticated else None user = request.user if request.user.is_authenticated else None
lists = models.List.objects.filter( lists = models.List.objects.filter(
~Q(user=user), ~Q(user=user),
).all() ).all()
lists = privacy_filter(request.user, lists, ['public', 'followers']) lists = privacy_filter(request.user, lists, ['public', 'followers'])
paginated = Paginator(lists, 12)
data = { data = {
'title': 'Lists', 'title': 'Lists',
'lists': lists, 'lists': paginated.page(page),
'list_form': forms.ListForm() 'list_form': forms.ListForm(),
'path': '/list',
} }
return TemplateResponse(request, 'lists/lists.html', data) return TemplateResponse(request, 'lists/lists.html', data)