1 LilSnippets-AddMasterView
Lance Edgar edited this page 2025-02-15 23:02:51 -06:00

Add a Master Web View for a Custom Table

So you have added a custom table to your database. Now you probably want to add the web view(s) for it!

This example will build off the Widget and Component examples from LilSnippets/AddTable.

Create Master View

The "master" view refers to a Python class which is endowed with basic CRUD features etc., and is (generally) tied to a table. For our example we will create a master view for the Widget table.

For this we will create a file at e.g. ~/src/poser/poser/web/views/widgets.py with contents:

from tailbone.views import MasterView

from poser.db import model


class WidgetsView(MasterView):
    """
    Widget master view
    """
    model_class = model.Widget
    
    grid_columns = [
        'description',
        'active',
    ]

    form_fields = [
        'description',
        'active',
    ]

    def configure_grid(self, g):
        super(WidgetsView, self).configure_grid(g)
        g.set_sort_defaults('description')
        g.set_link('description')


def includeme(config):
    WidgetsView.defaults(config)

(Optional) Add Rows to View

In our example, we have a secondary table (Components) which we would like to represent in the UI as "rows" under the (Widget) parent object. Or, perhaps better said: We would like to take advantage of that particular ("rows") pattern which is offered by the Tailbone package.

In any event, to do this we will configure the !WidgetsView class created in the last step, so as to add awareness of the Component "row" class. We will replace the code listed above, with some new code:

from tailbone.views import MasterView

from poser.db import model


class WidgetsView(MasterView):
    """
    Widget master view
    """
    model_class = model.Widget
    
    has_rows = True
    model_row_class = model.Component
    rows_creatable = True
    rows_editable = True
    rows_deletable = True

    grid_columns = [
        'description',
        'active',
    ]

    form_fields = [
        'description',
        'active',
    ]

    row_grid_columns = [
        'item_code',
        'description',
        'active',
    ]

    row_form_fields = [
        'item_code',
        'description',
        'active',
    ]

    def configure_grid(self, g):
        super(WidgetsView, self).configure_grid(g)
        g.set_sort_defaults('description')
        g.set_link('description')

    def save_create_row_form(self, form):
        widget = self.get_instance()
        component = self.objectify(form)
        widget.components.append(component)
        self.Session.flush()
        return component

    def redirect_after_create_row(self, component, mobile=False):
        return self.redirect(self.get_action_url('view', component.widget, mobile=mobile))

    def get_parent(self, component):
        return component.widget

    def get_row_data(self, widget):
        return self.Session.query(model.Component)\
                           .filter(model.Component.widget == widget)

    def row_grid_extra_class(self, component, i):
        if not component.active:
            return 'warning'


def includeme(config):
    WidgetsView.defaults(config)