src/pyams_utils/site.py
branchdev-tf
changeset 427 63284c98cdc1
parent 408 cf2304af0fab
equal deleted inserted replaced
426:2022e4da3ad9 427:63284c98cdc1
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 """PyAMS_utils.site module
    13 """PyAMS_utils.site module
       
    14 
       
    15 This modules provides classes of elements which can be used as application or site "root"
       
    16 objects.
       
    17 
       
    18 Il also provides functions which are used to manage site's "generations", used to upgrade
       
    19 objects while migrating from one version to another.
    14 """
    20 """
    15 
    21 
    16 from persistent.dict import PersistentDict
    22 from persistent.dict import PersistentDict
    17 from pyramid.exceptions import NotFound
    23 from pyramid.exceptions import NotFound
    18 from pyramid.path import DottedNameResolver
    24 from pyramid.path import DottedNameResolver
    46     A site root can be used as base application root in your ZODB.
    52     A site root can be used as base application root in your ZODB.
    47     It's also site root responsibility to manage your local site manager.
    53     It's also site root responsibility to manage your local site manager.
    48 
    54 
    49     BaseSiteRoot defines a basic ACL which gives all permissions to system administrator,
    55     BaseSiteRoot defines a basic ACL which gives all permissions to system administrator,
    50     and 'public' permission to everyone. But this ACL is generally overriden in subclasses
    56     and 'public' permission to everyone. But this ACL is generally overriden in subclasses
    51     which also inherit from :class:`pyams_security.security.ProtectedObject`.
    57     which also inherit from :py:class:`ProtectedObject <pyams_security.security.ProtectedObject>`.
    52     """
    58     """
    53 
    59 
    54     __acl__ = [(Allow, 'system:admin', ALL_PERMISSIONS),
    60     __acl__ = [(Allow, 'system:admin', ALL_PERMISSIONS),
    55                (Allow, Everyone, {PUBLIC_PERMISSION})]
    61                (Allow, Everyone, {PUBLIC_PERMISSION})]
    56 
    62 
    62     """Site root ++etc++ namespace traverser
    68     """Site root ++etc++ namespace traverser
    63 
    69 
    64     Gives access to local site manager from */++etc++site* URL
    70     Gives access to local site manager from */++etc++site* URL
    65     """
    71     """
    66 
    72 
    67     def traverse(self, name, furtherpath=None):
    73     def traverse(self, name, furtherpath=None):  # pylint: disable=unused-argument
       
    74         """Traverse to site manager;
       
    75         see :py:class:`ITraversable <zope.traversing.interfaces.ITraversable>`"""
    68         if name == 'site':
    76         if name == 'site':
    69             return self.context.getSiteManager()
    77             return self.context.getSiteManager()
    70         raise NotFound
    78         raise NotFound
    71 
    79 
    72 
    80 
    94             factory = resolver.maybe_resolve(factory)
   102             factory = resolver.maybe_resolve(factory)
    95         else:
   103         else:
    96             factory = request.registry.queryUtility(ISiteRootFactory, default=BaseSiteRoot)
   104             factory = request.registry.queryUtility(ISiteRootFactory, default=BaseSiteRoot)
    97         application = root[application_key] = factory()
   105         application = root[application_key] = factory()
    98         if IPossibleSite.providedBy(application):
   106         if IPossibleSite.providedBy(application):
    99             sm = LocalSiteManager(application, default_folder=False)
   107             lsm = LocalSiteManager(application, default_folder=False)
   100             application.setSiteManager(sm)
   108             application.setSiteManager(lsm)
   101         try:
   109         try:
   102             # if some components require a valid and complete registry
   110             # if some components require a valid and complete registry
   103             # with all registered utilities, they can subscribe to
   111             # with all registered utilities, they can subscribe to
   104             # INewLocalSiteCreatedEvent event interface
   112             # INewLocalSiteCreatedEvent event interface
   105             hooks.setSite(application)
   113             hooks.setSite(application)
   106             get_current_registry().notify(NewLocalSiteCreatedEvent(application))
   114             get_current_registry().notify(NewLocalSiteCreatedEvent(application))
   107         finally:
   115         finally:
   108             hooks.setSite(None)
   116             hooks.setSite(None)
   109         import transaction
   117         import transaction  # pylint: disable=import-outside-toplevel
   110         transaction.commit()
   118         transaction.commit()
   111     return application
   119     return application
   112 
   120 
   113 
   121 
   114 @implementer(ISiteUpgradeEvent)
   122 @implementer(ISiteUpgradeEvent)
   143                                                                                utility.generation))
   151                                                                                utility.generation))
   144                 utility.evolve(application, current)
   152                 utility.evolve(application, current)
   145                 generations[name] = utility.generation
   153                 generations[name] = utility.generation
   146         finally:
   154         finally:
   147             hooks.setSite(None)
   155             hooks.setSite(None)
   148         import transaction
   156         import transaction  # pylint: disable=import-outside-toplevel
   149         transaction.commit()
   157         transaction.commit()
   150     return application
   158     return application
   151 
   159 
   152 
   160 
   153 def check_required_utilities(site, utilities):
   161 def check_required_utilities(site, utilities):
   166     """
   174     """
   167     registry = get_current_registry()
   175     registry = get_current_registry()
   168     for interface, name, factory, default_id in utilities:
   176     for interface, name, factory, default_id in utilities:
   169         utility = query_utility(interface, name=name)
   177         utility = query_utility(interface, name=name)
   170         if utility is None:
   178         if utility is None:
   171             sm = site.getSiteManager()
   179             lsm = site.getSiteManager()
   172             if default_id in sm:
   180             if default_id in lsm:
   173                 continue
   181                 continue
   174             utility = factory()
   182             utility = factory()
   175             registry.notify(ObjectCreatedEvent(utility))
   183             registry.notify(ObjectCreatedEvent(utility))
   176             sm[default_id] = utility
   184             lsm[default_id] = utility
   177             sm.registerUtility(utility, interface, name=name)
   185             lsm.registerUtility(utility, interface, name=name)