[3521] | 1 | import grok |
---|
[4874] | 2 | import logging |
---|
| 3 | import os |
---|
[6522] | 4 | from hurry.file.interfaces import IFileRetrieval |
---|
[6362] | 5 | from zope.authentication.interfaces import IAuthentication |
---|
[5071] | 6 | from zope.component import createObject, getUtilitiesFor |
---|
[6137] | 7 | from zope.component.interfaces import ObjectEvent |
---|
[6362] | 8 | from zope.pluggableauth import PluggableAuthentication |
---|
[5054] | 9 | from waeup.sirp.authentication import setup_authentication |
---|
| 10 | from waeup.sirp.datacenter import DataCenter |
---|
[6522] | 11 | from waeup.sirp.imagestorage import ImageStorageFileRetrieval, ImageStorage |
---|
[5016] | 12 | from waeup.sirp.interfaces import ( |
---|
| 13 | IUniversity, IDataCenter, IWAeUPSIRPPluggable, |
---|
[6137] | 14 | IDataCenterStorageMovedEvent, IObjectUpgradeEvent, ) |
---|
[4920] | 15 | from waeup.sirp.users import UserContainer |
---|
[6361] | 16 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
[4789] | 17 | |
---|
[3521] | 18 | class University(grok.Application, grok.Container): |
---|
[4789] | 19 | """A university. |
---|
| 20 | """ |
---|
[4968] | 21 | grok.implements(IUniversity) |
---|
[6129] | 22 | |
---|
[4789] | 23 | # Setup authentication for this app. Note: this is only |
---|
| 24 | # initialized, when a new instance of this app is created. |
---|
| 25 | grok.local_utility( |
---|
| 26 | PluggableAuthentication, provides = IAuthentication, |
---|
[5054] | 27 | setup = setup_authentication,) |
---|
[5345] | 28 | |
---|
[6522] | 29 | grok.local_utility( |
---|
| 30 | ImageStorageFileRetrieval, provides = IFileRetrieval) |
---|
| 31 | |
---|
[4874] | 32 | @property |
---|
| 33 | def logger(self): |
---|
[5345] | 34 | """The application logger. |
---|
| 35 | |
---|
| 36 | Returns a standard logger object as provided by :mod:`logging` |
---|
| 37 | module from the standard library. |
---|
[6129] | 38 | |
---|
[5345] | 39 | Other components can use this logger to perform log entries |
---|
| 40 | into the 'main' logfile. |
---|
| 41 | |
---|
| 42 | The logger is initialized the first time, it is called. |
---|
| 43 | """ |
---|
[4874] | 44 | sitename = self.__name__ |
---|
| 45 | loggername = 'waeup.sirp.%s' % sitename |
---|
| 46 | logger = logging.getLogger(loggername) |
---|
| 47 | if not logger.handlers: |
---|
| 48 | logger = self._setupLogger(logger) |
---|
| 49 | return logger |
---|
[6129] | 50 | |
---|
[6361] | 51 | def __init__(self, name=None, skin=None, title=None, |
---|
| 52 | frontpage=None, **kw): |
---|
[3521] | 53 | super(University, self).__init__(**kw) |
---|
[6361] | 54 | if name is not None: |
---|
| 55 | self.name = name |
---|
| 56 | if skin is not None: |
---|
| 57 | self.skin = skin |
---|
| 58 | if title is not None: |
---|
| 59 | self.title = title |
---|
| 60 | if frontpage is not None: |
---|
| 61 | self.frontpage = frontpage |
---|
[4789] | 62 | self.setup() |
---|
[3521] | 63 | |
---|
[4789] | 64 | def setup(self): |
---|
[5345] | 65 | """Setup some hard-wired components. |
---|
| 66 | |
---|
| 67 | Create local datacenter, containers for users, students and |
---|
| 68 | the like. |
---|
| 69 | """ |
---|
[4789] | 70 | self['users'] = UserContainer() |
---|
| 71 | self['datacenter'] = DataCenter() |
---|
[6522] | 72 | self['images'] = ImageStorage() |
---|
[4874] | 73 | |
---|
[5016] | 74 | self['students'] = createObject(u'waeup.StudentContainer') |
---|
| 75 | self['hostels'] = createObject(u'waeup.HostelContainer') |
---|
| 76 | self._createPlugins() |
---|
| 77 | |
---|
| 78 | def _createPlugins(self): |
---|
| 79 | """Create instances of all plugins defined somewhere. |
---|
| 80 | """ |
---|
[5071] | 81 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 82 | plugin.setup(self, name, self.logger) |
---|
| 83 | return |
---|
[5421] | 84 | |
---|
| 85 | def updatePlugins(self): |
---|
| 86 | """Lookup all plugins and call their `update()` method. |
---|
| 87 | """ |
---|
[6138] | 88 | name = getattr(self, '__name__', '<Unnamed>') |
---|
| 89 | self.logger.info('Fire upgrade event for site %s' % name) |
---|
| 90 | grok.notify(ObjectUpgradeEvent(self)) |
---|
| 91 | self.logger.info('Done.') |
---|
| 92 | self.logger.info('Now upgrading any plugins.') |
---|
[5421] | 93 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 94 | plugin.update(self, name, self.logger) |
---|
[6138] | 95 | self.logger.info('Plugin update finished.') |
---|
[5421] | 96 | return |
---|
| 97 | |
---|
[4874] | 98 | def _setupLogger(self, logger): |
---|
| 99 | """Setup general application logger. |
---|
| 100 | |
---|
| 101 | The logfile will be stored in the datacenter logs/ dir. |
---|
| 102 | """ |
---|
| 103 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
| 104 | if not os.path.exists(logdir): |
---|
| 105 | os.mkdir(logdir) |
---|
[6329] | 106 | filename = os.path.join(logdir, 'main.log') |
---|
[4874] | 107 | |
---|
| 108 | # Create a rotating file handler logger for application. |
---|
| 109 | handler = logging.handlers.RotatingFileHandler( |
---|
| 110 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
| 111 | formatter = logging.Formatter( |
---|
| 112 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 113 | handler.setFormatter(formatter) |
---|
[6129] | 114 | |
---|
[4874] | 115 | # Don't send log msgs to ancestors. This stops displaying |
---|
| 116 | # logmessages on the commandline. |
---|
| 117 | logger.propagate = False |
---|
| 118 | logger.addHandler(handler) |
---|
| 119 | return logger |
---|
[6361] | 120 | attrs_to_fields(University) |
---|
[4884] | 121 | |
---|
| 122 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
| 123 | def handle_storage_move(obj, event): |
---|
| 124 | """Event handler, in case datacenter storage moves. |
---|
| 125 | |
---|
| 126 | We initialize the application log again, then. |
---|
| 127 | """ |
---|
| 128 | app = grok.getSite() |
---|
| 129 | if app is None: |
---|
| 130 | return |
---|
| 131 | if obj is not app['datacenter']: |
---|
| 132 | return |
---|
| 133 | logger = app.logger |
---|
| 134 | logger.warn('Log Dir moved. Closing.') |
---|
| 135 | handlers = logger.handlers |
---|
| 136 | for handler in handlers: |
---|
| 137 | logger.removeHandler(handler) |
---|
| 138 | app._setupLogger(logger) |
---|
| 139 | logger.warn('Log file moved. Opening.') |
---|
[6137] | 140 | |
---|
| 141 | class ObjectUpgradeEvent(ObjectEvent): |
---|
| 142 | """An event fired, when datacenter storage moves. |
---|
| 143 | """ |
---|
| 144 | grok.implements(IObjectUpgradeEvent) |
---|