Preparing for Django 1.10
[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, get_object_or_404, render
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 from wolnelektury.utils import ajax
13
14
15 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
16                        cache_key=None, context=None):
17     pictures = Picture.objects.all()
18     if filter:
19         pictures = pictures.filter(filter)
20     if get_filter:
21         pictures = pictures.filter(get_filter())
22     return render(request, template_name, {'book_list': list(pictures)})
23
24
25 def picture_detail(request, slug):
26     picture = get_object_or_404(Picture, slug=slug)
27
28     theme_things = split_tags(picture.related_themes())
29
30     return render(request, "picture/picture_detail.html", {
31         'picture': picture,
32         'themes': theme_things.get('theme', []),
33         'things': theme_things.get('thing', []),
34         'active_menu_item': 'gallery',
35     })
36
37
38 def picture_viewer(request, slug):
39     picture = get_object_or_404(Picture, slug=slug)
40     sponsors = []
41     for sponsor in picture.extra_info.get('sponsors', []):
42         have_sponsors = Sponsor.objects.filter(name=sponsor)
43         if have_sponsors.exists():
44             sponsors.append(have_sponsors[0])
45     return render(request, "picture/picture_viewer.html", {
46         'picture': picture,
47         'sponsors': sponsors,
48     })
49
50
51 @ajax(method='get')
52 def picture_page(request, key=None):
53     pictures = Picture.objects.order_by('-id')
54     if key is not None:
55         pictures = pictures.filter(id__lt=key)
56     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
57     picture_data = [
58         {
59             'id': picture.id,
60             'title': picture.title,
61             'author': picture.author_unicode(),
62             'epoch': picture.tag_unicode('epoch'),
63             'kind': picture.tag_unicode('kind'),
64             'genre': picture.tag_unicode('genre'),
65             'style': picture.extra_info['style'],
66             'image_url': picture.image_file.url,
67             'width': picture.width,
68             'height': picture.height,
69         }
70         for picture in pictures
71     ]
72     return {
73         'pictures': picture_data,
74         'count': Picture.objects.count(),
75     }
76
77
78 # =========
79 # = Admin =
80 # =========
81 @permission_required('picture.add_picture')
82 def import_picture(request):
83     """docstring for import_book"""
84     from django.http import HttpResponse
85     from picture.forms import PictureImportForm
86     from django.utils.translation import ugettext as _
87
88     import_form = PictureImportForm(request.POST, request.FILES)
89     if import_form.is_valid():
90         try:
91             import_form.save()
92         except:
93             import sys
94             import pprint
95             import traceback
96             info = sys.exc_info()
97             exception = pprint.pformat(info[1])
98             tb = '\n'.join(traceback.format_tb(info[2]))
99             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
100                                 {'exception': exception, 'tb': tb}, mimetype='text/plain')
101         return HttpResponse(_("Picture imported successfully"))
102     else:
103         return HttpResponse(_("Error importing file: %r") % import_form.errors)
104
105
106 @ssi_included
107 def picture_mini(request, pk, with_link=True):
108     picture = get_object_or_404(Picture, pk=pk)
109     return render(request, 'picture/picture_mini_box.html', {
110         'picture': picture,
111         'author': picture.author_unicode(),
112         'with_link': with_link,
113     })
114
115
116 @ssi_included
117 def picture_short(request, pk):
118     picture = get_object_or_404(Picture, pk=pk)
119
120     return render(request, 'picture/picture_short.html', {
121         'picture': picture,
122     })
123
124
125 @ssi_included
126 def picturearea_short(request, pk):
127     area = get_object_or_404(PictureArea, pk=pk)
128     themes = area.tags.filter(category='theme')
129     things = area.tags.filter(category='thing')
130     return render(request, 'picture/picturearea_short.html', {
131         'area': area,
132         'theme': themes[0] if themes else None,
133         'thing': things[0] if things else None,
134     })