def import_compressed_theme_data(self, data): compressed = zipfile.ZipFile(StringIO(data), 'r') paths = compressed.namelist() namespaces = [] main_dir = None for nl in paths: if nl.count('/') == 2: main_dir, nl, _ = nl.split('/') namespaces.append(nl) if not namespaces: raise Exception('No namespaces indentified') else: messages = [] for namespace in namespaces: i = 2 suffix = '' while is_local_theme_namespace(namespace + suffix): suffix = ' ' + str(i) i += 1 while namespace + suffix in self.theme_namespaces: suffix = ' ' + str(i) i += 1 calculated_namespace = namespace + suffix theme = Theme(namespace=calculated_namespace) for p in paths: if p.startswith(main_dir + '/' + namespace + '/templates/') and p.endswith('.body'): filename = p.split(main_dir + '/' + namespace + '/templates/')[1][:-5] theme.body_template_names.append(filename) theme.body_template_contents.append(validated_body_template(zipfile.ZipFile.read(compressed, p))) elif p.startswith(main_dir + '/' + namespace + '/css/') and p.endswith('.css'): filename = p.split(main_dir + '/' + namespace + '/css/')[1] theme.css_filenames.append(filename) theme.css_contents.append(zipfile.ZipFile.read(compressed, p)) elif p.startswith(main_dir + '/' + namespace + '/js/') and p.endswith('.js'): filename = p.split(main_dir + '/' + namespace + '/js/')[1] theme.js_filenames.append(filename) theme.js_contents.append(zipfile.ZipFile.read(compressed, p)) elif p.startswith(main_dir + '/' + namespace + '/images/') and p != main_dir + '/' + namespace + '/images/': filename = p.split(main_dir + '/' + namespace + '/images/')[1] content_type, _ = mimetypes.guess_type(filename) data = zipfile.ZipFile.read(compressed, p) handle = files.blobstore.create(mime_type=content_type, _blobinfo_uploaded_filename=filename) with files.open(handle, 'a') as f: f.write(data) files.finalize(handle) key = files.blobstore.get_blob_key(handle) theme.image_filenames.append(filename) theme.image_keys.append(key) key = theme.put() self.theme_keys.append(key) self.theme_namespaces.append(calculated_namespace) self.update() if messages: raise Exception(messages)
def import_compressed_theme_data(self, data): compressed = zipfile.ZipFile(StringIO(data), 'r') paths = compressed.namelist() namespaces = [] main_dir = None for nl in paths: if nl.count('/') == 2: main_dir, nl, _ = nl.split('/') namespaces.append(nl) if not namespaces: raise Exception('No namespaces indentified') else: messages = [] for namespace in namespaces: i = 2 suffix = '' while is_local_theme_namespace(namespace + suffix): suffix = ' ' + str(i) i += 1 while namespace + suffix in self.theme_namespaces: suffix = ' ' + str(i) i += 1 calculated_namespace = namespace + suffix theme = Theme(namespace=calculated_namespace) for p in paths: if p.startswith(main_dir + '/' + namespace + '/templates/') and p.endswith('.body'): filename = p.split(main_dir + '/' + namespace + '/templates/')[1][:-5] theme.body_template_names.append(filename) theme.body_template_contents.append(validated_body_template(db.Text(zipfile.ZipFile.read(compressed, p)))) elif p.startswith(main_dir + '/' + namespace + '/css/') and p.endswith('.css'): filename = p.split(main_dir + '/' + namespace + '/css/')[1] theme.css_filenames.append(filename) theme.css_contents.append(db.Text(zipfile.ZipFile.read(compressed, p))) elif p.startswith(main_dir + '/' + namespace + '/js/') and p.endswith('.js'): filename = p.split(main_dir + '/' + namespace + '/js/')[1] theme.js_filenames.append(filename) theme.js_contents.append(db.Text(zipfile.ZipFile.read(compressed, p))) elif p.startswith(main_dir + '/' + namespace + '/images/') and p != main_dir + '/' + namespace + '/images/': filename = p.split(main_dir + '/' + namespace + '/images/')[1] content_type, _ = mimetypes.guess_type(filename) data = db.Blob(zipfile.ZipFile.read(compressed, p)) key = File(filename=filename, data=data, content_type=content_type, section_path=self.section.path).put() theme.image_filenames.append(filename) theme.image_keys.append(str(key)) key = theme.put() self.theme_keys.append(str(key)) self.theme_namespaces.append(calculated_namespace) self.update() if messages: raise Exception(messages)
def get_theme(self, namespace): item = None try: key = self.theme_keys[self.theme_namespaces.index(namespace)] item = cache.get(CACHE_KEY_PREPEND + key) if not item: item = Theme.get(key) cache.set(CACHE_KEY_PREPEND + key, item) finally: if not item: raise Exception('NotFound') return item
def action_add(self): message = '' if self.section.handler.request.get('submit'): namespace = self.section.handler.request.get('namespace').replace( '/', '') if not namespace: message = '<div class="status error">Namespace is required</div>' elif namespace in self.theme_namespaces: message = '<div class="status error">Namespace "%s" already exists</div>' % namespace elif is_local_theme_namespace(namespace): message = '<div class="status error">Namespace "%s" is already a local theme namespace</div>' % namespace else: key = Theme(namespace=namespace).put() self.theme_keys.append(key) self.theme_namespaces.append(namespace) self.update() raise Exception('Redirect', self.section.action_redirect_path) f = form(self.section, self.section.full_path) f.add_control( control(self.section, 'text', 'namespace', '', 'Namespace')) f.add_control(control(self.section, 'submit', 'submit', 'Submit')) return '%s<h2>Add theme</h2>%s' % (message, unicode(f))
def import_compressed_theme_data(self, data): compressed = zipfile.ZipFile(StringIO(data), 'r') paths = compressed.namelist() namespaces = [] main_dir = None for nl in paths: if nl.count('/') == 2: main_dir, nl, _ = nl.split('/') namespaces.append(nl) if not namespaces: raise Exception('No namespaces indentified') else: messages = [] for namespace in namespaces: i = 2 suffix = '' while is_local_theme_namespace(namespace + suffix): suffix = ' ' + str(i) i += 1 while namespace + suffix in self.theme_namespaces: suffix = ' ' + str(i) i += 1 calculated_namespace = namespace + suffix theme = Theme(namespace=calculated_namespace) for p in paths: if p.startswith(main_dir + '/' + namespace + '/templates/') and p.endswith('.body'): filename = p.split(main_dir + '/' + namespace + '/templates/')[1][:-5] theme.body_template_names.append(filename) theme.body_template_contents.append( validated_body_template( zipfile.ZipFile.read(compressed, p))) elif p.startswith(main_dir + '/' + namespace + '/css/') and p.endswith('.css'): filename = p.split(main_dir + '/' + namespace + '/css/')[1] theme.css_filenames.append(filename) theme.css_contents.append( zipfile.ZipFile.read(compressed, p)) elif p.startswith(main_dir + '/' + namespace + '/js/') and p.endswith('.js'): filename = p.split(main_dir + '/' + namespace + '/js/')[1] theme.js_filenames.append(filename) theme.js_contents.append( zipfile.ZipFile.read(compressed, p)) elif p.startswith( main_dir + '/' + namespace + '/images/' ) and p != main_dir + '/' + namespace + '/images/': filename = p.split(main_dir + '/' + namespace + '/images/')[1] content_type, _ = mimetypes.guess_type(filename) data = zipfile.ZipFile.read(compressed, p) handle = files.blobstore.create( mime_type=content_type, _blobinfo_uploaded_filename=filename) with files.open(handle, 'a') as f: f.write(data) files.finalize(handle) key = files.blobstore.get_blob_key(handle) theme.image_filenames.append(filename) theme.image_keys.append(key) key = theme.put() self.theme_keys.append(key) self.theme_namespaces.append(calculated_namespace) self.update() if messages: raise Exception(messages)