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