[3521] | 1 | ## |
---|
| 2 | ## interfaces.py |
---|
[6361] | 3 | import os |
---|
[6353] | 4 | from hurry.workflow.interfaces import IWorkflow, IWorkflowInfo |
---|
[4789] | 5 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
[6147] | 6 | from zope import schema |
---|
[4789] | 7 | from zope.component import getUtility |
---|
[4882] | 8 | from zope.component.interfaces import IObjectEvent |
---|
[5955] | 9 | from zope.interface import Interface, Attribute, implements |
---|
[4789] | 10 | from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm |
---|
[3521] | 11 | |
---|
[6361] | 12 | default_frontpage = u'' + open(os.path.join( |
---|
| 13 | os.path.dirname(__file__), 'frontpage.rst'), 'rb').read() |
---|
| 14 | |
---|
[4858] | 15 | class FatalCSVError(Exception): |
---|
| 16 | """Some row could not be processed. |
---|
| 17 | """ |
---|
| 18 | pass |
---|
| 19 | |
---|
[6226] | 20 | class DuplicationError(Exception): |
---|
| 21 | """An exception that can be raised when duplicates are found. |
---|
| 22 | |
---|
| 23 | When raising :exc:`DuplicationError` you can, beside the usual |
---|
| 24 | message, specify a list of objects which are duplicates. These |
---|
| 25 | values can be used by catching code to print something helpful or |
---|
| 26 | similar. |
---|
| 27 | """ |
---|
| 28 | def __init__(self, msg, entries=[]): |
---|
| 29 | self.msg = msg |
---|
| 30 | self.entries = entries |
---|
| 31 | |
---|
| 32 | def __str__(self): |
---|
| 33 | return '%r' % self.msg |
---|
| 34 | |
---|
[4789] | 35 | def SimpleWAeUPVocabulary(*terms): |
---|
| 36 | """A well-buildt vocabulary provides terms with a value, token and |
---|
| 37 | title for each term |
---|
| 38 | """ |
---|
| 39 | return SimpleVocabulary([ |
---|
| 40 | SimpleTerm(value, value, title) for title, value in terms]) |
---|
| 41 | |
---|
[6143] | 42 | class RoleSource(BasicSourceFactory): |
---|
[6508] | 43 | """A source for global roles. |
---|
| 44 | """ |
---|
[6143] | 45 | def getValues(self): |
---|
[6157] | 46 | # late import: in interfaces we should not import local modules |
---|
| 47 | from waeup.sirp.permissions import getWAeUPRoleNames |
---|
| 48 | return getWAeUPRoleNames() |
---|
| 49 | |
---|
| 50 | def getTitle(self, value): |
---|
| 51 | # late import: in interfaces we should not import local modules |
---|
[6143] | 52 | from waeup.sirp.permissions import getRoles |
---|
[6157] | 53 | roles = dict(getRoles()) |
---|
| 54 | if value in roles.keys(): |
---|
| 55 | title = roles[value].title |
---|
| 56 | if '.' in title: |
---|
| 57 | title = title.split('.', 2)[1] |
---|
| 58 | return title |
---|
[6143] | 59 | |
---|
[4789] | 60 | class IWAeUPObject(Interface): |
---|
| 61 | """A WAeUP object. |
---|
[5663] | 62 | |
---|
| 63 | This is merely a marker interface. |
---|
[4789] | 64 | """ |
---|
| 65 | |
---|
| 66 | class IUniversity(IWAeUPObject): |
---|
[3521] | 67 | """Representation of a university. |
---|
| 68 | """ |
---|
| 69 | name = schema.TextLine( |
---|
| 70 | title = u'Name of University', |
---|
[6361] | 71 | default = u'Sample University', |
---|
[3521] | 72 | required = True, |
---|
| 73 | ) |
---|
[5955] | 74 | |
---|
[6065] | 75 | title = schema.TextLine( |
---|
| 76 | title = u'Title of frontpage', |
---|
[6361] | 77 | default = u'Welcome to the Student Information and Registration ' + |
---|
| 78 | u'Portal of Sample University', |
---|
[6065] | 79 | required = False, |
---|
| 80 | ) |
---|
| 81 | |
---|
[5407] | 82 | skin = schema.Choice( |
---|
| 83 | title = u'Skin', |
---|
| 84 | default = u'waeuptheme-gray1.css', |
---|
[5968] | 85 | vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary', |
---|
[5407] | 86 | required = True, |
---|
| 87 | ) |
---|
[6136] | 88 | |
---|
[6065] | 89 | frontpage = schema.Text( |
---|
[6132] | 90 | title = u'Content in reST format', |
---|
[6065] | 91 | required = False, |
---|
[6361] | 92 | default = default_frontpage, |
---|
[6136] | 93 | ) |
---|
[4789] | 94 | |
---|
[6136] | 95 | |
---|
[4789] | 96 | class IWAeUPContainer(IWAeUPObject): |
---|
| 97 | """A container for WAeUP objects. |
---|
| 98 | """ |
---|
| 99 | |
---|
| 100 | class IWAeUPContained(IWAeUPObject): |
---|
| 101 | """An item contained in an IWAeUPContainer. |
---|
| 102 | """ |
---|
[6136] | 103 | |
---|
[4789] | 104 | class IStudentContainer(IWAeUPContainer): |
---|
| 105 | """A container for StudentObjects. |
---|
| 106 | """ |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | class IHostelContainer(IWAeUPContainer): |
---|
| 110 | """A container for hostels. |
---|
| 111 | """ |
---|
| 112 | def addHostel(hostel): |
---|
| 113 | """Add an IHostel object. |
---|
| 114 | |
---|
| 115 | Returns the key, under which the object was stored. |
---|
| 116 | """ |
---|
| 117 | |
---|
| 118 | class IHostel(IWAeUPObject): |
---|
| 119 | """Representation of a hostel. |
---|
| 120 | """ |
---|
| 121 | name = schema.TextLine( |
---|
| 122 | title = u'Name of Hostel', |
---|
| 123 | default = u'Nobody', |
---|
| 124 | required = True, |
---|
| 125 | ) |
---|
| 126 | |
---|
[6136] | 127 | |
---|
[4789] | 128 | class IWAeUPExporter(Interface): |
---|
| 129 | """An exporter for objects. |
---|
| 130 | """ |
---|
| 131 | def export(obj, filepath=None): |
---|
| 132 | """Export by pickling. |
---|
| 133 | |
---|
| 134 | Returns a file-like object containing a representation of `obj`. |
---|
| 135 | |
---|
| 136 | This is done using `pickle`. If `filepath` is ``None``, a |
---|
| 137 | `cStringIO` object is returned, that contains the saved data. |
---|
| 138 | """ |
---|
| 139 | |
---|
| 140 | class IWAeUPXMLExporter(Interface): |
---|
| 141 | """An XML exporter for objects. |
---|
| 142 | """ |
---|
| 143 | def export(obj, filepath=None): |
---|
| 144 | """Export as XML. |
---|
| 145 | |
---|
| 146 | Returns an XML representation of `obj`. |
---|
| 147 | |
---|
| 148 | If `filepath` is ``None``, a StringIO` object is returned, |
---|
| 149 | that contains the transformed data. |
---|
| 150 | """ |
---|
| 151 | |
---|
| 152 | class IWAeUPXMLImporter(Interface): |
---|
| 153 | """An XML import for objects. |
---|
| 154 | """ |
---|
| 155 | def doImport(filepath): |
---|
| 156 | """Create Python object from XML. |
---|
| 157 | |
---|
| 158 | Returns a Python object. |
---|
| 159 | """ |
---|
| 160 | |
---|
[4858] | 161 | class IBatchProcessor(Interface): |
---|
| 162 | """A batch processor that handles mass-operations. |
---|
| 163 | """ |
---|
| 164 | name = schema.TextLine( |
---|
| 165 | title = u'Importer name' |
---|
| 166 | ) |
---|
| 167 | |
---|
| 168 | mode = schema.Choice( |
---|
| 169 | title = u'Import mode', |
---|
| 170 | values = ['create', 'update', 'remove'] |
---|
| 171 | ) |
---|
[6136] | 172 | |
---|
[5476] | 173 | def doImport(path, headerfields, mode='create', user='Unknown', |
---|
| 174 | logger=None): |
---|
[4858] | 175 | """Read data from ``path`` and update connected object. |
---|
[5476] | 176 | |
---|
| 177 | `headerfields` is a list of headerfields as read from the file |
---|
| 178 | to import. |
---|
| 179 | |
---|
| 180 | `mode` gives the import mode to use (``'create'``, |
---|
| 181 | ``'update'``, or ``'remove'``. |
---|
| 182 | |
---|
| 183 | `user` is a string describing the user performing the |
---|
| 184 | import. Normally fetched from current principal. |
---|
| 185 | |
---|
| 186 | `logger` is the logger to use during import. |
---|
[4858] | 187 | """ |
---|
| 188 | |
---|
[4789] | 189 | class IUserAccount(IWAeUPObject): |
---|
| 190 | """A user account. |
---|
| 191 | """ |
---|
| 192 | name = schema.TextLine( |
---|
| 193 | title = u'User ID', |
---|
[6512] | 194 | description = u'Login name of user', |
---|
[4789] | 195 | required = True,) |
---|
| 196 | title = schema.TextLine( |
---|
[6512] | 197 | title = u'Name', |
---|
| 198 | description = u'Real name of user', |
---|
[4789] | 199 | required = False,) |
---|
| 200 | description = schema.TextLine( |
---|
[6512] | 201 | title = u'Description', |
---|
[4789] | 202 | required = False,) |
---|
| 203 | password = schema.Password( |
---|
| 204 | title = u'Password', |
---|
| 205 | required = True,) |
---|
| 206 | roles = schema.List( |
---|
[6508] | 207 | title = u'Global roles', |
---|
[4789] | 208 | value_type = schema.Choice(source=RoleSource())) |
---|
[6136] | 209 | |
---|
| 210 | |
---|
[4789] | 211 | class IUserContainer(IWAeUPObject): |
---|
| 212 | """A container for users (principals). |
---|
| 213 | |
---|
| 214 | These users are used for authentication purposes. |
---|
| 215 | """ |
---|
| 216 | |
---|
| 217 | def addUser(name, password, title=None, description=None): |
---|
| 218 | """Add a user. |
---|
| 219 | """ |
---|
| 220 | |
---|
| 221 | def delUser(name): |
---|
| 222 | """Delete a user if it exists. |
---|
| 223 | """ |
---|
| 224 | |
---|
[6141] | 225 | class ILocalRolesAssignable(Interface): |
---|
| 226 | """The local roles assignable to an object. |
---|
| 227 | """ |
---|
| 228 | def __call__(): |
---|
| 229 | """Returns a list of dicts. |
---|
| 230 | |
---|
| 231 | Each dict contains a ``name`` referring to the role assignable |
---|
| 232 | for the specified object and a `title` to describe the range |
---|
| 233 | of users to which this role can be assigned. |
---|
| 234 | """ |
---|
| 235 | |
---|
[4789] | 236 | class IDataCenter(IWAeUPObject): |
---|
| 237 | """A data center. |
---|
| 238 | |
---|
| 239 | TODO : declare methods, at least those needed by pages. |
---|
| 240 | """ |
---|
| 241 | pass |
---|
| 242 | |
---|
| 243 | class IDataCenterFile(Interface): |
---|
| 244 | """A data center file. |
---|
| 245 | """ |
---|
[4858] | 246 | |
---|
| 247 | name = schema.TextLine( |
---|
| 248 | title = u'Filename') |
---|
| 249 | |
---|
| 250 | size = schema.TextLine( |
---|
| 251 | title = u'Human readable file size') |
---|
| 252 | |
---|
| 253 | uploaddate = schema.TextLine( |
---|
| 254 | title = u'Human readable upload datetime') |
---|
| 255 | |
---|
| 256 | lines = schema.Int( |
---|
| 257 | title = u'Number of lines in file') |
---|
[6136] | 258 | |
---|
[4789] | 259 | def getDate(): |
---|
| 260 | """Get creation timestamp from file in human readable form. |
---|
| 261 | """ |
---|
| 262 | |
---|
| 263 | def getSize(): |
---|
| 264 | """Get human readable size of file. |
---|
| 265 | """ |
---|
[4858] | 266 | |
---|
| 267 | def getLinesNumber(): |
---|
| 268 | """Get number of lines of file. |
---|
| 269 | """ |
---|
[4882] | 270 | |
---|
| 271 | class IDataCenterStorageMovedEvent(IObjectEvent): |
---|
| 272 | """Emitted, when the storage of a datacenter changes. |
---|
| 273 | """ |
---|
[5007] | 274 | |
---|
[6136] | 275 | class IObjectUpgradeEvent(IObjectEvent): |
---|
| 276 | """Can be fired, when an object shall be upgraded. |
---|
| 277 | """ |
---|
| 278 | |
---|
[6180] | 279 | class ILocalRoleSetEvent(IObjectEvent): |
---|
| 280 | """A local role was granted/revoked for a principal on an object. |
---|
| 281 | """ |
---|
| 282 | role_id = Attribute( |
---|
| 283 | "The role id that was set.") |
---|
| 284 | principal_id = Attribute( |
---|
| 285 | "The principal id for which the role was granted/revoked.") |
---|
| 286 | granted = Attribute( |
---|
| 287 | "Boolean. If false, then the role was revoked.") |
---|
| 288 | |
---|
[5007] | 289 | class IQueryResultItem(Interface): |
---|
| 290 | """An item in a search result. |
---|
| 291 | """ |
---|
| 292 | url = schema.TextLine( |
---|
| 293 | title = u'URL that links to the found item') |
---|
| 294 | title = schema.TextLine( |
---|
| 295 | title = u'Title displayed in search results.') |
---|
| 296 | description = schema.Text( |
---|
| 297 | title = u'Longer description of the item found.') |
---|
[6136] | 298 | |
---|
[5013] | 299 | class IWAeUPSIRPPluggable(Interface): |
---|
| 300 | """A component that might be plugged into a WAeUP SIRP app. |
---|
[5658] | 301 | |
---|
| 302 | Components implementing this interface are referred to as |
---|
| 303 | 'plugins'. They are normally called when a new |
---|
| 304 | :class:`waeup.sirp.app.University` instance is created. |
---|
| 305 | |
---|
| 306 | Plugins can setup and update parts of the central site without the |
---|
| 307 | site object (normally a :class:`waeup.sirp.app.University` object) |
---|
| 308 | needing to know about that parts. The site simply collects all |
---|
| 309 | available plugins, calls them and the plugins care for their |
---|
[5676] | 310 | respective subarea like the applicants area or the datacenter |
---|
[5658] | 311 | area. |
---|
| 312 | |
---|
| 313 | Currently we have no mechanism to define an order of plugins. A |
---|
| 314 | plugin should therefore make no assumptions about the state of the |
---|
| 315 | site or other plugins being run before and instead do appropriate |
---|
| 316 | checks if necessary. |
---|
| 317 | |
---|
| 318 | Updates can be triggered for instance by the respective form in |
---|
| 319 | the site configuration. You normally do updates when the |
---|
| 320 | underlying software changed. |
---|
[5013] | 321 | """ |
---|
[5069] | 322 | def setup(site, name, logger): |
---|
| 323 | """Create an instance of the plugin. |
---|
[5013] | 324 | |
---|
[5658] | 325 | The method is meant to be called by the central app (site) |
---|
| 326 | when it is created. |
---|
| 327 | |
---|
| 328 | `site`: |
---|
| 329 | The site that requests a setup. |
---|
| 330 | |
---|
| 331 | `name`: |
---|
| 332 | The name under which the plugin was registered (utility name). |
---|
| 333 | |
---|
| 334 | `logger`: |
---|
| 335 | A standard Python logger for the plugins use. |
---|
[5069] | 336 | """ |
---|
| 337 | |
---|
| 338 | def update(site, name, logger): |
---|
| 339 | """Method to update an already existing plugin. |
---|
| 340 | |
---|
| 341 | This might be called by a site when something serious |
---|
[5658] | 342 | changes. It is a poor-man replacement for Zope generations |
---|
| 343 | (but probably more comprehensive and better understandable). |
---|
| 344 | |
---|
| 345 | `site`: |
---|
| 346 | The site that requests an update. |
---|
| 347 | |
---|
| 348 | `name`: |
---|
| 349 | The name under which the plugin was registered (utility name). |
---|
| 350 | |
---|
| 351 | `logger`: |
---|
| 352 | A standard Python logger for the plugins use. |
---|
[5069] | 353 | """ |
---|
[5898] | 354 | |
---|
[5899] | 355 | class IAuthPluginUtility(Interface): |
---|
[5898] | 356 | """A component that cares for authentication setup at site creation. |
---|
| 357 | |
---|
| 358 | Utilities providing this interface are looked up when a Pluggable |
---|
| 359 | Authentication Utility (PAU) for any |
---|
| 360 | :class:`waeup.sirp.app.University` instance is created and put |
---|
| 361 | into ZODB. |
---|
| 362 | |
---|
| 363 | The setup-code then calls the `register` method of the utility and |
---|
| 364 | expects a modified (or unmodified) version of the PAU back. |
---|
| 365 | |
---|
| 366 | This allows to define any authentication setup modifications by |
---|
| 367 | submodules or third-party modules/packages. |
---|
| 368 | """ |
---|
| 369 | |
---|
| 370 | def register(pau): |
---|
| 371 | """Register any plugins wanted to be in the PAU. |
---|
| 372 | """ |
---|
| 373 | |
---|
| 374 | def unregister(pau): |
---|
| 375 | """Unregister any plugins not wanted to be in the PAU. |
---|
| 376 | """ |
---|
[6273] | 377 | |
---|
| 378 | class IObjectConverter(Interface): |
---|
| 379 | """Object converters are available as simple adapters, adapting |
---|
| 380 | interfaces (not regular instances). |
---|
| 381 | |
---|
| 382 | """ |
---|
| 383 | |
---|
[6277] | 384 | def fromStringDict(self, data_dict, context, form_fields=None): |
---|
| 385 | """Convert values in `data_dict`. |
---|
[6273] | 386 | |
---|
[6277] | 387 | Converts data in `data_dict` into real values based on |
---|
| 388 | `context` and `form_fields`. |
---|
[6273] | 389 | |
---|
[6277] | 390 | `data_dict` is a mapping (dict) from field names to values |
---|
| 391 | represented as strings. |
---|
[6273] | 392 | |
---|
[6277] | 393 | The fields (keys) to convert can be given in optional |
---|
| 394 | `form_fields`. If given, form_fields should be an instance of |
---|
| 395 | :class:`zope.formlib.form.Fields`. Suitable instances are for |
---|
| 396 | example created by :class:`grok.AutoFields`. |
---|
[6273] | 397 | |
---|
[6277] | 398 | If no `form_fields` are given, a default is computed from the |
---|
| 399 | associated interface. |
---|
[6273] | 400 | |
---|
[6277] | 401 | The `context` can be an existing object (implementing the |
---|
| 402 | associated interface) or a factory name. If it is a string, we |
---|
| 403 | try to create an object using |
---|
| 404 | :func:`zope.component.createObject`. |
---|
| 405 | |
---|
| 406 | Returns a tuple ``(<FIELD_ERRORS>, <INVARIANT_ERRORS>, |
---|
| 407 | <DATA_DICT>)`` where |
---|
| 408 | |
---|
| 409 | ``<FIELD_ERRORS>`` |
---|
| 410 | is a list of tuples ``(<FIELD_NAME>, <ERROR>)`` for each |
---|
| 411 | error that happened when validating the input data in |
---|
| 412 | `data_dict` |
---|
| 413 | |
---|
| 414 | ``<INVARIANT_ERRORS>`` |
---|
| 415 | is a list of invariant errors concerning several fields |
---|
| 416 | |
---|
| 417 | ``<DATA_DICT>`` |
---|
| 418 | is a dict with the values from input dict converted. |
---|
| 419 | |
---|
| 420 | If errors happen, i.e. the error lists are not empty, always |
---|
| 421 | an empty ``<DATA_DICT>`` is returned. |
---|
| 422 | |
---|
| 423 | If ``<DATA_DICT>` is non-empty, there were no errors. |
---|
[6273] | 424 | """ |
---|
[6293] | 425 | |
---|
[6338] | 426 | class IObjectHistory(Interface): |
---|
| 427 | |
---|
| 428 | messages = schema.List( |
---|
| 429 | title = u'List of messages stored', |
---|
| 430 | required = True, |
---|
| 431 | ) |
---|
| 432 | |
---|
| 433 | def addMessage(message): |
---|
| 434 | """Add a message. |
---|
| 435 | """ |
---|
| 436 | pass |
---|
[6353] | 437 | |
---|
| 438 | class IWAeUPWorkflowInfo(IWorkflowInfo): |
---|
| 439 | """A :class:`hurry.workflow.workflow.WorkflowInfo` with additional |
---|
| 440 | methods for convenience. |
---|
| 441 | """ |
---|
| 442 | def getManualTransitions(): |
---|
| 443 | """Get allowed manual transitions. |
---|
| 444 | |
---|
| 445 | Get a sorted list of tuples containing the `transition_id` and |
---|
| 446 | `title` of each allowed transition. |
---|
| 447 | """ |
---|
| 448 | pass |
---|
[6481] | 449 | |
---|
| 450 | class ISiteLoggers(Interface): |
---|
| 451 | |
---|
| 452 | loggers = Attribute("A list or generator of registered WAeUPLoggers") |
---|
| 453 | |
---|
| 454 | def register(name, filename=None, site=None, **options): |
---|
| 455 | """Register a logger `name` which logs to `filename`. |
---|
| 456 | |
---|
| 457 | If `filename` is not given, logfile will be `name` with |
---|
| 458 | ``.log`` as filename extension. |
---|
| 459 | """ |
---|
| 460 | |
---|
| 461 | def unregister(name): |
---|
| 462 | """Unregister a once registered logger. |
---|
| 463 | """ |
---|
| 464 | |
---|
| 465 | class ILogger(Interface): |
---|
| 466 | """A logger cares for setup, update and restarting of a Python logger. |
---|
| 467 | """ |
---|
| 468 | |
---|
| 469 | logger = Attribute("""A :class:`logging.Logger` instance""") |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | def __init__(name, filename=None, site=None, **options): |
---|
| 473 | """Create a WAeUP logger instance. |
---|
| 474 | """ |
---|
| 475 | pass |
---|
| 476 | |
---|
| 477 | def setup(): |
---|
| 478 | """Create a Python :class:`logging.Logger` instance. |
---|
| 479 | |
---|
| 480 | The created logger is based on the params given by constructor. |
---|
| 481 | """ |
---|
| 482 | pass |
---|
| 483 | |
---|
| 484 | def update(**options): |
---|
| 485 | """Update the logger. |
---|
| 486 | |
---|
| 487 | Updates the logger respecting modified `options` and changed |
---|
| 488 | paths. |
---|
| 489 | """ |
---|
| 490 | |
---|