1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
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
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()
17 pictures = pictures.filter(filter)
19 pictures = pictures.filter(get_filter())
21 if request.EXPERIMENTS['layout'].value:
22 template_name = 'catalogue/2022/author_detail.html'
23 return render(request, template_name, {'object_list': pictures})
25 return render(request, template_name, {'picture_list': list(pictures)})
28 def picture_detail(request, slug):
29 picture = get_object_or_404(Picture, slug=slug)
31 theme_things = split_tags(picture.related_themes())
33 if request.EXPERIMENTS['layout'].value:
34 template_name = 'picture/2022/picture_detail.html'
36 template_name = 'picture/picture_detail.html'
38 return render(request, template_name, {
40 'themes': theme_things.get('theme', []),
41 'things': theme_things.get('thing', []),
42 'active_menu_item': 'gallery',
46 def picture_viewer(request, slug):
47 picture = get_object_or_404(Picture, slug=slug)
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", {
60 def picture_page(request, key=None):
61 pictures = Picture.objects.order_by('-id')
63 pictures = pictures.filter(id__lt=key)
64 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
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,
78 for picture in pictures
81 'pictures': picture_data,
82 'count': Picture.objects.count(),
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 _
96 import_form = PictureImportForm(request.POST, request.FILES)
97 if import_form.is_valid():
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"))
111 return HttpResponse(_("Error importing file: %r") % import_form.errors)