]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/sximporter/views.py
Merge pull request #2 from udacity/update_django
[osqa.git] / forum_modules / sximporter / views.py
index 95e6826ca497ba43dc93b4a6378b8d524500a5af..fd597b77f513437ae89449911f37c677a3716c74 100644 (file)
@@ -1,27 +1,38 @@
-from django.shortcuts import render_to_response\r
-from django.template import RequestContext\r
-from forum.views.admin import super_user_required\r
-import importer\r
-from zipfile import ZipFile\r
-import os\r
-\r
-@super_user_required\r
-def sximporter(request):\r
-    list = []\r
-    if request.method == "POST" and "dump" in request.FILES:\r
-        dump = ZipFile(request.FILES['dump'])\r
-        members = [f for f in dump.namelist() if f.endswith('.xml')]\r
-        extract_to = os.path.join(os.path.dirname(__file__), 'tmp')\r
-\r
-        for m in members:\r
-            f = open(os.path.join(extract_to, m), 'w')\r
-            f.write(dump.read(m))\r
-            f.close()\r
-\r
-        #dump.extractall(extract_to, members)\r
-        dump.close()\r
-        importer.sximport(extract_to, request.POST)\r
-\r
-    return render_to_response('modules/sximporter/page.html', {\r
-    'names': list\r
-    }, context_instance=RequestContext(request))
\ No newline at end of file
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from forum.http_responses import HttpResponseUnauthorized
+from forum.models import User
+import importer
+from zipfile import ZipFile
+import os
+
+def sximporter(request):
+    if (not User.objects.exists()) or (request.user.is_authenticated() and request.user.is_superuser):
+        list = []
+        if request.method == "POST" and "dump" in request.FILES:
+            dump = ZipFile(request.FILES['dump'])
+            members = [f for f in dump.namelist() if f.endswith('.xml')]
+            extract_to = os.path.join(os.path.dirname(__file__), 'tmp')
+
+            if not os.path.exists(extract_to):
+                os.makedirs(extract_to)
+
+            for m in members:
+                f = open(os.path.join(extract_to, m), 'w')
+                f.write(dump.read(m))
+                f.close()
+
+            #dump.extractall(extract_to, members)
+            dump.close()
+
+            options = dict([(k, v) for k, v in request.POST.items()])
+            options['authenticated_user'] = (request.user.is_authenticated() and (request.user,) or (None,))[0]
+
+            importer.sximport(extract_to, options)
+
+        return render_to_response('modules/sximporter/page.html', {
+        'names': list
+        }, context_instance=RequestContext(request))
+    else:
+        return HttpResponseUnauthorized(request)
+