source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/interfaces.py @ 12115

Last change on this file since 12115 was 12089, checked in by Henrik Bettermann, 10 years ago

Implement application content components and rework workflows.

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