[3521] | 1 | import grok |
---|
[6522] | 2 | from hurry.file.interfaces import IFileRetrieval |
---|
[6362] | 3 | from zope.authentication.interfaces import IAuthentication |
---|
[5071] | 4 | from zope.component import createObject, getUtilitiesFor |
---|
[6137] | 5 | from zope.component.interfaces import ObjectEvent |
---|
[6362] | 6 | from zope.pluggableauth import PluggableAuthentication |
---|
[5054] | 7 | from waeup.sirp.authentication import setup_authentication |
---|
| 8 | from waeup.sirp.datacenter import DataCenter |
---|
[6522] | 9 | from waeup.sirp.imagestorage import ImageStorageFileRetrieval, ImageStorage |
---|
[5016] | 10 | from waeup.sirp.interfaces import ( |
---|
[6578] | 11 | IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, ) |
---|
[4920] | 12 | from waeup.sirp.users import UserContainer |
---|
[6578] | 13 | from waeup.sirp.utils.logger import Logger |
---|
[6361] | 14 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
[4789] | 15 | |
---|
[6578] | 16 | class University(grok.Application, grok.Container, Logger): |
---|
[4789] | 17 | """A university. |
---|
| 18 | """ |
---|
[4968] | 19 | grok.implements(IUniversity) |
---|
[6129] | 20 | |
---|
[4789] | 21 | # Setup authentication for this app. Note: this is only |
---|
| 22 | # initialized, when a new instance of this app is created. |
---|
| 23 | grok.local_utility( |
---|
| 24 | PluggableAuthentication, provides = IAuthentication, |
---|
[5054] | 25 | setup = setup_authentication,) |
---|
[5345] | 26 | |
---|
[6522] | 27 | grok.local_utility( |
---|
| 28 | ImageStorageFileRetrieval, provides = IFileRetrieval) |
---|
| 29 | |
---|
[6592] | 30 | def __init__(self, *args, **kw): |
---|
| 31 | super(University, self).__init__(*args, **kw) |
---|
[4789] | 32 | self.setup() |
---|
[6592] | 33 | return |
---|
[3521] | 34 | |
---|
[4789] | 35 | def setup(self): |
---|
[5345] | 36 | """Setup some hard-wired components. |
---|
| 37 | |
---|
| 38 | Create local datacenter, containers for users, students and |
---|
| 39 | the like. |
---|
| 40 | """ |
---|
[4789] | 41 | self['users'] = UserContainer() |
---|
| 42 | self['datacenter'] = DataCenter() |
---|
[6522] | 43 | self['images'] = ImageStorage() |
---|
[4874] | 44 | |
---|
[5016] | 45 | self['students'] = createObject(u'waeup.StudentContainer') |
---|
| 46 | self['hostels'] = createObject(u'waeup.HostelContainer') |
---|
| 47 | self._createPlugins() |
---|
| 48 | |
---|
| 49 | def _createPlugins(self): |
---|
| 50 | """Create instances of all plugins defined somewhere. |
---|
| 51 | """ |
---|
[5071] | 52 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 53 | plugin.setup(self, name, self.logger) |
---|
| 54 | return |
---|
[5421] | 55 | |
---|
| 56 | def updatePlugins(self): |
---|
| 57 | """Lookup all plugins and call their `update()` method. |
---|
| 58 | """ |
---|
[6138] | 59 | name = getattr(self, '__name__', '<Unnamed>') |
---|
| 60 | self.logger.info('Fire upgrade event for site %s' % name) |
---|
| 61 | grok.notify(ObjectUpgradeEvent(self)) |
---|
| 62 | self.logger.info('Done.') |
---|
| 63 | self.logger.info('Now upgrading any plugins.') |
---|
[5421] | 64 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 65 | plugin.update(self, name, self.logger) |
---|
[6138] | 66 | self.logger.info('Plugin update finished.') |
---|
[5421] | 67 | return |
---|
[6361] | 68 | attrs_to_fields(University) |
---|
[4884] | 69 | |
---|
[6137] | 70 | class ObjectUpgradeEvent(ObjectEvent): |
---|
| 71 | """An event fired, when datacenter storage moves. |
---|
| 72 | """ |
---|
| 73 | grok.implements(IObjectUpgradeEvent) |
---|
[6578] | 74 | |
---|
| 75 | @grok.subscribe(University, grok.IObjectAddedEvent) |
---|
| 76 | def handle_university_add(app, event): |
---|
| 77 | app.logger.info('University `%s` added.' % getattr(app, '__name__', None)) |
---|
| 78 | return |
---|