Use secure transport for requirements.
[wolnelektury.git] / apps / 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 collections import OrderedDict
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
13 # was picture/picture_list.html list (without thumbs)
14 def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html', cache_key=None, context=None):
15     """ generates a listing of all books, optionally filtered with a test function """
16
17     if get_filter:
18         filt = get_filter()
19     pictures_by_author, orphans = Picture.picture_list(filt)
20     books_nav = OrderedDict()
21     for tag in pictures_by_author:
22         if pictures_by_author[tag]:
23             books_nav.setdefault(tag.sort_key[0], []).append(tag)
24
25     return render_to_response(template_name, locals(),
26         context_instance=RequestContext(request))
27
28
29 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html', cache_key=None, context=None):
30     book_list = Picture.objects.all()
31     if filter:
32         book_list = book_list.filter(filter)
33     if get_filter:
34         book_list = book_list.filter(get_filter())
35     book_list = book_list.order_by('sort_key_author')
36     book_list = list(book_list)
37     return render_to_response(template_name, locals(),
38                               context_instance=RequestContext(request))
39
40 def picture_detail(request, slug):
41     picture = get_object_or_404(Picture, slug=slug)
42
43     theme_things = split_tags(picture.related_themes())
44
45     # categories = SortedDict()
46     # for tag in picture.tags.iterator():
47     #     categories.setdefault(tag.category, []).append(tag)
48
49     themes = theme_things.get('theme', [])
50     things = theme_things.get('thing', [])
51
52     extra_info = picture.extra_info
53
54     return render_to_response("picture/picture_detail.html", locals(),
55                               context_instance=RequestContext(request))
56
57
58 def picture_viewer(request, slug):
59     picture = get_object_or_404(Picture, slug=slug)
60     return render_to_response("picture/picture_viewer.html", locals(),
61                               context_instance=RequestContext(request))
62
63
64 # =========
65 # = Admin =
66 # =========
67 @permission_required('picture.add_picture')
68 def import_picture(request):
69     """docstring for import_book"""
70     from django.http import HttpResponse
71     from picture.forms import PictureImportForm
72     from django.utils.translation import ugettext as _
73
74     import_form = PictureImportForm(request.POST, request.FILES)
75     if import_form.is_valid():
76         try:
77             import_form.save()
78         except:
79             import sys
80             import pprint
81             import traceback
82             info = sys.exc_info()
83             exception = pprint.pformat(info[1])
84             tb = '\n'.join(traceback.format_tb(info[2]))
85             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
86         return HttpResponse(_("Picture imported successfully"))
87     else:
88         return HttpResponse(_("Error importing file: %r") % import_form.errors)
89
90
91 @ssi_included
92 def picture_short(request, pk):
93     picture = get_object_or_404(Picture, pk=pk)
94
95     return render(request, 'picture/picture_short.html', {
96         'picture': picture,
97         'main_link': picture.get_absolute_url(),
98         'request': request,
99         'tags': split_tags(picture.tags),
100         })
101
102
103 @ssi_included
104 def picturearea_short(request, pk):
105     area = get_object_or_404(PictureArea, pk=pk)
106     theme = area.tags.filter(category='theme')
107     theme = theme and theme[0] or None
108     thing = area.tags.filter(category='thing')
109     thing = thing and thing[0] or None
110     return render(request, 'picture/picturearea_short.html', locals())