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
8 from django.template import RequestContext
9 from picture.models import Picture
10 from catalogue.utils import split_tags
12 # was picture/picture_list.html list (without thumbs)
13 def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html', cache_key=None, context=None):
14 """ generates a listing of all books, optionally filtered with a test function """
18 pictures_by_author, orphans = Picture.picture_list(filt)
19 books_nav = OrderedDict()
20 for tag in pictures_by_author:
21 if pictures_by_author[tag]:
22 books_nav.setdefault(tag.sort_key[0], []).append(tag)
24 return render_to_response(template_name, locals(),
25 context_instance=RequestContext(request))
28 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html', cache_key=None, context=None):
29 book_list = Picture.objects.all()
31 book_list = book_list.filter(filter)
33 book_list = book_list.filter(get_filter())
34 book_list = book_list.order_by('sort_key_author')
35 book_list = list(book_list)
36 return render_to_response(template_name, locals(),
37 context_instance=RequestContext(request))
39 def picture_detail(request, slug):
40 picture = get_object_or_404(Picture, slug=slug)
42 theme_things = split_tags(picture.related_themes())
44 # categories = SortedDict()
45 # for tag in picture.tags.iterator():
46 # categories.setdefault(tag.category, []).append(tag)
48 themes = theme_things.get('theme', [])
49 things = theme_things.get('thing', [])
51 extra_info = picture.extra_info
53 return render_to_response("picture/picture_detail.html", locals(),
54 context_instance=RequestContext(request))
57 def picture_viewer(request, slug):
58 picture = get_object_or_404(Picture, slug=slug)
59 return render_to_response("picture/picture_viewer.html", locals(),
60 context_instance=RequestContext(request))
66 @permission_required('picture.add_picture')
67 def import_picture(request):
68 """docstring for import_book"""
69 from django.http import HttpResponse
70 from picture.forms import PictureImportForm
71 from django.utils.translation import ugettext as _
73 import_form = PictureImportForm(request.POST, request.FILES)
74 if import_form.is_valid():
82 exception = pprint.pformat(info[1])
83 tb = '\n'.join(traceback.format_tb(info[2]))
84 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
85 return HttpResponse(_("Picture imported successfully"))
87 return HttpResponse(_("Error importing file: %r") % import_form.errors)