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