tests fix
[redakcja.git] / apps / catalogue / views.py
index aa214de..df7c2bf 100644 (file)
@@ -5,7 +5,7 @@ from StringIO import StringIO
 
 from django.contrib import auth
 from django.contrib.auth.models import User
 
 from django.contrib import auth
 from django.contrib.auth.models import User
-from django.contrib.auth.decorators import login_required
+from django.contrib.auth.decorators import login_required, permission_required
 from django.core.urlresolvers import reverse
 from django.db.models import Count, Q
 from django import http
 from django.core.urlresolvers import reverse
 from django.db.models import Count, Q
 from django import http
@@ -19,6 +19,7 @@ from django.views.generic.simple import direct_to_template
 import librarian.html
 import librarian.text
 
 import librarian.html
 import librarian.text
 
+from apiclient import NotAuthorizedError
 from catalogue import forms
 from catalogue import helpers
 from catalogue.helpers import active_tab
 from catalogue import forms
 from catalogue import helpers
 from catalogue.helpers import active_tab
@@ -74,6 +75,7 @@ def logout_then_redirect(request):
     return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
 
 
     return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
 
 
+@permission_required('catalogue.add_book')
 @active_tab('create')
 def create_missing(request, slug=None):
     if slug is None:
 @active_tab('create')
 def create_missing(request, slug=None):
     if slug is None:
@@ -88,12 +90,12 @@ def create_missing(request, slug=None):
                 creator = request.user
             else:
                 creator = None
                 creator = request.user
             else:
                 creator = None
-            book = Book.objects.create(
+            book = Book.create(
+                text=form.cleaned_data['text'],
+                creator=creator,
                 slug=form.cleaned_data['slug'],
                 title=form.cleaned_data['title'],
             )
                 slug=form.cleaned_data['slug'],
                 title=form.cleaned_data['title'],
             )
-            book.chunk_set.all().update(creator=creator)
-            book[0].commit(text=form.cleaned_data['text'], author=creator)
 
             return http.HttpResponseRedirect(reverse("wiki_editor", args=[book.slug]))
     else:
 
             return http.HttpResponseRedirect(reverse("wiki_editor", args=[book.slug]))
     else:
@@ -108,6 +110,7 @@ def create_missing(request, slug=None):
     })
 
 
     })
 
 
+@permission_required('catalogue.add_book')
 @active_tab('upload')
 def upload(request):
     if request.method == "POST":
 @active_tab('upload')
 def upload(request):
     if request.method == "POST":
@@ -147,10 +150,11 @@ def upload(request):
 
             if not error_list:
                 for filename, slug, title in ok_list:
 
             if not error_list:
                 for filename, slug, title in ok_list:
-                    Book.create(creator=creator,
+                    book = Book.create(
+                        text=zip.read(filename).decode('utf-8'),
+                        creator=creator,
                         slug=slug,
                         title=title,
                         slug=slug,
                         title=title,
-                        text=zip.read(filename).decode('utf-8'),
                     )
 
             return direct_to_template(request, "catalogue/document_upload.html", extra_context={
                     )
 
             return direct_to_template(request, "catalogue/document_upload.html", extra_context={
@@ -293,8 +297,19 @@ def book(request, slug):
     else:
         form = None
 
     else:
         form = None
 
+    try:
+        book.assert_publishable()
+    except AssertionError, e:
+        publishable = False
+        publishable_error = e
+    else:
+        publishable = True
+        publishable_error = None
+
     return direct_to_template(request, "catalogue/book_detail.html", extra_context={
         "book": book,
     return direct_to_template(request, "catalogue/book_detail.html", extra_context={
         "book": book,
+        "publishable": publishable,
+        "publishable_error": publishable_error,
         "chunks": chunks,
         "need_fixing": need_fixing,
         "choose_master": choose_master,
         "chunks": chunks,
         "need_fixing": need_fixing,
         "choose_master": choose_master,
@@ -303,6 +318,7 @@ def book(request, slug):
     })
 
 
     })
 
 
+@permission_required('catalogue.add_chunk')
 def chunk_add(request, slug, chunk):
     try:
         doc = Chunk.get(slug, chunk)
 def chunk_add(request, slug, chunk):
     try:
         doc = Chunk.get(slug, chunk)
@@ -352,22 +368,24 @@ def chunk_edit(request, slug, chunk):
     })
 
 
     })
 
 
+@permission_required('catalogue.change_book')
 def book_append(request, slug):
     book = get_object_or_404(Book, slug=slug)
     if request.method == "POST":
 def book_append(request, slug):
     book = get_object_or_404(Book, slug=slug)
     if request.method == "POST":
-        form = forms.BookAppendForm(request.POST)
+        form = forms.BookAppendForm(book, request.POST)
         if form.is_valid():
             append_to = form.cleaned_data['append_to']
             append_to.append(book)
             return http.HttpResponseRedirect(append_to.get_absolute_url())
     else:
         if form.is_valid():
             append_to = form.cleaned_data['append_to']
             append_to.append(book)
             return http.HttpResponseRedirect(append_to.get_absolute_url())
     else:
-        form = forms.BookAppendForm()
+        form = forms.BookAppendForm(book)
     return direct_to_template(request, "catalogue/book_append_to.html", extra_context={
         "book": book,
         "form": form,
     })
 
 
     return direct_to_template(request, "catalogue/book_append_to.html", extra_context={
         "book": book,
         "form": form,
     })
 
 
+@permission_required('catalogue.change_book')
 def book_edit(request, slug):
     book = get_object_or_404(Book, slug=slug)
     if request.method == "POST":
 def book_edit(request, slug):
     book = get_object_or_404(Book, slug=slug)
     if request.method == "POST":
@@ -389,6 +407,8 @@ def publish(request, slug):
     book = get_object_or_404(Book, slug=slug)
     try:
         book.publish(request.user)
     book = get_object_or_404(Book, slug=slug)
     try:
         book.publish(request.user)
+    except NotAuthorizedError:
+        return http.HttpResponseRedirect(reverse('apiclient_oauth'))
     except BaseException, e:
         return http.HttpResponse(e)
     else:
     except BaseException, e:
         return http.HttpResponse(e)
     else: