[7193] | 1 | ## $Id: interfaces.py 10627 2013-09-20 12:17:39Z henrik $ |
---|
[3521] | 2 | ## |
---|
[7193] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[6361] | 18 | import os |
---|
[7221] | 19 | import re |
---|
[7702] | 20 | import codecs |
---|
[9217] | 21 | import zc.async.interfaces |
---|
[7670] | 22 | import zope.i18nmessageid |
---|
[6915] | 23 | from datetime import datetime |
---|
[7063] | 24 | from hurry.file.interfaces import IFileRetrieval |
---|
[8394] | 25 | from hurry.workflow.interfaces import IWorkflowInfo |
---|
[4789] | 26 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
[6147] | 27 | from zope import schema |
---|
[7233] | 28 | from zope.pluggableauth.interfaces import IPrincipalInfo |
---|
| 29 | from zope.security.interfaces import IGroupClosureAwarePrincipal as IPrincipal |
---|
[4789] | 30 | from zope.component import getUtility |
---|
[4882] | 31 | from zope.component.interfaces import IObjectEvent |
---|
[9217] | 32 | from zope.configuration.fields import Path |
---|
| 33 | from zope.container.interfaces import INameChooser, IContainer |
---|
[8394] | 34 | from zope.interface import Interface, Attribute |
---|
[7795] | 35 | from zope.schema.interfaces import IObject |
---|
[4789] | 36 | from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm |
---|
[8176] | 37 | from waeup.kofa.schema import PhoneNumber |
---|
[3521] | 38 | |
---|
[7811] | 39 | _ = MessageFactory = zope.i18nmessageid.MessageFactory('waeup.kofa') |
---|
[6990] | 40 | |
---|
[8214] | 41 | DELETION_MARKER = 'XXX' |
---|
| 42 | IGNORE_MARKER = '<IGNORE>' |
---|
[9217] | 43 | WAEUP_KEY = 'waeup.kofa' |
---|
| 44 | VIRT_JOBS_CONTAINER_NAME = 'jobs' |
---|
[8202] | 45 | |
---|
[7673] | 46 | CREATED = 'created' |
---|
| 47 | ADMITTED = 'admitted' |
---|
| 48 | CLEARANCE = 'clearance started' |
---|
| 49 | REQUESTED = 'clearance requested' |
---|
| 50 | CLEARED = 'cleared' |
---|
| 51 | PAID = 'school fee paid' |
---|
| 52 | RETURNING = 'returning' |
---|
| 53 | REGISTERED = 'courses registered' |
---|
| 54 | VALIDATED = 'courses validated' |
---|
[10446] | 55 | GRADUATED = 'graduated' |
---|
| 56 | TRANSCRIPT = 'transcript requested' |
---|
[7670] | 57 | |
---|
[10446] | 58 | |
---|
[9217] | 59 | #: A dict giving job status as tuple (<STRING>, <TRANSLATED_STRING>), |
---|
| 60 | #: the latter for UI purposes. |
---|
| 61 | JOB_STATUS_MAP = { |
---|
| 62 | zc.async.interfaces.NEW: ('new', _('new')), |
---|
| 63 | zc.async.interfaces.COMPLETED: ('completed', _('completed')), |
---|
| 64 | zc.async.interfaces.PENDING: ('pending', _('pending')), |
---|
| 65 | zc.async.interfaces.ACTIVE: ('active', _('active')), |
---|
| 66 | zc.async.interfaces.ASSIGNED: ('assigned', _('assigned')), |
---|
| 67 | zc.async.interfaces.CALLBACKS: ('callbacks', _('callbacks')), |
---|
| 68 | } |
---|
| 69 | |
---|
[8361] | 70 | #default_rest_frontpage = u'' + codecs.open(os.path.join( |
---|
| 71 | # os.path.dirname(__file__), 'frontpage.rst'), |
---|
| 72 | # encoding='utf-8', mode='rb').read() |
---|
| 73 | |
---|
| 74 | default_html_frontpage = u'' + codecs.open(os.path.join( |
---|
| 75 | os.path.dirname(__file__), 'frontpage.html'), |
---|
[7702] | 76 | encoding='utf-8', mode='rb').read() |
---|
[6361] | 77 | |
---|
[7819] | 78 | def SimpleKofaVocabulary(*terms): |
---|
[6915] | 79 | """A well-buildt vocabulary provides terms with a value, token and |
---|
| 80 | title for each term |
---|
| 81 | """ |
---|
| 82 | return SimpleVocabulary([ |
---|
| 83 | SimpleTerm(value, value, title) for title, value in terms]) |
---|
| 84 | |
---|
| 85 | def academic_sessions(): |
---|
| 86 | curr_year = datetime.now().year |
---|
[9115] | 87 | year_range = range(1995, curr_year + 2) |
---|
[6915] | 88 | return [('%s/%s' % (year,year+1), year) for year in year_range] |
---|
| 89 | |
---|
[7819] | 90 | academic_sessions_vocab = SimpleKofaVocabulary(*academic_sessions()) |
---|
[6915] | 91 | |
---|
[7819] | 92 | registration_states_vocab = SimpleKofaVocabulary( |
---|
[7677] | 93 | (_('created'), CREATED), |
---|
| 94 | (_('admitted'), ADMITTED), |
---|
| 95 | (_('clearance started'), CLEARANCE), |
---|
| 96 | (_('clearance requested'), REQUESTED), |
---|
| 97 | (_('cleared'), CLEARED), |
---|
| 98 | (_('school fee paid'), PAID), |
---|
| 99 | (_('courses registered'), REGISTERED), |
---|
| 100 | (_('courses validated'), VALIDATED), |
---|
[9671] | 101 | (_('returning'), RETURNING), |
---|
[10451] | 102 | (_('graduated'), GRADUATED), |
---|
| 103 | (_('transcript requested'), TRANSCRIPT), |
---|
[6990] | 104 | ) |
---|
| 105 | |
---|
[7795] | 106 | class SubjectSource(BasicSourceFactory): |
---|
[7918] | 107 | """A source for school subjects used in exam documentation. |
---|
| 108 | """ |
---|
[7795] | 109 | def getValues(self): |
---|
[7841] | 110 | subjects_dict = getUtility(IKofaUtils).EXAM_SUBJECTS_DICT |
---|
[7837] | 111 | return sorted(subjects_dict.keys()) |
---|
| 112 | |
---|
[7795] | 113 | def getTitle(self, value): |
---|
[7841] | 114 | subjects_dict = getUtility(IKofaUtils).EXAM_SUBJECTS_DICT |
---|
[7837] | 115 | return "%s:" % subjects_dict[value] |
---|
[7795] | 116 | |
---|
| 117 | class GradeSource(BasicSourceFactory): |
---|
[7918] | 118 | """A source for exam grades. |
---|
| 119 | """ |
---|
[7795] | 120 | def getValues(self): |
---|
[7918] | 121 | for entry in getUtility(IKofaUtils).EXAM_GRADES: |
---|
| 122 | yield entry[0] |
---|
[7837] | 123 | |
---|
[7795] | 124 | def getTitle(self, value): |
---|
[7918] | 125 | return dict(getUtility(IKofaUtils).EXAM_GRADES)[value] |
---|
[7795] | 126 | |
---|
[7850] | 127 | # Define a validation method for email addresses |
---|
[7221] | 128 | class NotAnEmailAddress(schema.ValidationError): |
---|
| 129 | __doc__ = u"Invalid email address" |
---|
| 130 | |
---|
[8638] | 131 | #: Regular expression to check email-address formats. As these can |
---|
| 132 | #: become rather complex (nearly everything is allowed by RFCs), we only |
---|
| 133 | #: forbid whitespaces, commas and dots following onto each other. |
---|
[7221] | 134 | check_email = re.compile( |
---|
[8638] | 135 | r"^[^@\s,]+@[^@\.\s,]+(\.[^@\.\s,]+)*$").match |
---|
[7221] | 136 | |
---|
| 137 | def validate_email(value): |
---|
| 138 | if not check_email(value): |
---|
| 139 | raise NotAnEmailAddress(value) |
---|
| 140 | return True |
---|
| 141 | |
---|
[7850] | 142 | # Define a validation method for international phone numbers |
---|
| 143 | class InvalidPhoneNumber(schema.ValidationError): |
---|
| 144 | __doc__ = u"Invalid phone number" |
---|
| 145 | |
---|
| 146 | # represent format +NNN-NNNN-NNNN |
---|
| 147 | RE_INT_PHONE = re.compile(r"^\+?\d+\-\d+\-[\d\-]+$") |
---|
| 148 | |
---|
| 149 | def validate_phone(value): |
---|
[7851] | 150 | if not RE_INT_PHONE.match(value): |
---|
[7850] | 151 | raise InvalidPhoneNumber(value) |
---|
| 152 | return True |
---|
| 153 | |
---|
[4858] | 154 | class FatalCSVError(Exception): |
---|
| 155 | """Some row could not be processed. |
---|
| 156 | """ |
---|
| 157 | pass |
---|
| 158 | |
---|
[6226] | 159 | class DuplicationError(Exception): |
---|
| 160 | """An exception that can be raised when duplicates are found. |
---|
| 161 | |
---|
| 162 | When raising :exc:`DuplicationError` you can, beside the usual |
---|
| 163 | message, specify a list of objects which are duplicates. These |
---|
| 164 | values can be used by catching code to print something helpful or |
---|
| 165 | similar. |
---|
| 166 | """ |
---|
| 167 | def __init__(self, msg, entries=[]): |
---|
| 168 | self.msg = msg |
---|
| 169 | self.entries = entries |
---|
| 170 | |
---|
| 171 | def __str__(self): |
---|
| 172 | return '%r' % self.msg |
---|
| 173 | |
---|
[6143] | 174 | class RoleSource(BasicSourceFactory): |
---|
[7178] | 175 | """A source for site roles. |
---|
[6508] | 176 | """ |
---|
[6143] | 177 | def getValues(self): |
---|
[6157] | 178 | # late import: in interfaces we should not import local modules |
---|
[7811] | 179 | from waeup.kofa.permissions import get_waeup_role_names |
---|
[7186] | 180 | return get_waeup_role_names() |
---|
[6157] | 181 | |
---|
| 182 | def getTitle(self, value): |
---|
| 183 | # late import: in interfaces we should not import local modules |
---|
[7811] | 184 | from waeup.kofa.permissions import get_all_roles |
---|
[7186] | 185 | roles = dict(get_all_roles()) |
---|
[6157] | 186 | if value in roles.keys(): |
---|
| 187 | title = roles[value].title |
---|
[6569] | 188 | if '.' in title: |
---|
| 189 | title = title.split('.', 2)[1] |
---|
[6157] | 190 | return title |
---|
[6143] | 191 | |
---|
[7313] | 192 | class CaptchaSource(BasicSourceFactory): |
---|
| 193 | """A source for captchas. |
---|
| 194 | """ |
---|
| 195 | def getValues(self): |
---|
[7323] | 196 | captchas = ['No captcha', 'Testing captcha', 'ReCaptcha'] |
---|
[7313] | 197 | try: |
---|
| 198 | # we have to 'try' because IConfiguration can only handle |
---|
[7817] | 199 | # interfaces from w.k.interface. |
---|
[7811] | 200 | from waeup.kofa.browser.interfaces import ICaptchaManager |
---|
[7313] | 201 | except: |
---|
| 202 | return captchas |
---|
| 203 | return sorted(getUtility(ICaptchaManager).getAvailCaptchas().keys()) |
---|
| 204 | |
---|
| 205 | def getTitle(self, value): |
---|
| 206 | return value |
---|
| 207 | |
---|
[7795] | 208 | class IResultEntry(Interface): |
---|
| 209 | """A school grade entry. |
---|
| 210 | """ |
---|
| 211 | subject = schema.Choice( |
---|
| 212 | title = _(u'Subject'), |
---|
| 213 | source = SubjectSource(), |
---|
| 214 | ) |
---|
| 215 | grade = schema.Choice( |
---|
| 216 | title = _(u'Grade'), |
---|
| 217 | source = GradeSource(), |
---|
| 218 | ) |
---|
| 219 | |
---|
| 220 | class IResultEntryField(IObject): |
---|
| 221 | """A zope.schema-like field for usage in interfaces. |
---|
| 222 | |
---|
| 223 | Marker interface to distuingish result entries from ordinary |
---|
| 224 | object fields. Needed for registration of widgets. |
---|
| 225 | """ |
---|
| 226 | |
---|
[7819] | 227 | class IKofaUtils(Interface): |
---|
[7358] | 228 | """A collection of methods which are subject to customization. |
---|
| 229 | """ |
---|
[7568] | 230 | |
---|
[7841] | 231 | PORTAL_LANGUAGE = Attribute("Dict of global language setting") |
---|
| 232 | PREFERRED_LANGUAGES_DICT = Attribute("Dict of preferred languages") |
---|
| 233 | EXAM_SUBJECTS_DICT = Attribute("Dict of examination subjects") |
---|
| 234 | EXAM_GRADES_DICT = Attribute("Dict of examination grades") |
---|
| 235 | INST_TYPES_DICT = Attribute("Dict if institution types") |
---|
| 236 | STUDY_MODES_DICT = Attribute("Dict of study modes") |
---|
| 237 | APP_CATS_DICT = Attribute("Dict of application categories") |
---|
| 238 | SEMESTER_DICT = Attribute("Dict of semesters or trimesters") |
---|
[8394] | 239 | INT_PHONE_PREFIXES = Attribute( |
---|
| 240 | "Dict of international phone number prefixes") |
---|
[7568] | 241 | |
---|
[7404] | 242 | def sendContactForm( |
---|
| 243 | from_name,from_addr,rcpt_name,rcpt_addr, |
---|
| 244 | from_username,usertype,portal,body,subject): |
---|
[7358] | 245 | """Send an email with data provided by forms. |
---|
| 246 | """ |
---|
| 247 | |
---|
[7475] | 248 | def fullname(firstname,lastname,middlename): |
---|
| 249 | """Full name constructor. |
---|
| 250 | """ |
---|
| 251 | |
---|
[8853] | 252 | def sendCredentials(user, password, url_info, msg): |
---|
[7475] | 253 | """Send credentials as email. |
---|
| 254 | |
---|
| 255 | Input is the applicant for which credentials are sent and the |
---|
| 256 | password. |
---|
| 257 | |
---|
| 258 | Returns True or False to indicate successful operation. |
---|
| 259 | """ |
---|
| 260 | |
---|
| 261 | def genPassword(length, chars): |
---|
| 262 | """Generate a random password. |
---|
| 263 | """ |
---|
| 264 | |
---|
[7819] | 265 | class IKofaObject(Interface): |
---|
| 266 | """A Kofa object. |
---|
[5663] | 267 | |
---|
| 268 | This is merely a marker interface. |
---|
[4789] | 269 | """ |
---|
| 270 | |
---|
[7819] | 271 | class IUniversity(IKofaObject): |
---|
[3521] | 272 | """Representation of a university. |
---|
| 273 | """ |
---|
[5955] | 274 | |
---|
[6065] | 275 | |
---|
[7819] | 276 | class IKofaContainer(IKofaObject): |
---|
| 277 | """A container for Kofa objects. |
---|
[4789] | 278 | """ |
---|
| 279 | |
---|
[7819] | 280 | class IKofaContained(IKofaObject): |
---|
| 281 | """An item contained in an IKofaContainer. |
---|
[4789] | 282 | """ |
---|
[6136] | 283 | |
---|
[7726] | 284 | class ICSVExporter(Interface): |
---|
| 285 | """A CSV file exporter for objects. |
---|
| 286 | """ |
---|
| 287 | fields = Attribute("""List of fieldnames in resulting CSV""") |
---|
[7907] | 288 | |
---|
| 289 | title = schema.TextLine( |
---|
| 290 | title = u'Title', |
---|
| 291 | description = u'Description to be displayed in selections.', |
---|
| 292 | ) |
---|
[7726] | 293 | def mangle_value(value, name, obj): |
---|
| 294 | """Mangle `value` extracted from `obj` or suobjects thereof. |
---|
| 295 | |
---|
[8394] | 296 | This is called by export before actually writing to the result |
---|
| 297 | file. |
---|
[7726] | 298 | """ |
---|
| 299 | |
---|
[9797] | 300 | def get_filtered(site, **kw): |
---|
| 301 | """Get datasets in `site` to be exported. |
---|
| 302 | |
---|
| 303 | The set of data is specified by keywords, which might be |
---|
| 304 | different for any implementaion of exporter. |
---|
| 305 | |
---|
| 306 | Returns an iterable. |
---|
| 307 | """ |
---|
| 308 | |
---|
[7730] | 309 | def export(iterable, filepath=None): |
---|
| 310 | """Export iterables as rows in a CSV file. |
---|
[7726] | 311 | |
---|
[8394] | 312 | If `filepath` is not given, a string with the data should be |
---|
| 313 | returned. |
---|
[7730] | 314 | |
---|
| 315 | What kind of iterables are acceptable depends on the specific |
---|
| 316 | exporter implementation. |
---|
[7726] | 317 | """ |
---|
| 318 | |
---|
[9766] | 319 | def export_all(site, filepath=None): |
---|
[7726] | 320 | """Export all items in `site` as CSV file. |
---|
| 321 | |
---|
[8394] | 322 | if `filepath` is not given, a string with the data should be |
---|
| 323 | returned. |
---|
[7726] | 324 | """ |
---|
| 325 | |
---|
[9797] | 326 | def export_filtered(site, filepath=None, **kw): |
---|
| 327 | """Export those items in `site` specified by `args` and `kw`. |
---|
| 328 | |
---|
| 329 | If `filepath` is not given, a string with the data should be |
---|
| 330 | returned. |
---|
| 331 | |
---|
| 332 | Which special keywords are supported is up to the respective |
---|
| 333 | exporter. |
---|
| 334 | """ |
---|
| 335 | |
---|
[7819] | 336 | class IKofaExporter(Interface): |
---|
[4789] | 337 | """An exporter for objects. |
---|
| 338 | """ |
---|
| 339 | def export(obj, filepath=None): |
---|
| 340 | """Export by pickling. |
---|
| 341 | |
---|
| 342 | Returns a file-like object containing a representation of `obj`. |
---|
| 343 | |
---|
| 344 | This is done using `pickle`. If `filepath` is ``None``, a |
---|
| 345 | `cStringIO` object is returned, that contains the saved data. |
---|
| 346 | """ |
---|
| 347 | |
---|
[7819] | 348 | class IKofaXMLExporter(Interface): |
---|
[4789] | 349 | """An XML exporter for objects. |
---|
| 350 | """ |
---|
| 351 | def export(obj, filepath=None): |
---|
| 352 | """Export as XML. |
---|
| 353 | |
---|
| 354 | Returns an XML representation of `obj`. |
---|
| 355 | |
---|
| 356 | If `filepath` is ``None``, a StringIO` object is returned, |
---|
| 357 | that contains the transformed data. |
---|
| 358 | """ |
---|
| 359 | |
---|
[7819] | 360 | class IKofaXMLImporter(Interface): |
---|
[4789] | 361 | """An XML import for objects. |
---|
| 362 | """ |
---|
| 363 | def doImport(filepath): |
---|
| 364 | """Create Python object from XML. |
---|
| 365 | |
---|
| 366 | Returns a Python object. |
---|
| 367 | """ |
---|
| 368 | |
---|
[4858] | 369 | class IBatchProcessor(Interface): |
---|
| 370 | """A batch processor that handles mass-operations. |
---|
| 371 | """ |
---|
| 372 | name = schema.TextLine( |
---|
[7933] | 373 | title = _(u'Processor name') |
---|
[4858] | 374 | ) |
---|
| 375 | |
---|
[5476] | 376 | def doImport(path, headerfields, mode='create', user='Unknown', |
---|
[8218] | 377 | logger=None, ignore_empty=True): |
---|
[4858] | 378 | """Read data from ``path`` and update connected object. |
---|
[5476] | 379 | |
---|
| 380 | `headerfields` is a list of headerfields as read from the file |
---|
| 381 | to import. |
---|
| 382 | |
---|
| 383 | `mode` gives the import mode to use (``'create'``, |
---|
| 384 | ``'update'``, or ``'remove'``. |
---|
| 385 | |
---|
| 386 | `user` is a string describing the user performing the |
---|
| 387 | import. Normally fetched from current principal. |
---|
| 388 | |
---|
| 389 | `logger` is the logger to use during import. |
---|
[8218] | 390 | |
---|
| 391 | `ignore_emtpy` in update mode ignores empty fields if true. |
---|
[4858] | 392 | """ |
---|
| 393 | |
---|
[7819] | 394 | class IContactForm(IKofaObject): |
---|
[7225] | 395 | """A contact form. |
---|
| 396 | """ |
---|
| 397 | |
---|
| 398 | email_from = schema.ASCIILine( |
---|
[7828] | 399 | title = _(u'Email Address:'), |
---|
[7225] | 400 | default = None, |
---|
| 401 | required = True, |
---|
| 402 | constraint=validate_email, |
---|
| 403 | ) |
---|
| 404 | |
---|
| 405 | email_to = schema.ASCIILine( |
---|
[7828] | 406 | title = _(u'Email to:'), |
---|
[7225] | 407 | default = None, |
---|
| 408 | required = True, |
---|
| 409 | constraint=validate_email, |
---|
| 410 | ) |
---|
| 411 | |
---|
| 412 | subject = schema.TextLine( |
---|
[7828] | 413 | title = _(u'Subject:'), |
---|
[7225] | 414 | required = True,) |
---|
| 415 | |
---|
| 416 | fullname = schema.TextLine( |
---|
[7828] | 417 | title = _(u'Full Name:'), |
---|
[7225] | 418 | required = True,) |
---|
| 419 | |
---|
| 420 | body = schema.Text( |
---|
[7828] | 421 | title = _(u'Text:'), |
---|
[7225] | 422 | required = True,) |
---|
| 423 | |
---|
[7819] | 424 | class IKofaPrincipalInfo(IPrincipalInfo): |
---|
| 425 | """Infos about principals that are users of Kofa Kofa. |
---|
[7233] | 426 | """ |
---|
| 427 | email = Attribute("The email address of a user") |
---|
| 428 | phone = Attribute("The phone number of a user") |
---|
[8757] | 429 | public_name = Attribute("The public name of a user") |
---|
[7225] | 430 | |
---|
[7233] | 431 | |
---|
[7819] | 432 | class IKofaPrincipal(IPrincipal): |
---|
| 433 | """A principle for Kofa Kofa. |
---|
[7233] | 434 | |
---|
| 435 | This interface extends zope.security.interfaces.IPrincipal and |
---|
| 436 | requires also an `id` and other attributes defined there. |
---|
| 437 | """ |
---|
| 438 | |
---|
| 439 | email = schema.TextLine( |
---|
[7828] | 440 | title = _(u'Email Address'), |
---|
[7233] | 441 | description = u'', |
---|
| 442 | required=False,) |
---|
| 443 | |
---|
[8176] | 444 | phone = PhoneNumber( |
---|
[7828] | 445 | title = _(u'Phone'), |
---|
[7233] | 446 | description = u'', |
---|
| 447 | required=False,) |
---|
| 448 | |
---|
[8757] | 449 | public_name = schema.TextLine( |
---|
| 450 | title = _(u'Public Name'), |
---|
| 451 | required = False,) |
---|
| 452 | |
---|
[10055] | 453 | class IFailedLoginInfo(IKofaObject): |
---|
| 454 | """Info about failed logins. |
---|
| 455 | |
---|
| 456 | Timestamps are supposed to be stored as floats using time.time() |
---|
| 457 | or similar. |
---|
| 458 | """ |
---|
| 459 | num = schema.Int( |
---|
| 460 | title = _(u'Number of failed logins'), |
---|
| 461 | description = _(u'Number of failed logins'), |
---|
| 462 | required = True, |
---|
| 463 | default = 0, |
---|
| 464 | ) |
---|
| 465 | |
---|
| 466 | last = schema.Float( |
---|
| 467 | title = _(u'Timestamp'), |
---|
| 468 | description = _(u'Timestamp of last failed login or `None`'), |
---|
| 469 | required = False, |
---|
| 470 | default = None, |
---|
| 471 | ) |
---|
| 472 | |
---|
| 473 | def as_tuple(): |
---|
| 474 | """Get login info as tuple ``<NUM>, <TIMESTAMP>``. |
---|
| 475 | """ |
---|
| 476 | |
---|
| 477 | def set_values(num=0, last=None): |
---|
| 478 | """Set number of failed logins and timestamp of last one. |
---|
| 479 | """ |
---|
| 480 | |
---|
| 481 | def increase(): |
---|
| 482 | """Increase the current number of failed logins and set timestamp. |
---|
| 483 | """ |
---|
| 484 | |
---|
| 485 | def reset(): |
---|
| 486 | """Set failed login counters back to zero. |
---|
| 487 | """ |
---|
| 488 | |
---|
| 489 | |
---|
[7819] | 490 | class IUserAccount(IKofaObject): |
---|
[4789] | 491 | """A user account. |
---|
| 492 | """ |
---|
[10055] | 493 | |
---|
| 494 | failed_logins = Attribute("""FailedLoginInfo for this account""") |
---|
| 495 | |
---|
[4789] | 496 | name = schema.TextLine( |
---|
[7828] | 497 | title = _(u'User Id'), |
---|
[6512] | 498 | description = u'Login name of user', |
---|
[4789] | 499 | required = True,) |
---|
[7221] | 500 | |
---|
[4789] | 501 | title = schema.TextLine( |
---|
[7828] | 502 | title = _(u'Full Name'), |
---|
[8759] | 503 | required = True,) |
---|
[7221] | 504 | |
---|
[8756] | 505 | public_name = schema.TextLine( |
---|
| 506 | title = _(u'Public Name'), |
---|
[8759] | 507 | description = u"Substitute for officer's real name " |
---|
| 508 | "in student object histories.", |
---|
[8756] | 509 | required = False,) |
---|
| 510 | |
---|
[7197] | 511 | description = schema.Text( |
---|
[7828] | 512 | title = _(u'Description/Notice'), |
---|
[4789] | 513 | required = False,) |
---|
[7221] | 514 | |
---|
| 515 | email = schema.ASCIILine( |
---|
[7828] | 516 | title = _(u'Email Address'), |
---|
[7221] | 517 | default = None, |
---|
[7222] | 518 | required = True, |
---|
[7221] | 519 | constraint=validate_email, |
---|
| 520 | ) |
---|
| 521 | |
---|
[8176] | 522 | phone = PhoneNumber( |
---|
[7828] | 523 | title = _(u'Phone'), |
---|
[7233] | 524 | default = None, |
---|
[8062] | 525 | required = False, |
---|
[7233] | 526 | ) |
---|
| 527 | |
---|
[4789] | 528 | roles = schema.List( |
---|
[8486] | 529 | title = _(u'Portal Roles'), |
---|
[8079] | 530 | value_type = schema.Choice(source=RoleSource()), |
---|
| 531 | required = False, |
---|
| 532 | ) |
---|
[6136] | 533 | |
---|
[10055] | 534 | |
---|
| 535 | |
---|
[7147] | 536 | class IPasswordValidator(Interface): |
---|
| 537 | """A password validator utility. |
---|
| 538 | """ |
---|
[6136] | 539 | |
---|
[7147] | 540 | def validate_password(password, password_repeat): |
---|
| 541 | """Validates a password by comparing it with |
---|
| 542 | control password and checking some other requirements. |
---|
| 543 | """ |
---|
| 544 | |
---|
| 545 | |
---|
[7819] | 546 | class IUsersContainer(IKofaObject): |
---|
[4789] | 547 | """A container for users (principals). |
---|
| 548 | |
---|
| 549 | These users are used for authentication purposes. |
---|
| 550 | """ |
---|
| 551 | |
---|
| 552 | def addUser(name, password, title=None, description=None): |
---|
| 553 | """Add a user. |
---|
| 554 | """ |
---|
| 555 | |
---|
| 556 | def delUser(name): |
---|
| 557 | """Delete a user if it exists. |
---|
| 558 | """ |
---|
| 559 | |
---|
[6141] | 560 | class ILocalRolesAssignable(Interface): |
---|
| 561 | """The local roles assignable to an object. |
---|
| 562 | """ |
---|
| 563 | def __call__(): |
---|
| 564 | """Returns a list of dicts. |
---|
| 565 | |
---|
| 566 | Each dict contains a ``name`` referring to the role assignable |
---|
| 567 | for the specified object and a `title` to describe the range |
---|
| 568 | of users to which this role can be assigned. |
---|
| 569 | """ |
---|
| 570 | |
---|
[7819] | 571 | class IConfigurationContainer(IKofaObject): |
---|
[6907] | 572 | """A container for session configuration objects. |
---|
| 573 | """ |
---|
| 574 | |
---|
| 575 | name = schema.TextLine( |
---|
[7828] | 576 | title = _(u'Name of University'), |
---|
| 577 | default = _(u'Sample University'), |
---|
[6907] | 578 | required = True, |
---|
| 579 | ) |
---|
| 580 | |
---|
[7459] | 581 | acronym = schema.TextLine( |
---|
[7828] | 582 | title = _(u'Abbreviated Title of University'), |
---|
[7819] | 583 | default = u'WAeUP.Kofa', |
---|
[7459] | 584 | required = True, |
---|
| 585 | ) |
---|
| 586 | |
---|
[6907] | 587 | skin = schema.Choice( |
---|
[7828] | 588 | title = _(u'Skin'), |
---|
[6907] | 589 | default = u'gray waeup theme', |
---|
[7811] | 590 | vocabulary = 'waeup.kofa.browser.theming.ThemesVocabulary', |
---|
[6907] | 591 | required = True, |
---|
| 592 | ) |
---|
| 593 | |
---|
| 594 | frontpage = schema.Text( |
---|
[8361] | 595 | title = _(u'Content in HTML format'), |
---|
[6907] | 596 | required = False, |
---|
[8361] | 597 | default = default_html_frontpage, |
---|
[6907] | 598 | ) |
---|
| 599 | |
---|
[7702] | 600 | frontpage_dict = schema.Dict( |
---|
| 601 | title = u'Content as language dictionary with values in html format', |
---|
[7485] | 602 | required = False, |
---|
[7702] | 603 | default = {}, |
---|
[7485] | 604 | ) |
---|
| 605 | |
---|
[7223] | 606 | name_admin = schema.TextLine( |
---|
[7828] | 607 | title = _(u'Name of Administrator'), |
---|
[7223] | 608 | default = u'Administrator', |
---|
[8230] | 609 | required = True, |
---|
[7223] | 610 | ) |
---|
| 611 | |
---|
[7221] | 612 | email_admin = schema.ASCIILine( |
---|
[7828] | 613 | title = _(u'Email Address of Administrator'), |
---|
[7221] | 614 | default = 'contact@waeup.org', |
---|
[8230] | 615 | required = True, |
---|
[7221] | 616 | constraint=validate_email, |
---|
| 617 | ) |
---|
| 618 | |
---|
| 619 | email_subject = schema.TextLine( |
---|
[7828] | 620 | title = _(u'Subject of Email to Administrator'), |
---|
| 621 | default = _(u'Kofa Contact'), |
---|
[8230] | 622 | required = True, |
---|
[7221] | 623 | ) |
---|
| 624 | |
---|
[7470] | 625 | smtp_mailer = schema.Choice( |
---|
[7828] | 626 | title = _(u'SMTP mailer to use when sending mail'), |
---|
[7470] | 627 | vocabulary = 'Mail Delivery Names', |
---|
| 628 | default = 'No email service', |
---|
| 629 | required = True, |
---|
| 630 | ) |
---|
| 631 | |
---|
[7313] | 632 | captcha = schema.Choice( |
---|
[7828] | 633 | title = _(u'Captcha used for public registration pages'), |
---|
[7313] | 634 | source = CaptchaSource(), |
---|
| 635 | default = u'No captcha', |
---|
| 636 | required = True, |
---|
| 637 | ) |
---|
[7221] | 638 | |
---|
[7664] | 639 | carry_over = schema.Bool( |
---|
[7828] | 640 | title = _(u'Carry-over Course Registration'), |
---|
[7664] | 641 | default = False, |
---|
| 642 | ) |
---|
| 643 | |
---|
[10627] | 644 | current_academic_session = schema.Choice( |
---|
| 645 | title = _(u'Current Academic Session'), |
---|
| 646 | source = academic_sessions_vocab, |
---|
| 647 | default = None, |
---|
| 648 | required = False, |
---|
| 649 | readonly = False, |
---|
| 650 | ) |
---|
| 651 | |
---|
[7819] | 652 | class ISessionConfiguration(IKofaObject): |
---|
[6915] | 653 | """A session configuration object. |
---|
[6907] | 654 | """ |
---|
| 655 | |
---|
[6915] | 656 | academic_session = schema.Choice( |
---|
[7828] | 657 | title = _(u'Academic Session'), |
---|
[6915] | 658 | source = academic_sessions_vocab, |
---|
| 659 | default = None, |
---|
| 660 | required = True, |
---|
| 661 | readonly = True, |
---|
| 662 | ) |
---|
| 663 | |
---|
[8260] | 664 | application_fee = schema.Float( |
---|
| 665 | title = _(u'Application Fee'), |
---|
[7927] | 666 | default = 0.0, |
---|
[7881] | 667 | required = False, |
---|
[6916] | 668 | ) |
---|
| 669 | |
---|
[8260] | 670 | clearance_fee = schema.Float( |
---|
[9243] | 671 | title = _(u'Acceptance Fee'), |
---|
[7927] | 672 | default = 0.0, |
---|
[7881] | 673 | required = False, |
---|
[6993] | 674 | ) |
---|
| 675 | |
---|
[8260] | 676 | booking_fee = schema.Float( |
---|
| 677 | title = _(u'Bed Booking Fee'), |
---|
[7927] | 678 | default = 0.0, |
---|
[7881] | 679 | required = False, |
---|
[7250] | 680 | ) |
---|
| 681 | |
---|
[9423] | 682 | maint_fee = schema.Float( |
---|
| 683 | title = _(u'Rent'), |
---|
| 684 | default = 0.0, |
---|
| 685 | required = False, |
---|
| 686 | ) |
---|
| 687 | |
---|
[10449] | 688 | transcript_fee = schema.Float( |
---|
| 689 | title = _(u'Transcript Fee'), |
---|
| 690 | default = 0.0, |
---|
| 691 | required = False, |
---|
| 692 | ) |
---|
| 693 | |
---|
[9814] | 694 | clearance_enabled = schema.Bool( |
---|
| 695 | title = _(u'Clearance enabled'), |
---|
| 696 | default = False, |
---|
| 697 | ) |
---|
| 698 | |
---|
[6918] | 699 | def getSessionString(): |
---|
| 700 | """Returns the session string from the vocabulary. |
---|
| 701 | """ |
---|
| 702 | |
---|
| 703 | |
---|
[6916] | 704 | class ISessionConfigurationAdd(ISessionConfiguration): |
---|
| 705 | """A session configuration object in add mode. |
---|
| 706 | """ |
---|
| 707 | |
---|
| 708 | academic_session = schema.Choice( |
---|
[7828] | 709 | title = _(u'Academic Session'), |
---|
[6916] | 710 | source = academic_sessions_vocab, |
---|
| 711 | default = None, |
---|
| 712 | required = True, |
---|
| 713 | readonly = False, |
---|
| 714 | ) |
---|
| 715 | |
---|
| 716 | ISessionConfigurationAdd['academic_session'].order = ISessionConfiguration[ |
---|
| 717 | 'academic_session'].order |
---|
| 718 | |
---|
[7819] | 719 | class IDataCenter(IKofaObject): |
---|
[4789] | 720 | """A data center. |
---|
| 721 | |
---|
[8394] | 722 | A data center manages files (uploads, downloads, etc.). |
---|
| 723 | |
---|
| 724 | Beside providing the bare paths needed to keep files, it also |
---|
| 725 | provides some helpers to put results of batch processing into |
---|
| 726 | well-defined final locations (with well-defined filenames). |
---|
| 727 | |
---|
| 728 | The main use-case is managing of site-related files, i.e. files |
---|
| 729 | for import, export etc. |
---|
| 730 | |
---|
| 731 | DataCenters are _not_ meant as storages for object-specific files |
---|
| 732 | like passport photographs and similar. |
---|
| 733 | |
---|
| 734 | It is up to the datacenter implementation how to organize data |
---|
| 735 | (paths) inside its storage path. |
---|
[4789] | 736 | """ |
---|
[8394] | 737 | storage = schema.Bytes( |
---|
| 738 | title = u'Path to directory where everything is kept.' |
---|
| 739 | ) |
---|
[4789] | 740 | |
---|
[8394] | 741 | deleted_path = schema.Bytes( |
---|
| 742 | title = u'Path were data about deleted objects should be stored.' |
---|
| 743 | ) |
---|
| 744 | |
---|
[9023] | 745 | def getPendingFiles(sort='name'): |
---|
[8394] | 746 | """Get a list of files stored in `storage` sorted by basename. |
---|
| 747 | """ |
---|
[9023] | 748 | |
---|
[9074] | 749 | def getFinishedFiles(): |
---|
| 750 | """Get a list of files stored in `finished` subfolder of `storage`. |
---|
[9023] | 751 | """ |
---|
| 752 | |
---|
[8394] | 753 | def setStoragePath(path, move=False, overwrite=False): |
---|
| 754 | """Set the path where to store files. |
---|
| 755 | |
---|
| 756 | If `move` is True, move over files from the current location |
---|
| 757 | to the new one. |
---|
| 758 | |
---|
| 759 | If `overwrite` is also True, overwrite any already existing |
---|
| 760 | files of same name in target location. |
---|
| 761 | |
---|
| 762 | Triggers a DataCenterStorageMovedEvent. |
---|
| 763 | """ |
---|
| 764 | |
---|
| 765 | def distProcessedFiles(successful, source_path, finished_file, |
---|
| 766 | pending_file, mode='create', move_orig=True): |
---|
| 767 | """Distribute processed files over final locations. |
---|
| 768 | """ |
---|
| 769 | |
---|
| 770 | |
---|
[4789] | 771 | class IDataCenterFile(Interface): |
---|
| 772 | """A data center file. |
---|
| 773 | """ |
---|
[4858] | 774 | |
---|
| 775 | name = schema.TextLine( |
---|
| 776 | title = u'Filename') |
---|
| 777 | |
---|
| 778 | size = schema.TextLine( |
---|
| 779 | title = u'Human readable file size') |
---|
| 780 | |
---|
| 781 | uploaddate = schema.TextLine( |
---|
| 782 | title = u'Human readable upload datetime') |
---|
| 783 | |
---|
| 784 | lines = schema.Int( |
---|
| 785 | title = u'Number of lines in file') |
---|
[6136] | 786 | |
---|
[4789] | 787 | def getDate(): |
---|
| 788 | """Get creation timestamp from file in human readable form. |
---|
| 789 | """ |
---|
| 790 | |
---|
| 791 | def getSize(): |
---|
| 792 | """Get human readable size of file. |
---|
| 793 | """ |
---|
[4858] | 794 | |
---|
| 795 | def getLinesNumber(): |
---|
| 796 | """Get number of lines of file. |
---|
| 797 | """ |
---|
[4882] | 798 | |
---|
| 799 | class IDataCenterStorageMovedEvent(IObjectEvent): |
---|
| 800 | """Emitted, when the storage of a datacenter changes. |
---|
| 801 | """ |
---|
[5007] | 802 | |
---|
[6136] | 803 | class IObjectUpgradeEvent(IObjectEvent): |
---|
| 804 | """Can be fired, when an object shall be upgraded. |
---|
| 805 | """ |
---|
| 806 | |
---|
[6180] | 807 | class ILocalRoleSetEvent(IObjectEvent): |
---|
| 808 | """A local role was granted/revoked for a principal on an object. |
---|
| 809 | """ |
---|
| 810 | role_id = Attribute( |
---|
| 811 | "The role id that was set.") |
---|
| 812 | principal_id = Attribute( |
---|
| 813 | "The principal id for which the role was granted/revoked.") |
---|
| 814 | granted = Attribute( |
---|
| 815 | "Boolean. If false, then the role was revoked.") |
---|
| 816 | |
---|
[5007] | 817 | class IQueryResultItem(Interface): |
---|
| 818 | """An item in a search result. |
---|
| 819 | """ |
---|
| 820 | url = schema.TextLine( |
---|
| 821 | title = u'URL that links to the found item') |
---|
| 822 | title = schema.TextLine( |
---|
| 823 | title = u'Title displayed in search results.') |
---|
| 824 | description = schema.Text( |
---|
| 825 | title = u'Longer description of the item found.') |
---|
[6136] | 826 | |
---|
[7819] | 827 | class IKofaPluggable(Interface): |
---|
| 828 | """A component that might be plugged into a Kofa Kofa app. |
---|
[5658] | 829 | |
---|
| 830 | Components implementing this interface are referred to as |
---|
| 831 | 'plugins'. They are normally called when a new |
---|
[7811] | 832 | :class:`waeup.kofa.app.University` instance is created. |
---|
[5658] | 833 | |
---|
| 834 | Plugins can setup and update parts of the central site without the |
---|
[7811] | 835 | site object (normally a :class:`waeup.kofa.app.University` object) |
---|
[5658] | 836 | needing to know about that parts. The site simply collects all |
---|
| 837 | available plugins, calls them and the plugins care for their |
---|
[5676] | 838 | respective subarea like the applicants area or the datacenter |
---|
[5658] | 839 | area. |
---|
| 840 | |
---|
| 841 | Currently we have no mechanism to define an order of plugins. A |
---|
| 842 | plugin should therefore make no assumptions about the state of the |
---|
| 843 | site or other plugins being run before and instead do appropriate |
---|
| 844 | checks if necessary. |
---|
| 845 | |
---|
| 846 | Updates can be triggered for instance by the respective form in |
---|
| 847 | the site configuration. You normally do updates when the |
---|
| 848 | underlying software changed. |
---|
[5013] | 849 | """ |
---|
[5069] | 850 | def setup(site, name, logger): |
---|
| 851 | """Create an instance of the plugin. |
---|
[5013] | 852 | |
---|
[5658] | 853 | The method is meant to be called by the central app (site) |
---|
| 854 | when it is created. |
---|
| 855 | |
---|
| 856 | `site`: |
---|
| 857 | The site that requests a setup. |
---|
| 858 | |
---|
| 859 | `name`: |
---|
| 860 | The name under which the plugin was registered (utility name). |
---|
| 861 | |
---|
| 862 | `logger`: |
---|
| 863 | A standard Python logger for the plugins use. |
---|
[5069] | 864 | """ |
---|
| 865 | |
---|
| 866 | def update(site, name, logger): |
---|
| 867 | """Method to update an already existing plugin. |
---|
| 868 | |
---|
| 869 | This might be called by a site when something serious |
---|
[5658] | 870 | changes. It is a poor-man replacement for Zope generations |
---|
| 871 | (but probably more comprehensive and better understandable). |
---|
| 872 | |
---|
| 873 | `site`: |
---|
| 874 | The site that requests an update. |
---|
| 875 | |
---|
| 876 | `name`: |
---|
| 877 | The name under which the plugin was registered (utility name). |
---|
| 878 | |
---|
| 879 | `logger`: |
---|
| 880 | A standard Python logger for the plugins use. |
---|
[5069] | 881 | """ |
---|
[5898] | 882 | |
---|
[5899] | 883 | class IAuthPluginUtility(Interface): |
---|
[5898] | 884 | """A component that cares for authentication setup at site creation. |
---|
| 885 | |
---|
| 886 | Utilities providing this interface are looked up when a Pluggable |
---|
| 887 | Authentication Utility (PAU) for any |
---|
[7811] | 888 | :class:`waeup.kofa.app.University` instance is created and put |
---|
[5898] | 889 | into ZODB. |
---|
| 890 | |
---|
| 891 | The setup-code then calls the `register` method of the utility and |
---|
| 892 | expects a modified (or unmodified) version of the PAU back. |
---|
| 893 | |
---|
| 894 | This allows to define any authentication setup modifications by |
---|
| 895 | submodules or third-party modules/packages. |
---|
| 896 | """ |
---|
| 897 | |
---|
| 898 | def register(pau): |
---|
| 899 | """Register any plugins wanted to be in the PAU. |
---|
| 900 | """ |
---|
| 901 | |
---|
| 902 | def unregister(pau): |
---|
| 903 | """Unregister any plugins not wanted to be in the PAU. |
---|
| 904 | """ |
---|
[6273] | 905 | |
---|
| 906 | class IObjectConverter(Interface): |
---|
| 907 | """Object converters are available as simple adapters, adapting |
---|
| 908 | interfaces (not regular instances). |
---|
| 909 | |
---|
| 910 | """ |
---|
| 911 | |
---|
[6277] | 912 | def fromStringDict(self, data_dict, context, form_fields=None): |
---|
| 913 | """Convert values in `data_dict`. |
---|
[6273] | 914 | |
---|
[6277] | 915 | Converts data in `data_dict` into real values based on |
---|
| 916 | `context` and `form_fields`. |
---|
[6273] | 917 | |
---|
[6277] | 918 | `data_dict` is a mapping (dict) from field names to values |
---|
| 919 | represented as strings. |
---|
[6273] | 920 | |
---|
[6277] | 921 | The fields (keys) to convert can be given in optional |
---|
| 922 | `form_fields`. If given, form_fields should be an instance of |
---|
| 923 | :class:`zope.formlib.form.Fields`. Suitable instances are for |
---|
| 924 | example created by :class:`grok.AutoFields`. |
---|
[6273] | 925 | |
---|
[6277] | 926 | If no `form_fields` are given, a default is computed from the |
---|
| 927 | associated interface. |
---|
[6273] | 928 | |
---|
[6277] | 929 | The `context` can be an existing object (implementing the |
---|
| 930 | associated interface) or a factory name. If it is a string, we |
---|
| 931 | try to create an object using |
---|
| 932 | :func:`zope.component.createObject`. |
---|
| 933 | |
---|
| 934 | Returns a tuple ``(<FIELD_ERRORS>, <INVARIANT_ERRORS>, |
---|
| 935 | <DATA_DICT>)`` where |
---|
| 936 | |
---|
| 937 | ``<FIELD_ERRORS>`` |
---|
| 938 | is a list of tuples ``(<FIELD_NAME>, <ERROR>)`` for each |
---|
| 939 | error that happened when validating the input data in |
---|
| 940 | `data_dict` |
---|
| 941 | |
---|
| 942 | ``<INVARIANT_ERRORS>`` |
---|
| 943 | is a list of invariant errors concerning several fields |
---|
| 944 | |
---|
| 945 | ``<DATA_DICT>`` |
---|
| 946 | is a dict with the values from input dict converted. |
---|
| 947 | |
---|
| 948 | If errors happen, i.e. the error lists are not empty, always |
---|
| 949 | an empty ``<DATA_DICT>`` is returned. |
---|
| 950 | |
---|
| 951 | If ``<DATA_DICT>` is non-empty, there were no errors. |
---|
[6273] | 952 | """ |
---|
[6293] | 953 | |
---|
[7932] | 954 | class IFieldConverter(Interface): |
---|
[8214] | 955 | def request_data(name, value, schema_field, prefix='', mode='create'): |
---|
[7932] | 956 | """Create a dict with key-value mapping as created by a request. |
---|
| 957 | |
---|
| 958 | `name` and `value` are expected to be parsed from CSV or a |
---|
| 959 | similar input and represent an attribute to be set to a |
---|
| 960 | representation of value. |
---|
| 961 | |
---|
[8214] | 962 | `mode` gives the mode of import. |
---|
| 963 | |
---|
[7932] | 964 | :meth:`update_request_data` is then requested to turn this |
---|
| 965 | name and value into vars as they would be sent by a regular |
---|
| 966 | form submit. This means we do not create the real values to be |
---|
| 967 | set but we only define the values that would be sent in a |
---|
| 968 | browser request to request the creation of those values. |
---|
| 969 | |
---|
| 970 | The returned dict should contain names and values of a faked |
---|
| 971 | browser request for the given `schema_field`. |
---|
| 972 | |
---|
| 973 | Field converters are normally registered as adapters to some |
---|
| 974 | specific zope.schema field. |
---|
| 975 | """ |
---|
| 976 | |
---|
[6338] | 977 | class IObjectHistory(Interface): |
---|
| 978 | |
---|
| 979 | messages = schema.List( |
---|
| 980 | title = u'List of messages stored', |
---|
| 981 | required = True, |
---|
| 982 | ) |
---|
| 983 | |
---|
| 984 | def addMessage(message): |
---|
| 985 | """Add a message. |
---|
| 986 | """ |
---|
[6353] | 987 | |
---|
[7819] | 988 | class IKofaWorkflowInfo(IWorkflowInfo): |
---|
[6353] | 989 | """A :class:`hurry.workflow.workflow.WorkflowInfo` with additional |
---|
| 990 | methods for convenience. |
---|
| 991 | """ |
---|
| 992 | def getManualTransitions(): |
---|
| 993 | """Get allowed manual transitions. |
---|
| 994 | |
---|
| 995 | Get a sorted list of tuples containing the `transition_id` and |
---|
| 996 | `title` of each allowed transition. |
---|
| 997 | """ |
---|
[6481] | 998 | |
---|
| 999 | class ISiteLoggers(Interface): |
---|
| 1000 | |
---|
[7819] | 1001 | loggers = Attribute("A list or generator of registered KofaLoggers") |
---|
[6481] | 1002 | |
---|
| 1003 | def register(name, filename=None, site=None, **options): |
---|
| 1004 | """Register a logger `name` which logs to `filename`. |
---|
| 1005 | |
---|
| 1006 | If `filename` is not given, logfile will be `name` with |
---|
| 1007 | ``.log`` as filename extension. |
---|
| 1008 | """ |
---|
| 1009 | |
---|
| 1010 | def unregister(name): |
---|
| 1011 | """Unregister a once registered logger. |
---|
| 1012 | """ |
---|
| 1013 | |
---|
| 1014 | class ILogger(Interface): |
---|
| 1015 | """A logger cares for setup, update and restarting of a Python logger. |
---|
| 1016 | """ |
---|
| 1017 | |
---|
| 1018 | logger = Attribute("""A :class:`logging.Logger` instance""") |
---|
| 1019 | |
---|
| 1020 | |
---|
| 1021 | def __init__(name, filename=None, site=None, **options): |
---|
[7819] | 1022 | """Create a Kofa logger instance. |
---|
[6481] | 1023 | """ |
---|
| 1024 | |
---|
| 1025 | def setup(): |
---|
| 1026 | """Create a Python :class:`logging.Logger` instance. |
---|
| 1027 | |
---|
| 1028 | The created logger is based on the params given by constructor. |
---|
| 1029 | """ |
---|
| 1030 | |
---|
| 1031 | def update(**options): |
---|
| 1032 | """Update the logger. |
---|
| 1033 | |
---|
| 1034 | Updates the logger respecting modified `options` and changed |
---|
| 1035 | paths. |
---|
| 1036 | """ |
---|
[6754] | 1037 | |
---|
| 1038 | class ILoggerCollector(Interface): |
---|
| 1039 | |
---|
| 1040 | def getLoggers(site): |
---|
| 1041 | """Return all loggers registered for `site`. |
---|
| 1042 | """ |
---|
| 1043 | |
---|
| 1044 | def registerLogger(site, logging_component): |
---|
| 1045 | """Register a logging component residing in `site`. |
---|
| 1046 | """ |
---|
| 1047 | |
---|
| 1048 | def unregisterLogger(site, logging_component): |
---|
| 1049 | """Unregister a logger. |
---|
| 1050 | """ |
---|
[7063] | 1051 | |
---|
| 1052 | # |
---|
| 1053 | # External File Storage and relatives |
---|
| 1054 | # |
---|
| 1055 | class IFileStoreNameChooser(INameChooser): |
---|
| 1056 | """See zope.container.interfaces.INameChooser for base methods. |
---|
| 1057 | """ |
---|
[7066] | 1058 | def checkName(name, attr=None): |
---|
[7063] | 1059 | """Check whether an object name is valid. |
---|
| 1060 | |
---|
| 1061 | Raises a user error if the name is not valid. |
---|
| 1062 | """ |
---|
| 1063 | |
---|
[7066] | 1064 | def chooseName(name, attr=None): |
---|
| 1065 | """Choose a unique valid file id for the object. |
---|
[7063] | 1066 | |
---|
[7066] | 1067 | The given name may be taken into account when choosing the |
---|
| 1068 | name (file id). |
---|
[7063] | 1069 | |
---|
[7066] | 1070 | chooseName is expected to always choose a valid file id (that |
---|
| 1071 | would pass the checkName test) and never raise an error. |
---|
| 1072 | |
---|
| 1073 | If `attr` is not ``None`` it might been taken into account as |
---|
| 1074 | well when generating the file id. Usual behaviour is to |
---|
| 1075 | interpret `attr` as a hint for what type of file for a given |
---|
| 1076 | context should be stored if there are several types |
---|
| 1077 | possible. For instance for a certain student some file could |
---|
| 1078 | be the connected passport photograph or some certificate scan |
---|
| 1079 | or whatever. Each of them has to be stored in a different |
---|
| 1080 | location so setting `attr` to a sensible value should give |
---|
| 1081 | different file ids returned. |
---|
[7063] | 1082 | """ |
---|
| 1083 | |
---|
| 1084 | class IExtFileStore(IFileRetrieval): |
---|
| 1085 | """A file storage that stores files in filesystem (not as blobs). |
---|
| 1086 | """ |
---|
| 1087 | root = schema.TextLine( |
---|
| 1088 | title = u'Root path of file store.', |
---|
| 1089 | ) |
---|
| 1090 | |
---|
| 1091 | def getFile(file_id): |
---|
| 1092 | """Get raw file data stored under file with `file_id`. |
---|
| 1093 | |
---|
| 1094 | Returns a file descriptor open for reading or ``None`` if the |
---|
| 1095 | file cannot be found. |
---|
| 1096 | """ |
---|
| 1097 | |
---|
[7071] | 1098 | def getFileByContext(context, attr=None): |
---|
[7063] | 1099 | """Get raw file data stored for the given context. |
---|
| 1100 | |
---|
| 1101 | Returns a file descriptor open for reading or ``None`` if no |
---|
| 1102 | such file can be found. |
---|
| 1103 | |
---|
[7071] | 1104 | Both, `context` and `attr` might be used to find (`context`) |
---|
| 1105 | and feed (`attr`) an appropriate file name chooser. |
---|
| 1106 | |
---|
[7063] | 1107 | This is a convenience method. |
---|
| 1108 | """ |
---|
| 1109 | |
---|
[7090] | 1110 | def deleteFile(file_id): |
---|
| 1111 | """Delete file stored under `file_id`. |
---|
| 1112 | |
---|
| 1113 | Remove file from filestore so, that it is not available |
---|
| 1114 | anymore on next call to getFile for the same file_id. |
---|
| 1115 | |
---|
| 1116 | Should not complain if no such file exists. |
---|
| 1117 | """ |
---|
| 1118 | |
---|
| 1119 | def deleteFileByContext(context, attr=None): |
---|
| 1120 | """Delete file for given `context` and `attr`. |
---|
| 1121 | |
---|
| 1122 | Both, `context` and `attr` might be used to find (`context`) |
---|
| 1123 | and feed (`attr`) an appropriate file name chooser. |
---|
| 1124 | |
---|
| 1125 | This is a convenience method. |
---|
| 1126 | """ |
---|
| 1127 | |
---|
[7063] | 1128 | def createFile(filename, f): |
---|
| 1129 | """Create file given by f with filename `filename` |
---|
| 1130 | |
---|
| 1131 | Returns a hurry.file.File-based object. |
---|
| 1132 | """ |
---|
| 1133 | |
---|
| 1134 | class IFileStoreHandler(Interface): |
---|
| 1135 | """Filestore handlers handle specific files for file stores. |
---|
| 1136 | |
---|
| 1137 | If a file to store/get provides a specific filename, a file store |
---|
| 1138 | looks up special handlers for that type of file. |
---|
| 1139 | |
---|
| 1140 | """ |
---|
| 1141 | def pathFromFileID(store, root, filename): |
---|
| 1142 | """Turn file id into path to store. |
---|
| 1143 | |
---|
| 1144 | Returned path should be absolute. |
---|
| 1145 | """ |
---|
| 1146 | |
---|
| 1147 | def createFile(store, root, filename, file_id, file): |
---|
| 1148 | """Return some hurry.file based on `store` and `file_id`. |
---|
| 1149 | |
---|
| 1150 | Some kind of callback method called by file stores to create |
---|
| 1151 | file objects from file_id. |
---|
| 1152 | |
---|
| 1153 | Returns a tuple ``(raw_file, path, file_like_obj)`` where the |
---|
[7819] | 1154 | ``file_like_obj`` should be a HurryFile, a KofaImageFile or |
---|
[7063] | 1155 | similar. ``raw_file`` is the (maybe changed) input file and |
---|
| 1156 | ``path`` the relative internal path to store the file at. |
---|
| 1157 | |
---|
| 1158 | Please make sure the ``raw_file`` is opened for reading and |
---|
| 1159 | the file descriptor set at position 0 when returned. |
---|
| 1160 | |
---|
| 1161 | This method also gets the raw input file object that is about |
---|
| 1162 | to be stored and is expected to raise any exceptions if some |
---|
| 1163 | kind of validation or similar fails. |
---|
| 1164 | """ |
---|
[7389] | 1165 | |
---|
| 1166 | class IPDF(Interface): |
---|
| 1167 | """A PDF representation of some context. |
---|
| 1168 | """ |
---|
| 1169 | |
---|
[8257] | 1170 | def __call__(view=None, note=None): |
---|
[7389] | 1171 | """Create a bytestream representing a PDF from context. |
---|
| 1172 | |
---|
| 1173 | If `view` is passed in additional infos might be rendered into |
---|
| 1174 | the document. |
---|
[8257] | 1175 | |
---|
| 1176 | `note` is optional HTML rendered at bottom of the created |
---|
| 1177 | PDF. Please consider the limited reportlab support for HTML, |
---|
| 1178 | but using font-tags and friends you certainly can get the |
---|
| 1179 | desired look. |
---|
[7389] | 1180 | """ |
---|
[7473] | 1181 | |
---|
| 1182 | class IMailService(Interface): |
---|
| 1183 | """A mail service. |
---|
| 1184 | """ |
---|
| 1185 | |
---|
| 1186 | def __call__(): |
---|
| 1187 | """Get the default mail delivery. |
---|
| 1188 | """ |
---|
[7576] | 1189 | |
---|
[9217] | 1190 | |
---|
[7576] | 1191 | class IDataCenterConfig(Interface): |
---|
| 1192 | path = Path( |
---|
| 1193 | title = u'Path', |
---|
[7828] | 1194 | description = u"Directory where the datacenter should store " |
---|
| 1195 | u"files by default (adjustable in web UI).", |
---|
[7576] | 1196 | required = True, |
---|
| 1197 | ) |
---|
[8346] | 1198 | |
---|
[9217] | 1199 | # |
---|
| 1200 | # Asynchronous job handling and related |
---|
| 1201 | # |
---|
| 1202 | class IJobManager(IKofaObject): |
---|
| 1203 | """A manager for asynchronous running jobs (tasks). |
---|
| 1204 | """ |
---|
| 1205 | def put(job, site=None): |
---|
| 1206 | """Put a job into task queue. |
---|
[8346] | 1207 | |
---|
[9217] | 1208 | If no `site` is given, queue job in context of current local |
---|
| 1209 | site. |
---|
| 1210 | |
---|
| 1211 | Returns a job_id to identify the put job. This job_id is |
---|
| 1212 | needed for further references to the job. |
---|
| 1213 | """ |
---|
| 1214 | |
---|
| 1215 | def jobs(site=None): |
---|
| 1216 | """Get an iterable of jobs stored. |
---|
| 1217 | """ |
---|
| 1218 | |
---|
| 1219 | def get(job_id, site=None): |
---|
| 1220 | """Get the job with id `job_id`. |
---|
| 1221 | |
---|
| 1222 | For the `site` parameter see :meth:`put`. |
---|
| 1223 | """ |
---|
| 1224 | |
---|
| 1225 | def remove(job_id, site=None): |
---|
| 1226 | """Remove job with `job_id` from stored jobs. |
---|
| 1227 | """ |
---|
| 1228 | |
---|
| 1229 | def start_test_job(site=None): |
---|
| 1230 | """Start a test job. |
---|
| 1231 | """ |
---|
| 1232 | |
---|
| 1233 | class IProgressable(Interface): |
---|
| 1234 | """A component that can indicate its progress status. |
---|
[8346] | 1235 | """ |
---|
[9217] | 1236 | percent = schema.Float( |
---|
| 1237 | title = u'Percent of job done already.', |
---|
[8346] | 1238 | ) |
---|
| 1239 | |
---|
[9217] | 1240 | class IJobContainer(IContainer): |
---|
| 1241 | """A job container contains IJob objects. |
---|
| 1242 | """ |
---|
| 1243 | |
---|
| 1244 | class IExportJob(zc.async.interfaces.IJob): |
---|
| 1245 | def __init__(site, exporter_name): |
---|
| 1246 | pass |
---|
| 1247 | |
---|
[9816] | 1248 | finished = schema.Bool( |
---|
| 1249 | title = u'`True` if the job finished.`', |
---|
| 1250 | default = False, |
---|
| 1251 | ) |
---|
| 1252 | |
---|
| 1253 | failed = schema.Bool( |
---|
| 1254 | title = u"`True` iff the job finished and didn't provide a file.", |
---|
| 1255 | default = None, |
---|
| 1256 | ) |
---|
| 1257 | |
---|
[9764] | 1258 | class IExportJobContainer(IKofaObject): |
---|
[9217] | 1259 | """A component that contains (maybe virtually) export jobs. |
---|
| 1260 | """ |
---|
[9718] | 1261 | def start_export_job(exporter_name, user_id, *args, **kwargs): |
---|
[9217] | 1262 | """Start asynchronous export job. |
---|
| 1263 | |
---|
| 1264 | `exporter_name` is the name of an exporter utility to be used. |
---|
| 1265 | |
---|
| 1266 | `user_id` is the ID of the user that triggers the export. |
---|
| 1267 | |
---|
[9718] | 1268 | `args` positional arguments passed to the export job created. |
---|
| 1269 | |
---|
| 1270 | `kwargs` keyword arguments passed to the export job. |
---|
| 1271 | |
---|
[9217] | 1272 | The job_id is stored along with exporter name and user id in a |
---|
| 1273 | persistent list. |
---|
| 1274 | |
---|
| 1275 | Returns the job ID of the job started. |
---|
| 1276 | """ |
---|
| 1277 | |
---|
| 1278 | def get_running_export_jobs(user_id=None): |
---|
| 1279 | """Get export jobs for user with `user_id` as list of tuples. |
---|
| 1280 | |
---|
| 1281 | Each tuples holds ``<job_id>, <exporter_name>, <user_id>`` in |
---|
| 1282 | that order. The ``<exporter_name>`` is the utility name of the |
---|
| 1283 | used exporter. |
---|
| 1284 | |
---|
| 1285 | If `user_id` is ``None``, all running jobs are returned. |
---|
| 1286 | """ |
---|
| 1287 | |
---|
| 1288 | def get_export_jobs_status(user_id=None): |
---|
| 1289 | """Get running/completed export jobs for `user_id` as list of tuples. |
---|
| 1290 | |
---|
| 1291 | Each tuple holds ``<raw status>, <status translated>, |
---|
| 1292 | <exporter title>`` in that order, where ``<status |
---|
| 1293 | translated>`` and ``<exporter title>`` are translated strings |
---|
| 1294 | representing the status of the job and the human readable |
---|
| 1295 | title of the exporter used. |
---|
| 1296 | """ |
---|
| 1297 | |
---|
| 1298 | def delete_export_entry(entry): |
---|
| 1299 | """Delete the export denoted by `entry`. |
---|
| 1300 | |
---|
| 1301 | Removes `entry` from the local `running_exports` list and also |
---|
| 1302 | removes the regarding job via the local job manager. |
---|
| 1303 | |
---|
| 1304 | `entry` is a tuple ``(<job id>, <exporter name>, <user id>)`` |
---|
| 1305 | as created by :meth:`start_export_job` or returned by |
---|
| 1306 | :meth:`get_running_export_jobs`. |
---|
| 1307 | """ |
---|
| 1308 | |
---|
| 1309 | def entry_from_job_id(job_id): |
---|
| 1310 | """Get entry tuple for `job_id`. |
---|
| 1311 | |
---|
| 1312 | Returns ``None`` if no such entry can be found. |
---|
| 1313 | """ |
---|
[9726] | 1314 | |
---|
| 1315 | class IExportContainerFinder(Interface): |
---|
| 1316 | """A finder for the central export container. |
---|
| 1317 | """ |
---|
| 1318 | def __call__(): |
---|
| 1319 | """Return the currently used global or site-wide IExportContainer. |
---|
| 1320 | """ |
---|
[9766] | 1321 | |
---|
| 1322 | class IFilteredQuery(IKofaObject): |
---|
| 1323 | """A query for objects. |
---|
| 1324 | """ |
---|
| 1325 | |
---|
| 1326 | defaults = schema.Dict( |
---|
| 1327 | title = u'Default Parameters', |
---|
| 1328 | required = True, |
---|
| 1329 | ) |
---|
| 1330 | |
---|
| 1331 | def __init__(**parameters): |
---|
| 1332 | """Instantiate a filtered query by passing in parameters. |
---|
| 1333 | """ |
---|
| 1334 | |
---|
| 1335 | def query(): |
---|
| 1336 | """Get an iterable of objects denoted by the set parameters. |
---|
| 1337 | |
---|
| 1338 | The search should be applied to objects inside current |
---|
| 1339 | site. It's the caller's duty to set the correct site before. |
---|
| 1340 | |
---|
| 1341 | Result can be any iterable like a catalog result set, a list, |
---|
| 1342 | or similar. |
---|
| 1343 | """ |
---|
| 1344 | |
---|
| 1345 | class IFilteredCatalogQuery(IFilteredQuery): |
---|
| 1346 | """A catalog-based query for objects. |
---|
| 1347 | """ |
---|
| 1348 | |
---|
| 1349 | cat_name = schema.TextLine( |
---|
| 1350 | title = u'Registered name of the catalog to search.', |
---|
| 1351 | required = True, |
---|
| 1352 | ) |
---|
| 1353 | |
---|
| 1354 | def query_catalog(catalog): |
---|
| 1355 | """Query catalog with the parameters passed to constructor. |
---|
| 1356 | """ |
---|