miraculixx / pyrules

Python Rules Engine
MIT License
22 stars 9 forks source link

Rules can be managed in Django admin #5

Closed miraculixx closed 9 years ago

miraculixx commented 9 years ago

This story will add Django models as a rule store.

Expected behavior

  1. Rules can be defined and modified in Django admin
  2. Rules are versioned (use django-reversion)
  3. Pyrules as such is kept agnostic from the basic model (i.e. pyrules can be used without Django, Django is just one storage option).

    Tasks

  4. implement the Django models
  5. add admin configuration
  6. implement unit tests
  7. Add a sample app (Admin + index page => enter a set of variables as the rule context, e.g. var=val [ ...], execute the rules and display the results).

    Implementation notes

    • we need the following models.
    • Rule - represents one specific rule coded in a python module, has attributes label, slug, description. A rule has a m2m relationship with Ruleset
    • Ruleset - a ruleset is a m2m collection of Rules into a named set. Attribute name
    • TableRuleset(Ruleset) - represents a pyrules.TableRuleset, coded in yaml or json, in attribute definition. yml or json format should be detected by the model (use a property, like is_yml or is_json).
    • note we do not currently define a TableRule for simplicity of the model. At a later point it maybe useful to have a specific set of django models for table rules (e.g. Variable, Condition, ActionTarget, Translation etc. -- not for now).

It shall be possible to get an actual Rule like so:

# basic
rule = models.Rule.objects.get(slug='my_rule').as_rule()
# great. this hints at a later possibility to implement other storage backends. for now Rulestore is just a wrapper for the django ORM.
store = Rulestore()
rule = store.get_rule(`my_rule`)
ruleset = store.get_ruleset('my_rules`)
antisvin commented 9 years ago

Some issues that should be discussed here:

  1. I suppose that TableRuleset should inherit from Rule rather than Ruleset (same as pyrules.rules classes)
  2. We probably need to add an intermediate model for Rule-Ruleset connection that has a field for specifying Rule ordering for a given Ruleset.
miraculixx commented 9 years ago

We probably need to add an intermediate model for Rule-Ruleset connection that has a field for specifying Rule ordering for a given Ruleset.

Again, good point. Indeed rule ordering should be by priority (higher priority => executes first, so sorted descending), as briefly mentioned at the end of #3 (for future). Having an intermediate model would allow to specify priorities per ruleset, which is a good thing.

miraculixx commented 9 years ago

I suppose that TableRuleset should inherit from Rule rather than Ruleset (same as pyrules.rules classes)

agree, as discussed.