X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/c9c3cb4c714911710f606f8dc47d5e65707aff67..f2f41b2266cce4fdadccf7f444ddf25aa99caffa:/forum_modules/exporter/views.py diff --git a/forum_modules/exporter/views.py b/forum_modules/exporter/views.py index fe8086f..edbeedc 100644 --- a/forum_modules/exporter/views.py +++ b/forum_modules/exporter/views.py @@ -1,3 +1,5 @@ +from __future__ import with_statement + import os, tarfile, ConfigParser, datetime from StringIO import StringIO @@ -13,15 +15,15 @@ from threading import Thread import settings as selsettings from forum import settings -from exporter import export, CACHE_KEY, EXPORT_STEPS, LAST_BACKUP, DATE_AND_AUTHOR_INF_SECTION, DATETIME_FORMAT - +from exporter import export, CACHE_KEY, EXPORT_STEPS, DATE_AND_AUTHOR_INF_SECTION, DATETIME_FORMAT +from importer import start_import @admin_tools_page(_('exporter'), _('XML data export')) def exporter(request): state = cache.get(CACHE_KEY) if state and state['running']: - return HttpResponseRedirect(reverse('exporter_running')) + return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter'))) if request.method == 'POST': form = ExporterForm(request.POST) @@ -31,28 +33,29 @@ def exporter(request): thread.setDaemon(True) thread.start() - return HttpResponseRedirect(reverse('exporter_running')) + return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter'))) else: form = ExporterForm() available = [] - #folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE) - - #for f in os.listdir(folder): - # if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.tar.gz'): - # try: - # tar = tarfile.open(os.path.join(folder, f), "r") - # inf = ConfigParser.SafeConfigParser() - # inf.readfp(tar.extractfile('backup.inf')) - # - # if inf.get(DATE_AND_AUTHOR_INF_SECTION, 'site') == settings.APP_URL: - # available.append({ - # 'author': User.objects.get(id=inf.get(DATE_AND_AUTHOR_INF_SECTION, 'author')), - # 'date': datetime.strptime(inf.get(DATE_AND_AUTHOR_INF_SECTION, 'finished'), ) - # }) - # except Exception, e: - # pass + folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE) + + for f in os.listdir(folder): + if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.backup.inf'): + try: + with open(os.path.join(folder, f), 'r') as inffile: + inf = ConfigParser.SafeConfigParser() + inf.readfp(inffile) + + if inf.get(DATE_AND_AUTHOR_INF_SECTION, 'site') == settings.APP_URL and os.path.exists( + os.path.join(folder, inf.get(DATE_AND_AUTHOR_INF_SECTION, 'file-name'))): + available.append({ + 'author': User.objects.get(id=inf.get(DATE_AND_AUTHOR_INF_SECTION, 'author')), + 'date': datetime.datetime.strptime(inf.get(DATE_AND_AUTHOR_INF_SECTION, 'finished'), DATETIME_FORMAT) + }) + except Exception, e: + pass return ('modules/exporter/exporter.html', { 'form': form, @@ -60,12 +63,13 @@ def exporter(request): }) @admin_page -def running(request): +def running(request, mode): state = cache.get(CACHE_KEY) if state is None: return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')])) return ('modules/exporter/running.html', { + 'mode': mode, 'steps': EXPORT_STEPS }) @@ -74,15 +78,25 @@ def state(request): @admin_page def download(request): - fname = LAST_BACKUP + if request.GET and request.GET.get('file', None): + fname = os.path.join(selsettings.EXPORTER_BACKUP_STORAGE, request.GET.get('file')) + else: + raise Http404 if not os.path.exists(fname): raise Http404 - response = HttpResponse(open(fname, 'rb').read(), content_type='application/x-gzip') + if fname.endswith('.gz'): + content_type='application/x-gzip' + filename = 'backup.tar.gz' + else: + content_type='application/zip' + filename = 'backup.zip' + + response = HttpResponse(open(fname, 'rb').read(), content_type=content_type) response['Content-Length'] = os.path.getsize(fname) - response['Content-Disposition'] = 'attachment; filename=backup.tar.gz' + response['Content-Disposition'] = 'attachment; filename=%s' % filename return response - +