Example #1
0
 def get_root():
   """Returns the root page."""
   key = 'rootpage'
   root = utility.memcache_get(key)
   if not root:
     root = Page.all().filter('parent_page =', None).get()
     utility.memcache_set(key, root)
   return root
Example #2
0
 def get_root():
   """Returns the root page."""
   key = 'rootpage'
   root = utility.memcache_get(key)
   if not root:
     root = Page.all().filter('parent_page =', None).get()
     utility.memcache_set(key, root)
   return root
Example #3
0
File: main.py Project: pjesi/volta
 def follow_url_backwards(pre_path, post_path):
   """Traverse the path backwards to find a cached page or the root."""
   key = 'path:' + '/'.join(pre_path)
   item = utility.memcache_get(key)
   if item:
     return follow_url_forwards(item, post_path)
   if not pre_path:
     return follow_url_forwards(models.Page.get_root(), post_path)
   return follow_url_backwards(pre_path[:-1], [pre_path[-1]] + post_path)
Example #4
0
 def follow_url_backwards(pre_path, post_path):
     """Traverse the path backwards to find a cached page or the root."""
     key = 'path:' + '/'.join(pre_path)
     item = utility.memcache_get(key)
     if item:
         return follow_url_forwards(item, post_path)
     if not pre_path:
         return follow_url_forwards(models.Page.get_root(), post_path)
     return follow_url_backwards(pre_path[:-1], [pre_path[-1]] + post_path)
Example #5
0
  def all_groups():
    """Returns a list of all of the groups in the system.

    Returns:
      A list of all groups

    """
    key = 'all_groups:'
    groups = utility.memcache_get(key)
    if not groups:
      groups = list(UserGroup.all())
      utility.memcache_set(key, groups)
    return groups
Example #6
0
  def all_groups():
    """Returns a list of all of the groups in the system.

    Returns:
      A list of all groups

    """
    key = 'all_groups:'
    groups = utility.memcache_get(key)
    if not groups:
      groups = list(UserGroup.all())
      utility.memcache_set(key, groups)
    return groups
Example #7
0
  def groups(self):
    """Returns a list of all of the groups the user is in.

    Returns:
      The list of groups the user is in

    """
    key = 'users_groups:%s' % self.key().id()
    groups = utility.memcache_get(key)
    if not groups:
      groups = list(UserGroup.all().filter('users = ', self.key()))
      utility.memcache_set(key, groups)
    return groups
Example #8
0
  def groups(self):
    """Returns a list of all of the groups the user is in.

    Returns:
      The list of groups the user is in

    """
    key = 'users_groups:%s' % self.key().id()
    groups = utility.memcache_get(key)
    if not groups:
      groups = list(UserGroup.all().filter('users = ', self.key()))
      utility.memcache_set(key, groups)
    return groups
Example #9
0
  def all_templates():
    """Returns a list of all of the groups in the system.

    Returns:
      A list of all groups

    """
    key = 'all_templates:'
    templates = utility.memcache_get(key)
    if not templates:
      templates = list(Template.all())
      utility.memcache_set(key, templates)
    return templates
Example #10
0
  def render(profile):
    """Retrieves the HTML for the sidebar.

    This method first checks the memcache layer for rendered HTML based on the
    given profile's access level and returns it if found.  If the HTML is not
    found, the sidebar's definition is loaded and each page is checked for
    existence and if the profile's access level has rights to view the page.
    HTML is then rendered and stored in memcache for future accesses

    Args:
      profile: profile of the user accessing the sidebar

    Returns:
      A string containing the HTML of the sidebar for the given profile's
      access level

    """
    if profile is not None:
      key = 'sidebar:%s' % profile.key().id()        
    else:
      key = 'sidebar'
         
    html = utility.memcache_get(key)
    if html:
      return html

    html = []
    sidebar = Sidebar.load()

    if not sidebar:
      return ''

    for section in yaml.load_all(sidebar.yaml):
      section_html = []

      for item in section['pages']:
        # pylint: disable-msg=E1103
        page = Page.get_by_id(int(item['id']))
        if not page or not page.user_can_read(profile):
          continue
        url = urlresolvers.reverse('views.main.get_url', args=[page.path])
        section_html.append('<li><a href="%s">%s</a></li>\n' %
                            (url, item['title']))

      if section_html:
        html.append('<h1>%s</h1>\n' % section['heading'])
        html.append('<ul>\n%s</ul>\n' % ''.join(section_html))

    html = ''.join(html)
    utility.memcache_set(key, html)
    return html
