wiki:KeepInterfaceSimplePlugin

Version 4 (modified by Jon Ashley, 7 years ago) (diff)

Begin to document upcoming changes for version 2.0 of this plugin

Dynamically show and hide fields and options, and enforce user-defined rules on commits

Description

This plugin helps manage configurations that contain lots of fields or have lots of rules about what is and isn't permitted. It can dynamically control what fields appear in the user interface depending on easily-configurable conditions. It can also block commits if necessary, such as when certain information hasn't been provided.

There are two main components, named 'assistant' and 'warden'. The Assistant manages the user-interface side, hiding and showing fields and options, and filling in fields for the user. The Warden enforces the rules that prevent commits.

Hiding a field

The Assistant uses simple C-like expressions to define what fields are presented to the user in different circumstances. For example, let's assume that the tickets contain a fields named 'approval', that should be hidden if the ticket is in the 'new' or 'closed' state. Write a kis_assistant rule for approval.visible:

[kis_assistant]
approval.visible = !(status == 'new' || status == 'closed')

The expression syntax is similar to that of C or Javascript. An acceptable alternative would be:

[kis_assistant]
approval.visible = status != 'new' && status != 'closed'

Because lists of states are quite common however, the Assistant also provides an in operator, with very high precedence. So another acceptable alternative would be:

[kis_assistant]
approval.visible = !status in 'new', 'closed'

In expressions, 'authname' evaluates to the authenticated name of the user, and 'status' evaluates to the current ticket status.

Hiding options

Let's assume that we have a set of people on our project with the role of 'approver' (i.e. they are members of the Trac permissions group 'approver'). Let's also assume that the 'approval' field mentioned above is a Select or Radio field that has options 'Not assessed', 'Denied' and 'Approved'. The basic set of options 'Not assessed' or 'Denied' are available to all, but the full set of options including 'Approved' is only available if the user is a member of the 'approver' group or if the field already had the value 'Approved' when the page was loaded.

approval.options.basic_set = 'Not assessed', 'Denied'
approval.available.basic_set = true
approval.options.full_set = 'Approved'
approval.available.full_set = has_role('approver') || _approval == 'Approved'

The condition for the basic set of options being available is therefore just 'true'. The full set of options is available if the function 'has_role' returned 'true' when called with the parameter 'approver', or if the value of 'approval' when the page was loaded was already set to 'Approved'. The underscore in front of 'approval' means "use the value of this field at the time the page was loaded".

Using templates

TBD

Updating the contents of fields automatically

TBD

Blocking commits

The Warden prevents commits from being made if certain conditions aren't met. For example, take the rules:

[kis_warden]
approval required to close = status == 'closed' && approval != 'Approved'
only designated approver can approve = !has_role('approver') && approval != _approval && approval == 'Approved'

The commit is blocked if any rule evaluates true. Therefore, the first rule means that the ticket cannot be closed if the 'approval' field has not been set to the value 'Approved'. The second rule means that only a user who is a member of the permissions group 'approver' can change the 'approval' field to that value.

Functions

The 'has_role(<group>)' function is built-in, and returns true if and only if the user is a member of the named group. (A second parameter can be given, and allows a different user to be named, but this isn't normally needed.)

Another built-in function is 'is_parent()', which returns true if and only if some other ticket has a field named 'parent' which contains a TracLink that points at the current ticket. It's designed to work with the TracChildTickets plugin.

User-defined functions

User functions can be defined by adding a Python file to the Trac plugins folder that implements the IConfigFunction interface. For example:

from trac.core import *
from kis import IConfigFunction

class MyConfigFunctions(Component):
    ''' Local functions for use by 'kisplugin' configuration files.
    '''
    implements(IConfigFunction)

    # Example: implement named string constants
    def safety(self, req, safety_enum):
        if safety_enum == 'YES':
            return 'Safety related'
        if safety_enum == 'OK':
            return 'Safety related - OK to close'
        if safety_enum == 'NO':
            return 'Not safety related'

This example would define a function 'safety()', implementing named constants. 'safety('OK')' returns the string 'Safety related - OK to close'. The 'req' parameter is the HTTP request object; the remaining parameters are the parameters of the function call passed in from the configuration file.

What's new in version 2

  • Function calls and user-defined functions.
  • Automatic updating of fields.
  • The operator 'has_role' has been retired and replaced with the 'has_role()' function.
  • Labels and templates now have to be string expressions (in other words, they now need to be surrounded by single-quotes).
  • New operators: arithmetic and comparison operators, and the ternary operator <expression> '?' <true_expression> ':' <false_expression>
  • Changes to operator precedence: now very similar to that of Javascript.

Bugs/Feature Requests

Existing bugs and feature requests for KeepInterfaceSimplePlugin are here.

If you have any issues, create a new ticket.

defect

10 / 10

enhancement

1 / 4

task

2 / 2

Download

Download the zipped source from here.

Source

You can check out KeepInterfaceSimplePlugin from here using Subversion, or browse the source with Trac.

Installation

General instructions on installing Trac plugins can be found on the TracPlugins page.

Recent Changes

17316 by ash on 2018-12-10 22:23:18
Version 2.5
17315 by ash on 2018-12-10 09:06:07
Correctly retrieve "previous" value of a field for a new ticket (i.e. blank)
17314 by ash on 2018-12-10 08:47:29
Fix problem in how Warden rules look up the current value of a field when it _isn't_ changing. Related to the changes made for #13494 and found in testing.
re #13494
(more)

Author/Contributors

Author: ash
Maintainer: Jon Ashley
Contributors: