Add option for smaller font in banners.
[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
8 from catalogue.utils import split_tags
9 from sponsors.models import Sponsor
10 from wolnelektury.utils import ajax
11
12
13 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
14                        cache_key=None, context=None):
15     pictures = Picture.objects.all()
16     if filter:
17         pictures = pictures.filter(filter)
18     if get_filter:
19         pictures = pictures.filter(get_filter())
20     return render(request, template_name, {'picture_list': list(pictures)})
21
22
23 def picture_detail(request, slug):
24     picture = get_object_or_404(Picture, slug=slug)
25
26     theme_things = split_tags(picture.related_themes())
27
28     return render(request, "picture/picture_detail.html", {
29         'picture': picture,
30         'themes': theme_things.get('theme', []),
31         'things': theme_things.get('thing', []),
32         'active_menu_item': 'gallery',
33     })
34
35
36 def picture_viewer(request, slug):
37     picture = get_object_or_404(Picture, slug=slug)
38     sponsors = []
39     for sponsor in picture.get_extra_info_json().get('sponsors', []):
40         have_sponsors = Sponsor.objects.filter(name=sponsor)
41         if have_sponsors.exists():
42             sponsors.append(have_sponsors[0])
43     return render(request, "picture/picture_viewer.html", {
44         'picture': picture,
45         'sponsors': sponsors,
46     })
47
48
49 @ajax(method='get')
50 def picture_page(request, key=None):
51     pictures = Picture.objects.order_by('-id')
52     if key is not None:
53         pictures = pictures.filter(id__lt=key)
54     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
55     picture_data = [
56         {
57             'id': picture.id,
58             'title': picture.title,
59             'author': picture.author_unicode(),
60             'epoch': picture.tag_unicode('epoch'),
61             'kind': picture.tag_unicode('kind'),
62             'genre': picture.tag_unicode('genre'),
63             'style': picture.get_extra_info_json()['style'],
64             'image_url': picture.image_file.url,
65             'width': picture.width,
66             'height': picture.height,
67         }
68         for picture in pictures
69     ]
70     return {
71         'pictures': picture_data,
72         'count': Picture.objects.count(),
73     }
74
75
76 # =========
77 # = Admin =
78 # =========
79 @permission_required('picture.add_picture')
80 def import_picture(request):
81     """docstring for import_book"""
82     from django.http import HttpResponse
83     from picture.forms import PictureImportForm
84     from django.utils.translation import ugettext as _
85
86     import_form = PictureImportForm(request.POST, request.FILES)
87     if import_form.is_valid():
88         try:
89             import_form.save()
90         except:
91             import sys
92             import pprint
93             import traceback
94             info = sys.exc_info()
95             exception = pprint.pformat(info[1])
96             tb = '\n'.join(traceback.format_tb(info[2]))
97             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
98                                 {'exception': exception, 'tb': tb}, mimetype='text/plain')
99         return HttpResponse(_("Picture imported successfully"))
100     else:
101         return HttpResponse(_("Error importing file: %r") % import_form.errors)