[3521] | 1 | ## |
---|
| 2 | ## interfaces.py |
---|
[4789] | 3 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
| 4 | from zope.component import getUtility |
---|
[4882] | 5 | from zope.component.interfaces import IObjectEvent |
---|
[5056] | 6 | try: |
---|
| 7 | from zope.catalog.interfaces import ICatalog |
---|
| 8 | except ImportError: |
---|
| 9 | # BBB |
---|
| 10 | from zope.app.catalog.interfaces import ICatalog |
---|
[5955] | 11 | from zope.interface import Interface, Attribute, implements |
---|
[3521] | 12 | from zope import schema |
---|
[4789] | 13 | from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm |
---|
[4920] | 14 | from waeup.sirp.permissions import RoleSource |
---|
[3521] | 15 | |
---|
[5956] | 16 | class IThemesProvider(Interface): |
---|
[5955] | 17 | """A component that delivers all themes defined. |
---|
| 18 | |
---|
| 19 | We normally do not deal with browser-related stuff in `waeup.sirp` |
---|
| 20 | but leave that to the `browser` subpackage. With themes (aka |
---|
| 21 | skins) we cannot follow that path because we need a theme as an |
---|
| 22 | attribute of each `University` instance. |
---|
| 23 | |
---|
| 24 | Therefore we must tell the browser subpackage how we expect themes |
---|
| 25 | to be announced code-wise. |
---|
| 26 | |
---|
| 27 | What we tell here is that we might ask for a utility providing |
---|
| 28 | IThemeProvider and that this component then should be callable to |
---|
| 29 | give us a list of themes and their respective internal names. |
---|
| 30 | """ |
---|
| 31 | def __call__(): |
---|
| 32 | """Get a list of all themes available. |
---|
| 33 | |
---|
| 34 | Returns a list of ``(<NAME>, <THEME>)`` tuples where |
---|
| 35 | ``<NAME>`` is the internal name under which a theme was |
---|
| 36 | registered and ``<THEME>`` is the real theme. |
---|
[5967] | 37 | |
---|
| 38 | Each <THEME> must have a `description` attribute. |
---|
[5955] | 39 | """ |
---|
| 40 | |
---|
[5956] | 41 | class ThemesSource(BasicSourceFactory): |
---|
[5955] | 42 | """A source for themes. |
---|
| 43 | """ |
---|
| 44 | def getValues(self): |
---|
| 45 | """Get names of themes. |
---|
| 46 | """ |
---|
[5956] | 47 | themes_provider = getUtility(IThemesProvider, name="default") |
---|
| 48 | return [x for x, y in themes_provider()] |
---|
[5955] | 49 | |
---|
| 50 | def getTitle(self, value): |
---|
| 51 | """Get a user-presentable description for a theme. |
---|
| 52 | """ |
---|
[5956] | 53 | themes_provider = getUtility(IThemesProvider, name="default") |
---|
| 54 | for name, theme in themes_provider(): |
---|
[5955] | 55 | if name == value: |
---|
| 56 | return theme.description |
---|
| 57 | return u'unnamed' |
---|
| 58 | |
---|
[5956] | 59 | class ThemesSourceBinder(object): |
---|
[5955] | 60 | """A source binder for the `ThemeSource`. |
---|
| 61 | |
---|
| 62 | Normally, we can pass instances of a source (like `ThemeSource`) |
---|
| 63 | directly to fields in interfaces (writing something like ``source |
---|
| 64 | = ThemeSource()``. With `ThemeSource` we cannot do this, because |
---|
| 65 | the set of available themes is not available before the whole |
---|
| 66 | `waeup.sirp` package was grokked (and parsed). |
---|
| 67 | |
---|
| 68 | We bypass that problem by defining this trivial source-binder, |
---|
| 69 | which is asked for themes only when the values of the source are |
---|
| 70 | really needed and after the process of grokking and parsing |
---|
| 71 | everything on startup. |
---|
| 72 | |
---|
| 73 | So, although this binder binds to some context (`IUniversity` is |
---|
| 74 | currently the only interface making use of this source, so |
---|
| 75 | `University` instances are the only context used), the context |
---|
| 76 | does not really matter. |
---|
| 77 | """ |
---|
| 78 | implements(schema.interfaces.IContextSourceBinder) |
---|
| 79 | |
---|
| 80 | def __call__(self, context): |
---|
[5956] | 81 | return ThemesSource() |
---|
[5955] | 82 | |
---|
[4858] | 83 | class FatalCSVError(Exception): |
---|
| 84 | """Some row could not be processed. |
---|
| 85 | """ |
---|
| 86 | pass |
---|
| 87 | |
---|
[4789] | 88 | def SimpleWAeUPVocabulary(*terms): |
---|
| 89 | """A well-buildt vocabulary provides terms with a value, token and |
---|
| 90 | title for each term |
---|
| 91 | """ |
---|
| 92 | return SimpleVocabulary([ |
---|
| 93 | SimpleTerm(value, value, title) for title, value in terms]) |
---|
| 94 | |
---|
[5955] | 95 | |
---|
[4789] | 96 | class IWAeUPObject(Interface): |
---|
| 97 | """A WAeUP object. |
---|
[5663] | 98 | |
---|
| 99 | This is merely a marker interface. |
---|
[4789] | 100 | """ |
---|
| 101 | |
---|
| 102 | class IUniversity(IWAeUPObject): |
---|
[3521] | 103 | """Representation of a university. |
---|
| 104 | """ |
---|
| 105 | name = schema.TextLine( |
---|
| 106 | title = u'Name of University', |
---|
| 107 | default = u'Unnamed', |
---|
| 108 | required = True, |
---|
| 109 | ) |
---|
[5955] | 110 | |
---|
| 111 | |
---|
[5407] | 112 | skin = schema.Choice( |
---|
| 113 | title = u'Skin', |
---|
| 114 | default = u'waeuptheme-gray1.css', |
---|
[5956] | 115 | source = ThemesSourceBinder(), |
---|
[5407] | 116 | required = True, |
---|
| 117 | ) |
---|
[4789] | 118 | |
---|
| 119 | faculties = Attribute("A container for faculties.") |
---|
| 120 | students = Attribute("A container for students.") |
---|
| 121 | hostels = Attribute("A container for hostels.") |
---|
| 122 | |
---|
| 123 | class IWAeUPContainer(IWAeUPObject): |
---|
| 124 | """A container for WAeUP objects. |
---|
| 125 | """ |
---|
| 126 | |
---|
| 127 | class IWAeUPContained(IWAeUPObject): |
---|
| 128 | """An item contained in an IWAeUPContainer. |
---|
| 129 | """ |
---|
| 130 | |
---|
| 131 | class IStudentContainer(IWAeUPContainer): |
---|
| 132 | """A container for StudentObjects. |
---|
| 133 | """ |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | class IHostelContainer(IWAeUPContainer): |
---|
| 137 | """A container for hostels. |
---|
| 138 | """ |
---|
| 139 | def addHostel(hostel): |
---|
| 140 | """Add an IHostel object. |
---|
| 141 | |
---|
| 142 | Returns the key, under which the object was stored. |
---|
| 143 | """ |
---|
| 144 | |
---|
| 145 | class IHostel(IWAeUPObject): |
---|
| 146 | """Representation of a hostel. |
---|
| 147 | """ |
---|
| 148 | name = schema.TextLine( |
---|
| 149 | title = u'Name of Hostel', |
---|
| 150 | default = u'Nobody', |
---|
| 151 | required = True, |
---|
| 152 | ) |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | class IWAeUPExporter(Interface): |
---|
| 156 | """An exporter for objects. |
---|
| 157 | """ |
---|
| 158 | def export(obj, filepath=None): |
---|
| 159 | """Export by pickling. |
---|
| 160 | |
---|
| 161 | Returns a file-like object containing a representation of `obj`. |
---|
| 162 | |
---|
| 163 | This is done using `pickle`. If `filepath` is ``None``, a |
---|
| 164 | `cStringIO` object is returned, that contains the saved data. |
---|
| 165 | """ |
---|
| 166 | |
---|
| 167 | class IWAeUPXMLExporter(Interface): |
---|
| 168 | """An XML exporter for objects. |
---|
| 169 | """ |
---|
| 170 | def export(obj, filepath=None): |
---|
| 171 | """Export as XML. |
---|
| 172 | |
---|
| 173 | Returns an XML representation of `obj`. |
---|
| 174 | |
---|
| 175 | If `filepath` is ``None``, a StringIO` object is returned, |
---|
| 176 | that contains the transformed data. |
---|
| 177 | """ |
---|
| 178 | |
---|
| 179 | class IWAeUPXMLImporter(Interface): |
---|
| 180 | """An XML import for objects. |
---|
| 181 | """ |
---|
| 182 | def doImport(filepath): |
---|
| 183 | """Create Python object from XML. |
---|
| 184 | |
---|
| 185 | Returns a Python object. |
---|
| 186 | """ |
---|
| 187 | |
---|
[4858] | 188 | class IBatchProcessor(Interface): |
---|
| 189 | """A batch processor that handles mass-operations. |
---|
| 190 | """ |
---|
| 191 | name = schema.TextLine( |
---|
| 192 | title = u'Importer name' |
---|
| 193 | ) |
---|
| 194 | |
---|
| 195 | mode = schema.Choice( |
---|
| 196 | title = u'Import mode', |
---|
| 197 | values = ['create', 'update', 'remove'] |
---|
| 198 | ) |
---|
| 199 | |
---|
[5476] | 200 | def doImport(path, headerfields, mode='create', user='Unknown', |
---|
| 201 | logger=None): |
---|
[4858] | 202 | """Read data from ``path`` and update connected object. |
---|
[5476] | 203 | |
---|
| 204 | `headerfields` is a list of headerfields as read from the file |
---|
| 205 | to import. |
---|
| 206 | |
---|
| 207 | `mode` gives the import mode to use (``'create'``, |
---|
| 208 | ``'update'``, or ``'remove'``. |
---|
| 209 | |
---|
| 210 | `user` is a string describing the user performing the |
---|
| 211 | import. Normally fetched from current principal. |
---|
| 212 | |
---|
| 213 | `logger` is the logger to use during import. |
---|
[4858] | 214 | """ |
---|
| 215 | |
---|
| 216 | class ISchemaTypeConverter(Interface): |
---|
| 217 | """A converter for zope.schema.types. |
---|
| 218 | """ |
---|
| 219 | def convert(string): |
---|
| 220 | """Convert string to certain schema type. |
---|
| 221 | """ |
---|
| 222 | |
---|
| 223 | |
---|
[4789] | 224 | class IUserAccount(IWAeUPObject): |
---|
| 225 | """A user account. |
---|
| 226 | """ |
---|
| 227 | name = schema.TextLine( |
---|
| 228 | title = u'User ID', |
---|
| 229 | description = u'Loginname', |
---|
| 230 | required = True,) |
---|
| 231 | title = schema.TextLine( |
---|
| 232 | title = u'Username', |
---|
| 233 | description = u'Real name', |
---|
| 234 | required = False,) |
---|
| 235 | description = schema.TextLine( |
---|
| 236 | title = u'User description', |
---|
| 237 | required = False,) |
---|
| 238 | password = schema.Password( |
---|
| 239 | title = u'Password', |
---|
| 240 | required = True,) |
---|
| 241 | roles = schema.List( |
---|
| 242 | title = u'Roles', |
---|
| 243 | value_type = schema.Choice(source=RoleSource())) |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | class IUserContainer(IWAeUPObject): |
---|
| 247 | """A container for users (principals). |
---|
| 248 | |
---|
| 249 | These users are used for authentication purposes. |
---|
| 250 | """ |
---|
| 251 | |
---|
| 252 | def addUser(name, password, title=None, description=None): |
---|
| 253 | """Add a user. |
---|
| 254 | """ |
---|
| 255 | |
---|
| 256 | def delUser(name): |
---|
| 257 | """Delete a user if it exists. |
---|
| 258 | """ |
---|
| 259 | |
---|
| 260 | class IDataCenter(IWAeUPObject): |
---|
| 261 | """A data center. |
---|
| 262 | |
---|
| 263 | TODO : declare methods, at least those needed by pages. |
---|
| 264 | """ |
---|
| 265 | pass |
---|
| 266 | |
---|
| 267 | class IDataCenterFile(Interface): |
---|
| 268 | """A data center file. |
---|
| 269 | """ |
---|
[4858] | 270 | |
---|
| 271 | name = schema.TextLine( |
---|
| 272 | title = u'Filename') |
---|
| 273 | |
---|
| 274 | size = schema.TextLine( |
---|
| 275 | title = u'Human readable file size') |
---|
| 276 | |
---|
| 277 | uploaddate = schema.TextLine( |
---|
| 278 | title = u'Human readable upload datetime') |
---|
| 279 | |
---|
| 280 | lines = schema.Int( |
---|
| 281 | title = u'Number of lines in file') |
---|
| 282 | |
---|
[4789] | 283 | def getDate(): |
---|
| 284 | """Get creation timestamp from file in human readable form. |
---|
| 285 | """ |
---|
| 286 | |
---|
| 287 | def getSize(): |
---|
| 288 | """Get human readable size of file. |
---|
| 289 | """ |
---|
[4858] | 290 | |
---|
| 291 | def getLinesNumber(): |
---|
| 292 | """Get number of lines of file. |
---|
| 293 | """ |
---|
[4882] | 294 | |
---|
| 295 | class IDataCenterStorageMovedEvent(IObjectEvent): |
---|
| 296 | """Emitted, when the storage of a datacenter changes. |
---|
| 297 | """ |
---|
[5007] | 298 | |
---|
| 299 | class IQueryResultItem(Interface): |
---|
| 300 | """An item in a search result. |
---|
| 301 | """ |
---|
| 302 | url = schema.TextLine( |
---|
| 303 | title = u'URL that links to the found item') |
---|
| 304 | title = schema.TextLine( |
---|
| 305 | title = u'Title displayed in search results.') |
---|
| 306 | description = schema.Text( |
---|
| 307 | title = u'Longer description of the item found.') |
---|
| 308 | |
---|
[5013] | 309 | class IWAeUPSIRPPluggable(Interface): |
---|
| 310 | """A component that might be plugged into a WAeUP SIRP app. |
---|
[5658] | 311 | |
---|
| 312 | Components implementing this interface are referred to as |
---|
| 313 | 'plugins'. They are normally called when a new |
---|
| 314 | :class:`waeup.sirp.app.University` instance is created. |
---|
| 315 | |
---|
| 316 | Plugins can setup and update parts of the central site without the |
---|
| 317 | site object (normally a :class:`waeup.sirp.app.University` object) |
---|
| 318 | needing to know about that parts. The site simply collects all |
---|
| 319 | available plugins, calls them and the plugins care for their |
---|
[5676] | 320 | respective subarea like the applicants area or the datacenter |
---|
[5658] | 321 | area. |
---|
| 322 | |
---|
| 323 | Currently we have no mechanism to define an order of plugins. A |
---|
| 324 | plugin should therefore make no assumptions about the state of the |
---|
| 325 | site or other plugins being run before and instead do appropriate |
---|
| 326 | checks if necessary. |
---|
| 327 | |
---|
| 328 | Updates can be triggered for instance by the respective form in |
---|
| 329 | the site configuration. You normally do updates when the |
---|
| 330 | underlying software changed. |
---|
[5013] | 331 | """ |
---|
[5069] | 332 | def setup(site, name, logger): |
---|
| 333 | """Create an instance of the plugin. |
---|
[5013] | 334 | |
---|
[5658] | 335 | The method is meant to be called by the central app (site) |
---|
| 336 | when it is created. |
---|
| 337 | |
---|
| 338 | `site`: |
---|
| 339 | The site that requests a setup. |
---|
| 340 | |
---|
| 341 | `name`: |
---|
| 342 | The name under which the plugin was registered (utility name). |
---|
| 343 | |
---|
| 344 | `logger`: |
---|
| 345 | A standard Python logger for the plugins use. |
---|
[5069] | 346 | """ |
---|
| 347 | |
---|
| 348 | def update(site, name, logger): |
---|
| 349 | """Method to update an already existing plugin. |
---|
| 350 | |
---|
| 351 | This might be called by a site when something serious |
---|
[5658] | 352 | changes. It is a poor-man replacement for Zope generations |
---|
| 353 | (but probably more comprehensive and better understandable). |
---|
| 354 | |
---|
| 355 | `site`: |
---|
| 356 | The site that requests an update. |
---|
| 357 | |
---|
| 358 | `name`: |
---|
| 359 | The name under which the plugin was registered (utility name). |
---|
| 360 | |
---|
| 361 | `logger`: |
---|
| 362 | A standard Python logger for the plugins use. |
---|
[5069] | 363 | """ |
---|
[5898] | 364 | |
---|
[5899] | 365 | class IAuthPluginUtility(Interface): |
---|
[5898] | 366 | """A component that cares for authentication setup at site creation. |
---|
| 367 | |
---|
| 368 | Utilities providing this interface are looked up when a Pluggable |
---|
| 369 | Authentication Utility (PAU) for any |
---|
| 370 | :class:`waeup.sirp.app.University` instance is created and put |
---|
| 371 | into ZODB. |
---|
| 372 | |
---|
| 373 | The setup-code then calls the `register` method of the utility and |
---|
| 374 | expects a modified (or unmodified) version of the PAU back. |
---|
| 375 | |
---|
| 376 | This allows to define any authentication setup modifications by |
---|
| 377 | submodules or third-party modules/packages. |
---|
| 378 | """ |
---|
| 379 | |
---|
| 380 | def register(pau): |
---|
| 381 | """Register any plugins wanted to be in the PAU. |
---|
| 382 | """ |
---|
| 383 | |
---|
| 384 | def unregister(pau): |
---|
| 385 | """Unregister any plugins not wanted to be in the PAU. |
---|
| 386 | """ |
---|