def get(self, request, *args, **kwargs): rule_id = request.GET.get('rule_id', None) groups = {} # 参数不全 if rule_id is None: return self.render_json_response({'state': False, "rules_num": 0}) rules = Rules(load_all=True).id_rule_map if rule_id in (u"", u"所有"): # 所有规则 return self.render_json_response({ 'state': True, 'strategy_groups': groups, "rules_num": len(rules.keys()) }) rule_obj = rules.get(rule_id) if not rule_obj: return self.render_json_response({'state': False, "rules_num": 0}) group_list = rule_obj.strategy_group_list for strategy_group in group_list: group_name = strategy_group[3] groups[rule_id] = group_name break return self.render_json_response({ 'state': True, 'strategy_groups': groups, "rules_num": len(rules.keys()) })
def before_render(self, request): self.rules = Rules(load_all=True)
def _get_all_rule_id_and_names(self): rule_id_and_names = Rules(load_all=True).get_all_rule_id_and_name() rule_id_and_names.insert(0, ('', u'所有规则')) return rule_id_and_names
def _get_all_strategy_groups(self): strategy_names = Rules(load_all=True).get_all_group_uuid_and_name() strategy_names.insert(0, ('', u'所有策略原子组')) return strategy_names
def _get_all_rule_id_and_names(): rule_id_and_names = Rules(load_all=True).get_all_rule_id_and_name() rule_id_and_names.insert(0, ('', _('All_Rules'))) return rule_id_and_names
def _get_all_strategy_groups(): strategy_names = Rules(load_all=True).get_all_group_uuid_and_name() strategy_names.insert(0, ('', _('All_Policy_Group'))) return strategy_names
def _build_rule_choices(cls): return Rules().get_all_rule_uuid_and_name()
#!/usr/bin/env python3 # coding: utf-8 from django.utils.translation import gettext_lazy as _ from aswan import settings from server.base import Response from risk_models.exceptions import RuleNotExistsException from risk_models.rule import calculate_rule, Rules, AccessCount rules = Rules(auto_refresh=True) ac = AccessCount(auto_persist=True) def query_handler(req_body): rule_id = req_body.get('rule_id') result, ec, error = None, 0, None try: if settings.DEBUG: assert rule_id rule_id = str(rule_id) control, weight = calculate_rule(rule_id, req_body, rules=rules, ac=ac) result = {'control': control, 'weight': weight} except AssertionError: error = _('must contain rule_id') ec = 100 except RuleNotExistsException: error = _('rule_id does not exist or is offline') ec = 101
def __init__(self, *args, **kwargs): self.rules = Rules(load_all=True)