📜 ⬆️ ⬇️

Where is my button?

“Where is my button? Why is it inactive? ”How often can a programmer hear these words from users of his product? Most likely, more than very often in order to think, or maybe the product itself must answer this question of users.

This is approximately what a normal inactive button looks like, which, for whatever reason, is currently not available to the user:

Inactive silent button
It is silent, not interactive and does not provide the user with the slightest information about why she is in this state and under what conditions will become active. And here is what a “more dignified”, more informative inactive button might look like:

Inactive Improved Button
')
What is the salt, you ask? But how to describe the rules in the Python programming language, so that these rules simultaneously calculate the status of the button activity and provide a report on the cause in case the button should be deactivated, I would like to tell you in this article.

Using a custom-made library , you can describe the following rule in the Python programming language:

from rules import _and_, _or_ # The rule def can_user_add_comment(user, topic): return _and_( lambda: (user, “  ”), lambda: (user.is_status_active(), “   ”), lambda: _or_( lambda: (user.is_in_admin_group(), “  ”), lambda: _and_( lambda: (topic.is_status_open(), “ ”), lambda: (user.has_enough_karma_to_add_comment(), “      ”), lambda: (user.is_below_comments_per_minute_quota(), “   ,     ”), ), ), ) 

Classes of logic operations accept operands wrapped in lambda , which will be called to calculate values ​​if necessary. Those. calculations are performed according to a “shortened scheme”, for example, _and_ calculates the values ​​of its arguments up to the first False , _or_ - up to the first True . Each operand consists of the actual test value (the first element of the tuple) and a message (the second element of the tuple), which is used when the test value is False . Also, if necessary, you can omit messages for non-relevant parts of the rule (operands) from the point of view of informing the user.

Then you can use this rule, for example in the Mako template, as follows:

 <% user = get_authenticated_user() granted = can_user_add_comment(user, current_topic) %> <input type=”button” value=” ” title=”${” ” if granted else unicode(granted)}” ${“disabled” if not granted else “”} /> 

As you can see from the example, the granted object, obtained as a result of a call to the can_user_add_comment() function, in a boolean context reports the button's activity status, and in a string context, reports the reason if it returned False in a boolean context. The reason for the inactivity of a button is recorded in its title and is retrieved by the user when the mouse is moved over it. By default, only specific messages are included in the report, the result of which the rules are calculated is False . But it is also possible to generate a full tree report on which logical branches of the entire rule returned False . To do this, you must call granted.build_report() or granted.build_html_report() .

In the arsenal of the library, the operations _and_ , _or_ , _not_ and _true_ .

As a result of a web search, I could not find anything similar, so I had to write my own library. The first version was released quite recently. It will be very interesting for me to learn about your experience in solving this small, but sometimes quite relevant, task.

Source: https://habr.com/ru/post/106718/


All Articles