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 if request.EXPERIMENTS['layout'].value:
20 related_tags = Tag.objects.usage_for_model(Picture, counts=True)
21 related_tags = sorted(related_tags, key=lambda t: -t.count)
22 suggestion_categories = Counter()
24 for t in related_tags:
25 if suggestion_categories[t.category] < 3:
26 suggestion_categories[t.category] += 1
28 if sum(suggestion_categories.values()) == 10:
30 template_name = 'catalogue/2022/author_detail.html'
31 return render(request, template_name, {
32 'object_list': pictures,
35 'suggest': suggestions,
36 'list_type': 'gallery',
39 return render(request, template_name, {'picture_list': list(pictures)})
42 def picture_detail(request, slug):
43 picture = get_object_or_404(Picture, slug=slug)
45 theme_things = split_tags(picture.related_themes())
47 if request.EXPERIMENTS['layout'].value:
48 template_name = 'picture/2022/picture_detail.html'
50 template_name = 'picture/picture_detail.html'
52 return render(request, template_name, {
54 'themes': theme_things.get('theme', []),
55 'things': theme_things.get('thing', []),
56 'active_menu_item': 'gallery',
60 def picture_viewer(request, slug):
61 picture = get_object_or_404(Picture, slug=slug)
63 for sponsor in picture.get_extra_info_json().get('sponsors', []):
64 have_sponsors = Sponsor.objects.filter(name=sponsor)
65 if have_sponsors.exists():
66 sponsors.append(have_sponsors[0])
67 return render(request, "picture/picture_viewer.html", {
74 def picture_page(request, key=None):
75 pictures = Picture.objects.order_by('-id')
77 pictures = pictures.filter(id__lt=key)
78 pictures = pictures[:settings.PICTURE_PAGE_SIZE]
82 'title': picture.title,
83 'author': picture.author_unicode(),
84 'epoch': picture.tag_unicode('epoch'),
85 'kind': picture.tag_unicode('kind'),
86 'genre': picture.tag_unicode('genre'),
87 'style': picture.get_extra_info_json()['style'],
88 'image_url': picture.image_file.url,
89 'width': picture.width,
90 'height': picture.height,
92 for picture in pictures
95 'pictures': picture_data,
96 'count': Picture.objects.count(),
103 @permission_required('picture.add_picture')
104 def import_picture(request):
105 """docstring for import_book"""
106 from django.http import HttpResponse
107 from picture.forms import PictureImportForm
108 from django.utils.translation import gettext as _
110 import_form = PictureImportForm(request.POST, request.FILES)
111 if import_form.is_valid():
118 info = sys.exc_info()
119 exception = pprint.pformat(info[1])
120 tb = '\n'.join(traceback.format_tb(info[2]))
121 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") %
122 {'exception': exception, 'tb': tb}, content_type='text/plain')
123 return HttpResponse(_("Picture imported successfully"))
125 return HttpResponse(_("Error importing file: %r") % import_form.errors)