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
13 # was picture/picture_list.html list (without thumbs)
14 def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html', cache_key=None, context=None):
15 """ generates a listing of all books, optionally filtered with a test function """
19 pictures_by_author, orphans = Picture.picture_list(filt)
20 books_nav = OrderedDict()
21 for tag in pictures_by_author:
22 if pictures_by_author[tag]:
23 books_nav.setdefault(tag.sort_key[0], []).append(tag)
25 return render_to_response(template_name, locals(),
26 context_instance=RequestContext(request))
29 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html', cache_key=None, context=None):
30 book_list = Picture.objects.all()
32 book_list = book_list.filter(filter)
34 book_list = book_list.filter(get_filter())
35 book_list = book_list.order_by('sort_key_author')
36 book_list = list(book_list)
37 return render_to_response(template_name, locals(),
38 context_instance=RequestContext(request))
40 def picture_detail(request, slug):
41 picture = get_object_or_404(Picture, slug=slug)
43 theme_things = split_tags(picture.related_themes())
45 # categories = SortedDict()
46 # for tag in picture.tags.iterator():
47 # categories.setdefault(tag.category, []).append(tag)
49 themes = theme_things.get('theme', [])
50 things = theme_things.get('thing', [])
52 extra_info = picture.extra_info
54 return render_to_response("picture/picture_detail.html", locals(),
55 context_instance=RequestContext(request))
58 def picture_viewer(request, slug):
59 picture = get_object_or_404(Picture, slug=slug)
60 return render_to_response("picture/picture_viewer.html", locals(),
61 context_instance=RequestContext(request))
67 @permission_required('picture.add_picture')
68 def import_picture(request):
69 """docstring for import_book"""
70 from django.http import HttpResponse
71 from picture.forms import PictureImportForm
72 from django.utils.translation import ugettext as _
74 import_form = PictureImportForm(request.POST, request.FILES)
75 if import_form.is_valid():
83 exception = pprint.pformat(info[1])
84 tb = '\n'.join(traceback.format_tb(info[2]))
85 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
86 return HttpResponse(_("Picture imported successfully"))
88 return HttpResponse(_("Error importing file: %r") % import_form.errors)
92 def picture_mini(request, pk, with_link=True):
93 picture = get_object_or_404(Picture, pk=pk)
94 author_str = ", ".join(tag.name
95 for tag in picture.tags.filter(category='author'))
96 return render(request, 'picture/picture_mini_box.html', {
98 'author_str': author_str,
99 'with_link': with_link,
104 def picture_short(request, pk):
105 picture = get_object_or_404(Picture, pk=pk)
107 return render(request, 'picture/picture_short.html', {
109 'main_link': picture.get_absolute_url(),
111 'tags': split_tags(picture.tags),
116 def picturearea_short(request, pk):
117 area = get_object_or_404(PictureArea, pk=pk)
118 theme = area.tags.filter(category='theme')
119 theme = theme and theme[0] or None
120 thing = area.tags.filter(category='thing')
121 thing = thing and thing[0] or None
122 return render(request, 'picture/picturearea_short.html', locals())