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
class PersistenceExample(Widget):
    def __init__(self, view):
        super().__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)
    
    fields = ExposedNames()
    fields.email_address = lambda i: EmailField(label='Email address', required=True)
    fields.text          = lambda i: Field(label='Comment', required=True)

    events = ExposedNames()
    events.submit = lambda i: Event(label='Submit', action=Action(i.submit))

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

class CommentForm(Form):
    def __init__(self, view):
        super().__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().__init__(view)
        comment_text = 'By %s: %s' % (comment.email_address, comment.text)
        self.add_child(P(view, text=comment_text))