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.
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
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag
11 from lesmianator.models import Poem, Continuations
14 def main_page(request):
15 last = Poem.objects.all().order_by('-created_at')[:10]
16 shelves = Tag.objects.filter(user__username='lesmianator')
18 return render_to_response(
19 'lesmianator/lesmianator.html',
20 {"last": last, "shelves": shelves},
21 context_instance=RequestContext(request))
25 def new_poem(request):
26 user = request.user if request.user.is_authenticated() else None
28 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
31 return render_to_response(
32 'lesmianator/poem.html',
34 context_instance=RequestContext(request))
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]
46 return render_to_response(
47 'lesmianator/poem.html',
48 {"poem": p, "books": [book], "book": book},
49 context_instance=RequestContext(request))
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]
62 book = books[0] if len(books) == 1 else None
64 return render_to_response(
65 'lesmianator/poem.html',
66 {"poem": p, "shelf": tag, "books": books, "book": book},
67 context_instance=RequestContext(request))
70 def get_poem(request, poem):
71 p = get_object_or_404(Poem, slug=poem)
74 books = Book.objects.filter(id__in=p.created_from)
75 book = books[0] if len(books) == 1 else None
79 return render_to_response(
80 'lesmianator/poem.html',
81 {"poem": p, "books": books, "book": book},
82 context_instance=RequestContext(request))