def customize_theme(self, vars): logger.debug('Customizing theme with variables: %s', vars) self.compile_css(self.get_current_theme(), vars, self.css_filename) vars_available = self.find_less_variables(self.get_current_theme(), flat=True) vars_diff = {} for key in vars: if key in vars_available and len(vars[key].strip()) > 0 and vars[key] != vars_available[key]: logger.debug('Customizing: %s -> %s', key, vars[key]) vars_diff[key] = vars[key] logger.debug('Customized %d variables for theme %s', len(vars_diff), self.get_current_theme()) Tag.setTag('current_theme_params', value=json.dumps(vars_diff)) # Clear the Varnish cache varnish.purge_all()
def load_theme(self, theme_name, **kwargs): # Create template overrides using data provided (our models handle versioning) logger.debug('Loading theme: %s', theme_name) for template_name in self.get_template_names(theme_name): # Read default template override contents provided by theme to = TemplateOverride(name=template_name) template_filename = os.path.join(self.base_dir(theme_name), 'templates', template_name) template_file = open(template_filename, 'r') to.content = template_file.read() # Add a Django template comment tag indicating theme type to the main.html override (for tests) if to.name == 'main.html': to.content += ( '\n{%% comment %%} Theme: %s {%% endcomment %%}\n' % theme_name) to.save() logger.debug('-- Created template override: %s', template_name) # Clear template override cache TemplateOverrideLoader.get_template_hash.delete_all() # Collect LESS files from appropriate sources and compile CSS self.compile_css(theme_name, {}, self.css_filename) # Copy images and script files to the active theme directory img_src_dir = os.path.join(self.base_dir(theme_name), 'images') if os.path.exists(img_src_dir): img_dest_dir = os.path.join(settings.MEDIA_ROOT, 'images', 'theme') distutils.dir_util.copy_tree(img_src_dir, img_dest_dir) script_src_dir = os.path.join(self.base_dir(theme_name), 'scripts') if os.path.exists(script_src_dir): script_dest_dir = os.path.join(settings.MEDIA_ROOT, 'scripts', 'theme') distutils.dir_util.copy_tree(script_src_dir, script_dest_dir) # If files need to be restored, copy them back to the desired locations. if kwargs.get('backup_info', None) is not None: self.restore_files(settings.MEDIA_ROOT, kwargs['backup_info']) Tag.setTag('current_theme_name', value=theme_name) Tag.setTag('current_theme_params', value='{}') Tag.unSetTag('current_theme_palette') self.unset_current_customization() # Clear the Varnish cache varnish.purge_all()
def clear_theme(self, theme_name=None, keep_files=None): if theme_name is None: theme_name = self.get_current_theme() # Remove template overrides matching the theme name logger.debug('Clearing theme: %s', theme_name) for template_name in self.get_template_names(theme_name): TemplateOverride.objects.filter(name=template_name).delete() logger.debug('-- Removed template override: %s', template_name) # Clear template override cache TemplateOverrideLoader.get_template_hash.delete_all() # If files are to be preserved, copy them to temporary locations # and return a record of those locations (backup_info). # This is much easier than writing new functions for removing and # copying directory trees. backup_info = self.backup_files(settings.MEDIA_ROOT, keep_files) if os.path.exists(settings.MEDIA_ROOT + 'images/theme'): distutils.dir_util.remove_tree(settings.MEDIA_ROOT + 'images/theme') if os.path.exists(settings.MEDIA_ROOT + 'scripts/theme'): distutils.dir_util.remove_tree(settings.MEDIA_ROOT + 'scripts/theme') # Remove compiled CSS file if os.path.exists(self.css_filename): os.remove(self.css_filename) Tag.unSetTag('current_theme_name') Tag.unSetTag('current_theme_params') Tag.unSetTag('current_theme_palette') self.unset_current_customization() # Clear the Varnish cache varnish.purge_all() return backup_info