def create_section(path, parent_path=None, name='', title='', keywords='', description='', theme='', is_private=False, is_default=False, redirect_to='', new_window=False, force=False): path = path.replace('/', '-').replace(' ', '-').strip() if path else None parent_path = parent_path.replace('/', '-').replace(' ', '-').strip() if parent_path else None if not force and not can_path_exist(path, parent_path): return None if is_default: try: old_default = Section.query(Section.is_default == True).fetch(1)[0] old_default.is_default=False old_default.put() except: pass # Probably no sections yet max_rank = 0 for item, _ in get_children(parent_path): if item['rank'] <= max_rank: max_rank = item['rank'] + 1 default_theme = configuration.default_theme() if not default_theme: default_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE if theme == default_theme: theme = '' section = Section(parent=section_key(path), path=path, parent_path=parent_path, rank=max_rank, name=name, title=title, keywords=keywords, description=description, theme=(theme if theme != template.DEFAULT_LOCAL_THEME_TEMPLATE else ''), is_private=is_private, redirect_to=redirect_to, new_window=new_window, is_default=is_default) section.put() cache.delete(CACHE_KEY_HIERARCHY) return section
def view_themes_previewer(self, params=None): if not configuration.theme_preview_enabled(): return '' combined_themes = get_local_theme_namespaces( ) + get_custom_theme_namespaces() if self.section.handler and self.section.handler.request.get( 'submit_themes_previewer'): selected_theme = self.section.handler.request.get( 'TEMPLATE_OVERRIDE_THEME') elif self.section.theme: selected_theme = self.section.theme else: selected_theme = configuration.default_theme() if not selected_theme: selected_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE f = form(self.section, self.section.full_path) f.add_control( selectcontrol(self.section, 'TEMPLATE_OVERRIDE_THEME', combined_themes, selected_theme)) f.add_control( control(self.section, 'submit', 'submit_themes_previewer', 'Preview theme')) self.section.css.append('themes-previewer.css') return '<div class="content themes-previewer">%s</div>' % unicode(f)
def update_section(old, path, parent_path, name, title, keywords, description, theme, is_private, is_default, redirect_to, new_window): path = path.replace('/', '-').replace(' ', '-').strip() if path else None parent_path = parent_path.replace('/', '-').replace(' ', '-').strip() if parent_path else None default_theme = configuration.default_theme() if not default_theme: default_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE if theme == default_theme: theme = '' if old.is_default: # Cannot change the default page except if another page is promoted is_default = True elif not old.is_default and is_default: old_default = Section.query(Section.is_default == True).fetch(1)[0] if old_default.path != old.path: old_default.is_default = False old_default.put() can_path_exist(path, parent_path, old.path) if old.path != path: for child in Section.query(Section.parent_path == old.path).fetch(): child.parent_path = path child.put() content.rename_section_paths(old.path, path) new = Section(parent=section_key(path), path=path, parent_path=parent_path, rank=old.rank, name=name, title=title, keywords=keywords, description=description, theme=theme, is_private=is_private, is_default=is_default, redirect_to=redirect_to, new_window=new_window) old.key.delete() new.put() cache.delete(CACHE_KEY_HIERARCHY) return new elif old.parent_path != parent_path: # Rerank old's siblings to account for its removal by pushing it to the end of the old sibling list update_section_rank(old, len(get_siblings(old.path))) # Make it the last of its new siblings max_rank = 0 for item, _ in get_children(parent_path): if item['rank'] <= max_rank: max_rank = item['rank'] + 1 old.rank = max_rank old.parent_path = parent_path old.name = name old.title = title old.keywords = keywords old.description = description old.theme = theme old.is_private = is_private old.is_default = is_default old.redirect_to = redirect_to old.new_window = new_window old.put() cache.delete(CACHE_KEY_HIERARCHY) return old
def get_section(handler, full_path): full_path = full_path.strip('/') path_parts = full_path.split('/') path = path_parts[0] path_namespace = path_parts[1] if len(path_parts) > 1 else None path_action = path_parts[2] if len(path_parts) > 2 else None path_params = path_parts[3:] if len(path_parts) > 3 else None try: if not path: section = Section.query(Section.is_default == True).fetch(1)[0] else: section = Section.query(ancestor=section_key(path)).fetch(1)[0] if section.is_default and path and not path_action: raise Exception('NotFound') # The default page with no action should only be accessible from root except: if not get_top_level(): section = create_section(path=FIRST_RUN_HOME_PATH, name='Home', title='GAE-CMS', is_default=True, force=True) else: raise Exception('NotFound', path, path_action, path_params) section.handler = handler section.full_path = full_path section.action_redirect_path = '/' + (path if not section.is_default else '') section.path_namespace = path_namespace section.path_action = path_action section.path_params = path_params section.classes = ['yui3-skin-sam path-' + section.path] if path_namespace: section.classes.append('content-' + path_namespace) if path_action: section.classes.append('action-' + path_action) default_theme = configuration.default_theme() if not default_theme: default_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE section.theme_namespace, section.theme_template = (section.theme if section.theme else default_theme).split('/') section.yuicss = [] section.themecss = [] section.css = ['core.css'] section.yuijs = [] section.localthemejs = [] section.js = [] section.viewport_content = None section.mobile_ua = utils.mobile_ua(section) section.logout_url = users.create_logout_url('/' + section.path if not section.is_default else '') section.login_url = users.create_login_url('/' + section.path if not section.is_default else '') section.has_siblings = len(get_siblings(section.path)) > 1 section.has_children = len(get_children(section.path)) > 0 section.configuration = configuration.get_configuration()._to_dict() return section
def get_theme_namespace_template(self): TEMPLATE_OVERRIDE_THEME = self.handler.request.get('TEMPLATE_OVERRIDE_THEME') if self.handler and self.handler.request.get('TEMPLATE_OVERRIDE_THEME') else None DEFAULT_THEME = configuration.default_theme() if TEMPLATE_OVERRIDE_THEME and configuration.theme_preview_enabled(): theme_namespace_template = TEMPLATE_OVERRIDE_THEME elif not self.theme and not DEFAULT_THEME: theme_namespace_template = DEFAULT_LOCAL_THEME_TEMPLATE elif not self.theme: theme_namespace_template = DEFAULT_THEME else: theme_namespace_template = self.theme return theme_namespace_template
def view_themes_previewer(self, params=None): if not configuration.theme_preview_enabled(): return '' combined_themes = get_local_theme_namespaces() + get_custom_theme_namespaces() if self.section.handler and self.section.handler.request.get('submit_themes_previewer'): selected_theme = self.section.handler.request.get('TEMPLATE_OVERRIDE_THEME') elif self.section.theme: selected_theme = self.section.theme else: selected_theme = configuration.default_theme() if not selected_theme: selected_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE f = form(self.section, self.section.full_path) f.add_control(selectcontrol(self.section, 'TEMPLATE_OVERRIDE_THEME', combined_themes, selected_theme)) f.add_control(control(self.section, 'submit', 'submit_themes_previewer', 'Preview theme')) self.section.css.append('themes-previewer.css') return '<div class="content themes-previewer">%s</div>' % unicode(f)
def get_form(s, path, parent_path, name=None, title=None, keywords=None, description=None, theme=None, is_private=False, is_default=False, redirect_to=None, new_window=False): f = form(s, s.full_path) f.add_control(control(s, 'text', 'path', path, 'Path')) f.add_control(control(s, 'text', 'parent_path', parent_path if parent_path else '', 'Parent path')) f.add_control(control(s, 'text', 'name', name if name else '', 'Name', 30)) f.add_control(control(s, 'text', 'title', title if title else '', 'Title', 60)) f.add_control(textareacontrol(s, 'keywords', keywords if keywords else '', 'Keywords', 60, 5)) f.add_control(textareacontrol(s, 'description', description if description else '', 'Description', 60, 5)) combined_themes = get_local_theme_namespaces() + get_custom_theme_namespaces() default_theme = configuration.default_theme() if not default_theme: default_theme = template.DEFAULT_LOCAL_THEME_TEMPLATE f.add_control(selectcontrol(s, 'theme', combined_themes, theme if theme else default_theme, 'Theme')) f.add_control(checkboxcontrol(s, 'is_private', is_private, 'Is private')) if not is_default: f.add_control(checkboxcontrol(s, 'is_default', is_default, 'Is default')) f.add_control(control(s, 'text', 'redirect_to', redirect_to if redirect_to else '', 'Redirect to', 60)) f.add_control(checkboxcontrol(s, 'new_window', new_window, 'New window')) f.add_control(control(s, 'submit', 'submit')) return unicode(f)