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