source: main/waeup.sirp/trunk/src/waeup/sirp/app.py @ 5411

Last change on this file since 5411 was 5407, checked in by Henrik Bettermann, 14 years ago

Implement skin switch.

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1import grok
2import logging
3import os
4try:
5    from zope.authentication.interfaces import IAuthentication
6except:
7    # BBB
8    from zope.app.security.interfaces import IAuthentication
9from zope.component import createObject, getUtilitiesFor
10try:
11    from zope.pluggableauth import PluggableAuthentication
12except ImportError:
13    # BBB
14    from zope.app.authentication.authentication import PluggableAuthentication
15
16from waeup.sirp.authentication import setup_authentication
17from waeup.sirp.datacenter import DataCenter
18from waeup.sirp.interfaces import (
19    IUniversity, IDataCenter, IWAeUPSIRPPluggable,
20    IDataCenterStorageMovedEvent)
21from waeup.sirp.users import UserContainer
22
23class University(grok.Application, grok.Container):
24    """A university.
25    """
26    grok.implements(IUniversity)
27   
28    # Setup authentication for this app. Note: this is only
29    # initialized, when a new instance of this app is created.
30    grok.local_utility(
31        PluggableAuthentication, provides = IAuthentication,
32        setup = setup_authentication,)
33
34    # The name of the university.
35    name=u'Sample University'
36   
37    # The default layout.
38    skin=u'waeuptheme-gray1.css'
39   
40    @property
41    def logger(self):
42        """The application logger.
43
44        Returns a standard logger object as provided by :mod:`logging`
45        module from the standard library.
46       
47        Other components can use this logger to perform log entries
48        into the 'main' logfile.
49
50        The logger is initialized the first time, it is called.
51        """
52        sitename = self.__name__
53        loggername = 'waeup.sirp.%s' % sitename
54        logger = logging.getLogger(loggername)
55        if not logger.handlers:
56            logger = self._setupLogger(logger)
57        return logger
58       
59   
60    def __init__(self, name=name, skin=skin, **kw):
61        super(University, self).__init__(**kw)
62        self.name = name
63        self.skin = skin
64        self.setup()
65
66    def setup(self):
67        """Setup some hard-wired components.
68
69        Create local datacenter, containers for users, students and
70        the like.
71        """
72        self['users'] = UserContainer()
73        self['datacenter'] = DataCenter()
74
75        self['students'] = createObject(u'waeup.StudentContainer')
76        self['hostels'] = createObject(u'waeup.HostelContainer')
77        self._createPlugins()
78
79    def _createPlugins(self):
80        """Create instances of all plugins defined somewhere.
81        """
82        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
83            plugin.setup(self, name, self.logger)
84        return
85       
86    def _setupLogger(self, logger):
87        """Setup general application logger.
88
89        The logfile will be stored in the datacenter logs/ dir.
90        """
91        logdir = os.path.join(self['datacenter'].storage, 'logs')
92        if not os.path.exists(logdir):
93            os.mkdir(logdir)
94        filename = os.path.join(logdir, 'application.log')
95
96        # Create a rotating file handler logger for application.
97        handler = logging.handlers.RotatingFileHandler(
98            filename, maxBytes=5*1024**1, backupCount=7)
99        formatter = logging.Formatter(
100            '%(asctime)s - %(levelname)s - %(message)s')
101        handler.setFormatter(formatter)
102       
103        # Don't send log msgs to ancestors. This stops displaying
104        # logmessages on the commandline.
105        logger.propagate = False
106        logger.addHandler(handler)
107        return logger
108
109@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
110def handle_storage_move(obj, event):
111    """Event handler, in case datacenter storage moves.
112
113    We initialize the application log again, then.
114    """
115    app = grok.getSite()
116    if app is None:
117        return
118    if obj is not app['datacenter']:
119        return
120    logger = app.logger
121    logger.warn('Log Dir moved. Closing.')
122    handlers = logger.handlers
123    for handler in handlers:
124        logger.removeHandler(handler)
125    app._setupLogger(logger)
126    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.