35e9b5f8d4ea3d10eedd9a9c3836811004a150da
[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.conf import settings
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
15 # WTF/unused
16 # # was picture/picture_list.html list (without thumbs)
17 # def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html',
18 #                  cache_key=None, context=None):
19 #     """ generates a listing of all books, optionally filtered with a test function """
20 #
21 #     if get_filter:
22 #         filt = get_filter()
23 #     pictures_by_author, orphans = Picture.picture_list(filt)
24 #     books_nav = OrderedDict()
25 #     for tag in pictures_by_author:
26 #         if pictures_by_author[tag]:
27 #             books_nav.setdefault(tag.sort_key[0], []).append(tag)
28 #
29 #     return render_to_response(template_name, locals(), context_instance=RequestContext(request))
30 from wolnelektury.utils import ajax
31
32
33 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
34                        cache_key=None, context=None):
35     book_list = Picture.objects.all()
36     if filter:
37         book_list = book_list.filter(filter)
38     if get_filter:
39         book_list = book_list.filter(get_filter())
40     book_list = book_list.order_by('sort_key_author')
41     book_list = list(book_list)
42     return render_to_response(template_name, {'book_list': book_list}, context_instance=RequestContext(request))
43
44
45 def picture_detail(request, slug):
46     picture = get_object_or_404(Picture, slug=slug)
47
48     theme_things = split_tags(picture.related_themes())
49
50     # categories = SortedDict()
51     # for tag in picture.tags.iterator():
52     #     categories.setdefault(tag.category, []).append(tag)
53
54     return render_to_response("picture/picture_detail.html", {
55         'picture': picture,
56         'themes': theme_things.get('theme', []),
57         'things': theme_things.get('thing', []),
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", {
69         'picture': picture,
70         'sponsors': sponsors,
71     }, context_instance=RequestContext(request))
72
73
74 @ajax(method='get')
75 def picture_page(request, key=None):
76     pictures = Picture.objects.order_by('-id')
77     if key is not None:
78         pictures = pictures.filter(id__lt=key)
79     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
80     picture_data = [
81         {
82             'id': picture.id,
83             'title': picture.title,
84             'author': picture.author_unicode(),
85             'image_url': picture.image_file.url,
86             'width': picture.width,
87             'height': picture.height,
88         }
89         for picture in pictures
90     ]
91     return {
92         'pictures': picture_data,
93         'count': Picture.objects.count(),
94     }
95
96
97 # =========
98 # = Admin =
99 # =========
100 @permission_required('picture.add_picture')
101 def import_picture(request):
102     """docstring for import_book"""
103     from django.http import HttpResponse
104     from picture.forms import PictureImportForm
105     from django.utils.translation import ugettext as _
106
107     import_form = PictureImportForm(request.POST, request.FILES)
108     if import_form.is_valid():
109         try:
110             import_form.save()
111         except:
112             import sys
113             import pprint
114             import traceback
115             info = sys.exc_info()
116             exception = pprint.pformat(info[1])
117             tb = '\n'.join(traceback.format_tb(info[2]))
118             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
119                                 {'exception': exception, 'tb': tb}, mimetype='text/plain')
120         return HttpResponse(_("Picture imported successfully"))
121     else:
122         return HttpResponse(_("Error importing file: %r") % import_form.errors)
123
124
125 @ssi_included
126 def picture_mini(request, pk, with_link=True):
127     picture = get_object_or_404(Picture, pk=pk)
128     author_str = ", ".join(tag.name for tag in picture.tags.filter(category='author'))
129     return render(request, 'picture/picture_mini_box.html', {
130         'picture': picture,
131         'author_str': author_str,
132         'with_link': with_link,
133     })
134
135
136 @ssi_included
137 def picture_short(request, pk):
138     picture = get_object_or_404(Picture, pk=pk)
139
140     return render(request, 'picture/picture_short.html', {
141         'picture': picture,
142         'main_link': picture.get_absolute_url(),
143         'request': request,
144         'tags': split_tags(picture.tags),
145         })
146
147
148 @ssi_included
149 def picturearea_short(request, pk):
150     area = get_object_or_404(PictureArea, pk=pk)
151     themes = area.tags.filter(category='theme')
152     things = area.tags.filter(category='thing')
153     return render(request, 'picture/picturearea_short.html', {
154         'area': area,
155         'theme': themes[0] if themes else None,
156         'thing': things[0] if things else None,
157     })