Example #11
0
  def render(profile):
    """Retrieves the HTML for the sidebar.

    This method first checks the memcache layer for rendered HTML based on the
    given profile's access level and returns it if found.  If the HTML is not
    found, the sidebar's definition is loaded and each page is checked for
    existence and if the profile's access level has rights to view the page.
    HTML is then rendered and stored in memcache for future accesses

    Args:
      profile: profile of the user accessing the sidebar

    Returns:
      A string containing the HTML of the sidebar for the given profile's
      access level

    """
    if profile is not None:
      key = 'sidebar:%s' % profile.key().id()        
    else:
      key = 'sidebar'
         
    html = utility.memcache_get(key)
    if html:
      return html

    html = []
    sidebar = Sidebar.load()

    if not sidebar:
      return ''

    for section in yaml.load_all(sidebar.yaml):
      section_html = []

      for item in section['pages']:
        # pylint: disable-msg=E1103
        page = Page.get_by_id(int(item['id']))
        if not page or not page.user_can_read(profile):
          continue
        url = urlresolvers.reverse('views.main.get_url', args=[page.path])
        section_html.append('<li><a href="%s">%s</a></li>\n' %
                            (url, item['title']))

      if section_html:
        html.append('<h1>%s</h1>\n' % section['heading'])
        html.append('<ul>\n%s</ul>\n' % ''.join(section_html))

    html = ''.join(html)
    utility.memcache_set(key, html)
    return html
Example #12
0
  def attached_files(self):
    """Returns all files attached to the current page.

    Returns:
      A query representing the list of all attached files

    """
    key = 'file-list:%s' % self.key().id()
    file_list = utility.memcache_get(key)
    if not file_list:
      # Convert the iterator to a list for caching
      file_list = list(self.filestore_children.order('name'))
      utility.memcache_set(key, file_list)
    return file_list
Example #13
0
  def attached_files(self):
    """Returns all files attached to the current page.

    Returns:
      A query representing the list of all attached files

    """
    key = 'file-list:%s' % self.key().id()
    file_list = utility.memcache_get(key)
    if not file_list:
      # Convert the iterator to a list for caching
      file_list = list(self.filestore_children.order('name'))
      utility.memcache_set(key, file_list)
    return file_list
Example #14
0
  def __get_acl(self):
    """Returns the ACL for the object by recursion up the path."""
    key = 'acl:%s' % self.key().id()
    acl = utility.memcache_get(key)
    if acl:
      return acl

    if self.acl_data:
      acl = self.acl_data

    if not acl:
      acl = self.parent_page.acl

    utility.memcache_set(key, acl)
    return acl
Example #15
0
  def __get_acl(self):
    """Returns the ACL for the object by recursion up the path."""
    key = 'acl:%s' % self.key().id()
    acl = utility.memcache_get(key)
    if acl:
      return acl

    if self.acl_data:
      acl = self.acl_data

    if not acl:
      acl = self.parent_page.acl

    utility.memcache_set(key, acl)
    return acl
Example #16
0
 def breadcrumbs(self):
   """Returns the HTML representation of the breadcrumbs for the page."""
   key = 'breadcrumbs:%s' % self.key().id()
   breadcrumbs = utility.memcache_get(key)
   
   if breadcrumbs:
     return breadcrumbs
   
   breadcrumbs = []
   if self.parent_page:
     breadcrumbs = self.parent_page.breadcrumbs
     breadcrumbs.append({'path': '/' + self.parent_page.path,
                         'name': self.parent_page.name})
     
   utility.memcache_set(key, breadcrumbs)
   return breadcrumbs 
Example #17
0
  def load(email):
    """Retrieves a given user profile, through memcache if possible.

    Args:
      email: email address of the profile to load

    Returns:
      UserProfile object for the given email address

    """
    key = 'email:' + email
    profile = utility.memcache_get(key)
    if not profile:
      profile = UserProfile.all().filter('email =', email).get()
      utility.memcache_set(key, profile)
    return profile
