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
16 # # was picture/picture_list.html list (without thumbs)
17 # def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html',
18 # cache_key=None, context=None):
19 # """ generates a listing of all books, optionally filtered with a test function """
23 # pictures_by_author, orphans = Picture.picture_list(filt)
24 # books_nav = OrderedDict()
25 # for tag in pictures_by_author:
26 # if pictures_by_author[tag]:
27 # books_nav.setdefault(tag.sort_key[0], []).append(tag)
29 # return render_to_response(template_name, locals(), context_instance=RequestContext(request))
30 from wolnelektury.utils import ajax
33 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
34 cache_key=None, context=None):
35 pictures = Picture.objects.all()
37 pictures = pictures.filter(filter)
39 pictures = pictures.filter(get_filter())
40 return render_to_response(template_name, {'book_list': list(pictures)}, context_instance=RequestContext(request))
43 def picture_detail(request, slug):
44 picture = get_object_or_404(Picture, slug=slug)
46 theme_things = split_tags(picture.related_themes())
48 # categories = SortedDict()
49 # for tag in picture.tags.iterator():
50 # categories.setdefault(tag.category, []).append(tag)
52 return render_to_response("picture/picture_detail.html", {
54 'themes': theme_things.get('theme', []),
55 'things': theme_things.get('thing', []),
56 }, context_instance=RequestContext(request))
59 def picture_viewer(request, slug):
60 picture = get_object_or_404(Picture, slug=slug)
62 for sponsor in picture.extra_info.get('sponsors', []):
63 have_sponsors = Sponsor.objects.filter(name=sponsor)
64 if have_sponsors.exists():
65 sponsors.append(have_sponsors[0])
66 return render_to_response("picture/picture_viewer.html", {
69 }, context_instance=RequestContext(request))
73 def picture_page(request, key=None):
74 pictures = Picture.objects.order_by('-id')
76 pictures = pictures.filter(id__lt=key)
77 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
81 'title': picture.title,
82 'author': picture.author_unicode(),
83 'epoch': picture.tag_unicode('epoch'),
84 'kind': picture.tag_unicode('kind'),
85 'genre': picture.tag_unicode('genre'),
86 'style': picture.extra_info['style'],
87 'image_url': picture.image_file.url,
88 'width': picture.width,
89 'height': picture.height,
91 for picture in pictures
94 'pictures': picture_data,
95 'count': Picture.objects.count(),
102 @permission_required('picture.add_picture')
103 def import_picture(request):
104 """docstring for import_book"""
105 from django.http import HttpResponse
106 from picture.forms import PictureImportForm
107 from django.utils.translation import ugettext as _
109 import_form = PictureImportForm(request.POST, request.FILES)
110 if import_form.is_valid():
117 info = sys.exc_info()
118 exception = pprint.pformat(info[1])
119 tb = '\n'.join(traceback.format_tb(info[2]))
120 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
121 {'exception': exception, 'tb': tb}, mimetype='text/plain')
122 return HttpResponse(_("Picture imported successfully"))
124 return HttpResponse(_("Error importing file: %r") % import_form.errors)
128 def picture_mini(request, pk, with_link=True):
129 picture = get_object_or_404(Picture, pk=pk)
130 return render(request, 'picture/picture_mini_box.html', {
132 'author': picture.author_unicode(),
133 'with_link': with_link,
138 def picture_short(request, pk):
139 picture = get_object_or_404(Picture, pk=pk)
141 return render(request, 'picture/picture_short.html', {
143 'main_link': picture.get_absolute_url(),
145 'tags': split_tags(picture.tags),
150 def picturearea_short(request, pk):
151 area = get_object_or_404(PictureArea, pk=pk)
152 themes = area.tags.filter(category='theme')
153 things = area.tags.filter(category='thing')
154 return render(request, 'picture/picturearea_short.html', {
156 'theme': themes[0] if themes else None,
157 'thing': things[0] if things else None,