Wypisywanie błędów przy importowaniu książek (obecnie wypisujemy po prostu wyjątek...
[wolnelektury.git] / apps / catalogue / views.py
index 1718d97..cc7669b 100644 (file)
@@ -1,13 +1,16 @@
 # -*- coding: utf-8 -*-
 import tempfile
 import zipfile
 # -*- coding: utf-8 -*-
 import tempfile
 import zipfile
+import sys
+import pprint
+import traceback
 
 from django.template import RequestContext
 from django.shortcuts import render_to_response, get_object_or_404
 from django.http import HttpResponse, HttpResponseRedirect, Http404
 from django.core.urlresolvers import reverse
 from django.db.models import Q
 
 from django.template import RequestContext
 from django.shortcuts import render_to_response, get_object_or_404
 from django.http import HttpResponse, HttpResponseRedirect, Http404
 from django.core.urlresolvers import reverse
 from django.db.models import Q
-from django.contrib.auth.decorators import login_required
+from django.contrib.auth.decorators import login_required, user_passes_test
 from django.utils.datastructures import SortedDict
 from django.views.decorators.http import require_POST
 from django.contrib import auth
 from django.utils.datastructures import SortedDict
 from django.views.decorators.http import require_POST
 from django.contrib import auth
@@ -23,6 +26,9 @@ from catalogue.utils import split_tags
 from newtagging import views as newtagging_views
 
 
 from newtagging import views as newtagging_views
 
 
+staff_required = user_passes_test(lambda user: user.is_staff)
+
+
 class LazyEncoder(simplejson.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, Promise):
 class LazyEncoder(simplejson.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, Promise):
@@ -115,6 +121,7 @@ def book_detail(request, slug):
     book_children = book.children.all().order_by('parent_number')
     extra_where = 'catalogue_tag.category = "theme"'
     book_themes = models.Tag.objects.related_for_model(book_tag, models.Fragment, counts=True, extra={'where': [extra_where]})
     book_children = book.children.all().order_by('parent_number')
     extra_where = 'catalogue_tag.category = "theme"'
     book_themes = models.Tag.objects.related_for_model(book_tag, models.Fragment, counts=True, extra={'where': [extra_where]})
+    extra_info = book.get_extra_info_value()
     
     form = forms.SearchForm()
     return render_to_response('catalogue/book_detail.html', locals(),
     
     form = forms.SearchForm()
     return render_to_response('catalogue/book_detail.html', locals(),
@@ -354,3 +361,24 @@ def logout_then_redirect(request):
     auth.logout(request)
     return HttpResponseRedirect(request.GET.get('next', '/'))
 
     auth.logout(request)
     return HttpResponseRedirect(request.GET.get('next', '/'))
 
+
+
+# =========
+# = Admin =
+# =========
+@login_required
+@staff_required
+def import_book(request):
+    """docstring for import_book"""
+    book_import_form = forms.BookImportForm(request.POST, request.FILES)
+    if book_import_form.is_valid():
+        try:
+            book_import_form.save()
+        except:
+            info = sys.exc_info()
+            exception = pprint.pformat(info[1])
+            tb = '\n'.join(traceback.format_tb(info[2]))
+            return HttpResponse("An error occurred: %s\n\n%s" % (exception, tb), mimetype='text/plain')
+        return HttpResponse("Book imported successfully")
+    else:
+        return HttpResponse("Error importing file: %r" % book_import_form.errors)
\ No newline at end of file