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

Last change on this file since 6136 was 6135, checked in by uli, 14 years ago

Remove local experimantal stuff.

  • Property svn:eol-style set to native
File size: 4.8 KB
RevLine 
[3521]1import grok
[4874]2import logging
3import os
[5054]4try:
5    from zope.authentication.interfaces import IAuthentication
6except:
7    # BBB
8    from zope.app.security.interfaces import IAuthentication
[5071]9from zope.component import createObject, getUtilitiesFor
[5054]10try:
11    from zope.pluggableauth import PluggableAuthentication
12except ImportError:
13    # BBB
14    from zope.app.authentication.authentication import PluggableAuthentication
[3521]15
[5054]16from waeup.sirp.authentication import setup_authentication
17from waeup.sirp.datacenter import DataCenter
[5016]18from waeup.sirp.interfaces import (
19    IUniversity, IDataCenter, IWAeUPSIRPPluggable,
20    IDataCenterStorageMovedEvent)
[4920]21from waeup.sirp.users import UserContainer
[4789]22
[3521]23class University(grok.Application, grok.Container):
[4789]24    """A university.
25    """
[4968]26    grok.implements(IUniversity)
[6129]27
[4789]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,
[5054]32        setup = setup_authentication,)
[5345]33
[5407]34    # The name of the university.
[5345]35    name=u'Sample University'
[6129]36
[5407]37    # The default layout.
[5964]38    skin=u'gray waeup theme'
[6129]39
[6065]40    # The default frontpage.
41    frontpage= """
[6087]42This is the default frontpage of the portal written
[6129]43in `reStructuredText (reST)
44<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
45markup language.
[6065]46
[6087]47Some more reST examples for getting started:
[6065]48
491. Heading
50----------
51
52Text
53
541.2 Heading
55...........
56
57Text
58
592. Heading
60----------
61
62Text
63
642.1 Heading
65...........
66
67Text
68
692.2 Heading
70...........
71
72Text
73
74"""
[6129]75
[6065]76    # The default title.
77    title=u'Welcome to the Student Information and Registration Portal of %s' % name
[6129]78
[4874]79    @property
80    def logger(self):
[5345]81        """The application logger.
82
83        Returns a standard logger object as provided by :mod:`logging`
84        module from the standard library.
[6129]85
[5345]86        Other components can use this logger to perform log entries
87        into the 'main' logfile.
88
89        The logger is initialized the first time, it is called.
90        """
[4874]91        sitename = self.__name__
92        loggername = 'waeup.sirp.%s' % sitename
93        logger = logging.getLogger(loggername)
94        if not logger.handlers:
95            logger = self._setupLogger(logger)
96        return logger
[6129]97
98
99    def __init__(self, name=name, skin=skin, title=title,
100                 frontpage=frontpage, **kw):
[3521]101        super(University, self).__init__(**kw)
102        self.name = name
[5407]103        self.skin = skin
[6065]104        self.title = title
105        self.frontpage = frontpage
[4789]106        self.setup()
[3521]107
[4789]108    def setup(self):
[5345]109        """Setup some hard-wired components.
110
111        Create local datacenter, containers for users, students and
112        the like.
113        """
[4789]114        self['users'] = UserContainer()
115        self['datacenter'] = DataCenter()
[4874]116
[5016]117        self['students'] = createObject(u'waeup.StudentContainer')
118        self['hostels'] = createObject(u'waeup.HostelContainer')
119        self._createPlugins()
120
121    def _createPlugins(self):
122        """Create instances of all plugins defined somewhere.
123        """
[5071]124        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
125            plugin.setup(self, name, self.logger)
126        return
[5421]127
128    def updatePlugins(self):
129        """Lookup all plugins and call their `update()` method.
130        """
131        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
132            plugin.update(self, name, self.logger)
133        return
134
[4874]135    def _setupLogger(self, logger):
136        """Setup general application logger.
137
138        The logfile will be stored in the datacenter logs/ dir.
139        """
140        logdir = os.path.join(self['datacenter'].storage, 'logs')
141        if not os.path.exists(logdir):
142            os.mkdir(logdir)
143        filename = os.path.join(logdir, 'application.log')
144
145        # Create a rotating file handler logger for application.
146        handler = logging.handlers.RotatingFileHandler(
147            filename, maxBytes=5*1024**1, backupCount=7)
148        formatter = logging.Formatter(
149            '%(asctime)s - %(levelname)s - %(message)s')
150        handler.setFormatter(formatter)
[6129]151
[4874]152        # Don't send log msgs to ancestors. This stops displaying
153        # logmessages on the commandline.
154        logger.propagate = False
155        logger.addHandler(handler)
156        return logger
[4884]157
158@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
159def handle_storage_move(obj, event):
160    """Event handler, in case datacenter storage moves.
161
162    We initialize the application log again, then.
163    """
164    app = grok.getSite()
165    if app is None:
166        return
167    if obj is not app['datacenter']:
168        return
169    logger = app.logger
170    logger.warn('Log Dir moved. Closing.')
171    handlers = logger.handlers
172    for handler in handlers:
173        logger.removeHandler(handler)
174    app._setupLogger(logger)
175    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.