fields() does not allow one to Keep Model Classes Pure

6 views
Skip to first unread message

Terrence Brannon

unread,
Apr 19, 2015, 4:19:10 AM4/19/15
to reahl-...@googlegroups.com
`fields()` is a special method in
which is used to mediate user input and domain objects.

The special method does not allow domain classes to be 100% pure.

In contrast the other Python-only web framework, Nagare, does not
modify the domain objects themselves. Instead they use a Proxy class
called an "editor" as shown in the Putting all together section

--
Terrence Brannon

Iwan Vosloo

unread,
Apr 21, 2015, 4:44:26 AM4/21/15
to reahl-...@googlegroups.com
Hi Terrence,
Firstly - it sounds like you use the word "pure" here to mean that you
do not want any code on the domain objects that refer to user interface
stuff. Is that right?

Nothing stops you from doing something similar with Reahl. For example:

Let's say you have a Person without our fields/events. Then you could
create a PersonEditor like this:

class PersonEditor(object):
def __init__(self, person):
self.person = person

@property
def fields(self):
delegated_fields = FieldIndex(self.person)
delegated_fields.name = Field(label='Name', required=True)
delegated_fields.age = IntegerField(label='Age', min_value=0,
required=True)
return delegated_fields

I like the idea of putting .fields() on the actual domain objects
though, because you could re-use the single definition of, say what
Person.fields.age is from different Facade objects (like the
PersonEditor example) without repeating yourself. Remember .fields() and
.events() do not provide a user interface. They're just hooks you can
build a user interface on top of.

Anyways, there are many possibilities. Another possibility is to put
.fields() on a Widget itself. Something like:

class PersonEditForm(Form):
def __init__(self, view, person):
super(PersonEditForm, self).__init__(view, 'edit_person')
self.person = person
self.name = person.name
self.age = person.age
self.add_child(LabelledBlockInput(TextInput(view, self.name)))
self.add_child(LabelledBlockInput(TextInput(view, self.age)))

def save(self):
self.person.name = self.name
self.person.age = self.age

@exposed
def fields(self, fields):
fields.name = Field(label='Name', required=True)
fields.age = IntegerField(label='Age', min_value=0, required=True)

@exposed
def events(self, events):
events.save = Event(label='Save', action=Action(self.save))



(You can also pull the delegated_fields trick here if you want to.)


There are so many possibilities. Exactly what you do is best decided on
the merits of your specific situation.

Regards
-Iwan

--
Reahl, the Python only web framework: http://www.reahl.org

Reply all
Reply to author
Forward
0 new messages