source: main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py @ 16159

Last change on this file since 16159 was 16059, checked in by Henrik Bettermann, 5 years ago

Improve referee reports.

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