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())
20 return render(request, template_name, {'picture_list': list(pictures)})
23 def picture_detail(request, slug):
24 picture = get_object_or_404(Picture, slug=slug)
26 theme_things = split_tags(picture.related_themes())
28 return render(request, "picture/picture_detail.html", {
30 'themes': theme_things.get('theme', []),
31 'things': theme_things.get('thing', []),
32 'active_menu_item': 'gallery',
36 def picture_viewer(request, slug):
37 picture = get_object_or_404(Picture, slug=slug)
39 for sponsor in picture.get_extra_info_json().get('sponsors', []):
40 have_sponsors = Sponsor.objects.filter(name=sponsor)
41 if have_sponsors.exists():
42 sponsors.append(have_sponsors[0])
43 return render(request, "picture/picture_viewer.html", {
50 def picture_page(request, key=None):
51 pictures = Picture.objects.order_by('-id')
53 pictures = pictures.filter(id__lt=key)
54 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
58 'title': picture.title,
59 'author': picture.author_unicode(),
60 'epoch': picture.tag_unicode('epoch'),
61 'kind': picture.tag_unicode('kind'),
62 'genre': picture.tag_unicode('genre'),
63 'style': picture.get_extra_info_json()['style'],
64 'image_url': picture.image_file.url,
65 'width': picture.width,
66 'height': picture.height,
68 for picture in pictures
71 'pictures': picture_data,
72 'count': Picture.objects.count(),
79 @permission_required('picture.add_picture')
80 def import_picture(request):
81 """docstring for import_book"""
82 from django.http import HttpResponse
83 from picture.forms import PictureImportForm
84 from django.utils.translation import gettext as _
86 import_form = PictureImportForm(request.POST, request.FILES)
87 if import_form.is_valid():
95 exception = pprint.pformat(info[1])
96 tb = '\n'.join(traceback.format_tb(info[2]))
97 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
98 {'exception': exception, 'tb': tb}, content_type='text/plain')
99 return HttpResponse(_("Picture imported successfully"))
101 return HttpResponse(_("Error importing file: %r") % import_form.errors)