Layout fixes, cover update for files.
[wolnelektury.git] / src / picture / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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
14
15
16 def picture_list_thumb(request):
17     pictures = Picture.objects.all()
18         
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()
23         suggestions = []
24         for t in related_tags:
25             if suggestion_categories[t.category] < 3:
26                 suggestion_categories[t.category] += 1
27                 suggestions.append(t)
28                 if sum(suggestion_categories.values()) == 10:
29                     break
30         template_name = 'catalogue/2022/author_detail.html'
31         return render(request, template_name, {
32             'object_list': pictures,
33             'title': 'Galeria',
34             'tags': [],
35             'suggest': suggestions,
36             'list_type': 'gallery',
37         })
38     else:
39         return render(request, template_name, {'picture_list': list(pictures)})
40
41
42 def picture_detail(request, slug):
43     picture = get_object_or_404(Picture, slug=slug)
44
45     theme_things = split_tags(picture.related_themes())
46
47     if request.EXPERIMENTS['layout'].value:
48         template_name = 'picture/2022/picture_detail.html'
49     else:
50         template_name = 'picture/picture_detail.html'
51     
52     return render(request, template_name, {
53         'picture': picture,
54         'themes': theme_things.get('theme', []),
55         'things': theme_things.get('thing', []),
56         'active_menu_item': 'gallery',
57     })
58
59
60 def picture_viewer(request, slug):
61     picture = get_object_or_404(Picture, slug=slug)
62     sponsors = []
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", {
68         'picture': picture,
69         'sponsors': sponsors,
70     })
71
72
73 @ajax(method='get')
74 def picture_page(request, key=None):
75     pictures = Picture.objects.order_by('-id')
76     if key is not None:
77         pictures = pictures.filter(id__lt=key)
78     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
79     picture_data = [
80         {
81             'id': picture.id,
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,
91         }
92         for picture in pictures
93     ]
94     return {
95         'pictures': picture_data,
96         'count': Picture.objects.count(),
97     }
98
99
100 # =========
101 # = Admin =
102 # =========
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 _
109
110     import_form = PictureImportForm(request.POST, request.FILES)
111     if import_form.is_valid():
112         try:
113             import_form.save()
114         except:
115             import sys
116             import pprint
117             import traceback
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"))
124     else:
125         return HttpResponse(_("Error importing file: %r") % import_form.errors)