[3521] | 1 | import grok |
---|
[4874] | 2 | import logging |
---|
| 3 | import os |
---|
[5054] | 4 | try: |
---|
| 5 | from zope.authentication.interfaces import IAuthentication |
---|
| 6 | except: |
---|
| 7 | # BBB |
---|
| 8 | from zope.app.security.interfaces import IAuthentication |
---|
[5071] | 9 | from zope.component import createObject, getUtilitiesFor |
---|
[5054] | 10 | try: |
---|
| 11 | from zope.pluggableauth import PluggableAuthentication |
---|
| 12 | except ImportError: |
---|
| 13 | # BBB |
---|
| 14 | from zope.app.authentication.authentication import PluggableAuthentication |
---|
[3521] | 15 | |
---|
[5054] | 16 | from waeup.sirp.authentication import setup_authentication |
---|
| 17 | from waeup.sirp.datacenter import DataCenter |
---|
[5016] | 18 | from waeup.sirp.interfaces import ( |
---|
| 19 | IUniversity, IDataCenter, IWAeUPSIRPPluggable, |
---|
| 20 | IDataCenterStorageMovedEvent) |
---|
[4920] | 21 | from waeup.sirp.users import UserContainer |
---|
[4789] | 22 | |
---|
[3521] | 23 | class University(grok.Application, grok.Container): |
---|
[4789] | 24 | """A university. |
---|
| 25 | """ |
---|
[4968] | 26 | grok.implements(IUniversity) |
---|
[5345] | 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' |
---|
[5054] | 36 | |
---|
[5407] | 37 | # The default layout. |
---|
[5964] | 38 | skin=u'gray waeup theme' |
---|
[5407] | 39 | |
---|
[4874] | 40 | @property |
---|
| 41 | def logger(self): |
---|
[5345] | 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 | """ |
---|
[4874] | 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 | |
---|
[4789] | 59 | |
---|
[5407] | 60 | def __init__(self, name=name, skin=skin, **kw): |
---|
[3521] | 61 | super(University, self).__init__(**kw) |
---|
| 62 | self.name = name |
---|
[5407] | 63 | self.skin = skin |
---|
[4789] | 64 | self.setup() |
---|
[3521] | 65 | |
---|
[4789] | 66 | def setup(self): |
---|
[5345] | 67 | """Setup some hard-wired components. |
---|
| 68 | |
---|
| 69 | Create local datacenter, containers for users, students and |
---|
| 70 | the like. |
---|
| 71 | """ |
---|
[4789] | 72 | self['users'] = UserContainer() |
---|
| 73 | self['datacenter'] = DataCenter() |
---|
[4874] | 74 | |
---|
[5016] | 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 | """ |
---|
[5071] | 82 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 83 | plugin.setup(self, name, self.logger) |
---|
| 84 | return |
---|
[5421] | 85 | |
---|
| 86 | def updatePlugins(self): |
---|
| 87 | """Lookup all plugins and call their `update()` method. |
---|
| 88 | """ |
---|
| 89 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 90 | plugin.update(self, name, self.logger) |
---|
| 91 | return |
---|
| 92 | |
---|
[4874] | 93 | def _setupLogger(self, logger): |
---|
| 94 | """Setup general application logger. |
---|
| 95 | |
---|
| 96 | The logfile will be stored in the datacenter logs/ dir. |
---|
| 97 | """ |
---|
| 98 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
| 99 | if not os.path.exists(logdir): |
---|
| 100 | os.mkdir(logdir) |
---|
| 101 | filename = os.path.join(logdir, 'application.log') |
---|
| 102 | |
---|
| 103 | # Create a rotating file handler logger for application. |
---|
| 104 | handler = logging.handlers.RotatingFileHandler( |
---|
| 105 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
| 106 | formatter = logging.Formatter( |
---|
| 107 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 108 | handler.setFormatter(formatter) |
---|
| 109 | |
---|
| 110 | # Don't send log msgs to ancestors. This stops displaying |
---|
| 111 | # logmessages on the commandline. |
---|
| 112 | logger.propagate = False |
---|
| 113 | logger.addHandler(handler) |
---|
| 114 | return logger |
---|
[4884] | 115 | |
---|
| 116 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
| 117 | def handle_storage_move(obj, event): |
---|
| 118 | """Event handler, in case datacenter storage moves. |
---|
| 119 | |
---|
| 120 | We initialize the application log again, then. |
---|
| 121 | """ |
---|
| 122 | app = grok.getSite() |
---|
| 123 | if app is None: |
---|
| 124 | return |
---|
| 125 | if obj is not app['datacenter']: |
---|
| 126 | return |
---|
| 127 | logger = app.logger |
---|
| 128 | logger.warn('Log Dir moved. Closing.') |
---|
| 129 | handlers = logger.handlers |
---|
| 130 | for handler in handlers: |
---|
| 131 | logger.removeHandler(handler) |
---|
| 132 | app._setupLogger(logger) |
---|
| 133 | logger.warn('Log file moved. Opening.') |
---|