1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.contrib.auth.decorators import permission_required
7 from django.shortcuts import render, get_object_or_404, render
8 from picture.models import Picture, PictureArea
9 from catalogue.utils import split_tags
10 from ssify import ssi_included
11 from sponsors.models import Sponsor
12 from wolnelektury.utils import ajax
15 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
16 cache_key=None, context=None):
17 pictures = Picture.objects.all()
19 pictures = pictures.filter(filter)
21 pictures = pictures.filter(get_filter())
22 return render(request, template_name, {'book_list': list(pictures)})
25 def picture_detail(request, slug):
26 picture = get_object_or_404(Picture, slug=slug)
28 theme_things = split_tags(picture.related_themes())
30 return render(request, "picture/picture_detail.html", {
32 'themes': theme_things.get('theme', []),
33 'things': theme_things.get('thing', []),
34 'active_menu_item': 'gallery',
38 def picture_viewer(request, slug):
39 picture = get_object_or_404(Picture, slug=slug)
41 for sponsor in picture.extra_info.get('sponsors', []):
42 have_sponsors = Sponsor.objects.filter(name=sponsor)
43 if have_sponsors.exists():
44 sponsors.append(have_sponsors[0])
45 return render(request, "picture/picture_viewer.html", {
52 def picture_page(request, key=None):
53 pictures = Picture.objects.order_by('-id')
55 pictures = pictures.filter(id__lt=key)
56 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
60 'title': picture.title,
61 'author': picture.author_unicode(),
62 'epoch': picture.tag_unicode('epoch'),
63 'kind': picture.tag_unicode('kind'),
64 'genre': picture.tag_unicode('genre'),
65 'style': picture.extra_info['style'],
66 'image_url': picture.image_file.url,
67 'width': picture.width,
68 'height': picture.height,
70 for picture in pictures
73 'pictures': picture_data,
74 'count': Picture.objects.count(),
81 @permission_required('picture.add_picture')
82 def import_picture(request):
83 """docstring for import_book"""
84 from django.http import HttpResponse
85 from picture.forms import PictureImportForm
86 from django.utils.translation import ugettext as _
88 import_form = PictureImportForm(request.POST, request.FILES)
89 if import_form.is_valid():
97 exception = pprint.pformat(info[1])
98 tb = '\n'.join(traceback.format_tb(info[2]))
99 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
100 {'exception': exception, 'tb': tb}, mimetype='text/plain')
101 return HttpResponse(_("Picture imported successfully"))
103 return HttpResponse(_("Error importing file: %r") % import_form.errors)
107 def picture_mini(request, pk, with_link=True):
108 picture = get_object_or_404(Picture, pk=pk)
109 return render(request, 'picture/picture_mini_box.html', {
111 'author': picture.author_unicode(),
112 'with_link': with_link,
117 def picture_short(request, pk):
118 picture = get_object_or_404(Picture, pk=pk)
120 return render(request, 'picture/picture_short.html', {
126 def picturearea_short(request, pk):
127 area = get_object_or_404(PictureArea, pk=pk)
128 themes = area.tags.filter(category='theme')
129 things = area.tags.filter(category='thing')
130 return render(request, 'picture/picturearea_short.html', {
132 'theme': themes[0] if themes else None,
133 'thing': things[0] if things else None,