]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/exporter/views.py
resolves OSQA-729, encode OpenID query dict using the django smart unicode utility...
[osqa.git] / forum_modules / exporter / views.py
1 from __future__ import with_statement
2
3 import os, tarfile, ConfigParser, datetime
4
5 from StringIO import StringIO
6 from django.http import HttpResponse, HttpResponseRedirect, Http404
7 from django.utils.translation import ugettext as _
8 from django.utils import simplejson
9 from django.core.cache import cache
10 from django.core.urlresolvers import reverse
11 from forum.views.admin import admin_tools_page, admin_page
12 from forum.models import User
13 from forms import ExporterForm
14 from threading import Thread
15 import settings as selsettings
16 from forum import settings
17
18 from exporter import export, CACHE_KEY, EXPORT_STEPS, DATE_AND_AUTHOR_INF_SECTION, DATETIME_FORMAT
19 from importer import start_import
20
21 @admin_tools_page(_('exporter'), _('XML data export'))
22 def exporter(request):
23     state = cache.get(CACHE_KEY)
24
25     if state and state['running']:
26         return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
27
28     if request.method == 'POST':
29         form = ExporterForm(request.POST)
30
31         if form.is_valid():
32             thread = Thread(target=export, args=[form.cleaned_data, request.user])
33             thread.setDaemon(True)
34             thread.start()
35
36             return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
37     else:
38         form = ExporterForm()
39
40     available = []
41
42     folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE)
43
44     for f in os.listdir(folder):
45         if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.backup.inf'):
46             try:
47                 with open(os.path.join(folder, f), 'r') as inffile:
48                     inf = ConfigParser.SafeConfigParser()
49                     inf.readfp(inffile)
50
51                     if inf.get(DATE_AND_AUTHOR_INF_SECTION, 'site') == settings.APP_URL and os.path.exists(
52                                     os.path.join(folder, inf.get(DATE_AND_AUTHOR_INF_SECTION, 'file-name'))):
53                         available.append({
54                             'author': User.objects.get(id=inf.get(DATE_AND_AUTHOR_INF_SECTION, 'author')),
55                             'date': datetime.datetime.strptime(inf.get(DATE_AND_AUTHOR_INF_SECTION, 'finished'), DATETIME_FORMAT)
56                         })
57             except Exception, e:
58                 pass
59
60     return ('modules/exporter/exporter.html', {
61         'form': form,
62         'available': available,
63     })
64
65 @admin_page
66 def running(request, mode):
67     state = cache.get(CACHE_KEY)
68     if state is None:
69         return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
70
71     return ('modules/exporter/running.html', {
72         'mode': mode,
73         'steps': EXPORT_STEPS
74     })
75
76 def state(request):
77     return HttpResponse(simplejson.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
78
79 @admin_page
80 def download(request):
81     if request.GET and request.GET.get('file', None):
82         fname = os.path.join(selsettings.EXPORTER_BACKUP_STORAGE, request.GET.get('file'))
83     else:
84         raise Http404
85
86     if not os.path.exists(fname):
87         raise Http404
88
89     if fname.endswith('.gz'):
90         content_type='application/x-gzip'
91         filename = 'backup.tar.gz'
92     else:
93         content_type='application/zip'
94         filename = 'backup.zip'
95
96     response = HttpResponse(open(fname, 'rb').read(), content_type=content_type)
97     response['Content-Length'] = os.path.getsize(fname)
98     response['Content-Disposition'] = 'attachment; filename=%s' % filename
99     return response
100
101
102