Forgotten deploy step.
[wolnelektury.git] / src / catalogue / views.py
index 5273ab4..2e43bd0 100644 (file)
@@ -20,6 +20,7 @@ from django.utils.translation import ugettext as _, ugettext_lazy
 
 from ajaxable.utils import AjaxableFormView
 from pdcounter import views as pdcounter_views
+from paypal.rest import user_is_subscribed
 from picture.models import Picture, PictureArea
 from ssify import ssi_included, ssi_expect, SsiVariable as Var
 from catalogue import constants
@@ -51,7 +52,6 @@ def book_list(request, filters=None, template_name='catalogue/book_list.html',
     for tag in books_by_author:
         if books_by_author[tag]:
             books_nav.setdefault(tag.sort_key[0], []).append(tag)
-    # WTF: dlaczego nie include?
     return render_to_response(template_name, {
         'rendered_nav': render_to_string(nav_template_name, {'books_nav': books_nav}),
         'rendered_book_list': render_to_string(list_template_name, {
@@ -140,15 +140,7 @@ def object_list(request, objects, fragments=None, related_tags=None, tags=None,
 
 def literature(request):
     books = Book.objects.filter(parent=None)
-
-    # last_published = Book.objects.exclude(cover_thumb='').filter(parent=None).order_by('-created_at')[:20]
-    # most_popular = Book.objects.exclude(cover_thumb='')\
-    #                    .order_by('-popularity__count', 'sort_key_author', 'sort_key')[:20]
     return object_list(request, books, related_tags=get_top_level_related_tags([]))
-    # extra={
-    #     'last_published': last_published,
-    #     'most_popular': most_popular,
-    # })
 
 
 def gallery(request):
@@ -282,7 +274,6 @@ def book_detail(request, slug):
 
     return render_to_response('catalogue/book_detail.html', {
         'book': book,
-        'tags': book.tags.exclude(category__in=('set', 'theme')),
         'book_children': book.children.all().order_by('parent_number', 'sort_key'),
         'active_menu_item': 'books',
     }, context_instance=RequestContext(request))
@@ -307,6 +298,9 @@ def player(request, slug):
 def book_text(request, slug):
     book = get_object_or_404(Book, slug=slug)
 
+    if book.preview and not user_is_subscribed(request.user):
+        return HttpResponseRedirect(book.get_absolute_url())
+
     if not book.has_html_file():
         raise Http404
     return render_to_response('catalogue/book_text.html', {'book': book}, context_instance=RequestContext(request))
@@ -352,6 +346,18 @@ def tag_info(request, tag_id):
     return HttpResponse(tag.description)
 
 
+def embargo_link(request, format_, slug):
+    book = get_object_or_404(Book, slug=slug)
+    if format_ not in Book.formats:
+        raise Http404
+    media_file = book.get_media(format_)
+    if not book.preview:
+        return HttpResponseRedirect(media_file.url)
+    if not user_is_subscribed(request.user):
+        return HttpResponseRedirect(book.get_absolute_url())
+    return HttpResponse(media_file, content_type=constants.EBOOK_CONTENT_TYPES[format_])
+
+
 def download_zip(request, format, slug=None):
     if format in Book.ebook_formats:
         url = Book.zip_format(format)
@@ -379,8 +385,15 @@ class CustomPDFFormView(AjaxableFormView):
         """Override to parse view args and give additional args to the form."""
         return (obj,), {}
 
+    def validate_object(self, obj, request):
+        book = obj
+        if book.preview and not user_is_subscribed(request.user):
+            return HttpResponseRedirect(book.get_absolute_url())
+        return super(CustomPDFFormView, self).validate_object(obj, request)
+
     def get_object(self, request, slug, *args, **kwargs):
-        return get_object_or_404(Book, slug=slug)
+        book = get_object_or_404(Book, slug=slug)
+        return book
 
     def context_description(self, request, obj):
         return obj.pretty_title()
@@ -426,17 +439,9 @@ def book_short(request, pk):
     ))(ssi_expect(pk, int)))
 def book_wide(request, pk):
     book = get_object_or_404(Book, pk=pk)
-    extra_info = book.extra_info
 
     return render(request, 'catalogue/book_wide.html', {
         'book': book,
-        'parents': book.parents(),
-        'tags': split_tags(book.tags.exclude(category__in=('set', 'theme'))),
-        'show_lang': book.language_code() != settings.LANGUAGE_CODE,
-
-        'main_link': reverse('book_text', args=[book.slug]) if book.html_file else None,
-        'extra_info': extra_info,
-        'hide_about': extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl'),
     })