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
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'gray waeup theme'
39
40    # The default frontpage.
41    frontpage= """
42This is the default frontpage of the portal written
43in `reStructuredText (reST)
44<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
45markup language.
46
47Some more reST examples for getting started:
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"""
75
76    # The default title.
77    title=u'Welcome to the Student Information and Registration Portal of %s' % name
78
79    @property
80    def logger(self):
81        """The application logger.
82
83        Returns a standard logger object as provided by :mod:`logging`
84        module from the standard library.
85
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        """
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
97
98
99    def __init__(self, name=name, skin=skin, title=title,
100                 frontpage=frontpage, **kw):
101        super(University, self).__init__(**kw)
102        self.name = name
103        self.skin = skin
104        self.title = title
105        self.frontpage = frontpage
106        self.setup()
107
108    def setup(self):
109        """Setup some hard-wired components.
110
111        Create local datacenter, containers for users, students and
112        the like.
113        """
114        self['users'] = UserContainer()
115        self['datacenter'] = DataCenter()
116
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        """
124        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
125            plugin.setup(self, name, self.logger)
126        return
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
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)
151
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
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.