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
12 from exporter import export, CACHE_KEY, EXPORT_STEPS, LAST_BACKUP
15 @admin_tools_page(_('exporter'), _('XML data export'))
16 def exporter(request):
17 state = cache.get(CACHE_KEY)
19 if state and state['running']:
20 return HttpResponseRedirect(reverse('exporter_running'))
22 if request.method == 'POST':
23 form = ExporterForm(request.POST)
26 thread = Thread(target=export, args=[form.cleaned_data])
27 thread.setDaemon(True)
30 return HttpResponseRedirect(reverse('exporter_running'))
34 return ('modules/exporter/exporter.html', {
40 state = cache.get(CACHE_KEY)
42 return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
44 return ('modules/exporter/running.html', {
49 return HttpResponse(simplejson.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
52 def download(request):
55 if not os.path.exists(fname):
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'