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