Persistence

Reahl stands on the shoulders of many other giant projects. To deal with persistent objects we built integration for the SqlAlchemy ORM.


Leave a comment

By [email protected]: testing

By [email protected]: testing again

class PersistenceExample(Widget):
    def __init__(self, view):
        super(PersistenceExample, self).__init__(view)

        self.add_child(CommentForm(view))
        for comment in Session.query(Comment).all():
            self.add_child(CommentBox(view, comment))

class Comment(Base):
    __tablename__ = 'features_comment'
    
    id            = Column(Integer, primary_key=True)
    email_address = Column(UnicodeText)
    text          = Column(UnicodeText)
    
    @exposed
    def fields(self, fields):
        fields.email_address = EmailField(label='Email address', required=True)
        fields.text          = Field(label='Comment', required=True)

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

    def submit(self):
        Session.add(self)

class CommentForm(Form):
    def __init__(self, view):
        super(CommentForm, self).__init__(view, 'myform')

        new_comment = Comment()
        grouped_inputs = FieldSet(view, legend_text='Leave a comment').use_layout(FormLayout())
        self.add_child(grouped_inputs)

        grouped_inputs.layout.add_input(TextInput(self, new_comment.fields.email_address))
        grouped_inputs.layout.add_input(TextInput(self, new_comment.fields.text))

        self.define_event_handler(new_comment.events.submit)
        grouped_inputs.add_child(ButtonInput(self, new_comment.events.submit))

class CommentBox(Div):
    def __init__(self, view, comment):
        super(CommentBox, self).__init__(view)
        comment_text = 'By %s: %s' % (comment.email_address, comment.text)
        self.add_child(P(view, text=comment_text))