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