fixed links in the gallery list
[wolnelektury.git] / src / lesmianator / 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.shortcuts import render_to_response, get_object_or_404
6 from django.template import RequestContext
7 from django.views.decorators import cache
8
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag
11 from lesmianator.models import Poem, Continuations
12
13
14 def main_page(request):
15     last = Poem.objects.all().order_by('-created_at')[:10]
16     shelves = Tag.objects.filter(user__username='lesmianator')
17
18     return render_to_response(
19         'lesmianator/lesmianator.html',
20         {"last": last, "shelves": shelves},
21         context_instance=RequestContext(request))
22
23
24 @cache.never_cache
25 def new_poem(request):
26     user = request.user if request.user.is_authenticated() else None
27     text = Poem.write()
28     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
29     p.save()
30
31     return render_to_response(
32         'lesmianator/poem.html',
33         {"poem": p},
34         context_instance=RequestContext(request))
35
36
37 @cache.never_cache
38 def poem_from_book(request, slug):
39     book = get_object_or_404(Book, slug=slug)
40     user = request.user if request.user.is_authenticated() else None
41     text = Poem.write(Continuations.get(book))
42     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
43     p.created_from = [book.id]
44     p.save()
45
46     return render_to_response(
47         'lesmianator/poem.html',
48         {"poem": p, "books": [book], "book": book},
49         context_instance=RequestContext(request))
50
51
52 @cache.never_cache
53 def poem_from_set(request, shelf):
54     user = request.user if request.user.is_authenticated() else None
55     tag = get_object_or_404(Tag, category='set', slug=shelf)
56     text = Poem.write(Continuations.get(tag))
57     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
58     books = Book.tagged.with_any((tag,))
59     p.created_from = [b.id for b in books]
60     p.save()
61
62     book = books[0] if len(books) == 1 else None
63
64     return render_to_response(
65         'lesmianator/poem.html',
66         {"poem": p, "shelf": tag, "books": books, "book": book},
67         context_instance=RequestContext(request))
68
69
70 def get_poem(request, poem):
71     p = get_object_or_404(Poem, slug=poem)
72     p.visit()
73     if p.created_from:
74         books = Book.objects.filter(id__in=p.created_from)
75         book = books[0] if len(books) == 1 else None
76     else:
77         books = book = None
78
79     return render_to_response(
80         'lesmianator/poem.html',
81         {"poem": p, "books": books, "book": book},
82         context_instance=RequestContext(request))