1 from __future__ import with_statement
3 import os, tarfile, ConfigParser, datetime
6 from StringIO import StringIO
7 from django.http import HttpResponse, HttpResponseRedirect, Http404
8 from django.utils.translation import ugettext as _
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
18 from exporter import export, CACHE_KEY, EXPORT_STEPS, DATE_AND_AUTHOR_INF_SECTION, DATETIME_FORMAT
19 from importer import start_import
21 @admin_tools_page(_('exporter'), _('XML data export'))
22 def exporter(request):
23 state = cache.get(CACHE_KEY)
25 if state and state['running']:
26 return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
28 if request.method == 'POST':
29 form = ExporterForm(request.POST)
32 thread = Thread(target=export, args=[form.cleaned_data, request.user])
33 thread.setDaemon(True)
36 return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
42 folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE)
44 for f in os.listdir(folder):
45 if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.backup.inf'):
47 with open(os.path.join(folder, f), 'r') as inffile:
48 inf = ConfigParser.SafeConfigParser()
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'))):
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)
60 return ('modules/exporter/exporter.html', {
62 'available': available,
66 def running(request, mode):
67 state = cache.get(CACHE_KEY)
69 return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
71 return ('modules/exporter/running.html', {
77 return HttpResponse(json.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
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'))
86 if not os.path.exists(fname):
89 if fname.endswith('.gz'):
90 content_type='application/x-gzip'
91 filename = 'backup.tar.gz'
93 content_type='application/zip'
94 filename = 'backup.zip'
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