source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/refereeentries.py @ 17310

Last change on this file since 17310 was 14012, checked in by Henrik Bettermann, 8 years ago

Add email_sent attribute.

Add tests.

File size: 4.2 KB
Line 
1## $Id: schoolgrades.py 12426 2015-01-08 14:25:54Z uli $
2##
3## Copyright (C) 2012 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##
18"""Components representing and aggregating referee entries.
19"""
20import grok
21from zope.formlib.interfaces import IInputWidget, IDisplayWidget
22from zope.publisher.interfaces.browser import IBrowserRequest
23from zope.schema.fieldproperty import FieldProperty
24from zope.schema import Object
25from waeup.kofa.interfaces import IRefereeEntry, IRefereeEntryField
26from waeup.kofa.widgets.objectwidget import (
27    KofaObjectWidget, KofaObjectDisplayWidget
28    )
29
30
31#: A unique default value.
32DEFAULT_VALUE = object()
33
34
35class RefereeEntry(grok.Model):
36    """A referee entry contains a name and a email.
37    """
38    grok.implements(IRefereeEntry)
39    name = FieldProperty(IRefereeEntry['name'])
40    email = FieldProperty(IRefereeEntry['email'])
41
42    def __init__(self, name=None, email=None, email_sent=False):
43        super(RefereeEntry, self).__init__()
44        if name is not None:
45            self.name = name
46        if email is not None:
47            self.email = email
48        self.email_sent = email_sent
49        if not email_sent == True:
50            self.email_sent = False
51        return
52
53    def __eq__(self, obj):
54        """Two RefereeEntry objects are equal if their `name` and
55           `email` are equal.
56        """
57        for name in ('name', 'email',):
58            if getattr(self, name) != getattr(obj, name, DEFAULT_VALUE):
59                return False
60        return True
61
62    def __ne__(self, other):
63        """Two RefereeEntries are not equal, if their equality test fails.
64
65        a != b <-> not(a == b). Python doc tell, that __ne__ should
66        also be rovided, whenever __eq__ is implemented.
67        """
68        return not self.__eq__(other)
69
70    def to_string(self):
71        """A string representation that can be used in exports.
72
73        Returned is a unicode string of format
74        ``(u'<NAME>','<EMAIL>',<EMAILSENT>)``.
75        """
76        return unicode((self.name, self.email, self.email_sent))
77
78    @classmethod
79    def from_string(cls, string):
80        """Create new RefereeEntry instance based on `string`.
81
82        The string is expected to be in format as delivered by
83        meth:`to_string`.
84
85        This is a classmethod. This means, you normally will call::
86
87          RefereeEntry.from_string(mystring)
88
89        i.e. use the `RefereeEntry` class, not an instance thereof.
90        """
91        string = string.replace("u''", "None").replace("''", "None")
92        name, email, email_sent = eval(string)
93        return cls(name, email, email_sent)
94
95
96class RefereeEntryField(Object):
97    """A zope.schema-like field for usage in interfaces.
98
99    If you want to define an interface containing referee entries, you
100    can do so like this::
101
102      class IMyInterface(Interface):
103          my_referee_entry = RefereeEntryField()
104
105    Default widgets are registered to render referee entry fields.
106    """
107    grok.implements(IRefereeEntryField)
108
109    def __init__(self, **kw):
110        super(RefereeEntryField, self).__init__(IRefereeEntry, **kw)
111        return
112
113
114# register KofaObjectWidgets as default widgets for IRefereeEntryFields
115@grok.adapter(IRefereeEntryField, IBrowserRequest)
116@grok.implementer(IInputWidget)
117def referee_entry_input_widget(obj, req):
118    return KofaObjectWidget(obj, req, RefereeEntry)
119
120
121# register a display widget for IRefereeEntryFields
122@grok.adapter(IRefereeEntryField, IBrowserRequest)
123@grok.implementer(IDisplayWidget)
124def referee_entry_display_widget(obj, req):
125    return KofaObjectDisplayWidget(obj, req, RefereeEntry)
Note: See TracBrowser for help on using the repository browser.