11b08bc3c0bb42b435fbfd3a43765cce019095ab
[wolnelektury.git] / apps / picture / views.py
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.
4 #
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
11
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 """
15
16     if get_filter:
17         filt = get_filter()
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)
23
24     return render_to_response(template_name, locals(),
25         context_instance=RequestContext(request))
26
27
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()
30     if filter:
31         book_list = book_list.filter(filter)
32     if get_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))
38
39 def picture_detail(request, slug):
40     picture = get_object_or_404(Picture, slug=slug)
41
42     theme_things = split_tags(picture.related_themes())
43
44     # categories = SortedDict()
45     # for tag in picture.tags.iterator():
46     #     categories.setdefault(tag.category, []).append(tag)
47
48     themes = theme_things.get('theme', [])
49     things = theme_things.get('thing', [])
50
51     extra_info = picture.extra_info
52
53     return render_to_response("picture/picture_detail.html", locals(),
54                               context_instance=RequestContext(request))
55
56
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))
61
62
63 # =========
64 # = Admin =
65 # =========
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 _
72
73     import_form = PictureImportForm(request.POST, request.FILES)
74     if import_form.is_valid():
75         try:
76             import_form.save()
77         except:
78             import sys
79             import pprint
80             import traceback
81             info = sys.exc_info()
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"))
86     else:
87         return HttpResponse(_("Error importing file: %r") % import_form.errors)
88
89