views.py 1.43 KB
Newer Older
1
from django.shortcuts import get_object_or_404, render
2
from django.contrib.auth.decorators import login_required
3
from django.http import Http404
Joost Rijneveld's avatar
Joost Rijneveld committed
4
from django.conf import settings
5
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
6

7
from .models import Album
Joost Rijneveld's avatar
Joost Rijneveld committed
8
9
10
11
from utils.snippets import sanitize_path

from sendfile import sendfile
import os
12
13
14
15
16
17

COVER_FILENAME = 'cover.jpg'


@login_required
def index(request):
18
    albums = Album.objects.filter(hidden=False).order_by('-date')
19
20
21
22
23
24
25
26
27
28
29
30

    paginator = Paginator(albums, 12)
    page = request.GET.get('page')
    try:
        albums = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        albums = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        albums = paginator.page(paginator.num_pages)
    return render(request, 'photos/index.html', {'albums': albums})
31
32
33


@login_required
34
35
def album(request, slug):
    album = get_object_or_404(Album, slug=slug)
36
37
    context = {'album': album, 'photos': album.photo_set.filter(hidden=False)}
    return render(request, 'photos/album.html', context)
38
39
40


@login_required
Joost Rijneveld's avatar
Joost Rijneveld committed
41
42
43
44
45
46
def download(request, path):
    path = sanitize_path(path)
    path = os.path.join(settings.MEDIA_ROOT, 'photos', *path.split('/')[1:])
    if not os.path.isfile(path):
        raise Http404("Photo not found.")
    return sendfile(request, path, attachment=True)