1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from collections import Counter
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
9 from catalogue.models import Tag
10 from catalogue.utils import split_tags
11 from sponsors.models import Sponsor
12 from wolnelektury.utils import ajax
13 from catalogue.helpers import get_top_level_related_tags
16 def picture_list_thumb(request):
17 pictures = Picture.objects.all()
19 related_tags = Tag.objects.usage_for_model(Picture, counts=True)
20 related_tags = sorted(related_tags, key=lambda t: -t.count)
21 suggestion_categories = Counter()
23 for t in related_tags:
24 if suggestion_categories[t.category] < 3:
25 suggestion_categories[t.category] += 1
27 if sum(suggestion_categories.values()) == 10:
29 template_name = 'catalogue/2022/author_detail.html'
30 return render(request, template_name, {
31 'object_list': pictures,
34 'suggest': suggestions,
35 'list_type': 'gallery',
39 def picture_detail(request, slug):
40 picture = get_object_or_404(Picture, slug=slug)
42 theme_things = split_tags(picture.related_themes())
44 template_name = 'picture/2022/picture_detail.html'
46 return render(request, template_name, {
48 'themes': theme_things.get('theme', []),
49 'things': theme_things.get('thing', []),
50 'active_menu_item': 'gallery',
54 def picture_viewer(request, slug):
55 picture = get_object_or_404(Picture, slug=slug)
57 for sponsor in picture.get_extra_info_json().get('sponsors', []):
58 have_sponsors = Sponsor.objects.filter(name=sponsor)
59 if have_sponsors.exists():
60 sponsors.append(have_sponsors[0])
61 return render(request, "picture/picture_viewer.html", {
68 def picture_page(request, key=None):
69 pictures = Picture.objects.order_by('-id')
71 pictures = pictures.filter(id__lt=key)
72 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
76 'title': picture.title,
77 'author': picture.author_unicode(),
78 'epoch': picture.tag_unicode('epoch'),
79 'kind': picture.tag_unicode('kind'),
80 'genre': picture.tag_unicode('genre'),
81 'style': picture.get_extra_info_json()['style'],
82 'image_url': picture.image_file.url,
83 'width': picture.width,
84 'height': picture.height,
86 for picture in pictures
89 'pictures': picture_data,
90 'count': Picture.objects.count(),
97 @permission_required('picture.add_picture')
98 def import_picture(request):
99 """docstring for import_book"""
100 from django.http import HttpResponse
101 from picture.forms import PictureImportForm
102 from django.utils.translation import gettext as _
104 import_form = PictureImportForm(request.POST, request.FILES)
105 if import_form.is_valid():
112 info = sys.exc_info()
113 exception = pprint.pformat(info[1])
114 tb = '\n'.join(traceback.format_tb(info[2]))
115 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
116 {'exception': exception, 'tb': tb}, content_type='text/plain')
117 return HttpResponse(_("Picture imported successfully"))
119 return HttpResponse(_("Error importing file: %r") % import_form.errors)