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