]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/exporter/views.py
1b9f664656f542706cace3267128c8885aefe973
[osqa.git] / forum_modules / exporter / views.py
1 import os
2
3 from django.http import HttpResponse, HttpResponseRedirect, Http404
4 from django.utils.translation import ugettext as _
5 from django.utils import simplejson
6 from django.core.cache import cache
7 from django.core.urlresolvers import reverse
8 from forum.views.admin import admin_tools_page, admin_page
9 from forms import ExporterForm
10 from threading import Thread
11
12 from exporter import export, CACHE_KEY, EXPORT_STEPS, LAST_BACKUP
13
14
15 @admin_tools_page(_('exporter'), _('XML data export'))
16 def exporter(request):
17     state = cache.get(CACHE_KEY)
18
19     if state and state['running']:
20         return HttpResponseRedirect(reverse('exporter_running'))
21
22     if request.method == 'POST':
23         form = ExporterForm(request.POST)
24
25         if form.is_valid():
26             thread = Thread(target=export, args=[form.cleaned_data])
27             thread.setDaemon(True)
28             thread.start()
29
30             return HttpResponseRedirect(reverse('exporter_running'))
31     else:
32         form = ExporterForm()
33
34     return ('modules/exporter/exporter.html', {
35         'form': form
36     })
37
38 @admin_page
39 def running(request):
40     state = cache.get(CACHE_KEY)
41     if state is None:
42         return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
43
44     return ('modules/exporter/running.html', {
45         'steps': EXPORT_STEPS
46     })
47
48 def state(request):
49     return HttpResponse(simplejson.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
50
51 @admin_page
52 def download(request):
53     fname = LAST_BACKUP
54
55     if not os.path.exists(fname):
56         raise Http404
57
58     response = HttpResponse(open(fname, 'rb').read(), content_type='application/x-gzip')
59     response['Content-Length'] = os.path.getsize(fname)
60     response['Content-Disposition'] = 'attachment; filename=backup.tar.gz'
61     return response
62
63