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