admin: remove obsolete files when uploading a new theme version (#3024)

This commit is contained in:
Frédéric Péters 2013-06-06 17:11:03 +02:00
parent c3c1c4c362
commit efca84df46
1 changed files with 16 additions and 2 deletions

View File

@ -26,6 +26,7 @@ except ImportError:
lasso = None
import zipfile
import base64
import shutil
try:
import elementtree.ElementTree as ET
@ -468,8 +469,21 @@ class SettingsDirectory(QommonSettingsDirectory):
get_session().message = ('error', _('Failed to read theme file.'))
return redirect('themes')
theme_dir = os.path.join(get_publisher().app_dir, 'themes')
# TODO: should check and read desc.xml first; and if theme already
# exists, it should remove the whole directory
filename_list = [x for x in z.namelist() if x[0] != '/' and x[-1] != '/']
if len(filename_list) == 0:
get_session().message = ('error', _('Empty theme file.'))
return redirect('themes')
theme_name = filename_list[0].split('/')[0]
if not ('%s/desc.xml' % theme_name) in filename_list:
get_session().message = ('error', _('Theme is missing a desc.xml file.'))
return redirect('themes')
desc_xml = z.read('%s/desc.xml' % theme_name)
theme_dict = qommon.template.get_theme_dict(cStringIO.StringIO(desc_xml))
if theme_dict.get('name') != theme_name:
get_session().message = ('error', _('desc.xml is missing a name attribute.'))
return redirect('themes')
if os.path.exists(os.path.join(theme_dir, theme_name)):
shutil.rmtree(os.path.join(theme_dir, theme_name))
for f in z.namelist():
if f[-1] == '/':
continue