source: main/waeup.kofa/trunk/src/waeup/kofa/schoolgrades.py @ 14181

Last change on this file since 14181 was 12426, checked in by uli, 10 years ago

Make tests work again.

  • Property svn:keywords set to Id
File size: 4.0 KB
RevLine 
[7779]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 school grades.
19"""
20import grok
[7785]21from zope.formlib.interfaces import IInputWidget, IDisplayWidget
22from zope.publisher.interfaces.browser import IBrowserRequest
[7779]23from zope.schema.fieldproperty import FieldProperty
[7785]24from zope.schema import Object
[7811]25from waeup.kofa.interfaces import IResultEntry, IResultEntryField
26from waeup.kofa.widgets.objectwidget import (
[7819]27    KofaObjectWidget, KofaObjectDisplayWidget
[7785]28    )
[7779]29
[12424]30
[12426]31#: A unique default value.
32DEFAULT_VALUE = object()
33
34
[7779]35class ResultEntry(grok.Model):
[7785]36    """A result entry contains a subject and a grade.
[7779]37    """
38    grok.implements(IResultEntry)
39    subject = FieldProperty(IResultEntry['subject'])
40    grade = FieldProperty(IResultEntry['grade'])
[7785]41
[7921]42    def __init__(self, subject=None, grade=None):
43        super(ResultEntry, self).__init__()
44        if subject is not None:
45            self.subject = subject
46        if grade is not None:
47            self.grade = grade
48        return
49
[12418]50    def __eq__(self, obj):
[12426]51        """Two ResultEntry objects are equal if their `subject` and
52           `grade` are equal.
53        """
[12418]54        for name in ('subject', 'grade',):
[12426]55            if getattr(self, name) != getattr(obj, name, DEFAULT_VALUE):
56                return False
57        return True
[12418]58
[12426]59    def __ne__(self, other):
60        """Two ResultEntries are not equal, if their equality test fails.
61
62        a != b <-> not(a == b). Python doc tell, that __ne__ should
63        also be rovided, whenever __eq__ is implemented.
64        """
65        return not self.__eq__(other)
66
[7921]67    def to_string(self):
[9055]68        """A string representation that can be used in exports.
[7921]69
70        Returned is a unicode string of format ``(u'<SUBJ>',u'<GRADE>')``.
71        """
72        return unicode((self.subject, self.grade))
73
74    @classmethod
75    def from_string(cls, string):
76        """Create new ResultEntry instance based on `string`.
77
78        The string is expected to be in format as delivered by
79        meth:`to_string`.
80
81        This is a classmethod. This means, you normally will call::
82
83          ResultEntry.from_string(mystring)
84
85        i.e. use the `ResultEntry` class, not an instance thereof.
86        """
87        string = string.replace("u''", "None")
88        subject, grade = eval(string)
89        return cls(subject, grade)
90
[12424]91
[7785]92class ResultEntryField(Object):
93    """A zope.schema-like field for usage in interfaces.
94
95    If you want to define an interface containing result entries, you
96    can do so like this::
97
98      class IMyInterface(Interface):
99          my_result_entry = ResultEntryField()
100
101    Default widgets are registered to render result entry fields.
102    """
103    grok.implements(IResultEntryField)
104
105    def __init__(self, **kw):
106        super(ResultEntryField, self).__init__(IResultEntry, **kw)
107        return
108
[12424]109
[7819]110# register KofaObjectWidgets as default widgets for IResultEntryFields
[7785]111@grok.adapter(IResultEntryField, IBrowserRequest)
112@grok.implementer(IInputWidget)
113def result_entry_input_widget(obj, req):
[7819]114    return KofaObjectWidget(obj, req, ResultEntry)
[7785]115
[12424]116
[7785]117# register a display widget for IResultEntryFields
118@grok.adapter(IResultEntryField, IBrowserRequest)
119@grok.implementer(IDisplayWidget)
120def result_entry_display_widget(obj, req):
[7819]121    return KofaObjectDisplayWidget(obj, req, ResultEntry)
Note: See TracBrowser for help on using the repository browser.