35f0f3d44fa7d450bb578e295709b3e48df67f3d
[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     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()
22     suggestions = []
23     for t in related_tags:
24         if suggestion_categories[t.category] < 3:
25             suggestion_categories[t.category] += 1
26             suggestions.append(t)
27             if sum(suggestion_categories.values()) == 10:
28                 break
29     template_name = 'catalogue/2022/author_detail.html'
30     return render(request, template_name, {
31         'object_list': pictures,
32         'title': 'Galeria',
33         'tags': [],
34         'suggest': suggestions,
35         'list_type': 'gallery',
36     })
37
38
39 def picture_detail(request, slug):
40     picture = get_object_or_404(Picture, slug=slug)
41
42     theme_things = split_tags(picture.related_themes())
43
44     template_name = 'picture/2022/picture_detail.html'
45     
46     return render(request, template_name, {
47         'picture': picture,
48         'themes': theme_things.get('theme', []),
49         'things': theme_things.get('thing', []),
50         'active_menu_item': 'gallery',
51     })
52
53
54 def picture_viewer(request, slug):
55     picture = get_object_or_404(Picture, slug=slug)
56     sponsors = []
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", {
62         'picture': picture,
63         'sponsors': sponsors,
64     })
65
66
67 @ajax(method='get')
68 def picture_page(request, key=None):
69     pictures = Picture.objects.order_by('-id')
70     if key is not None:
71         pictures = pictures.filter(id__lt=key)
72     pictures = pictures[:settings.PICTURE_PAGE_SIZE]
73     picture_data = [
74         {
75             'id': picture.id,
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,
85         }
86         for picture in pictures
87     ]
88     return {
89         'pictures': picture_data,
90         'count': Picture.objects.count(),
91     }
92
93
94 # =========
95 # = Admin =
96 # =========
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 _
103
104     import_form = PictureImportForm(request.POST, request.FILES)
105     if import_form.is_valid():
106         try:
107             import_form.save()
108         except:
109             import sys
110             import pprint
111             import traceback
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"))
118     else:
119         return HttpResponse(_("Error importing file: %r") % import_form.errors)