Differences between revisions 1 and 2
Revision 1 as of 2018-08-02 05:15:22
Size: 3479
Editor: LanceEdgar
Comment: Initial content
Revision 2 as of 2020-02-09 03:43:02
Size: 3359
Editor: LanceEdgar
Comment: Remove some python2 cruft
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
from __future__ import unicode_literals, absolute_import
Line 54: Line 52:
from __future__ import unicode_literals, absolute_import

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

LilSnippets/AddMasterView (last edited 2020-02-09 03:43:02 by LanceEdgar)