1 | import grok |
---|
2 | from zope.authentication.interfaces import IAuthentication |
---|
3 | from zope.component import getUtilitiesFor |
---|
4 | from zope.component.interfaces import ObjectEvent |
---|
5 | from zope.pluggableauth import PluggableAuthentication |
---|
6 | from waeup.sirp.authentication import setup_authentication |
---|
7 | from waeup.sirp.datacenter import DataCenter |
---|
8 | from waeup.sirp.students.container import StudentsContainer |
---|
9 | from waeup.sirp.hostels.container import HostelsContainer |
---|
10 | from waeup.sirp.interfaces import ( |
---|
11 | IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, ) |
---|
12 | from waeup.sirp.userscontainer import UsersContainer |
---|
13 | from waeup.sirp.utils.logger import Logger |
---|
14 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
15 | from waeup.sirp.configuration import ConfigurationContainer |
---|
16 | |
---|
17 | class University(grok.Application, grok.Container, Logger): |
---|
18 | """A university. |
---|
19 | """ |
---|
20 | grok.implements(IUniversity) |
---|
21 | |
---|
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, |
---|
26 | setup = setup_authentication,) |
---|
27 | |
---|
28 | def __init__(self, *args, **kw): |
---|
29 | super(University, self).__init__(*args, **kw) |
---|
30 | self.setup() |
---|
31 | return |
---|
32 | |
---|
33 | def setup(self): |
---|
34 | """Setup some hard-wired components. |
---|
35 | |
---|
36 | Create local datacenter, containers for users, students and |
---|
37 | the like. |
---|
38 | """ |
---|
39 | self['users'] = UsersContainer() |
---|
40 | self['datacenter'] = DataCenter() |
---|
41 | self['students'] = StudentsContainer() |
---|
42 | self['configuration'] = ConfigurationContainer() |
---|
43 | self['hostels'] = HostelsContainer() |
---|
44 | self._createPlugins() |
---|
45 | |
---|
46 | def _createPlugins(self): |
---|
47 | """Create instances of all plugins defined somewhere. |
---|
48 | """ |
---|
49 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
50 | plugin.setup(self, name, self.logger) |
---|
51 | return |
---|
52 | |
---|
53 | def updatePlugins(self): |
---|
54 | """Lookup all plugins and call their `update()` method. |
---|
55 | """ |
---|
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.') |
---|
61 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
62 | plugin.update(self, name, self.logger) |
---|
63 | self.logger.info('Plugin update finished.') |
---|
64 | return |
---|
65 | attrs_to_fields(University) |
---|
66 | |
---|
67 | class ObjectUpgradeEvent(ObjectEvent): |
---|
68 | """An event fired, when datacenter storage moves. |
---|
69 | """ |
---|
70 | grok.implements(IObjectUpgradeEvent) |
---|
71 | |
---|
72 | @grok.subscribe(University, grok.IObjectAddedEvent) |
---|
73 | def handle_university_added(app, event): |
---|
74 | app.logger.info('University `%s` added.' % getattr(app, '__name__', None)) |
---|
75 | return |
---|