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:

   1 from __future__ import unicode_literals, absolute_import
   2 
   3 from tailbone.views import MasterView
   4 
   5 from poser.db import model
   6 
   7 
   8 class WidgetsView(MasterView):
   9     """
  10     Widget master view
  11     """
  12     model_class = model.Widget
  13     
  14     grid_columns = [
  15         'description',
  16         'active',
  17     ]
  18 
  19     form_fields = [
  20         'description',
  21         'active',
  22     ]
  23 
  24     def configure_grid(self, g):
  25         super(WidgetsView, self).configure_grid(g)
  26         g.set_sort_defaults('description')
  27         g.set_link('description')
  28 
  29 
  30 def includeme(config):
  31     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:

   1 from __future__ import unicode_literals, absolute_import
   2 
   3 from tailbone.views import MasterView
   4 
   5 from poser.db import model
   6 
   7 
   8 class WidgetsView(MasterView):
   9     """
  10     Widget master view
  11     """
  12     model_class = model.Widget
  13     
  14     has_rows = True
  15     model_row_class = model.Component
  16     rows_creatable = True
  17     rows_editable = True
  18     rows_deletable = True
  19 
  20     grid_columns = [
  21         'description',
  22         'active',
  23     ]
  24 
  25     form_fields = [
  26         'description',
  27         'active',
  28     ]
  29 
  30     row_grid_columns = [
  31         'item_code',
  32         'description',
  33         'active',
  34     ]
  35 
  36     row_form_fields = [
  37         'item_code',
  38         'description',
  39         'active',
  40     ]
  41 
  42     def configure_grid(self, g):
  43         super(WidgetsView, self).configure_grid(g)
  44         g.set_sort_defaults('description')
  45         g.set_link('description')
  46 
  47     def save_create_row_form(self, form):
  48         widget = self.get_instance()
  49         component = self.objectify(form)
  50         widget.components.append(component)
  51         self.Session.flush()
  52         return component
  53 
  54     def redirect_after_create_row(self, component, mobile=False):
  55         return self.redirect(self.get_action_url('view', component.widget, mobile=mobile))
  56 
  57     def get_parent(self, component):
  58         return component.widget
  59 
  60     def get_row_data(self, widget):
  61         return self.Session.query(model.Component)\
  62                            .filter(model.Component.widget == widget)
  63 
  64     def row_grid_extra_class(self, component, i):
  65         if not component.active:
  66             return 'warning'
  67 
  68 
  69 def includeme(config):
  70     WidgetsView.defaults(config)

LilSnippets/AddMasterView (last edited 2018-08-02 05:15:22 by LanceEdgar)