dcc380713e2193788a9c1ef602326668be3b2578
[wolnelektury.git] / apps / picture / views.py
1
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 from django.core.paginator import Paginator
7 from picture.models import Picture
8
9
10 def picture_list(request, filter=None, template_name='catalogue/picture_list.html'):
11     """ generates a listing of all books, optionally filtered with a test function """
12
13     pictures_by_author, orphans = Picture.picture_list()
14     books_nav = SortedDict()
15     for tag in pictures_by_author:
16         if pictures_by_author[tag]:
17             books_nav.setdefault(tag.sort_key[0], []).append(tag)
18
19     return render_to_response(template_name, locals(),
20         context_instance=RequestContext(request))
21
22
23 def picture_list_thumb(request, filter=None, template_name='picture/picture_list_thumb.html'):
24     picture_list = Picture.objects.all()
25     return render_to_response(template_name, locals(),
26                               context_instance=RequestContext(request))
27
28 def picture_detail(request, slug):
29     picture = get_object_or_404(Picture, slug=slug)
30
31     categories = SortedDict()
32     for tag in picture.tags.iterator():
33         categories.setdefault(tag.category, []).append(tag)
34
35     picture_themes = []
36
37     return render_to_response("picture/picture_detail.html", locals(),
38                               context_instance=RequestContext(request))
39
40
41 def picture_viewer(request, slug):
42     picture = get_object_or_404(Picture, slug=slug)
43     
44     return render_to_response("picture/picture_viewer.html", locals(),
45                               context_instance=RequestContext(request))
46                               
47
48 # =========
49 # = Admin =
50 # =========
51 @permission_required('picture.add_picture')
52 def import_picture(request):
53     """docstring for import_book"""
54     from django.http import HttpResponse
55     from picture.forms import PictureImportForm
56     from django.utils.translation import ugettext as _
57
58     import_form = PictureImportForm(request.POST, request.FILES)
59     if import_form.is_valid():
60         try:
61             import_form.save()
62         except:
63             import sys
64             import pprint
65             import traceback
66             info = sys.exc_info()
67             exception = pprint.pformat(info[1])
68             tb = '\n'.join(traceback.format_tb(info[2]))
69             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
70         return HttpResponse(_("Picture imported successfully"))
71     else:
72         return HttpResponse(_("Error importing file: %r") % import_form.errors)
73
74