Add possibility to download a theme

This commit is contained in:
Frédéric Péters 2011-06-24 08:19:00 +00:00
parent 7ff8cabfcc
commit 927e26e78f
2 changed files with 49 additions and 13 deletions

View File

@ -193,7 +193,8 @@ class SettingsDirectory(QommonSettingsDirectory):
_q_exports = ['', 'themes', 'users',
'template', 'misc', 'emails', 'debug_options', 'language',
('import', 'p_import'), 'export', 'identification', 'sitename',
'sms', 'certificates', 'texts', 'utf8switch', 'upload_theme', 'session']
'sms', 'certificates', 'texts', 'utf8switch', 'upload_theme',
'session', 'download_theme']
certificates = CertificatesDirectory()
emails = EmailsDirectory()
@ -321,6 +322,7 @@ class SettingsDirectory(QommonSettingsDirectory):
if icon:
'<img src="/themes/%s/icon.png" alt="" class="theme-icon" />' % theme
'<p class="details">%s' % desc
' [<a href="download_theme?theme=%s">%s</a>]' % (theme, _('download'))
if author:
'<br/>by %s' % author
'</p>'
@ -339,6 +341,31 @@ class SettingsDirectory(QommonSettingsDirectory):
get_publisher().write_cfg()
return redirect('.')
def download_theme(self):
theme_id = get_request().form.get('theme')
if not theme_id:
return redirect('themes')
theme_directory = qommon.template.get_theme_directory(theme_id)
if not theme_directory:
return redirect('themes')
parent_theme_directory = os.path.dirname(theme_directory)
c = cStringIO.StringIO()
z = zipfile.ZipFile(c, 'w')
for base, dirnames, filenames in os.walk(theme_directory):
basetheme = base[len(parent_theme_directory)+1:]
for filename in filenames:
z.write(os.path.join(base, filename), os.path.join(basetheme, filename))
z.close()
response = get_response()
response.set_content_type('application/zip')
response.set_header('content-disposition', 'attachment; filename=%s.zip' % theme_id)
return c.getvalue()
def upload_theme [html] (self):
form = Form(enctype='multipart/form-data')
form.add(FileWidget, 'file', title = _('Theme'), required = True)

View File

@ -37,23 +37,32 @@ from quixote.html import htmltext
import errors
import ezt
def get_theme_directory(theme_id):
system_location = os.path.join(get_publisher().data_dir, 'themes', theme_id)
local_location = os.path.join(get_publisher().app_dir, 'themes', theme_id)
if os.path.exists(local_location):
location = local_location
elif os.path.exists(system_location):
location = system_location
else:
return None
while os.path.islink(location):
location = os.path.join(os.path.dirname(location), os.readlink(location))
if not os.path.exists(location):
return None
return location
class ThemesDirectory(Directory):
def _q_lookup(self, name):
if name in ('.', '..'):
raise errors.TraversalError()
system_location = os.path.join(get_publisher().data_dir, 'themes', name)
local_location = os.path.join(get_publisher().app_dir, 'themes', name)
if os.path.exists(local_location):
location = local_location
elif os.path.exists(system_location):
location = system_location
else:
raise errors.TraversalError()
while os.path.islink(location):
location = os.path.join(os.path.dirname(location), os.readlink(location))
if not os.path.exists(location):
location = get_theme_directory(name)
if location is None:
raise errors.TraversalError()
if os.path.isdir(location):