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

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

pep8.

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