3f04e3c897656dfb7706ecdbfcd138cd152d476e
[wolnelektury.git] / apps / picture / views.py
1 from picture.models import Picture
2 from django.contrib.auth.decorators import permission_required
3 from django.utils.datastructures import SortedDict
4 from django.shortcuts import render_to_response, get_object_or_404
5 from django.template import RequestContext
6
7
8 def picture_list(request, filter=None, template_name='catalogue/picture_list.html'):
9     """ generates a listing of all books, optionally filtered with a test function """
10
11     pictures_by_author, orphans = Picture.picture_list()
12     books_nav = SortedDict()
13     for tag in pictures_by_author:
14         if pictures_by_author[tag]:
15             books_nav.setdefault(tag.sort_key[0], []).append(tag)
16
17             #    import pdb; pdb.set_trace()
18     return render_to_response(template_name, locals(),
19         context_instance=RequestContext(request))
20
21
22 def picture_detail(request, picture):
23     picture = get_object_or_404(Picture, slug=picture)
24
25     categories = SortedDict()
26     for tag in picture.tags.iterator():
27         categories.setdefault(tag.category, []).append(tag)
28
29     picture_themes = []
30
31     return render_to_response("catalogue/picture_detail.html", locals(),
32                               context_instance=RequestContext(request))
33
34 # =========
35 # = Admin =
36 # =========
37 @permission_required('picture.add_picture')
38 def import_picture(request):
39     """docstring for import_book"""
40     from django.http import HttpResponse
41     from picture.forms import PictureImportForm
42     from django.utils.translation import ugettext as _
43
44     import_form = PictureImportForm(request.POST, request.FILES)
45     if import_form.is_valid():
46         try:
47             import_form.save()
48         except:
49             import sys
50             import pprint
51             import traceback
52             info = sys.exc_info()
53             exception = pprint.pformat(info[1])
54             tb = '\n'.join(traceback.format_tb(info[2]))
55             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
56         return HttpResponse(_("Picture imported successfully"))
57     else:
58         return HttpResponse(_("Error importing file: %r") % import_form.errors)