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

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

Adding some attributes to ApplicantsContainer?. Generate title and code of containers automatically.

  • 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) <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
44markup language.
45
46Some more reST examples for getting started:
47
481. Heading
49----------
50
51Text
52
531.2 Heading
54...........
55
56Text
57
582. Heading
59----------
60
61Text
62
632.1 Heading
64...........
65
66Text
67
682.2 Heading
69...........
70
71Text
72
73"""
74   
75    # The default title.
76    title=u'Welcome to the Student Information and Registration Portal of %s' % name
77   
78    @property
79    def logger(self):
80        """The application logger.
81
82        Returns a standard logger object as provided by :mod:`logging`
83        module from the standard library.
84       
85        Other components can use this logger to perform log entries
86        into the 'main' logfile.
87
88        The logger is initialized the first time, it is called.
89        """
90        sitename = self.__name__
91        loggername = 'waeup.sirp.%s' % sitename
92        logger = logging.getLogger(loggername)
93        if not logger.handlers:
94            logger = self._setupLogger(logger)
95        return logger
96       
97   
98    def __init__(self, name=name, skin=skin, title=title, frontpage=frontpage, **kw):
99        super(University, self).__init__(**kw)
100        self.name = name
101        self.skin = skin
102        self.title = title
103        self.frontpage = frontpage
104        self.setup()
105
106    def setup(self):
107        """Setup some hard-wired components.
108
109        Create local datacenter, containers for users, students and
110        the like.
111        """
112        self['users'] = UserContainer()
113        self['datacenter'] = DataCenter()
114
115        self['students'] = createObject(u'waeup.StudentContainer')
116        self['hostels'] = createObject(u'waeup.HostelContainer')
117        self._createPlugins()
118
119    def _createPlugins(self):
120        """Create instances of all plugins defined somewhere.
121        """
122        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
123            plugin.setup(self, name, self.logger)
124        return
125
126    def updatePlugins(self):
127        """Lookup all plugins and call their `update()` method.
128        """
129        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
130            plugin.update(self, name, self.logger)
131        return
132
133    def _setupLogger(self, logger):
134        """Setup general application logger.
135
136        The logfile will be stored in the datacenter logs/ dir.
137        """
138        logdir = os.path.join(self['datacenter'].storage, 'logs')
139        if not os.path.exists(logdir):
140            os.mkdir(logdir)
141        filename = os.path.join(logdir, 'application.log')
142
143        # Create a rotating file handler logger for application.
144        handler = logging.handlers.RotatingFileHandler(
145            filename, maxBytes=5*1024**1, backupCount=7)
146        formatter = logging.Formatter(
147            '%(asctime)s - %(levelname)s - %(message)s')
148        handler.setFormatter(formatter)
149       
150        # Don't send log msgs to ancestors. This stops displaying
151        # logmessages on the commandline.
152        logger.propagate = False
153        logger.addHandler(handler)
154        return logger
155
156@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
157def handle_storage_move(obj, event):
158    """Event handler, in case datacenter storage moves.
159
160    We initialize the application log again, then.
161    """
162    app = grok.getSite()
163    if app is None:
164        return
165    if obj is not app['datacenter']:
166        return
167    logger = app.logger
168    logger.warn('Log Dir moved. Closing.')
169    handlers = logger.handlers
170    for handler in handlers:
171        logger.removeHandler(handler)
172    app._setupLogger(logger)
173    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.