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