194bb98202f2ca91777c9bb43c526d994d88bbce
[wolnelektury.git] / src / picture / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django.contrib.auth.decorators import permission_required
6 from django.shortcuts import render, get_object_or_404, render
7 from picture.models import Picture
8 from catalogue.utils import split_tags
9 from sponsors.models import Sponsor
10 from wolnelektury.utils import ajax
11
12
13 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
14                        cache_key=None, context=None):
15     pictures = Picture.objects.all()
16     if filter:
17         pictures = pictures.filter(filter)
18     if get_filter:
19         pictures = pictures.filter(get_filter())
20
21     if request.EXPERIMENTS['layout'].value:
22         template_name = 'catalogue/2022/author_detail.html'
23         return render(request, template_name, {'object_list': pictures})
24     else:
25         return render(request, template_name, {'picture_list': list(pictures)})
26
27
28 def picture_detail(request, slug):
29     picture = get_object_or_404(Picture, slug=slug)
30
31     theme_things = split_tags(picture.related_themes())
32
33     if request.EXPERIMENTS['layout'].value:
34         template_name = 'picture/2022/picture_detail.html'
35     else:
36         template_name = 'picture/picture_detail.html'
37     
38     return render(request, template_name, {
39         'picture': picture,
40         'themes': theme_things.get('theme', []),
41         'things': theme_things.get('thing', []),
42         'active_menu_item': 'gallery',
43     })
44
45
46 def picture_viewer(request, slug):
47     picture = get_object_or_404(Picture, slug=slug)
48     sponsors = []
49     for sponsor in picture.get_extra_info_json().get('sponsors', []):
50         have_sponsors = Sponsor.objects.filter(name=sponsor)
51         if have_sponsors.exists():
52             sponsors.append(have_sponsors[0])
53     return render(request, "picture/picture_viewer.html", {
54         'picture': picture,
55         'sponsors': sponsors,
56     })
57
58
59 @ajax(method='get')
60 def picture_page(request, key=None):
61     pictures = Picture.objects.order_by('-id')
62     if key is not None:
63         pictures = pictures.filter(id__lt=key)
64     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
65     picture_data = [
66         {
67             'id': picture.id,
68             'title': picture.title,
69             'author': picture.author_unicode(),
70             'epoch': picture.tag_unicode('epoch'),
71             'kind': picture.tag_unicode('kind'),
72             'genre': picture.tag_unicode('genre'),
73             'style': picture.get_extra_info_json()['style'],
74             'image_url': picture.image_file.url,
75             'width': picture.width,
76             'height': picture.height,
77         }
78         for picture in pictures
79     ]
80     return {
81         'pictures': picture_data,
82         'count': Picture.objects.count(),
83     }
84
85
86 # =========
87 # = Admin =
88 # =========
89 @permission_required('picture.add_picture')
90 def import_picture(request):
91     """docstring for import_book"""
92     from django.http import HttpResponse
93     from picture.forms import PictureImportForm
94     from django.utils.translation import gettext as _
95
96     import_form = PictureImportForm(request.POST, request.FILES)
97     if import_form.is_valid():
98         try:
99             import_form.save()
100         except:
101             import sys
102             import pprint
103             import traceback
104             info = sys.exc_info()
105             exception = pprint.pformat(info[1])
106             tb = '\n'.join(traceback.format_tb(info[2]))
107             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
108                                 {'exception': exception, 'tb': tb}, content_type='text/plain')
109         return HttpResponse(_("Picture imported successfully"))
110     else:
111         return HttpResponse(_("Error importing file: %r") % import_form.errors)