[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) |
---|
[4789] | 27 | # Setup authentication for this app. Note: this is only |
---|
| 28 | # initialized, when a new instance of this app is created. |
---|
| 29 | grok.local_utility( |
---|
| 30 | PluggableAuthentication, provides = IAuthentication, |
---|
[5054] | 31 | setup = setup_authentication,) |
---|
| 32 | |
---|
[4874] | 33 | @property |
---|
| 34 | def logger(self): |
---|
| 35 | sitename = self.__name__ |
---|
| 36 | loggername = 'waeup.sirp.%s' % sitename |
---|
| 37 | logger = logging.getLogger(loggername) |
---|
| 38 | if not logger.handlers: |
---|
| 39 | logger = self._setupLogger(logger) |
---|
| 40 | return logger |
---|
| 41 | |
---|
[4789] | 42 | |
---|
| 43 | def __init__(self, name=u'Sample University', **kw): |
---|
[3521] | 44 | super(University, self).__init__(**kw) |
---|
| 45 | self.name = name |
---|
[4789] | 46 | self.setup() |
---|
[3521] | 47 | |
---|
[4789] | 48 | def setup(self): |
---|
| 49 | self['users'] = UserContainer() |
---|
| 50 | self['datacenter'] = DataCenter() |
---|
[4874] | 51 | |
---|
[5016] | 52 | self['students'] = createObject(u'waeup.StudentContainer') |
---|
| 53 | self['hostels'] = createObject(u'waeup.HostelContainer') |
---|
| 54 | self._createPlugins() |
---|
| 55 | |
---|
| 56 | def _createPlugins(self): |
---|
| 57 | """Create instances of all plugins defined somewhere. |
---|
| 58 | """ |
---|
[5071] | 59 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 60 | plugin.setup(self, name, self.logger) |
---|
| 61 | return |
---|
[5016] | 62 | |
---|
[4874] | 63 | def _setupLogger(self, logger): |
---|
| 64 | """Setup general application logger. |
---|
| 65 | |
---|
| 66 | The logfile will be stored in the datacenter logs/ dir. |
---|
| 67 | """ |
---|
| 68 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
| 69 | if not os.path.exists(logdir): |
---|
| 70 | os.mkdir(logdir) |
---|
| 71 | filename = os.path.join(logdir, 'application.log') |
---|
| 72 | |
---|
| 73 | # Create a rotating file handler logger for application. |
---|
| 74 | handler = logging.handlers.RotatingFileHandler( |
---|
| 75 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
| 76 | formatter = logging.Formatter( |
---|
| 77 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 78 | handler.setFormatter(formatter) |
---|
| 79 | |
---|
| 80 | # Don't send log msgs to ancestors. This stops displaying |
---|
| 81 | # logmessages on the commandline. |
---|
| 82 | logger.propagate = False |
---|
| 83 | logger.addHandler(handler) |
---|
| 84 | return logger |
---|
[4884] | 85 | |
---|
| 86 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
| 87 | def handle_storage_move(obj, event): |
---|
| 88 | """Event handler, in case datacenter storage moves. |
---|
| 89 | |
---|
| 90 | We initialize the application log again, then. |
---|
| 91 | """ |
---|
| 92 | app = grok.getSite() |
---|
| 93 | if app is None: |
---|
| 94 | return |
---|
| 95 | if obj is not app['datacenter']: |
---|
| 96 | return |
---|
| 97 | logger = app.logger |
---|
| 98 | logger.warn('Log Dir moved. Closing.') |
---|
| 99 | handlers = logger.handlers |
---|
| 100 | for handler in handlers: |
---|
| 101 | logger.removeHandler(handler) |
---|
| 102 | app._setupLogger(logger) |
---|
| 103 | logger.warn('Log file moved. Opening.') |
---|