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, PictureArea
8 from catalogue.utils import split_tags
9 from ssify import ssi_included
10 from sponsors.models import Sponsor
11 from wolnelektury.utils import ajax
14 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
15 cache_key=None, context=None):
16 pictures = Picture.objects.all()
18 pictures = pictures.filter(filter)
20 pictures = pictures.filter(get_filter())
21 return render(request, template_name, {'book_list': list(pictures)})
24 def picture_detail(request, slug):
25 picture = get_object_or_404(Picture, slug=slug)
27 theme_things = split_tags(picture.related_themes())
29 return render(request, "picture/picture_detail.html", {
31 'themes': theme_things.get('theme', []),
32 'things': theme_things.get('thing', []),
33 'active_menu_item': 'gallery',
37 def picture_viewer(request, slug):
38 picture = get_object_or_404(Picture, slug=slug)
40 for sponsor in picture.extra_info.get('sponsors', []):
41 have_sponsors = Sponsor.objects.filter(name=sponsor)
42 if have_sponsors.exists():
43 sponsors.append(have_sponsors[0])
44 return render(request, "picture/picture_viewer.html", {
51 def picture_page(request, key=None):
52 pictures = Picture.objects.order_by('-id')
54 pictures = pictures.filter(id__lt=key)
55 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
59 'title': picture.title,
60 'author': picture.author_unicode(),
61 'epoch': picture.tag_unicode('epoch'),
62 'kind': picture.tag_unicode('kind'),
63 'genre': picture.tag_unicode('genre'),
64 'style': picture.extra_info['style'],
65 'image_url': picture.image_file.url,
66 'width': picture.width,
67 'height': picture.height,
69 for picture in pictures
72 'pictures': picture_data,
73 'count': Picture.objects.count(),
80 @permission_required('picture.add_picture')
81 def import_picture(request):
82 """docstring for import_book"""
83 from django.http import HttpResponse
84 from picture.forms import PictureImportForm
85 from django.utils.translation import ugettext as _
87 import_form = PictureImportForm(request.POST, request.FILES)
88 if import_form.is_valid():
96 exception = pprint.pformat(info[1])
97 tb = '\n'.join(traceback.format_tb(info[2]))
98 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
99 {'exception': exception, 'tb': tb}, mimetype='text/plain')
100 return HttpResponse(_("Picture imported successfully"))
102 return HttpResponse(_("Error importing file: %r") % import_form.errors)
106 def picture_short(request, pk):
107 picture = get_object_or_404(Picture, pk=pk)
109 return render(request, 'picture/picture_short.html', {
115 def picturearea_short(request, pk):
116 area = get_object_or_404(PictureArea, pk=pk)
117 themes = area.tags.filter(category='theme')
118 things = area.tags.filter(category='thing')
119 return render(request, 'picture/picturearea_short.html', {
121 'theme': themes[0] if themes else None,
122 'thing': things[0] if things else None,