source: main/waeup.sirp/branches/workshop2010-playground/src/waeup/sirp/app.py @ 11107

Last change on this file since 11107 was 5557, checked in by gbenga, 14 years ago

Making fresh changes to University

  • Property svn:eol-style set to native
File size: 4.3 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    # The administrator e-mail account
41    email = u'e-mail'
42   
43    @property
44    def logger(self):
45        """The application logger.
46
47        Returns a standard logger object as provided by :mod:`logging`
48        module from the standard library.
49       
50        Other components can use this logger to perform log entries
51        into the 'main' logfile.
52
53        The logger is initialized the first time, it is called.
54        """
55        sitename = self.__name__
56        loggername = 'waeup.sirp.%s' % sitename
57        logger = logging.getLogger(loggername)
58        if not logger.handlers:
59            logger = self._setupLogger(logger)
60        return logger
61       
62   
63    def __init__(self, name=name, skin=skin, **kw):
64        super(University, self).__init__(**kw)
65        self.name = name
66        self.skin = skin
67        self.setup()
68
69    def setup(self):
70        """Setup some hard-wired components.
71
72        Create local datacenter, containers for users, students and
73        the like.
74        """
75        self['users'] = UserContainer()
76        self['datacenter'] = DataCenter()
77
78        self['students'] = createObject(u'waeup.StudentContainer')
79        self['hostels'] = createObject(u'waeup.HostelContainer')
80        self._createPlugins()
81
82    def _createPlugins(self):
83        """Create instances of all plugins defined somewhere.
84        """
85        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
86            plugin.setup(self, name, self.logger)
87        return
88
89    def updatePlugins(self):
90        """Lookup all plugins and call their `update()` method.
91        """
92        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
93            plugin.update(self, name, self.logger)
94        return
95
96    def _setupLogger(self, logger):
97        """Setup general application logger.
98
99        The logfile will be stored in the datacenter logs/ dir.
100        """
101        logdir = os.path.join(self['datacenter'].storage, 'logs')
102        if not os.path.exists(logdir):
103            os.mkdir(logdir)
104        filename = os.path.join(logdir, 'application.log')
105
106        # Create a rotating file handler logger for application.
107        handler = logging.handlers.RotatingFileHandler(
108            filename, maxBytes=5*1024**1, backupCount=7)
109        formatter = logging.Formatter(
110            '%(asctime)s - %(levelname)s - %(message)s')
111        handler.setFormatter(formatter)
112       
113        # Don't send log msgs to ancestors. This stops displaying
114        # logmessages on the commandline.
115        logger.propagate = False
116        logger.addHandler(handler)
117        return logger
118
119@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
120def handle_storage_move(obj, event):
121    """Event handler, in case datacenter storage moves.
122
123    We initialize the application log again, then.
124    """
125    app = grok.getSite()
126    if app is None:
127        return
128    if obj is not app['datacenter']:
129        return
130    logger = app.logger
131    logger.warn('Log Dir moved. Closing.')
132    handlers = logger.handlers
133    for handler in handlers:
134        logger.removeHandler(handler)
135    app._setupLogger(logger)
136    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.