]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/sximporter/views.py
merge hernani > trunk
[osqa.git] / forum_modules / sximporter / views.py
1 from django.shortcuts import render_to_response\r
2 from django.template import RequestContext\r
3 from forum.http_responses import HttpResponseUnauthorized\r
4 from forum.models import User\r
5 import importer\r
6 from zipfile import ZipFile\r
7 import os\r
8 \r
9 def sximporter(request):\r
10     if (not User.objects.exists()) or (request.user.is_authenticated() and request.user.is_superuser):\r
11         list = []\r
12         if request.method == "POST" and "dump" in request.FILES:\r
13             dump = ZipFile(request.FILES['dump'])\r
14             members = [f for f in dump.namelist() if f.endswith('.xml')]\r
15             extract_to = os.path.join(os.path.dirname(__file__), 'tmp')\r
16 \r
17             if not os.path.exists(extract_to):\r
18                 os.makedirs(extract_to)\r
19 \r
20             for m in members:\r
21                 f = open(os.path.join(extract_to, m), 'w')\r
22                 f.write(dump.read(m))\r
23                 f.close()\r
24 \r
25             #dump.extractall(extract_to, members)\r
26             dump.close()\r
27 \r
28             options = dict([(k, v) for k, v in request.POST.items()])\r
29             options['authenticated_user'] = (request.user.is_authenticated() and (request.user,) or (None,))[0]\r
30 \r
31             importer.sximport(extract_to, options)\r
32 \r
33         return render_to_response('modules/sximporter/page.html', {\r
34         'names': list\r
35         }, context_instance=RequestContext(request))\r
36     else:\r
37         return HttpResponseUnauthorized(request)\r
38 \r