Skip to content

Instantly share code, notes, and snippets.

@onjin
Created November 19, 2014 10:28
Show Gist options
  • Save onjin/2acc27efe96926d7d9e8 to your computer and use it in GitHub Desktop.
Save onjin/2acc27efe96926d7d9e8 to your computer and use it in GitHub Desktop.
python business_rules package example
# pip install business-rules
from decimal import Decimal
from pprint import pprint
from business_rules import (
variables, actions, fields, export_rule_data, run_all
)
class Request(object):
def __init__(self, contractor, cart):
self.contractor = contractor
self.cart = cart
class Contractor(object):
def __init__(self, symbol):
self.symbol = symbol
class Cart(object):
def __init__(self, total):
self.total = total
def get_total(self):
return self.total
class PaymentMethod(object):
def __init__(self, symbol, gateway_symbol, amount, is_active):
self.symbol = symbol
self.gateway_symbol = gateway_symbol
self.amount = amount
self.is_active = is_active
class PaymentMethodVariables(variables.BaseVariables):
def __init__(self, request, method):
self.method = method
self.request = request
@variables.string_rule_variable()
def current_contractor(self):
return getattr(self.request.contractor, 'symbol')
@variables.string_rule_variable()
def method_symbol(self):
return self.method.symbol
@variables.string_rule_variable()
def gateway_symbol(self):
return self.method.gateway.symbol
@variables.numeric_rule_variable()
def cart_total(self):
return self.request.cart.get_total()
class PaymentMethodActions(actions.BaseActions):
def __init__(self, request, method):
self.method = method
self.request = request
@actions.rule_action(params={
'active': fields.FIELD_SELECT_MULTIPLE
})
def change_active(self, active):
self.method.is_active = active
@actions.rule_action(params={
'amount': fields.FIELD_NUMERIC,
})
def set_amount(self, amount):
self.method.amount = amount
if __name__ == '__main__':
request = Request(Contractor('stefan'), Cart(Decimal('101')))
method = PaymentMethod('cash', 'general', Decimal('1.12'), True)
rules = export_rule_data(PaymentMethodVariables, PaymentMethodActions)
pprint(rules)
run_all(
rule_list=[
{
'conditions': {
'all': [
{
'name': 'current_contractor',
'operator': 'equal_to',
'value': 'stefan',
},
],
},
'actions': [
{
'name': 'change_active',
'params': {
'active': False,
},
},
]
},
{
'conditions': {
'all': [
{
'name': 'cart_total',
'operator': 'greater_than',
'value': Decimal('100'),
},
],
},
'actions': [
{
'name': 'change_active',
'params': {
'active': True,
},
},
{
'name': 'set_amount',
'params': {
'amount': Decimal('0'),
},
},
]
},
],
defined_variables=PaymentMethodVariables(request, method),
defined_actions=PaymentMethodActions(request, method),
stop_on_first_trigger=False
)
pprint(method.__dict__)
@onjin
Copy link
Author

onjin commented Aug 27, 2020

ou, I see now. The example in README has a syntax error, but I've found proper example in tests,

so try this one

        {
            "conditions": {
                "any": [
                    {
                        "name": "tipo_vinculacion",
                        "operator": "equal_to",
                        "value": "Asociado",
                    },
                    {
                        "all": [
                            {
                                "name": "numero_salarios_minimo",
                                "operator": "greater_than_or_equal_to",
                                "value": 1,
                            },
                            {
                                "name": "actividad_economica",
                                "operator": "equal_to",
                                "value": "Asalariado",
                            },
                        ],
                    },
                ],
            },
            "actions": [{"name": "aplica_o_no_aplica"}],
        },
    ]```

@esmejia277
Copy link

ou, I see now. The example in README has a syntax error, but I've found proper example in tests,

so try this one

        {
            "conditions": {
                "any": [
                    {
                        "name": "tipo_vinculacion",
                        "operator": "equal_to",
                        "value": "Asociado",
                    },
                    {
                        "all": [
                            {
                                "name": "numero_salarios_minimo",
                                "operator": "greater_than_or_equal_to",
                                "value": 1,
                            },
                            {
                                "name": "actividad_economica",
                                "operator": "equal_to",
                                "value": "Asalariado",
                            },
                        ],
                    },
                ],
            },
            "actions": [{"name": "aplica_o_no_aplica"}],
        },
    ]```

Ohh, finally it worked, thanks a lot!!

@onjin
Copy link
Author

onjin commented Aug 27, 2020

That's great :) you welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment