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