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