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