def get_local_else_global(section_path, namespace): for content_type in get_all_content_types(): item = cache.get( CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace))) if item: return item item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, None, namespace))) if item: return item for content_type in get_all_content_types(): m = __import__('framework.content.' + content_type.lower(), globals(), locals(), [str(content_type.lower())]) concrete = getattr(m, content_type) try: item = concrete.gql( "WHERE section_path IN :1 AND namespace=:2 LIMIT 1", [section_path, None], namespace).fetch(1)[0] cache.set( CACHE_KEY_PREPEND + str(content_key(content_type, item.section_path, namespace)), item) return item except: pass return None
def action_get(self): if not self.section.path_params or len(self.section.path_params) != 3: raise Exception('NotFound') theme = self.get_theme(self.section.path_params[0]) resource = self.section.path_params[1] filename = self.section.path_params[2] if resource == 'css': filenames, contents = theme.css_filenames, theme.css_contents content_type = 'text/css' elif resource == 'js': filenames, contents = theme.js_filenames, theme.js_contents content_type = 'text/javascript' elif resource == 'image': data = None try: key = theme.image_keys[theme.image_filenames.index(filename)] data = cache.get(CACHE_KEY_PREPEND + str(key)) if not data: data = BlobInfo.get(key) cache.set(CACHE_KEY_PREPEND + str(key), data) finally: if not data: raise Exception('NotFound') raise Exception('SendFileBlob', data.open().read(), data.content_type) else: raise Exception('NotFound') try: index = filenames.index(filename) except: raise Exception('NotFound') else: raise Exception('SendFileBlob', str(contents[index]), content_type)
def get_local_else_global(section_path, namespace): for content_type in get_all_content_types(): item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace))) if item: return item item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, None, namespace))) if item: return item for content_type in get_all_content_types(): m = __import__('framework.content.' + content_type.lower(), globals(), locals(), [str(content_type.lower())]) concrete = getattr(m, content_type) try: item = concrete.gql("WHERE section_path IN :1 AND namespace=:2 LIMIT 1", [section_path, None], namespace).fetch(1)[0] cache.set(CACHE_KEY_PREPEND + str(content_key(content_type, item.section_path, namespace)), item) return item except: pass return None
def get_file(self, filename): item = None try: key = self.file_keys[self.filenames.index(filename)] item = cache.get(CACHE_KEY_PREPEND + str(key)) if not item: item = BlobInfo.get(key) cache.set(CACHE_KEY_PREPEND + str(key), item) finally: return item
def get_file(self, filename): item = None try: key = self.file_keys[self.filenames.index(filename)] item = cache.get(CACHE_KEY_PREPEND + key) if not item: item = File.get(key) cache.set(CACHE_KEY_PREPEND + key, item) finally: return item
def get(content_type, section_path, namespace): item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace))) if item: return item m = __import__('framework.content.' + content_type.lower(), globals(), locals(), [str(content_type.lower())]) concrete = getattr(m, content_type) try: item = concrete.query(ancestor=content_key(content_type, section_path, namespace)).fetch(1)[0] cache.set(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace)), item) return item except: return None
def get_theme(self, namespace): item = None try: key = self.theme_keys[self.theme_namespaces.index(namespace)] item = cache.get(CACHE_KEY_PREPEND + str(key)) if not item: item = key.get() cache.set(CACHE_KEY_PREPEND + str(key), item) finally: if not item: raise Exception('NotFound') return item
def get(content_type, section_path, namespace): item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace))) if item: return item m = __import__("framework.content." + content_type.lower(), globals(), locals(), [str(content_type.lower())]) concrete = getattr(m, content_type) try: item = concrete.gql("WHERE ANCESTOR IS :1 LIMIT 1", content_key(content_type, section_path, namespace))[0] cache.set(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace)), item) return item except: return None
def get(content_type, section_path, namespace): item = cache.get(CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace))) if item: return item m = __import__('framework.content.' + content_type.lower(), globals(), locals(), [str(content_type.lower())]) concrete = getattr(m, content_type) try: item = concrete.query(ancestor=content_key(content_type, section_path, namespace)).fetch(1)[0] cache.set( CACHE_KEY_PREPEND + str(content_key(content_type, section_path, namespace)), item) return item except: return None
def get_configuration(): try: item = cache.get(CACHE_KEY) if not item: item = Configuration.gql("").fetch()[0] cache.set(CACHE_KEY, item) return item except: item = Configuration(parent=content_key('Configuration', None, 'configuration'), namespace = 'configuration', SITE_HEADER = 'gae-cms', SITE_SUB_HEADER = 'Python-based Content Management System for Google App Engine', ) item.put() cache.set(CACHE_KEY, item) return item
def get_configuration(): try: item = cache.get(CACHE_KEY) if not item: item = Configuration.gql("").fetch()[0] cache.set(CACHE_KEY, item) return item except: item = Configuration( parent=content_key('Configuration', None, 'configuration'), namespace='configuration', SITE_HEADER='gae-cms', SITE_SUB_HEADER= 'Python-based Content Management System for Google App Engine', ) item.put() cache.set(CACHE_KEY, item) return item
def get(self, path): try: path = path.strip('/') path, extension = os.path.splitext(path) contents = cache.get(path + extension) last_modified = cache.get(CACHE_LAST_MODIFIED_PREPEND + path + extension) if not contents: contents = '' ''' YUI ''' yui_parts = '' if path.find('___yui___') < 0 else path[ path.find('___yui___'):].replace('___yui___', '', 1) yui_parts = yui_parts if yui_parts.find( '___') < 0 else yui_parts[:yui_parts.find('___')] rest_parts = path.replace('___yui___', '', 1).replace( yui_parts, '', 1).replace('___local___', '', 1) if '___theme___' in rest_parts: local_parts, theme_parts = rest_parts.split('___theme___') else: local_parts, theme_parts = rest_parts, None if yui_parts: yui_version = '3.7.2/build/' yui_absolute = 'http://yui.yahooapis.com/combo?' yui_parts = yui_parts.split('__') yui_parts = [(yui_version + x.replace('_', '/') + '-min' + extension) for x in yui_parts] result = urlfetch.fetch(yui_absolute + '&'.join(yui_parts)) if result.status_code == 200: contents += result.content else: raise Exception('NotFound') ''' Local ''' filenames = [(x + extension) for x in local_parts.split('_')] if len(filenames) != len(utils.unique_list(filenames)): raise Exception('NotFound') files = utils.file_search(filenames) if extension == '.css': contents += (''.join([ parse_content(open(f, 'r').read(), True) for f in files ])) else: contents += (''.join( [parse_content(open(f, 'r').read()) for f in files])) ''' Theme ''' if theme_parts: theme_namespace, theme_parts = theme_parts.split('___') filenames = [(x + extension) for x in theme_parts.split('_')] if len(filenames) != len(utils.unique_list(filenames)): raise Exception('NotFound') elif is_local_theme_namespace(theme_namespace): if extension == '.css': contents += (''.join([ parse_content( open( './themes/' + theme_namespace + '/' + extension.strip('.') + '/' + f, 'r').read(), True, theme_namespace) for f in filenames ])) else: contents += (''.join([ parse_content( open( './themes/' + theme_namespace + '/' + extension.strip('.') + '/' + f, 'r').read(), False, theme_namespace) for f in filenames ])) else: t = get_custom_theme(theme_namespace) for f in filenames: if extension == '.css': index = t.css_filenames.index(f) contents += parse_content( t.css_contents[index], True, theme_namespace) elif extension == '.js': index = t.js_filenames.index(f) contents += parse_content( t.js_contents[index], False, theme_namespace) cache.set(path + extension, contents) last_modified = datetime.utcnow() cache.set(CACHE_LAST_MODIFIED_PREPEND + path + extension, last_modified) if not contents.strip(): raise Exception('NotFound') if not last_modified: last_modified = datetime.utcnow() cache.set(CACHE_LAST_MODIFIED_PREPEND + path + extension, last_modified) content_type = 'application/javascript' if extension == '.js' else 'text/css' response = webapp2.Response(template.get(contents.strip()), content_type=content_type) response.headers['Connection'] = 'Keep-Alive' response.headers['Date'] = datetime.utcnow().strftime( "%a, %d %b %Y %H:%M:%S GMT") response.headers['Last-Modified'] = last_modified.strftime( "%a, %d %b %Y %H:%M:%S GMT") response.headers['Expires'] = ( last_modified + timedelta(8)).strftime("%a, %d %b %Y %H:%M:%S GMT") response.cache_control.no_cache = None response.cache_control.public = True response.cache_control.max_age = 604800000 # One week return response except Exception as inst: message = '' if configuration.debug_mode(): message = '<div class="status error">' + unicode( inst) + '<br><br>' + traceback.format_exc().replace( '\n', '<br><br>') + '</div>' response = webapp2.Response( template.get( '<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>Document or file requested by the client was not found.%s</body></html>' % message)) response.set_status(404) return response
def get(self, path): try: path = path.strip('/') path, extension = os.path.splitext(path) contents = cache.get(path + extension) last_modified = cache.get(CACHE_LAST_MODIFIED_PREPEND + path + extension) if not contents: contents = '' ''' YUI ''' yui_parts = '' if path.find('___yui___') < 0 else path[path.find('___yui___'):].replace('___yui___', '', 1) yui_parts = yui_parts if yui_parts.find('___') < 0 else yui_parts[:yui_parts.find('___')] rest_parts = path.replace('___yui___', '', 1).replace(yui_parts, '', 1).replace('___local___', '', 1) if '___theme___' in rest_parts: local_parts, theme_parts = rest_parts.split('___theme___') else: local_parts, theme_parts = rest_parts, None if yui_parts: yui_version = '3.7.2/build/' yui_absolute = 'http://yui.yahooapis.com/combo?' yui_parts = yui_parts.split('__') yui_parts = [(yui_version + x.replace('_', '/') + '-min' + extension) for x in yui_parts] result = urlfetch.fetch(yui_absolute + '&'.join(yui_parts)) if result.status_code == 200: contents += result.content else: raise Exception('NotFound') ''' Local ''' filenames = [(x + extension) for x in local_parts.split('_')] if len(filenames) != len(utils.unique_list(filenames)): raise Exception('NotFound') files = utils.file_search(filenames) if extension == '.css': contents += (''.join([parse_content(open(f, 'r').read(), True) for f in files])) else: contents += (''.join([parse_content(open(f, 'r').read()) for f in files])) ''' Theme ''' if theme_parts: theme_namespace, theme_parts = theme_parts.split('___') filenames = [(x + extension) for x in theme_parts.split('_')] if len(filenames) != len(utils.unique_list(filenames)): raise Exception('NotFound') elif is_local_theme_namespace(theme_namespace): if extension == '.css': contents += (''.join([parse_content(open('./themes/' + theme_namespace + '/' + extension.strip('.') + '/' + f, 'r').read(), True, theme_namespace) for f in filenames])) else: contents += (''.join([parse_content(open('./themes/' + theme_namespace + '/' + extension.strip('.') + '/' + f, 'r').read(), False, theme_namespace) for f in filenames])) else: t = get_custom_theme(theme_namespace) for f in filenames: if extension == '.css': index = t.css_filenames.index(f) contents += parse_content(t.css_contents[index], True, theme_namespace) elif extension == '.js': index = t.js_filenames.index(f) contents += parse_content(t.js_contents[index], False, theme_namespace) cache.set(path + extension, contents) last_modified = datetime.utcnow() cache.set(CACHE_LAST_MODIFIED_PREPEND + path + extension, last_modified) if not contents.strip(): raise Exception('NotFound') if not last_modified: last_modified = datetime.utcnow() cache.set(CACHE_LAST_MODIFIED_PREPEND + path + extension, last_modified) content_type = 'application/javascript' if extension == '.js' else 'text/css' response = webapp2.Response(template.get(contents.strip()), content_type=content_type) response.headers['Connection'] = 'Keep-Alive' response.headers['Date'] = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") response.headers['Last-Modified'] = last_modified.strftime("%a, %d %b %Y %H:%M:%S GMT") response.headers['Expires'] = (last_modified + timedelta(8)).strftime("%a, %d %b %Y %H:%M:%S GMT") response.cache_control.no_cache = None response.cache_control.public = True response.cache_control.max_age = 604800000 # One week return response except Exception as inst: message = '' if configuration.debug_mode(): message = '<div class="status error">' + unicode(inst) + '<br><br>' + traceback.format_exc().replace('\n', '<br><br>') + '</div>' response = webapp2.Response(template.get('<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>Document or file requested by the client was not found.%s</body></html>' % message)) response.set_status(404) return response
def get_custom_themes(): custom_themes = cache.get(CACHE_KEY) if not custom_themes: custom_themes = Theme.query().fetch() cache.set(CACHE_KEY, custom_themes) return custom_themes
def get_custom_themes(): custom_themes = cache.get(CACHE_KEY) if not custom_themes: custom_themes = Theme.gql("") cache.set(CACHE_KEY, custom_themes) return custom_themes
def get_top_level(): hierarchy = copy.deepcopy(cache.get(CACHE_KEY_HIERARCHY)) if not hierarchy: hierarchy = db_get_hierarchy() cache.set(CACHE_KEY_HIERARCHY, hierarchy) return hierarchy