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