Back to index

moin  1.9.0~rc2
wiki_groups.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - wiki group backend
00004 
00005 The wiki_groups backend allows to define groups on wiki pages. See
00006 SystemPagesGroup as example of a group page.
00007 
00008 Normally, the name of the group page has to end with Group like
00009 FriendsGroup. This lets MoinMoin recognize it as a group. This default
00010 pattern could be changed (e.g. for non-english languages etc.), see
00011 HelpOnConfiguration.
00012 
00013 MoinMoin.formatter.groups is used to extract group members from a
00014 page.
00015 
00016 
00017 @copyright: 2008 MoinMoin:ThomasWaldmann,
00018             2009 MoinMoin:DmitrijsMilajevs
00019 @license: GPL, see COPYING for details
00020 """
00021 
00022 from MoinMoin import caching, wikiutil
00023 from MoinMoin.Page import Page
00024 from MoinMoin.datastruct.backends import GreedyGroup, BaseGroupsBackend, GroupDoesNotExistError
00025 from MoinMoin.formatter.groups import Formatter
00026 
00027 
00028 class WikiGroup(GreedyGroup):
00029 
00030     def _load_group(self):
00031         request = self.request
00032         group_name = self.name
00033 
00034         page = Page(request, group_name)
00035         if page.exists():
00036             arena = 'pagegroups'
00037             key = wikiutil.quoteWikinameFS(group_name)
00038             cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
00039             try:
00040                 cache_mtime = cache.mtime()
00041                 page_mtime = wikiutil.version2timestamp(page.mtime_usecs())
00042                 # TODO: fix up-to-date check mtime granularity problems.
00043                 #
00044                 # cache_mtime is float while page_mtime is integer
00045                 # The comparision needs to be done on the lowest type of both
00046                 if int(cache_mtime) > int(page_mtime):
00047                     # cache is uptodate
00048                     return cache.content()
00049                 else:
00050                     raise caching.CacheError
00051             except caching.CacheError:
00052                 # either cache does not exist, is erroneous or not uptodate: recreate it
00053                 members, member_groups = super(WikiGroup, self)._load_group()
00054                 cache.update((members, member_groups))
00055                 return members, member_groups
00056         else:
00057             raise GroupDoesNotExistError(group_name)
00058 
00059 
00060 class WikiGroups(BaseGroupsBackend):
00061 
00062     def __contains__(self, group_name):
00063         return self.is_group_name(group_name) and Page(self.request, group_name).exists()
00064 
00065     def __iter__(self):
00066         """
00067         To find group pages, request.cfg.cache.page_group_regexact pattern is used.
00068         """
00069         return iter(self.request.rootpage.getPageList(user='', filter=self.page_group_regex.search))
00070 
00071     def __getitem__(self, group_name):
00072         return WikiGroup(request=self.request, name=group_name, backend=self)
00073 
00074     def _retrieve_members(self, group_name):
00075         """
00076         MoinMoin.formatter.groups is used to extract group members from a page.
00077         """
00078         formatter = Formatter(self.request)
00079         page = Page(self.request, group_name, formatter=formatter)
00080 
00081         request_page = getattr(self.request, "page", None)
00082         self.request.page = page
00083         # send_special is set to True because acl of the page should
00084         # not be processed to avoid infinite recursion in the
00085         # following case.
00086         #
00087         # Consider page UserGroup content:
00088         #
00089         # #acl UserGroup:read,write,admin All:read
00090         #
00091         #  * ExampleUser
00092         #  * TestGroup
00093         #
00094         page.send_page(content_only=True, send_special=True)
00095 
00096         if request_page:
00097             self.request.page = request_page
00098         else:
00099             del self.request.page
00100 
00101         return formatter.members
00102