1 import os, tarfile, ConfigParser, datetime
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
16 from exporter import export, CACHE_KEY, EXPORT_STEPS, LAST_BACKUP, DATE_AND_AUTHOR_INF_SECTION, DATETIME_FORMAT
17 from importer import start_import
20 @admin_tools_page(_('exporter'), _('XML data export'))
21 def exporter(request):
22 state = cache.get(CACHE_KEY)
24 if state and state['running']:
25 return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
27 if request.method == 'POST':
28 form = ExporterForm(request.POST)
31 thread = Thread(target=export, args=[form.cleaned_data, request.user])
32 thread.setDaemon(True)
35 return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='exporter')))
41 folder = unicode(selsettings.EXPORTER_BACKUP_STORAGE)
43 for f in os.listdir(folder):
44 if (not os.path.isdir(os.path.join(folder, f))) and f.endswith('.backup.inf'):
46 with open(os.path.join(folder, f), 'r') as inffile:
47 inf = ConfigParser.SafeConfigParser()
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'))):
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)
59 return ('modules/exporter/exporter.html', {
61 'available': available,
65 def running(request, mode):
66 state = cache.get(CACHE_KEY)
68 return HttpResponseRedirect(reverse('admin_tools', args=[_('exporter')]))
70 return ('modules/exporter/running.html', {
76 return HttpResponse(simplejson.dumps(cache.get(CACHE_KEY)), mimetype="application/json")
79 def download(request):
82 if not os.path.exists(fname):
85 response = HttpResponse(open(fname, 'rb').read(), content_type='application/x-gzip')
86 response['Content-Length'] = os.path.getsize(fname)
87 response['Content-Disposition'] = 'attachment; filename=backup.tar.gz'
91 @admin_tools_page(_('importer'), _('XML data restore'))
92 def importer(request):
93 thread = Thread(target=start_import, args=[
94 '/Users/admin/dev/pyenv/osqa/maintain/forum_modules/exporter/backups/localhost-201010121118.tar.gz',
96 'sql2008': 'sql-server-2008',
97 'sql2005': 'sql-server-2005',
98 'sql2000': 'sql-server-2000',
99 'design' : 'database-design',
100 'fulltextsearch' : 'full-text',
101 'access': 'microsoft-access',
102 'subquery': 'sub-query',
103 'count': 'aggregates',
105 'metadata': 'meta-data',
106 'stored-procedure': 'stored-procedures',
107 'temp-tables': 'temporary-tables',
108 'sqlce': 'sql-server-ce',
109 'maintenance-plan': 'maintenance-plans',
110 'meta': 'meta-askssc',
111 'msaccess2000': 'microsoft-access',
112 'agent': 'sql-agent',
113 'udf': 'user-defined-function',
114 'report': 'reporting',
116 'case': 'case-statement',
117 'export': 'export-data',
118 'recursive': 'recursion',
119 'table-variables': 'table-variable',
121 'install': 'installation',
122 'function': 'user-defined-function',
123 'average': 'aggregates',
124 'aggregate-function': 'aggregates',
125 'email': 'database-email',
126 'distinct': 'aggregates',
127 'dynamic-query': 'dynamic',
129 'permission': 'permissions',
130 'shrink': 'shrink-database',
131 'normalise': 'normalization',
132 'datatype-text': 'datatypes',
133 'reporting-services': 'ssrs',
134 'aggregate-sum': 'aggregates',
135 'aggregate-max': 'aggregates',
136 'bulk-import': 'bulk-insert',
137 'attach-database': 'attach',
138 'scripting': 'script',
139 'analysis-services': 'ssas',
140 'create-table': 'create',
141 '2005': 'sql-server-2005'
144 thread.setDaemon(True)
147 return HttpResponseRedirect(reverse('exporter_running', kwargs=dict(mode='importer')))
149 #return ('modules/exporter/importer.html', {