Package: django-logical-rules
A tool to manage logical rules throughout your application. Logical rules are more powerful than permission or rule tables because they are written in python. Register a rule once and work with it throughout your app, from templates to generic view mixins. Instead of cluttering your models with rule-style and permission-style methods define those rules inrules.pyand then get easy access to them in your views and templates.InstallationUsepipto install from PyPI:pip install django-logical-rulesAddlogical_rulesto your settings.py file:INSTALLED_APPS = (
    ...
    'logical_rules',
    ...
)Additional RequirementsIf you want to use the messaging features installDjango messages framework.ConfigurationRules are defined inrules.pyfiles within your apps. Here’s an example of a rule:import logical_rules

def user_can_edit_mymodel(object, user):
        """
                Confirms a user can edit a specific model
                ...owners only!
        """
        return object.owner == user
logical_rules.site.register("user_can_edit_mymodel", user_can_edit_mymodel)To include your models in the registry you will need to do run the autodiscover, a bit like django.contrib.admin (I generally put this inurls.py):import logical_rules
logical_rules.autodiscover()UsageTemplate TagOnce you have created a rule, it’s easy to use anywhere in your templates:{% load logical_rules_tags %}
{% testrule user_can_edit_mymodel object request.user %}
        <p>You are the owner!</p>
{% endtestrule %}Note:Don’t use quotes around the rule name in the template.RulesMixinIf you are extending Django’s class-based generic views, you might find this mixin useful. It allows you to define rules that should be applied before rendering a view. Here’s an example usage:class MyView(RulesMixin, DetailView):

   def update_logical_rules(self):
      super(MyView, self).update_logical_rules()
      self.add_logical_rule({
         'name': 'user_can_edit_mymodel',
         'param_callbacks': [
            ('object', 'get_object'),
            ('user', 'get_request_user')
         ]
      })param_callbacksare our technique for getting the parameters for your rule. These are assumed to be methods on your class.get_request_user()is defined in RuleMixin since it’s so common.get_object()is a method on the DetailView class.Rule dictionaries can have other properties, likeredirect_urlandresponse_callback. Ifredirect_urlis defined, then the view will return anHttpResponseRedirectto that URL. Ifresponse_callbackis defined, then the view will return the result of that method.Messaging integration is possible withmessageandmessage_leveloptions.Finally, we’ve added two commonly used rules. As an optional substitute forlogin_required, we haveuser_is_authenticatedand to test a generic expression, we haveevaluate_expression.Direct Callingimport logical_rules
if logical_rules.site.test_rule(rule['name'], arg1, arg2):
        print "passed"
else:
        print "failed"ContributingThink this needs something else? To contribute todjango-logical-rulescreate a fork onBitbucket. Clone your fork, make some changes, and submit a pull request.Bugs are great contributions too! Feel free to add an issue onBitbucket: