photos_cards.py 1.98 KB
Newer Older
1
2
3
4
5
6
from django import template
from django.conf import settings
from django.template.defaultfilters import date
from django.urls import reverse

from thaliawebsite.templatetags.grid_item import grid_item
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
7
from utils.media.services import get_thumbnail_url
8
9
10
11
12
13
14
15
16
17

register = template.Library()


@register.inclusion_tag('includes/grid_item.html')
def album_card(album):
    class_name = 'album-card'
    image_url = ''

    if album.cover:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
18
19
        image_url = get_thumbnail_url(album.cover.file,
                                      settings.THUMBNAIL_SIZES['medium'])
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        if album.cover.rotation > 0:
            class_name += ' rotate{}'.format(album.cover.rotation)

    if not album.accessible:
        class_name += ' grayscale'

    return grid_item(
        title=album.title,
        meta_text='<p>{}</p>'.format(date(album.date, 'Y-m-d')),
        url=album.get_absolute_url,
        image_url=image_url,
        class_name=class_name,
    )


@register.inclusion_tag('includes/grid_item.html')
def photo_card(photo):
    class_name = 'photo-card rotate{}'.format(photo.rotation)
38
39
    anchor_attrs = (f'data-fancybox-rotation="{photo.rotation}" '
                    f'data-fancybox="gallery"')
40
41
42
43
44
45
46

    if photo.album.shareable:
        anchor_attrs += ' data-download={}'.format(
            reverse('photos:shared-download',
                    args=[photo.album.slug, photo.album.access_token, photo]))
    else:
        anchor_attrs += ' data-download={}'.format(
Tom van Bussel's avatar
Tom van Bussel committed
47
            reverse('photos:download', args=[photo.album.slug, photo]))
48

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
49
50
    image_url = get_thumbnail_url(photo.file,
                                  settings.THUMBNAIL_SIZES['medium'])
51
52
53
54
55
56

    if photo.rotation > 0:
        class_name += ' rotate{}'.format(photo.rotation)

    return grid_item(
        title='',
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
57
58
59
        url=get_thumbnail_url(photo.file,
                              settings.THUMBNAIL_SIZES['large'],
                              fit=False),
60
61
62
63
        image_url=image_url,
        class_name=class_name,
        anchor_attrs=anchor_attrs
    )