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