Example #18
0
 def breadcrumbs(self):
   """Returns the HTML representation of the breadcrumbs for the page."""
   key = 'breadcrumbs:%s' % self.key().id()
   breadcrumbs = utility.memcache_get(key)
   
   if breadcrumbs:
     return breadcrumbs
   
   breadcrumbs = []
   if self.parent_page:
     breadcrumbs = self.parent_page.breadcrumbs
     breadcrumbs.append({'path': '/' + self.parent_page.path,
                         'name': self.parent_page.name})
     
   utility.memcache_set(key, breadcrumbs)
   return breadcrumbs 
Example #19
0
  def load(email):
    """Retrieves a given user profile, through memcache if possible.

    Args:
      email: email address of the profile to load

    Returns:
      UserProfile object for the given email address

    """
    key = 'email:' + email
    profile = utility.memcache_get(key)
    if not profile:
      profile = UserProfile.all().filter('email =', email).get()
      utility.memcache_set(key, profile)
    return profile
Example #20
0
  def __has_access(self, user, access_type):
    """Determines if user has the specified access type.

    Args:
      user: UserProfile to check
      access_type: Type of access to check, either 'read' or 'write'

    Returns:
      True if the user has the requested access, False otherwise

    """
    if user is not None:
      key = 'acl-has-%s:%s-%s' % (access_type, self.key().id(), user.key().id())
    else:
      key = 'acl-has-%s:%s' % (access_type, self.key().id())
    has_access = utility.memcache_get(key)

    if has_access is not None:
      return has_access

    global_access = self.__getattribute__('global_%s' % access_type)
    user_list = self.__getattribute__('user_%s' % access_type)
    group_list = self.__getattribute__('group_%s' % access_type)
    
    if global_access:
      has_access = True
        
    if user is not None:
      if user.is_superuser or user.key() in user_list:
        has_access = True
      else:
        for group in UserGroup.get(group_list):
          if user.key() in group.users:
            has_access = True
            break

    if has_access is None:
      has_access = False

    utility.memcache_set(key, has_access)
    return has_access
Example #21
0
  def __has_access(self, user, access_type):
    """Determines if user has the specified access type.

    Args:
      user: UserProfile to check
      access_type: Type of access to check, either 'read' or 'write'

    Returns:
      True if the user has the requested access, False otherwise

    """
    if user is not None:
      key = 'acl-has-%s:%s-%s' % (access_type, self.key().id(), user.key().id())
    else:
      key = 'acl-has-%s:%s' % (access_type, self.key().id())
    has_access = utility.memcache_get(key)

    if has_access is not None:
      return has_access

    global_access = self.__getattribute__('global_%s' % access_type)
    user_list = self.__getattribute__('user_%s' % access_type)
    group_list = self.__getattribute__('group_%s' % access_type)
    
    if global_access:
      has_access = True
        
    if user is not None:
      if user.is_superuser or user.key() in user_list:
        has_access = True
      else:
        for group in UserGroup.get(group_list):
          if user.key() in group.users:
            has_access = True
            break

    if has_access is None:
      has_access = False

    utility.memcache_set(key, has_access)
    return has_access
Example #22
0
  def contains_page(page):
    """Determines if the page is referenced in the sidebar.

    Args:
      page: Page to check if it exists in the sidebar

    """
    key = 'page-in-sidebar:%s' % page.key().id()
    in_sidebar = utility.memcache_get(key)
    if in_sidebar is not None:
      return in_sidebar

    sidebar = Sidebar.load()

    if sidebar is not None:
      for section in yaml.load_all(sidebar.yaml):
        for item in section['pages']:
          if item['id'] == page.key().id():
            utility.memcache_set(key, True)
            return True

    utility.memcache_set(key, False)
    return False
Example #23
0
  def contains_page(page):
    """Determines if the page is referenced in the sidebar.

    Args:
      page: Page to check if it exists in the sidebar

    """
    key = 'page-in-sidebar:%s' % page.key().id()
    in_sidebar = utility.memcache_get(key)
    if in_sidebar is not None:
      return in_sidebar

    sidebar = Sidebar.load()

    if sidebar is not None:
      for section in yaml.load_all(sidebar.yaml):
        for item in section['pages']:
          if item['id'] == page.key().id():
            utility.memcache_set(key, True)
            return True

    utility.memcache_set(key, False)
    return False