def __hidrate(self, raw_data):
        rules = []
        for element in raw_data:
            rule = Rule(str(element['_id']), element['userid'],
                        element['name'], element['rule_text'],
                        element['triggers'])
            rule.trigger_min_interval = self.default_val(
                element, 'trigger_min_interval', 0)
            rules.append(rule)

        return rules
Beispiel #2
0
    def from_request_data(self, data: dict) -> Rule:
        if 'name' not in data or 'text' not in data or 'active' not in data:
            raise Exception(
                'Request data must contain id, name, text and active keys')
        if 'id' not in data:
            id = str(time.time())
        else:
            id = data['id']
        rule = Rule(id, data['name'], data['text'], data['active'])
        rule.add_commands(self.__get_rule_commands(data))

        return rule
    def build_list(cls, rules: dict, formats: dict, sources: dict,
                   destinations: dict) -> List[Rule]:
        rules_list = []
        for rule in rules:
            rules_list.append(
                Rule(rule, cls._retrieve_format_list(rules[rule], formats),
                     cls._retrieve_source_list(rules[rule], sources),
                     cls._retrieve_destination_list(rules[rule],
                                                    destinations)))

        return rules_list
Beispiel #4
0
    def get_all(self) -> Dict[str, Rule]:
        rules_data = self.get(self.__REDIS_KEY)
        if not rules_data:
            return {}
        rules = {}
        for id, rule_data in rules_data.items():
            commands = []
            for data in rule_data['rule_commands']:
                email_text = None
                if 'email_text' in data:
                    email_text = data['email_text']
                commands.append(RuleCommand(data['actuator_id'], data['actuator_state'], data['voice_text'], email_text))
            if 'lock_after_activation' not in rule_data:
                rule_data['lock_after_activation'] = 60
            rule = Rule(rule_data['id'], rule_data['name'], rule_data['text'], rule_data['active'],
                        int(rule_data['lock_after_activation']))
            rule.add_commands(commands)
            rules[id] = rule

        return rules
 def post(self, user_id):
     data = json.loads(self.request.body.decode("utf-8"))
     rule = Rule(None, user_id, data['rule_name'], data['rule_text'],
                 data['triggers'])
     self.__rules_repo.create(rule)
     self.set_status(201)
 def __get_lock_key(self, rule: Rule, context: str):
     return self.RULE_LOCK_KEY.format(rule.get_id(), context)
 def create(self, rule: Rule) -> None:
     self.get_collection().insert_one(rule.to_mongo_doc())