Added import book form to admin.
authorMarek Stępniowski <marek@stepniowski.com>
Wed, 3 Dec 2008 20:13:38 +0000 (21:13 +0100)
committerMarek Stępniowski <marek@stepniowski.com>
Wed, 3 Dec 2008 20:13:38 +0000 (21:13 +0100)
apps/catalogue/views.py
wolnelektury/templates/admin/catalogue/book/change_list.html [new file with mode: 0644]
wolnelektury/urls.py

index 7870cba..b61d5e8 100644 (file)
@@ -7,7 +7,7 @@ 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.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 +23,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):
@@ -355,3 +358,21 @@ 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:
+            # return HttpResponse("Error importing book: %r" % (sys.exc_info(),))
+        return HttpResponse("Book imported successfully")
+    else:
+        return HttpResponse("Error importing file: %r" % book_import_form.errors)
\ No newline at end of file
diff --git a/wolnelektury/templates/admin/catalogue/book/change_list.html b/wolnelektury/templates/admin/catalogue/book/change_list.html
new file mode 100644 (file)
index 0000000..3ded503
--- /dev/null
@@ -0,0 +1,8 @@
+{% extends "admin/change_list.html" %}
+
+{% block content %}
+    <form action="{% url import_book %}" method="post" enctype="multipart/form-data">
+        <p><input type="file" id="id_book_xml_file" name="book_xml_file" /> <input type="submit" value="Importuj książkę"/></p>
+    </form>
+    {{ block.super }}
+{% endblock content %}
\ No newline at end of file
index 58e3c74..a142c73 100644 (file)
@@ -23,6 +23,7 @@ urlpatterns = patterns('',
         name='about_us'),
     
     # Admin panel
         name='about_us'),
     
     # Admin panel
+    url(r'^admin/catalogue/book/import$', 'catalogue.views.import_book', name='import_book'),
     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
     url(r'^admin/(.*)$', admin.site.root),
     
     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
     url(r'^admin/(.*)$', admin.site.root),