]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/exporter/views.py
947d8efab748573d917948008e75b6a279d73d1a
[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 from importer import start_import
18
19
20 @admin_tools_page(_('exporter'), _('XML data export'))
21 def exporter(request):
22     state = cache.get(CACHE_KEY)
23
24     if state and state['running']:
25         return HttpResponseRedirect(reverse('exporter_running'))
26
27     if request.method == 'POST':
28         form = ExporterForm(request.POST)
29
30         if form.is_valid():
31             thread = Thread(target=export, args=[form.cleaned_data, request.user])
32             thread.setDaemon(True)
33             thread.start()
34
35             return HttpResponseRedirect(reverse('exporter_running'))
36     else:
37         form = ExporterForm()
38
39     available = []
40
41     folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE)
42
43     for f in os.listdir(folder):
44         if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.backup.inf'):
45             try:
46                 with open(os.path.join(folder, f), 'r') as inffile:
47                     inf = ConfigParser.SafeConfigParser()
48                     inf.readfp(inffile)
49
50                     if inf.get(DATE_AND_AUTHOR_INF_SECTION, 'site') == settings.APP_URL and os.path.exists(
51                                     os.path.join(folder, inf.get(DATE_AND_AUTHOR_INF_SECTION, 'file-name'))):
52                         available.append({
53                             'author': User.objects.get(id=inf.get(DATE_AND_AUTHOR_INF_SECTION, 'author')),
54                             'date': datetime.datetime.strptime(inf.get(DATE_AND_AUTHOR_INF_SECTION, 'finished'), DATETIME_FORMAT)
55                         })
56             except Exception, e:
57                 pass
58
59     return ('modules/exporter/exporter.html', {
60         'form': form,
61         'available': available,
62     })
63
64 @admin_page
65 def running(request):
66     state = cache.get(CACHE_KEY)
67     if state is None:
68         return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
69
70     return ('modules/exporter/running.html', {
71         'steps': EXPORT_STEPS
72     })
73
74 def state(request):
75     return HttpResponse(simplejson.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
76
77 @admin_page
78 def download(request):
79     fname = LAST_BACKUP
80
81     if not os.path.exists(fname):
82         raise Http404
83
84     response = HttpResponse(open(fname, 'rb').read(), content_type='application/x-gzip')
85     response['Content-Length'] = os.path.getsize(fname)
86     response['Content-Disposition'] = 'attachment; filename=backup.tar.gz'
87     return response
88
89
90 @admin_page
91 def importer(request):
92     start_import('/Users/admin/dev/pyenv/osqa/maintain/forum_modules/exporter/backups/localhost-201010121118.tar.gz', request.user)
93
94     return ('modules/exporter/importer.html', {
95
96     })
97
98