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_to_response, get_object_or_404, render
8 from django.template import RequestContext
9 from picture.models import Picture, PictureArea
10 from catalogue.utils import split_tags
11 from ssify import ssi_included
12 from sponsors.models import Sponsor
13 from wolnelektury.utils import ajax
16 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
17 cache_key=None, context=None):
18 pictures = Picture.objects.all()
20 pictures = pictures.filter(filter)
22 pictures = pictures.filter(get_filter())
23 return render_to_response(template_name, {'book_list': list(pictures)}, context_instance=RequestContext(request))
26 def picture_detail(request, slug):
27 picture = get_object_or_404(Picture, slug=slug)
29 theme_things = split_tags(picture.related_themes())
31 return render_to_response("picture/picture_detail.html", {
33 'themes': theme_things.get('theme', []),
34 'things': theme_things.get('thing', []),
35 'active_menu_item': 'gallery',
36 }, context_instance=RequestContext(request))
39 def picture_viewer(request, slug):
40 picture = get_object_or_404(Picture, slug=slug)
42 for sponsor in picture.extra_info.get('sponsors', []):
43 have_sponsors = Sponsor.objects.filter(name=sponsor)
44 if have_sponsors.exists():
45 sponsors.append(have_sponsors[0])
46 return render_to_response("picture/picture_viewer.html", {
49 }, context_instance=RequestContext(request))
53 def picture_page(request, key=None):
54 pictures = Picture.objects.order_by('-id')
56 pictures = pictures.filter(id__lt=key)
57 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
61 'title': picture.title,
62 'author': picture.author_unicode(),
63 'epoch': picture.tag_unicode('epoch'),
64 'kind': picture.tag_unicode('kind'),
65 'genre': picture.tag_unicode('genre'),
66 'style': picture.extra_info['style'],
67 'image_url': picture.image_file.url,
68 'width': picture.width,
69 'height': picture.height,
71 for picture in pictures
74 'pictures': picture_data,
75 'count': Picture.objects.count(),
82 @permission_required('picture.add_picture')
83 def import_picture(request):
84 """docstring for import_book"""
85 from django.http import HttpResponse
86 from picture.forms import PictureImportForm
87 from django.utils.translation import ugettext as _
89 import_form = PictureImportForm(request.POST, request.FILES)
90 if import_form.is_valid():
98 exception = pprint.pformat(info[1])
99 tb = '\n'.join(traceback.format_tb(info[2]))
100 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
101 {'exception': exception, 'tb': tb}, mimetype='text/plain')
102 return HttpResponse(_("Picture imported successfully"))
104 return HttpResponse(_("Error importing file: %r") % import_form.errors)
108 def picture_mini(request, pk, with_link=True):
109 picture = get_object_or_404(Picture, pk=pk)
110 return render(request, 'picture/picture_mini_box.html', {
112 'author': picture.author_unicode(),
113 'with_link': with_link,
118 def picture_short(request, pk):
119 picture = get_object_or_404(Picture, pk=pk)
121 return render(request, 'picture/picture_short.html', {
127 def picturearea_short(request, pk):
128 area = get_object_or_404(PictureArea, pk=pk)
129 themes = area.tags.filter(category='theme')
130 things = area.tags.filter(category='thing')
131 return render(request, 'picture/picturearea_short.html', {
133 'theme': themes[0] if themes else None,
134 'thing': things[0] if things else None,