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.
5 from django.contrib.auth.decorators import permission_required
6 from django.shortcuts import render_to_response, get_object_or_404, render
7 from django.template import RequestContext
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
15 # # was picture/picture_list.html list (without thumbs)
16 # def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html',
17 # cache_key=None, context=None):
18 # """ generates a listing of all books, optionally filtered with a test function """
22 # pictures_by_author, orphans = Picture.picture_list(filt)
23 # books_nav = OrderedDict()
24 # for tag in pictures_by_author:
25 # if pictures_by_author[tag]:
26 # books_nav.setdefault(tag.sort_key[0], []).append(tag)
28 # return render_to_response(template_name, locals(), context_instance=RequestContext(request))
31 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
32 cache_key=None, context=None):
33 book_list = Picture.objects.all()
35 book_list = book_list.filter(filter)
37 book_list = book_list.filter(get_filter())
38 book_list = book_list.order_by('sort_key_author')
39 book_list = list(book_list)
40 return render_to_response(template_name, locals(), context_instance=RequestContext(request))
43 def picture_detail(request, slug):
44 picture = get_object_or_404(Picture, slug=slug)
46 theme_things = split_tags(picture.related_themes())
48 # categories = SortedDict()
49 # for tag in picture.tags.iterator():
50 # categories.setdefault(tag.category, []).append(tag)
52 themes = theme_things.get('theme', [])
53 things = theme_things.get('thing', [])
55 extra_info = picture.extra_info
57 return render_to_response("picture/picture_detail.html", locals(),
58 context_instance=RequestContext(request))
61 def picture_viewer(request, slug):
62 picture = get_object_or_404(Picture, slug=slug)
64 for sponsor in picture.extra_info.get('sponsors', []):
65 have_sponsors = Sponsor.objects.filter(name=sponsor)
66 if have_sponsors.exists():
67 sponsors.append(have_sponsors[0])
68 return render_to_response("picture/picture_viewer.html", locals(),
69 context_instance=RequestContext(request))
75 @permission_required('picture.add_picture')
76 def import_picture(request):
77 """docstring for import_book"""
78 from django.http import HttpResponse
79 from picture.forms import PictureImportForm
80 from django.utils.translation import ugettext as _
82 import_form = PictureImportForm(request.POST, request.FILES)
83 if import_form.is_valid():
91 exception = pprint.pformat(info[1])
92 tb = '\n'.join(traceback.format_tb(info[2]))
93 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
94 {'exception': exception, 'tb': tb}, mimetype='text/plain')
95 return HttpResponse(_("Picture imported successfully"))
97 return HttpResponse(_("Error importing file: %r") % import_form.errors)
101 def picture_mini(request, pk, with_link=True):
102 picture = get_object_or_404(Picture, pk=pk)
103 return render(request, 'picture/picture_mini_box.html', {
105 'author': picture.author_unicode(),
106 'with_link': with_link,
111 def picture_short(request, pk):
112 picture = get_object_or_404(Picture, pk=pk)
114 return render(request, 'picture/picture_short.html', {
116 'main_link': picture.get_absolute_url(),
118 'tags': split_tags(picture.tags),
123 def picturearea_short(request, pk):
124 area = get_object_or_404(PictureArea, pk=pk)
125 theme = area.tags.filter(category='theme')
126 theme = theme and theme[0] or None
127 thing = area.tags.filter(category='thing')
128 thing = thing and thing[0] or None
129 return render(request, 'picture/picturearea_short.html', locals())