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 collections import OrderedDict
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
14 # was picture/picture_list.html list (without thumbs)
15 def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html', cache_key=None, context=None):
16 """ generates a listing of all books, optionally filtered with a test function """
20 pictures_by_author, orphans = Picture.picture_list(filt)
21 books_nav = OrderedDict()
22 for tag in pictures_by_author:
23 if pictures_by_author[tag]:
24 books_nav.setdefault(tag.sort_key[0], []).append(tag)
26 return render_to_response(template_name, locals(),
27 context_instance=RequestContext(request))
30 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html', cache_key=None, context=None):
31 book_list = Picture.objects.all()
33 book_list = book_list.filter(filter)
35 book_list = book_list.filter(get_filter())
36 book_list = book_list.order_by('sort_key_author')
37 book_list = list(book_list)
38 return render_to_response(template_name, locals(),
39 context_instance=RequestContext(request))
41 def picture_detail(request, slug):
42 picture = get_object_or_404(Picture, slug=slug)
44 theme_things = split_tags(picture.related_themes())
46 # categories = SortedDict()
47 # for tag in picture.tags.iterator():
48 # categories.setdefault(tag.category, []).append(tag)
50 themes = theme_things.get('theme', [])
51 things = theme_things.get('thing', [])
53 extra_info = picture.extra_info
55 return render_to_response("picture/picture_detail.html", locals(),
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", locals(),
67 context_instance=RequestContext(request))
73 @permission_required('picture.add_picture')
74 def import_picture(request):
75 """docstring for import_book"""
76 from django.http import HttpResponse
77 from picture.forms import PictureImportForm
78 from django.utils.translation import ugettext as _
80 import_form = PictureImportForm(request.POST, request.FILES)
81 if import_form.is_valid():
89 exception = pprint.pformat(info[1])
90 tb = '\n'.join(traceback.format_tb(info[2]))
91 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
92 return HttpResponse(_("Picture imported successfully"))
94 return HttpResponse(_("Error importing file: %r") % import_form.errors)
98 def picture_mini(request, pk, with_link=True):
99 picture = get_object_or_404(Picture, pk=pk)
100 author_str = ", ".join(tag.name
101 for tag in picture.tags.filter(category='author'))
102 return render(request, 'picture/picture_mini_box.html', {
104 'author_str': author_str,
105 'with_link': with_link,
110 def picture_short(request, pk):
111 picture = get_object_or_404(Picture, pk=pk)
113 return render(request, 'picture/picture_short.html', {
115 'main_link': picture.get_absolute_url(),
117 'tags': split_tags(picture.tags),
122 def picturearea_short(request, pk):
123 area = get_object_or_404(PictureArea, pk=pk)
124 theme = area.tags.filter(category='theme')
125 theme = theme and theme[0] or None
126 thing = area.tags.filter(category='thing')
127 thing = thing and thing[0] or None
128 return render(request, 'picture/picturearea_short.html', locals())