[7779] | 1 | ## $Id$ |
---|
| 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 | """ |
---|
| 20 | import grok |
---|
[7785] | 21 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
| 22 | from zope.publisher.interfaces.browser import IBrowserRequest |
---|
[7779] | 23 | from zope.schema.fieldproperty import FieldProperty |
---|
[7785] | 24 | from zope.schema import Object |
---|
[7811] | 25 | from waeup.kofa.interfaces import IResultEntry, IResultEntryField |
---|
| 26 | from waeup.kofa.widgets.objectwidget import ( |
---|
[7819] | 27 | KofaObjectWidget, KofaObjectDisplayWidget |
---|
[7785] | 28 | ) |
---|
[7779] | 29 | |
---|
| 30 | class 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 | |
---|
| 45 | def to_string(self): |
---|
| 46 | """A string representation that can be used in imports/export. |
---|
| 47 | |
---|
| 48 | Returned is a unicode string of format ``(u'<SUBJ>',u'<GRADE>')``. |
---|
| 49 | """ |
---|
| 50 | return unicode((self.subject, self.grade)) |
---|
| 51 | |
---|
| 52 | @classmethod |
---|
| 53 | def from_string(cls, string): |
---|
| 54 | """Create new ResultEntry instance based on `string`. |
---|
| 55 | |
---|
| 56 | The string is expected to be in format as delivered by |
---|
| 57 | meth:`to_string`. |
---|
| 58 | |
---|
| 59 | This is a classmethod. This means, you normally will call:: |
---|
| 60 | |
---|
| 61 | ResultEntry.from_string(mystring) |
---|
| 62 | |
---|
| 63 | i.e. use the `ResultEntry` class, not an instance thereof. |
---|
| 64 | """ |
---|
| 65 | string = string.replace("u''", "None") |
---|
| 66 | subject, grade = eval(string) |
---|
| 67 | return cls(subject, grade) |
---|
| 68 | |
---|
[7785] | 69 | class ResultEntryField(Object): |
---|
| 70 | """A zope.schema-like field for usage in interfaces. |
---|
| 71 | |
---|
| 72 | If you want to define an interface containing result entries, you |
---|
| 73 | can do so like this:: |
---|
| 74 | |
---|
| 75 | class IMyInterface(Interface): |
---|
| 76 | my_result_entry = ResultEntryField() |
---|
| 77 | |
---|
| 78 | Default widgets are registered to render result entry fields. |
---|
| 79 | """ |
---|
| 80 | grok.implements(IResultEntryField) |
---|
| 81 | |
---|
| 82 | def __init__(self, **kw): |
---|
| 83 | super(ResultEntryField, self).__init__(IResultEntry, **kw) |
---|
| 84 | return |
---|
| 85 | |
---|
[7819] | 86 | # register KofaObjectWidgets as default widgets for IResultEntryFields |
---|
[7785] | 87 | @grok.adapter(IResultEntryField, IBrowserRequest) |
---|
| 88 | @grok.implementer(IInputWidget) |
---|
| 89 | def result_entry_input_widget(obj, req): |
---|
[7819] | 90 | return KofaObjectWidget(obj, req, ResultEntry) |
---|
[7785] | 91 | |
---|
| 92 | # register a display widget for IResultEntryFields |
---|
| 93 | @grok.adapter(IResultEntryField, IBrowserRequest) |
---|
| 94 | @grok.implementer(IDisplayWidget) |
---|
| 95 | def result_entry_display_widget(obj, req): |
---|
[7819] | 96 | return KofaObjectDisplayWidget(obj, req, ResultEntry) |
---|