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