1f87386505c4ef18592138dff4265ca4e6e3f572
[wolnelektury.git] / src / picture / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from collections import OrderedDict
6 from django.contrib.auth.decorators import permission_required
7 from django.shortcuts import render_to_response, get_object_or_404, render
8 from django.template import RequestContext
9 from picture.models import Picture, PictureArea
10 from catalogue.utils import split_tags
11 from ssify import ssi_included
12 from sponsors.models import Sponsor
13
14 # was picture/picture_list.html list (without thumbs)
15 def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html', cache_key=None, context=None):
16     """ generates a listing of all books, optionally filtered with a test function """
17
18     if get_filter:
19         filt = get_filter()
20     pictures_by_author, orphans = Picture.picture_list(filt)
21     books_nav = OrderedDict()
22     for tag in pictures_by_author:
23         if pictures_by_author[tag]:
24             books_nav.setdefault(tag.sort_key[0], []).append(tag)
25
26     return render_to_response(template_name, locals(),
27         context_instance=RequestContext(request))
28
29
30 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html', cache_key=None, context=None):
31     book_list = Picture.objects.all()
32     if filter:
33         book_list = book_list.filter(filter)
34     if get_filter:
35         book_list = book_list.filter(get_filter())
36     book_list = book_list.order_by('sort_key_author')
37     book_list = list(book_list)
38     return render_to_response(template_name, locals(),
39                               context_instance=RequestContext(request))
40
41 def picture_detail(request, slug):
42     picture = get_object_or_404(Picture, slug=slug)
43
44     theme_things = split_tags(picture.related_themes())
45
46     # categories = SortedDict()
47     # for tag in picture.tags.iterator():
48     #     categories.setdefault(tag.category, []).append(tag)
49
50     themes = theme_things.get('theme', [])
51     things = theme_things.get('thing', [])
52
53     extra_info = picture.extra_info
54
55     return render_to_response("picture/picture_detail.html", locals(),
56                               context_instance=RequestContext(request))
57
58
59 def picture_viewer(request, slug):
60     picture = get_object_or_404(Picture, slug=slug)
61     sponsors = []
62     for sponsor in picture.extra_info.get('sponsors', []):
63         have_sponsors = Sponsor.objects.filter(name=sponsor)
64         if have_sponsors.exists():
65             sponsors.append(have_sponsors[0])
66     return render_to_response("picture/picture_viewer.html", locals(),
67                               context_instance=RequestContext(request))
68
69
70 # =========
71 # = Admin =
72 # =========
73 @permission_required('picture.add_picture')
74 def import_picture(request):
75     """docstring for import_book"""
76     from django.http import HttpResponse
77     from picture.forms import PictureImportForm
78     from django.utils.translation import ugettext as _
79
80     import_form = PictureImportForm(request.POST, request.FILES)
81     if import_form.is_valid():
82         try:
83             import_form.save()
84         except:
85             import sys
86             import pprint
87             import traceback
88             info = sys.exc_info()
89             exception = pprint.pformat(info[1])
90             tb = '\n'.join(traceback.format_tb(info[2]))
91             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
92         return HttpResponse(_("Picture imported successfully"))
93     else:
94         return HttpResponse(_("Error importing file: %r") % import_form.errors)
95
96
97 @ssi_included
98 def picture_mini(request, pk, with_link=True):
99     picture = get_object_or_404(Picture, pk=pk)
100     author_str = ", ".join(tag.name
101         for tag in picture.tags.filter(category='author'))
102     return render(request, 'picture/picture_mini_box.html', {
103         'picture': picture,
104         'author_str': author_str,
105         'with_link': with_link,
106     })
107
108
109 @ssi_included
110 def picture_short(request, pk):
111     picture = get_object_or_404(Picture, pk=pk)
112
113     return render(request, 'picture/picture_short.html', {
114         'picture': picture,
115         'main_link': picture.get_absolute_url(),
116         'request': request,
117         'tags': split_tags(picture.tags),
118         })
119
120
121 @ssi_included
122 def picturearea_short(request, pk):
123     area = get_object_or_404(PictureArea, pk=pk)
124     theme = area.tags.filter(category='theme')
125     theme = theme and theme[0] or None
126     thing = area.tags.filter(category='thing')
127     thing = thing and thing[0] or None
128     return render(request, 'picture/picturearea_short.html', locals())