98485afec005555fa74aaae527ffc0b024b160bd
[wolnelektury.git] / src / picture / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django.contrib.auth.decorators import permission_required
6 from django.shortcuts import render, get_object_or_404, render
7 from picture.models import Picture, PictureArea
8 from catalogue.utils import split_tags
9 from ssify import ssi_included
10 from sponsors.models import Sponsor
11 from wolnelektury.utils import ajax
12
13
14 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
15                        cache_key=None, context=None):
16     pictures = Picture.objects.all()
17     if filter:
18         pictures = pictures.filter(filter)
19     if get_filter:
20         pictures = pictures.filter(get_filter())
21     return render(request, template_name, {'book_list': list(pictures)})
22
23
24 def picture_detail(request, slug):
25     picture = get_object_or_404(Picture, slug=slug)
26
27     theme_things = split_tags(picture.related_themes())
28
29     return render(request, "picture/picture_detail.html", {
30         'picture': picture,
31         'themes': theme_things.get('theme', []),
32         'things': theme_things.get('thing', []),
33         'active_menu_item': 'gallery',
34     })
35
36
37 def picture_viewer(request, slug):
38     picture = get_object_or_404(Picture, slug=slug)
39     sponsors = []
40     for sponsor in picture.extra_info.get('sponsors', []):
41         have_sponsors = Sponsor.objects.filter(name=sponsor)
42         if have_sponsors.exists():
43             sponsors.append(have_sponsors[0])
44     return render(request, "picture/picture_viewer.html", {
45         'picture': picture,
46         'sponsors': sponsors,
47     })
48
49
50 @ajax(method='get')
51 def picture_page(request, key=None):
52     pictures = Picture.objects.order_by('-id')
53     if key is not None:
54         pictures = pictures.filter(id__lt=key)
55     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
56     picture_data = [
57         {
58             'id': picture.id,
59             'title': picture.title,
60             'author': picture.author_unicode(),
61             'epoch': picture.tag_unicode('epoch'),
62             'kind': picture.tag_unicode('kind'),
63             'genre': picture.tag_unicode('genre'),
64             'style': picture.extra_info['style'],
65             'image_url': picture.image_file.url,
66             'width': picture.width,
67             'height': picture.height,
68         }
69         for picture in pictures
70     ]
71     return {
72         'pictures': picture_data,
73         'count': Picture.objects.count(),
74     }
75
76
77 # =========
78 # = Admin =
79 # =========
80 @permission_required('picture.add_picture')
81 def import_picture(request):
82     """docstring for import_book"""
83     from django.http import HttpResponse
84     from picture.forms import PictureImportForm
85     from django.utils.translation import ugettext as _
86
87     import_form = PictureImportForm(request.POST, request.FILES)
88     if import_form.is_valid():
89         try:
90             import_form.save()
91         except:
92             import sys
93             import pprint
94             import traceback
95             info = sys.exc_info()
96             exception = pprint.pformat(info[1])
97             tb = '\n'.join(traceback.format_tb(info[2]))
98             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
99                                 {'exception': exception, 'tb': tb}, mimetype='text/plain')
100         return HttpResponse(_("Picture imported successfully"))
101     else:
102         return HttpResponse(_("Error importing file: %r") % import_form.errors)
103
104
105 @ssi_included
106 def picture_short(request, pk):
107     picture = get_object_or_404(Picture, pk=pk)
108
109     return render(request, 'picture/picture_short.html', {
110         'picture': picture,
111     })
112
113
114 @ssi_included
115 def picturearea_short(request, pk):
116     area = get_object_or_404(PictureArea, pk=pk)
117     themes = area.tags.filter(category='theme')
118     things = area.tags.filter(category='thing')
119     return render(request, 'picture/picturearea_short.html', {
120         'area': area,
121         'theme': themes[0] if themes else None,
122         'thing': things[0] if things else None,
123     })