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(), “ , ”), ), ), )
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. <% 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 “”} />
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()
._and_
, _or_
, _not_
and _true_
.Source: https://habr.com/ru/post/106718/
All Articles