1 | import grok |
---|
2 | import logging |
---|
3 | import os |
---|
4 | try: |
---|
5 | from zope.authentication.interfaces import IAuthentication |
---|
6 | except: |
---|
7 | # BBB |
---|
8 | from zope.app.security.interfaces import IAuthentication |
---|
9 | from zope.component import createObject, getUtilitiesFor |
---|
10 | from zope.component.interfaces import ObjectEvent |
---|
11 | try: |
---|
12 | from zope.pluggableauth import PluggableAuthentication |
---|
13 | except ImportError: |
---|
14 | # BBB |
---|
15 | from zope.app.authentication.authentication import PluggableAuthentication |
---|
16 | |
---|
17 | from waeup.sirp.authentication import setup_authentication |
---|
18 | from waeup.sirp.datacenter import DataCenter |
---|
19 | from waeup.sirp.interfaces import ( |
---|
20 | IUniversity, IDataCenter, IWAeUPSIRPPluggable, |
---|
21 | IDataCenterStorageMovedEvent, IObjectUpgradeEvent, ) |
---|
22 | from waeup.sirp.users import UserContainer |
---|
23 | |
---|
24 | class University(grok.Application, grok.Container): |
---|
25 | """A university. |
---|
26 | """ |
---|
27 | grok.implements(IUniversity) |
---|
28 | |
---|
29 | # Setup authentication for this app. Note: this is only |
---|
30 | # initialized, when a new instance of this app is created. |
---|
31 | grok.local_utility( |
---|
32 | PluggableAuthentication, provides = IAuthentication, |
---|
33 | setup = setup_authentication,) |
---|
34 | |
---|
35 | # The name of the university. |
---|
36 | name=u'Sample University' |
---|
37 | |
---|
38 | # The default layout. |
---|
39 | skin=u'gray waeup theme' |
---|
40 | |
---|
41 | # The default frontpage. |
---|
42 | frontpage= """ |
---|
43 | This is the default frontpage of the portal written |
---|
44 | in `reStructuredText (reST) |
---|
45 | <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_ |
---|
46 | markup language. |
---|
47 | |
---|
48 | Some more reST examples for getting started: |
---|
49 | |
---|
50 | 1. Heading |
---|
51 | ---------- |
---|
52 | |
---|
53 | Text |
---|
54 | |
---|
55 | 1.2 Heading |
---|
56 | ........... |
---|
57 | |
---|
58 | Text |
---|
59 | |
---|
60 | 2. Heading |
---|
61 | ---------- |
---|
62 | |
---|
63 | Text |
---|
64 | |
---|
65 | 2.1 Heading |
---|
66 | ........... |
---|
67 | |
---|
68 | Text |
---|
69 | |
---|
70 | 2.2 Heading |
---|
71 | ........... |
---|
72 | |
---|
73 | Text |
---|
74 | |
---|
75 | """ |
---|
76 | |
---|
77 | # The default title. |
---|
78 | title=u'Welcome to the Student Information and Registration Portal of %s' % name |
---|
79 | |
---|
80 | @property |
---|
81 | def logger(self): |
---|
82 | """The application logger. |
---|
83 | |
---|
84 | Returns a standard logger object as provided by :mod:`logging` |
---|
85 | module from the standard library. |
---|
86 | |
---|
87 | Other components can use this logger to perform log entries |
---|
88 | into the 'main' logfile. |
---|
89 | |
---|
90 | The logger is initialized the first time, it is called. |
---|
91 | """ |
---|
92 | sitename = self.__name__ |
---|
93 | loggername = 'waeup.sirp.%s' % sitename |
---|
94 | logger = logging.getLogger(loggername) |
---|
95 | if not logger.handlers: |
---|
96 | logger = self._setupLogger(logger) |
---|
97 | return logger |
---|
98 | |
---|
99 | |
---|
100 | def __init__(self, name=name, skin=skin, title=title, |
---|
101 | frontpage=frontpage, **kw): |
---|
102 | super(University, self).__init__(**kw) |
---|
103 | self.name = name |
---|
104 | self.skin = skin |
---|
105 | self.title = title |
---|
106 | self.frontpage = frontpage |
---|
107 | self.setup() |
---|
108 | |
---|
109 | def setup(self): |
---|
110 | """Setup some hard-wired components. |
---|
111 | |
---|
112 | Create local datacenter, containers for users, students and |
---|
113 | the like. |
---|
114 | """ |
---|
115 | self['users'] = UserContainer() |
---|
116 | self['datacenter'] = DataCenter() |
---|
117 | |
---|
118 | self['students'] = createObject(u'waeup.StudentContainer') |
---|
119 | self['hostels'] = createObject(u'waeup.HostelContainer') |
---|
120 | self._createPlugins() |
---|
121 | |
---|
122 | def _createPlugins(self): |
---|
123 | """Create instances of all plugins defined somewhere. |
---|
124 | """ |
---|
125 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
126 | plugin.setup(self, name, self.logger) |
---|
127 | return |
---|
128 | |
---|
129 | def updatePlugins(self): |
---|
130 | """Lookup all plugins and call their `update()` method. |
---|
131 | """ |
---|
132 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
133 | plugin.update(self, name, self.logger) |
---|
134 | return |
---|
135 | |
---|
136 | def _setupLogger(self, logger): |
---|
137 | """Setup general application logger. |
---|
138 | |
---|
139 | The logfile will be stored in the datacenter logs/ dir. |
---|
140 | """ |
---|
141 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
142 | if not os.path.exists(logdir): |
---|
143 | os.mkdir(logdir) |
---|
144 | filename = os.path.join(logdir, 'application.log') |
---|
145 | |
---|
146 | # Create a rotating file handler logger for application. |
---|
147 | handler = logging.handlers.RotatingFileHandler( |
---|
148 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
149 | formatter = logging.Formatter( |
---|
150 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
151 | handler.setFormatter(formatter) |
---|
152 | |
---|
153 | # Don't send log msgs to ancestors. This stops displaying |
---|
154 | # logmessages on the commandline. |
---|
155 | logger.propagate = False |
---|
156 | logger.addHandler(handler) |
---|
157 | return logger |
---|
158 | |
---|
159 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
160 | def handle_storage_move(obj, event): |
---|
161 | """Event handler, in case datacenter storage moves. |
---|
162 | |
---|
163 | We initialize the application log again, then. |
---|
164 | """ |
---|
165 | app = grok.getSite() |
---|
166 | if app is None: |
---|
167 | return |
---|
168 | if obj is not app['datacenter']: |
---|
169 | return |
---|
170 | logger = app.logger |
---|
171 | logger.warn('Log Dir moved. Closing.') |
---|
172 | handlers = logger.handlers |
---|
173 | for handler in handlers: |
---|
174 | logger.removeHandler(handler) |
---|
175 | app._setupLogger(logger) |
---|
176 | logger.warn('Log file moved. Opening.') |
---|
177 | |
---|
178 | class ObjectUpgradeEvent(ObjectEvent): |
---|
179 | """An event fired, when datacenter storage moves. |
---|
180 | """ |
---|
181 | grok.implements(IObjectUpgradeEvent) |
---|