wiki:KeepInterfaceSimplePlugin

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

Continue to add documentation for version 2.0

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".

Note that the options are hidden, not removed. The user will still be able to select the option in most browsers by using keyboard shortcuts. Use a Warden rule to restrict the values accepted when a ticket is submitted, if that is what is needed.

Updating the contents of fields automatically

The 'update' attribute of a field defines a rule for automatically updating the field's content. Normally, it is re-evaluated whenever one of the fields used to determine the outcome of the rule is changed.

For example, take the rule:

priority.update = (effort > 5) ? 'high' : 'low'

This assumes that a custom field named 'effort' is defined. If the 'effort' field is changed to a value greater than 5, then the priority field is set to 'high'. Otherwise it is set to low.

Sometimes it's necessary to update a field only under certain conditions. In that case, the optional 'update.when' attribute can be used to define those conditions.

For example:

priority.update.when = milestone == 'Build 42'

Now the rule stated previously will be applied when the milestone is changed to 'Build 42', not when the 'effort' field is changed. The 'update.when' rule is re-evaluated whenever one of the fields used to determine the outcome of the rule is changed.

Using templates

The 'template' attribute on a field assigns a name to a block of template text that could be used to pre-populate the field. The 'available' attribute for that name then defines the condition under which the field will be populated with that text.

For example:

evaluation.template.change = '=== Description ===\\nDescribe the change fully...'
evaluation.available.change = evaluation_template == 'Change'
evaluation.template.fault = '=== Description ===\\nDescribe the fault fully...'
evaluation.available.fault = evaluation_template == 'Fault'
evaluation.template.none = ''
evaluation.available.none = evaluation_template == 'None'

This assumes that a custom field named 'evaluation_template' is defined (either a Select or a Radio field) with options 'None', 'Change' and 'Fault'. 'evaluation' is a Textarea field. When 'evaluation_template' is set to 'Change', the 'evaluation' field will be initialised with the value of the 'evaluation.template.change' option (shown here in a cut-down form; it would normally contain template entries for all the items of information that might be wanted in a Change evaluation). Similarly for 'evaluation_template' values of 'Fault' or 'None'.

A field is only initialised from a template if it is currently either empty or unchanged from one of the alternative template values. Template fields can be preferred over the use of automatically-updated fields because of this behaviour.

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 Trac link that points at the current ticket. It's designed to work with the ChildTicketsPlugin.

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')', for example, 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.

Expression syntax

In expressions, field names evaluate to the current value of the corresponding field, except for the special names status, which evaluates to the ticket status, authname, which evaluates to the current username, true which evaluates True and false, which evaluates False. If the field name is prefixed with an underscore, it evaluates to the value of the field at the time the page was loaded.

Text-type fields evaluate to their contents, checkboxes evaluate to true if checked or false if not, and Select or Radio fields evaluate to the selected item if an item is selected or undefined if no item is selected.

The full grammar of the expressions is:

                expression ::= or_expression
                             | or_expression "?" expression ":" expression
             or_expression ::= and_expression
                             | and_expression "||" or_expression
            and_expression ::= equality
                             | equality "&&" and_expression
                  equality ::= comparison
                             | comparison "==" | "!=" | "~=" equality
                comparison ::= sum
                             | sum "<" | ">" | "<=" | ">=" comparison
                       sum ::= product
                             | product "+" | "-" sum
                   product ::= negation
                             | negation "*" | "/" product
                  negation ::= func_term
                             | "-" | "!" negation
                 func_term ::= term
                             | term "in" cmp_list
                             | <function_name> "(" param_list ")"
                  cmp_list ::= "(" cmp_list ")"
                             | func_term
                             | func_term "," cmp_list
                param_list ::= *empty*
                             | term
                             | term "," param_list
                      term ::= "(" expression ")"
                             | <number>
                             | <field>
                             | "'" <string> "'"

~= is an operator that returns True only if the value on the left is matched by the regular expression on the right. in is an operator that returns True only if the value on the left appears in the list on the right. The operators !, ==, !=, || and && are negation, equality, inequality, OR and AND respectively.

Note that the && and || operators evaluate in the same way as the Javascript operators (or the Python and and or operators). So 'x && y' evaluates to 'x' if 'x' is false; 'y' if 'x' is true. 'x || y' evaluates to 'x' if 'x' is true; 'y' if 'x' is false.

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: