Beispiel #1
0
    def test_get_annotated_accessible_albums(self):
        request = self.rf.get("/")
        request.member = None
        album = Album(date=datetime(year=2017, month=1, day=1), slug="test")
        album.save()

        self.assertEqual(Album.objects.count(), 1)

        with self.subTest(membership=None):
            albums = Album.objects.all()
            albums = get_annotated_accessible_albums(request, albums)
            for album in albums:
                self.assertFalse(album.accessible)

        request.member = self.member
        with self.subTest(membership=None):
            albums = Album.objects.all()
            albums = get_annotated_accessible_albums(request, albums)
            for album in albums:
                self.assertFalse(album.accessible)

        membership = Membership.objects.create(
            user=self.member,
            type=Membership.MEMBER,
            since=datetime(year=2016, month=1, day=1),
        )
        with self.subTest(membership_since=membership.since,
                          membership_until=membership.until):
            albums = Album.objects.all()
            albums = get_annotated_accessible_albums(request, albums)
            for album in albums:
                self.assertTrue(album.accessible)

        membership.until = datetime(year=2016, month=1, day=1)
        membership.save()
        with self.subTest(membership_since=membership.since,
                          membership_until=membership.until):
            albums = Album.objects.all()
            albums = get_annotated_accessible_albums(request, albums)
            for album in albums:
                self.assertFalse(album.accessible)

        membership.until = datetime(year=2017, month=1, day=1)
        membership.save()
        with self.subTest(membership_since=membership.since,
                          membership_until=membership.until):
            albums = Album.objects.all()
            albums = get_annotated_accessible_albums(request, albums)
            for album in albums:
                self.assertTrue(album.accessible)
Beispiel #2
0
def index(request):
    """Render the index page showing multiple album cards."""
    keywords = request.GET.get("keywords", "").split()

    # Only show published albums
    albums = Album.objects.filter(hidden=False)
    for key in keywords:
        albums = albums.filter(**{f"title_{get_language()}__icontains": key})

    albums = get_annotated_accessible_albums(request, albums)

    albums = albums.order_by("-date")
    paginator = Paginator(albums, 16)

    page = request.GET.get("page")
    page = 1 if page is None or not page.isdigit() else int(page)
    try:
        albums = paginator.page(page)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        albums = paginator.page(paginator.num_pages)
        page = paginator.num_pages

    # Show the two pages before and after the current page
    page_range_start = max(1, page - 2)
    page_range_stop = min(page + 3, paginator.num_pages + 1)

    # Add extra pages if we show less than 5 pages
    page_range_start = min(page_range_start, page_range_stop - 5)
    page_range_start = max(1, page_range_start)

    # Add extra pages if we still show less than 5 pages
    page_range_stop = max(page_range_stop, page_range_start + 5)
    page_range_stop = min(page_range_stop, paginator.num_pages + 1)

    page_range = range(page_range_start, page_range_stop)

    return render(
        request,
        "photos/index.html",
        {
            "albums": albums,
            "page_range": page_range,
            "keywords": keywords
        },
    )
Beispiel #3
0
 def get_queryset(self):
     """Return albums that are annotated to be accessible by the request user."""
     return services.get_annotated_accessible_albums(
         self.request, Album.objects.all())
Beispiel #4
0
 def get_queryset(self):
     return services.get_annotated_accessible_albums(
         self.request, Album.objects.all())