[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) |
---|
[5345] | 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' |
---|
[5054] | 36 | |
---|
[5407] | 37 | # The default layout. |
---|
[5964] | 38 | skin=u'gray waeup theme' |
---|
[5407] | 39 | |
---|
[6065] | 40 | # The default frontpage. |
---|
| 41 | frontpage= """ |
---|
[6087] | 42 | This is the default frontpage of the portal written |
---|
| 43 | in `reStructuredText (reST) <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_ |
---|
[6085] | 44 | markup language. |
---|
[6065] | 45 | |
---|
[6087] | 46 | Some more reST examples for getting started: |
---|
[6065] | 47 | |
---|
| 48 | 1. Heading |
---|
| 49 | ---------- |
---|
| 50 | |
---|
| 51 | Text |
---|
| 52 | |
---|
| 53 | 1.2 Heading |
---|
| 54 | ........... |
---|
| 55 | |
---|
| 56 | Text |
---|
| 57 | |
---|
| 58 | 2. Heading |
---|
| 59 | ---------- |
---|
| 60 | |
---|
| 61 | Text |
---|
| 62 | |
---|
| 63 | 2.1 Heading |
---|
| 64 | ........... |
---|
| 65 | |
---|
| 66 | Text |
---|
| 67 | |
---|
| 68 | 2.2 Heading |
---|
| 69 | ........... |
---|
| 70 | |
---|
| 71 | Text |
---|
| 72 | |
---|
| 73 | """ |
---|
| 74 | |
---|
| 75 | # The default title. |
---|
| 76 | title=u'Welcome to the Student Information and Registration Portal of %s' % name |
---|
| 77 | |
---|
[4874] | 78 | @property |
---|
| 79 | def logger(self): |
---|
[5345] | 80 | """The application logger. |
---|
| 81 | |
---|
| 82 | Returns a standard logger object as provided by :mod:`logging` |
---|
| 83 | module from the standard library. |
---|
| 84 | |
---|
| 85 | Other components can use this logger to perform log entries |
---|
| 86 | into the 'main' logfile. |
---|
| 87 | |
---|
| 88 | The logger is initialized the first time, it is called. |
---|
| 89 | """ |
---|
[4874] | 90 | sitename = self.__name__ |
---|
| 91 | loggername = 'waeup.sirp.%s' % sitename |
---|
| 92 | logger = logging.getLogger(loggername) |
---|
| 93 | if not logger.handlers: |
---|
| 94 | logger = self._setupLogger(logger) |
---|
| 95 | return logger |
---|
| 96 | |
---|
[4789] | 97 | |
---|
[6065] | 98 | def __init__(self, name=name, skin=skin, title=title, frontpage=frontpage, **kw): |
---|
[3521] | 99 | super(University, self).__init__(**kw) |
---|
| 100 | self.name = name |
---|
[5407] | 101 | self.skin = skin |
---|
[6065] | 102 | self.title = title |
---|
| 103 | self.frontpage = frontpage |
---|
[4789] | 104 | self.setup() |
---|
[3521] | 105 | |
---|
[4789] | 106 | def setup(self): |
---|
[5345] | 107 | """Setup some hard-wired components. |
---|
| 108 | |
---|
| 109 | Create local datacenter, containers for users, students and |
---|
| 110 | the like. |
---|
| 111 | """ |
---|
[4789] | 112 | self['users'] = UserContainer() |
---|
| 113 | self['datacenter'] = DataCenter() |
---|
[4874] | 114 | |
---|
[5016] | 115 | self['students'] = createObject(u'waeup.StudentContainer') |
---|
| 116 | self['hostels'] = createObject(u'waeup.HostelContainer') |
---|
| 117 | self._createPlugins() |
---|
| 118 | |
---|
| 119 | def _createPlugins(self): |
---|
| 120 | """Create instances of all plugins defined somewhere. |
---|
| 121 | """ |
---|
[5071] | 122 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 123 | plugin.setup(self, name, self.logger) |
---|
| 124 | return |
---|
[5421] | 125 | |
---|
| 126 | def updatePlugins(self): |
---|
| 127 | """Lookup all plugins and call their `update()` method. |
---|
| 128 | """ |
---|
| 129 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 130 | plugin.update(self, name, self.logger) |
---|
| 131 | return |
---|
| 132 | |
---|
[4874] | 133 | def _setupLogger(self, logger): |
---|
| 134 | """Setup general application logger. |
---|
| 135 | |
---|
| 136 | The logfile will be stored in the datacenter logs/ dir. |
---|
| 137 | """ |
---|
| 138 | logdir = os.path.join(self['datacenter'].storage, 'logs') |
---|
| 139 | if not os.path.exists(logdir): |
---|
| 140 | os.mkdir(logdir) |
---|
| 141 | filename = os.path.join(logdir, 'application.log') |
---|
| 142 | |
---|
| 143 | # Create a rotating file handler logger for application. |
---|
| 144 | handler = logging.handlers.RotatingFileHandler( |
---|
| 145 | filename, maxBytes=5*1024**1, backupCount=7) |
---|
| 146 | formatter = logging.Formatter( |
---|
| 147 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 148 | handler.setFormatter(formatter) |
---|
| 149 | |
---|
| 150 | # Don't send log msgs to ancestors. This stops displaying |
---|
| 151 | # logmessages on the commandline. |
---|
| 152 | logger.propagate = False |
---|
| 153 | logger.addHandler(handler) |
---|
| 154 | return logger |
---|
[4884] | 155 | |
---|
| 156 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
| 157 | def handle_storage_move(obj, event): |
---|
| 158 | """Event handler, in case datacenter storage moves. |
---|
| 159 | |
---|
| 160 | We initialize the application log again, then. |
---|
| 161 | """ |
---|
| 162 | app = grok.getSite() |
---|
| 163 | if app is None: |
---|
| 164 | return |
---|
| 165 | if obj is not app['datacenter']: |
---|
| 166 | return |
---|
| 167 | logger = app.logger |
---|
| 168 | logger.warn('Log Dir moved. Closing.') |
---|
| 169 | handlers = logger.handlers |
---|
| 170 | for handler in handlers: |
---|
| 171 | logger.removeHandler(handler) |
---|
| 172 | app._setupLogger(logger) |
---|
| 173 | logger.warn('Log file moved. Opening.') |
---|