Skip to content

Commit

Permalink
Dynamic Tables: options to expose a dynamic component on a tab
Browse files Browse the repository at this point in the history
  • Loading branch information
nursix committed Feb 22, 2017
1 parent 5846262 commit 03ca8d4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nursix-dev-884-g31c5c23 (2017-02-21 10:12:43)
nursix-dev-900-g5846262 (2017-02-22 12:30:19)
7 changes: 6 additions & 1 deletion modules/s3/s3model.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ def add_dynamic_components(cls, tablename, exclude=None):
if mtable is None:
return

loaded = cls.get_config(tablename, "dynamic_components_loaded")
if loaded:
# Already loaded
return

join = ttable.on(ttable.id == ftable.table_id)
query = (ftable.master == tablename) & \
(ftable.component_key == True) & \
Expand All @@ -682,7 +687,7 @@ def add_dynamic_components(cls, tablename, exclude=None):
)

# Don't do this again during the same request cycle
cls.configure(tablename, dynamic_components=False)
cls.configure(tablename, dynamic_components_loaded=True)

components = {}
for row in rows:
Expand Down
67 changes: 67 additions & 0 deletions modules/s3/s3navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,9 @@ def render(self, r):

rheader_tabs = []

if r.resource.get_config("dynamic_components"):
self.dynamic_tabs(r.resource.tablename)

tabs = tuple(t for t in self.tabs if t.active(r))

mtab = False
Expand Down Expand Up @@ -1510,6 +1513,70 @@ def render(self, r):
rheader_tabs = ""
return rheader_tabs

# -------------------------------------------------------------------------
def dynamic_tabs(self, master):
"""
Add dynamic tabs
@param master: the name of the master table
"""

T = current.T
s3db = current.s3db

tabs = self.tabs
if not tabs:
return

ftable = s3db.s3_field
query = (ftable.component_key == True) & \
(ftable.component_tab == True) & \
(ftable.master == master) & \
(ftable.deleted == False)
rows = current.db(query).select(ftable.component_alias,
ftable.settings,
)
for row in rows:
alias = row.component_alias
if not alias:
continue

static_tab = False
for tab in tabs:
if tab.component == alias:
static_tab = True
break

if not static_tab:

label = None
position = None

settings = row.settings
if settings:

label = settings.get("tab_label")

position = settings.get("tab_position")
if position is not None:
try:
position = int(position)
except ValueError:
position = None
if position < 1 or position >= len(tabs):
position = None

if not label:
# Generate default label from component alias
label = T(" ".join(s.capitalize()
for s in alias.split("_")
))
tab = S3ComponentTab((label, alias))
if not position:
tabs.append(tab)
else:
tabs.insert(position, tab)

# =============================================================================
class S3ComponentTab(object):
""" Class representing a single Component Tab """
Expand Down
5 changes: 5 additions & 0 deletions modules/s3db/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def model(self):
label = T("Component Alias"),
requires = IS_EMPTY_OR((IS_LENGTH(128), IS_LOWER())),
),
Field("component_tab", "boolean",
label = T("Show on Tab"),
default = False,
represent = s3_yes_no_represent,
),
Field("settings", "json",
label = T("Settings"),
requires = IS_EMPTY_OR(IS_JSONS3()),
Expand Down
2 changes: 1 addition & 1 deletion static/formats/s3csv/s3/table.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Table,Field,Label,DataType,ComponentKey,ComponentAlias,Options,Unique,Required,DefaultValue,Settings,Comments
Table,Field,Label,DataType,ComponentKey,ComponentAlias,ComponentTab,Options,Unique,Required,DefaultValue,Settings,Comments
15 changes: 15 additions & 0 deletions static/formats/s3csv/s3/table.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DataType...........string..............the data type of the field
ComponentKey.......true|false..........use this field as component key
ComponentAlias.....string..............the alias for the component
ComponentTab.......true|false..........show the component on a tab
Options............JSON................the field options
Unique.............true|false..........field value must be unique
Required...........true|false..........field value must not be empty
Expand Down Expand Up @@ -96,6 +97,20 @@
</data>
</xsl:if>

<xsl:variable name="ComponentTab" select="col[@field='ComponentTab']/text()"/>
<data field="component_tab">
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="$ComponentTab='true'">
<xsl:value-of select="'true'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'false'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</data>

<xsl:variable name="Label" select="col[@field='Label']/text()"/>
<xsl:if test="$Label!=''">
<data field="label">
Expand Down

0 comments on commit 03ca8d4

Please sign in to comment.