allow empty part name in bookmedia
[wolnelektury.git] / src / picture / views.py
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.
4 #
5 from django.conf import settings
6 from django.contrib.auth.decorators import permission_required
7 from django.shortcuts import render_to_response, get_object_or_404, render
8 from django.template import RequestContext
9 from picture.models import Picture, PictureArea
10 from catalogue.utils import split_tags
11 from ssify import ssi_included
12 from sponsors.models import Sponsor
13
14
15 # WTF/unused
16 # # was picture/picture_list.html list (without thumbs)
17 # def picture_list(request, filter=None, get_filter=None, template_name='catalogue/picture_list.html',
18 #                  cache_key=None, context=None):
19 #     """ generates a listing of all books, optionally filtered with a test function """
20 #
21 #     if get_filter:
22 #         filt = get_filter()
23 #     pictures_by_author, orphans = Picture.picture_list(filt)
24 #     books_nav = OrderedDict()
25 #     for tag in pictures_by_author:
26 #         if pictures_by_author[tag]:
27 #             books_nav.setdefault(tag.sort_key[0], []).append(tag)
28 #
29 #     return render_to_response(template_name, locals(), context_instance=RequestContext(request))
30 from wolnelektury.utils import ajax
31
32
33 def picture_list_thumb(request, filter=None, get_filter=None, template_name='picture/picture_list_thumb.html',
34                        cache_key=None, context=None):
35     pictures = Picture.objects.all()
36     if filter:
37         pictures = pictures.filter(filter)
38     if get_filter:
39         pictures = pictures.filter(get_filter())
40     return render_to_response(template_name, {'book_list': list(pictures)}, context_instance=RequestContext(request))
41
42
43 def picture_detail(request, slug):
44     picture = get_object_or_404(Picture, slug=slug)
45
46     theme_things = split_tags(picture.related_themes())
47
48     # categories = SortedDict()
49     # for tag in picture.tags.iterator():
50     #     categories.setdefault(tag.category, []).append(tag)
51
52     return render_to_response("picture/picture_detail.html", {
53         'picture': picture,
54         'themes': theme_things.get('theme', []),
55         'things': theme_things.get('thing', []),
56         'active_menu_item': 'gallery',
57     }, context_instance=RequestContext(request))
58
59
60 def picture_viewer(request, slug):
61     picture = get_object_or_404(Picture, slug=slug)
62     sponsors = []
63     for sponsor in picture.extra_info.get('sponsors', []):
64         have_sponsors = Sponsor.objects.filter(name=sponsor)
65         if have_sponsors.exists():
66             sponsors.append(have_sponsors[0])
67     return render_to_response("picture/picture_viewer.html", {
68         'picture': picture,
69         'sponsors': sponsors,
70     }, context_instance=RequestContext(request))
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.extra_info['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 ugettext 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}, mimetype='text/plain')
123         return HttpResponse(_("Picture imported successfully"))
124     else:
125         return HttpResponse(_("Error importing file: %r") % import_form.errors)
126
127
128 @ssi_included
129 def picture_mini(request, pk, with_link=True):
130     picture = get_object_or_404(Picture, pk=pk)
131     return render(request, 'picture/picture_mini_box.html', {
132         'picture': picture,
133         'author': picture.author_unicode(),
134         'with_link': with_link,
135     })
136
137
138 @ssi_included
139 def picture_short(request, pk):
140     picture = get_object_or_404(Picture, pk=pk)
141
142     return render(request, 'picture/picture_short.html', {
143         'picture': picture,
144         'main_link': picture.get_absolute_url(),
145         'request': request,
146         'tags': split_tags(picture.tags),
147         })
148
149
150 @ssi_included
151 def picturearea_short(request, pk):
152     area = get_object_or_404(PictureArea, pk=pk)
153     themes = area.tags.filter(category='theme')
154     things = area.tags.filter(category='thing')
155     return render(request, 'picture/picturearea_short.html', {
156         'area': area,
157         'theme': themes[0] if themes else None,
158         'thing': things[0] if things else None,
159     })