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.http import Http404
6 from django.shortcuts import render_to_response, get_object_or_404
7 from django.template import RequestContext
8 from django.contrib.auth.decorators import login_required
9 from django.views.decorators import cache
11 from catalogue.utils import get_random_hash
12 from catalogue.models import Book, Tag
13 from lesmianator.models import Poem, Continuations
16 def main_page(request):
17 last = Poem.objects.all().order_by('-created_at')[:10]
18 shelves = Tag.objects.filter(user__username='lesmianator')
20 return render_to_response('lesmianator/lesmianator.html',
21 {"last": last, "shelves": shelves},
22 context_instance=RequestContext(request))
26 def new_poem(request):
27 user = request.user if request.user.is_authenticated() else None
29 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
32 return render_to_response('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('lesmianator/poem.html',
47 {"poem": p, "books": [book], "book": book},
48 context_instance=RequestContext(request))
52 def poem_from_set(request, shelf):
53 user = request.user if request.user.is_authenticated() else None
54 tag = get_object_or_404(Tag, category='set', slug=shelf)
55 text = Poem.write(Continuations.get(tag))
56 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
57 books = Book.tagged.with_any((tag,))
58 p.created_from = [b.id for b in books]
61 book = books[0] if len(books) == 1 else None
63 return render_to_response('lesmianator/poem.html',
64 {"poem": p, "shelf": tag, "books": books, "book": book},
65 context_instance=RequestContext(request))
67 def get_poem(request, poem):
68 p = get_object_or_404(Poem, slug=poem)
71 books = Book.objects.filter(id__in=p.created_from)
72 book = books[0] if len(books) == 1 else None
76 return render_to_response('lesmianator/poem.html',
77 {"poem": p, "books": books, "book": book},
78 context_instance=RequestContext(request))