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

Last change on this file since 18180 was 18166, checked in by Henrik Bettermann, 15 months ago

Implement student password expiration feature.

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