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

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

Do not compare ResultEntry? objects but their attribtes.

  • Property svn:keywords set to Id
File size: 3.6 KB
RevLine 
[7779]1## $Id: schoolgrades.py 12418 2015-01-08 10:27:06Z henrik $
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
30class ResultEntry(grok.Model):
[7785]31    """A result entry contains a subject and a grade.
[7779]32    """
33    grok.implements(IResultEntry)
34    subject = FieldProperty(IResultEntry['subject'])
35    grade = FieldProperty(IResultEntry['grade'])
[7785]36
[7921]37    def __init__(self, subject=None, grade=None):
38        super(ResultEntry, self).__init__()
39        if subject is not None:
40            self.subject = subject
41        if grade is not None:
42            self.grade = grade
43        return
44
[12418]45    def __eq__(self, obj):
46        default = object()
47        result = []
48        for name in ('subject', 'grade',):
49            result.append(
50                getattr(self, name) == getattr(obj, name, default))
51        return False not in result
52
[7921]53    def to_string(self):
[9055]54        """A string representation that can be used in exports.
[7921]55
56        Returned is a unicode string of format ``(u'<SUBJ>',u'<GRADE>')``.
57        """
58        return unicode((self.subject, self.grade))
59
60    @classmethod
61    def from_string(cls, string):
62        """Create new ResultEntry instance based on `string`.
63
64        The string is expected to be in format as delivered by
65        meth:`to_string`.
66
67        This is a classmethod. This means, you normally will call::
68
69          ResultEntry.from_string(mystring)
70
71        i.e. use the `ResultEntry` class, not an instance thereof.
72        """
73        string = string.replace("u''", "None")
74        subject, grade = eval(string)
75        return cls(subject, grade)
76
[7785]77class ResultEntryField(Object):
78    """A zope.schema-like field for usage in interfaces.
79
80    If you want to define an interface containing result entries, you
81    can do so like this::
82
83      class IMyInterface(Interface):
84          my_result_entry = ResultEntryField()
85
86    Default widgets are registered to render result entry fields.
87    """
88    grok.implements(IResultEntryField)
89
90    def __init__(self, **kw):
91        super(ResultEntryField, self).__init__(IResultEntry, **kw)
92        return
93
[7819]94# register KofaObjectWidgets as default widgets for IResultEntryFields
[7785]95@grok.adapter(IResultEntryField, IBrowserRequest)
96@grok.implementer(IInputWidget)
97def result_entry_input_widget(obj, req):
[7819]98    return KofaObjectWidget(obj, req, ResultEntry)
[7785]99
100# register a display widget for IResultEntryFields
101@grok.adapter(IResultEntryField, IBrowserRequest)
102@grok.implementer(IDisplayWidget)
103def result_entry_display_widget(obj, req):
[7819]104    return KofaObjectDisplayWidget(obj, req, ResultEntry)
Note: See TracBrowser for help on using the repository browser.