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