[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. |
---|
| 38 | skin=u'waeuptheme-gray1.css' |
---|
| 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 |
---|
[5016] | 85 | |
---|
[4874] | 86 | def _setupLogger(self, logger): |
---|
| 87 | """Setup general application logger. |
---|
| 88 | |
---|
| 89 | The logfile will be stored in the datacenter logs/ dir. |
---|
| 90 | """ |
---|
| 91 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
| 92 | if not os.path.exists(logdir): |
---|
| 93 | os.mkdir(logdir) |
---|
| 94 | filename = os.path.join(logdir, 'application.log') |
---|
| 95 | |
---|
| 96 | # Create a rotating file handler logger for application. |
---|
| 97 | handler = logging.handlers.RotatingFileHandler( |
---|
| 98 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
| 99 | formatter = logging.Formatter( |
---|
| 100 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 101 | handler.setFormatter(formatter) |
---|
| 102 | |
---|
| 103 | # Don't send log msgs to ancestors. This stops displaying |
---|
| 104 | # logmessages on the commandline. |
---|
| 105 | logger.propagate = False |
---|
| 106 | logger.addHandler(handler) |
---|
| 107 | return logger |
---|
[4884] | 108 | |
---|
| 109 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
| 110 | def handle_storage_move(obj, event): |
---|
| 111 | """Event handler, in case datacenter storage moves. |
---|
| 112 | |
---|
| 113 | We initialize the application log again, then. |
---|
| 114 | """ |
---|
| 115 | app = grok.getSite() |
---|
| 116 | if app is None: |
---|
| 117 | return |
---|
| 118 | if obj is not app['datacenter']: |
---|
| 119 | return |
---|
| 120 | logger = app.logger |
---|
| 121 | logger.warn('Log Dir moved. Closing.') |
---|
| 122 | handlers = logger.handlers |
---|
| 123 | for handler in handlers: |
---|
| 124 | logger.removeHandler(handler) |
---|
| 125 | app._setupLogger(logger) |
---|
| 126 | logger.warn('Log file moved. Opening.') |
---|