Created
November 19, 2014 10:28
-
-
Save onjin/2acc27efe96926d7d9e8 to your computer and use it in GitHub Desktop.
python business_rules package example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ohh, finally it worked, thanks a lot!!