def post_bot(id=0): incoming = request.get_json() print(incoming) new_rules = incoming["rules"] bot = Bot.query.get(id) if bot: bot.name = incoming["bot"]["name"] bot.prefix = incoming["bot"]["prefix"] bot.user_id = incoming["bot"]["userId"] bot.description = incoming["bot"]["description"] bot.is_draft = incoming["bot"]["isDraft"] for old_rule in bot.rules: db.session.delete(old_rule) for new_rule in new_rules: new_rule_content = json.dumps(new_rule["content"]) db.session.add(Rule(content=new_rule_content, bot_id=bot.id)) else: bot = Bot(name=incoming["bot"]["name"], prefix=incoming["bot"]["prefix"], user_id=incoming["bot"]["userId"], description=incoming["bot"]["description"], is_draft=incoming["bot"]["isDraft"]) db.session.add(bot) db.session.commit() print(bot) for new_rule in new_rules: new_rule_content = json.dumps(new_rule["content"]) db.session.add(Rule(content=new_rule_content, bot_id=bot.id)) db.session.commit() return jsonify(True)
def test_apply_to_data(self): rule_1 = Rule(field='visitor_id', comparator="MATCH", value=self.visitor_data['visitor_id']) rule_2 = Rule(field='account_id', comparator="MATCH", value=self.visitor_data['account_id']) rule_3 = Rule(field='sessions', comparator="MATCH", value=self.visitor_data['sessions']) rule_4 = Rule(field='sessions_count', comparator="MATCH", value=self.visitor_data['sessions_count']) self.assertFalse(rule_1.apply_to_data(self.visitor_data)) self.assertFalse(rule_2.apply_to_data(self.visitor_data)) self.assertFalse(rule_3.apply_to_data(self.visitor_data)) self.assertTrue(rule_4.apply_to_data(self.visitor_data))
def create_rule(): request_dict = RuleSchema.validate_request() rule = Rule() new_rule = rule.create(request_dict, commit=False) action_ids = request.get_json().get('actions') if not isinstance(action_ids, list): raise FormInvalid(field='actions') actions = Action.query \ .filter(Action.id.in_(action_ids)) \ .many() for action in actions: new_rule.actions.append(action) rule_json = get_rule_json(new_rule) url = f"{current_app.config.get('STREAM_RULE_URL')}/" stream_rule_http('post', url=url, json=rule_json) db.session.commit() record = new_rule.to_dict() return jsonify(record), 201
def test_rule_crud(self): # Create rule = Rule(group_id=3, field="foo", comparator="MATCH", value="foo bar") db.session.add(rule) db.session.commit() self.assertIn(rule, Rule.query.all()) self.assertIsInstance(rule.group_id, int) self.assertIsInstance(rule.field, unicode) self.assertIsInstance(rule.comparator, unicode) self.assertIsInstance(rule.value, unicode) self.assertIsInstance(rule.created_at, DT.datetime) self.assertIsInstance(rule.updated_at, DT.datetime) # Read rule = Rule.query.filter_by(id=rule.id).first() self.assertEqual(rule.group_id, 3) self.assertEqual(rule.field, "foo") self.assertEqual(rule.comparator, "MATCH") self.assertEqual(rule.value, "foo bar") # Update old_created_at = rule.created_at old_updated_at = rule.updated_at rule.group_id = 5 rule.field = "bar" rule.comparator = "NOT_MATCH" rule.value = "bar foo" rule = Rule.query.filter_by(id=rule.id).first() self.assertIsInstance(rule, Rule) self.assertEqual(rule.group_id, 5) self.assertEqual(rule.field, "bar") self.assertEqual(rule.comparator, "NOT_MATCH") self.assertEqual(rule.value, "bar foo") self.assertEqual(rule.created_at, old_created_at) self.assertNotEqual(rule.updated_at, old_updated_at) # Delete db.session.delete(rule) count = Rule.query.filter_by(id=rule.id).count() self.assertEqual(0, count)
def create_rule(term_id): ''' Add a rule to the database ''' check_admin() term = Term.query.filter(Term.id == term_id).first_or_404() form = RuleForm() if form.validate_on_submit(): rule = Rule() rule.name = form.name.data rule.identifier = form.identifier.data rule.description = form.description.data rule.notes = form.notes.data term.rules.append(rule) db.session.add(rule) db.session.commit() flash('You have successfully added the %s rule.' % rule.name) # Redirect to term page return redirect(url_for('main.show_term', selected_term=term.id)) # Load term template return render_template('admin/rules/rule.html', action="Add", form=form)
def add_rule(): form = AddRuleForm() if form.validate_on_submit(): reference = form.reference.data category = form.category.data fixedPayment = form.fixedPayment.data rule = Rule(reference=reference, category=category, fixedPayment=(fixedPayment == "True")) db.session.add(rule) db.session.commit() return redirect(url_for('list_records')) return render_template('home/addrule.html', form=form)
def test_check_rules_for_duplicate(self): u = User.query.get(1) # This one should match the FLUR rule above r_test1 = Rule(id=2, lat=47.6492247, lng=-122.34912, time=datetime(2018, 4, 20, 5, 55), user_id=1) # This one should not match because the location does not match either rule r_test2 = Rule(id=3, lat=47.6492247, lng=-112.32724, time=datetime(2018, 4, 20, 5, 55), user_id=1) # This one should not match because the time doesn't match either rule r_test3 = Rule(id=4, lat=47.6492247, lng=-122.34912, time=datetime(2018, 4, 20, 7, 55), user_id=1) self.assertTrue(u.check_rules_for_duplicate(r_test1)) self.assertFalse(u.check_rules_for_duplicate(r_test2)) self.assertFalse(u.check_rules_for_duplicate(r_test3))
def post(self): ''' 添加 ''' args = parse_base.parse_args() name = args.get('name') url = args.get('url') menu_id = args.get('menu_id') method = args.get('method') # 权限规则名称可以重复,但是URL&请求方法不能重复 _data = Rule.query.filter_by(url=url, method=method, is_del='0').first() if _data: abort(RET.Forbidden, msg='权限规则已存在') model_data = Rule() model_data.name = name model_data.url = url model_data.menu_id = menu_id model_data.method = method model_data.last_editor = g.admin.username if model_data.add(): data = {'status': RET.Created, 'msg': '添加成功', 'data': model_data} return marshal(data, sing_fields) abort(RET.BadRequest, msg='添加失败,请重试')
def test_apply(self): rule_1 = Rule(field='sessions_count', comparator="MATCH", value=self.visitor_data['sessions_count']) rule_2 = Rule(field='foo', comparator="MATCH", value=self.visitor_data['sessions_count']) rule_3 = Rule(field='sessions_count', comparator="MATCH", value="Foo") self.assertTrue(rule_1.apply(self.visitor_data)) self.assertFalse(rule_2.apply(self.visitor_data)) self.assertFalse(rule_3.apply(self.visitor_data)) rule_1 = Rule(field='session_start', comparator="MATCH", value=self.session['session_start']) rule_2 = Rule(field='foo', comparator="MATCH", value=self.session['session_start']) rule_3 = Rule(field='session_start', comparator="MATCH", value="Foo") self.assertTrue(rule_1.apply(self.visitor_data)) self.assertFalse(rule_2.apply(self.visitor_data)) self.assertFalse(rule_3.apply(self.visitor_data)) rule_1 = Rule(field='page_url', comparator="MATCH", value=self.pageview['page_url']) rule_2 = Rule(field='foo', comparator="MATCH", value=self.pageview['page_url']) rule_3 = Rule(field='page_url', comparator="MATCH", value="Foo") self.assertTrue(rule_1.apply(self.visitor_data)) self.assertFalse(rule_2.apply(self.visitor_data)) self.assertFalse(rule_3.apply(self.visitor_data))
def test_compare(self): r_match = Rule(comparator="MATCH", value="FooBar") r_not_match = Rule(comparator="NOT_MATCH", value="FooBaz") r_contain = Rule(comparator="CONTAIN", value="Bar") r_not_contain = Rule(comparator="NOT_CONTAIN", value="Spam") r_start_with = Rule(comparator="START_WITH", value="Foo") r_not_start_with = Rule(comparator="NOT_START_WITH", value="Bar") r_end_with = Rule(comparator="END_WITH", value="Bar") r_not_end_with = Rule(comparator="NOT_END_WITH", value="Foo") r_less_than = Rule(comparator="LESS_THAN", value="10") r_greater_than = Rule(comparator="GREATER_THAN", value="10") self.assertTrue(r_match.compare("FooBar")) self.assertFalse(r_match.compare("BarFoo")) self.assertTrue(r_not_match.compare("FooBar")) self.assertFalse(r_not_match.compare("FooBaz")) self.assertTrue(r_contain.compare("FooBar")) self.assertFalse(r_contain.compare("FooBaz")) self.assertTrue(r_not_contain.compare("FooBar")) self.assertFalse(r_not_contain.compare("Foo Spam")) self.assertTrue(r_start_with.compare("FooBar")) self.assertFalse(r_start_with.compare("BarFoo")) self.assertTrue(r_not_start_with.compare("FooBar")) self.assertFalse(r_not_start_with.compare("BarFoo")) self.assertTrue(r_end_with.compare("FooBar")) self.assertFalse(r_end_with.compare("BarFoo")) self.assertTrue(r_not_end_with.compare("FooBar")) self.assertFalse(r_not_end_with.compare("BarFoo")) self.assertTrue(r_less_than.compare("20")) self.assertFalse(r_less_than.compare("5")) self.assertTrue(r_greater_than.compare("5")) self.assertTrue(r_greater_than.compare("5.0")) self.assertTrue(r_greater_than.compare(5.0)) self.assertFalse(r_greater_than.compare("20")) self.assertFalse(r_greater_than.compare("20.0")) self.assertFalse(r_greater_than.compare(20.0))
def test_apply_to_pageview(self): rule_1 = Rule(field='page_url', comparator="MATCH", value=self.pageview['page_url']) rule_2 = Rule(field='referrer_url', comparator="MATCH", value=self.pageview['referrer_url']) rule_3 = Rule(field='page_title', comparator="MATCH", value=self.pageview['page_title']) rule_4 = Rule(field='timestamp', comparator="MATCH", value=self.pageview['timestamp']) rule_5 = Rule(field='language', comparator="MATCH", value=self.pageview['language']) rule_6 = Rule(field='event', comparator="MATCH", value=self.pageview['event']) rule_7 = Rule(field='placeholders', comparator="MATCH", value=self.pageview['placeholders']) rule_8 = Rule(field='prefix', comparator="MATCH", value=self.pageview['prefix']) rule_9 = Rule(field='script_version', comparator="MATCH", value=self.pageview['script_version']) self.assertTrue(rule_1.apply_to_pageview(self.pageview)) self.assertTrue(rule_2.apply_to_pageview(self.pageview)) self.assertTrue(rule_3.apply_to_pageview(self.pageview)) self.assertTrue(rule_4.apply_to_pageview(self.pageview)) self.assertTrue(rule_5.apply_to_pageview(self.pageview)) self.assertFalse(rule_6.apply_to_pageview(self.pageview)) self.assertFalse(rule_7.apply_to_pageview(self.pageview)) self.assertFalse(rule_8.apply_to_pageview(self.pageview)) self.assertFalse(rule_9.apply_to_pageview(self.pageview))
def test_apply_to_session(self): rule_1 = Rule(field='session_start', comparator="MATCH", value=self.session['session_start']) rule_2 = Rule(field='session_end', comparator="MATCH", value=self.session['session_end']) rule_3 = Rule(field='session_duration', comparator="MATCH", value=self.session['session_duration']) rule_4 = Rule(field='request_address', comparator="MATCH", value=self.session['request_address']) rule_5 = Rule(field='user_agent', comparator="MATCH", value=self.session['user_agent']) rule_6 = Rule(field='screen_color', comparator="MATCH", value=self.session['screen_color']) rule_7 = Rule(field='screen_width', comparator="MATCH", value=self.session['screen_width']) rule_8 = Rule(field='screen_height', comparator="MATCH", value=self.session['screen_height']) rule_9 = Rule(field='pageview_count', comparator="MATCH", value=self.session['pageview_count']) rule_0 = Rule(field='pageviews', comparator="MATCH", value=self.session['pageviews']) self.assertTrue(rule_1.apply_to_session(self.session)) self.assertTrue(rule_2.apply_to_session(self.session)) self.assertTrue(rule_3.apply_to_session(self.session)) self.assertTrue(rule_4.apply_to_session(self.session)) self.assertTrue(rule_5.apply_to_session(self.session)) self.assertTrue(rule_6.apply_to_session(self.session)) self.assertTrue(rule_7.apply_to_session(self.session)) self.assertTrue(rule_8.apply_to_session(self.session)) self.assertTrue(rule_9.apply_to_session(self.session)) self.assertFalse(rule_0.apply_to_session(self.session))
name="fakebot6", prefix="%", description="The fifth fake bot ever here", is_draft=False) bot7 = Bot(user_id=1, name="fakebot7", prefix="%", description="The fifth fake bot ever here", is_draft=False) bot8 = Bot(user_id=1, name="fakebot8", prefix="%", description="The fifth fake bot ever here", is_draft=False) rule1 = Rule(bot_id=1, content=ruleString1) rule2 = Rule(bot_id=2, content=ruleString2) rule3 = Rule(bot_id=1, content=ruleString3) rule4 = Rule(bot_id=4, content=ruleString5) rule5 = Rule(bot_id=4, content=ruleString6) rule6 = Rule(bot_id=6, content=ruleString6) rule7 = Rule(bot_id=1, content=ruleString7) db.session.add(user1) db.session.add(user2) db.session.add(user3) db.session.add(user4) db.session.add(bot1) db.session.add(bot2) db.session.add(bot3) db.session.add(bot4)
db.session.commit() # Create Table and Columns l = Location(name='PROD_DB', description='/data/prod/risk_mart'); t = Table(name='ACCOUNT_HISTORY', location=l) c1 = Column(name='DATE_KEY', type='NUM', length='8', format='DATE9.', table=t) c2 = Column(name='ACCOUNT_ID', type='NUM', length='8', format='21.', table=t) c3 = Column(name='APPLICATION_ID', type='NUM', length='8', format='3.', table=t) c4 = Column(name='BALANCE_AMT', type='NUM', length='8', format='20.', table=t) db.session.add(l) db.session.add(t) db.session.add(c1) db.session.add(c2) db.session.add(c3) db.session.add(c4) # Create Business Rule t = Term.query.filter_by(id=1).first() r = Rule(identifier='BR_001', name='New Customer Rule', description='This is a new customer rule', notes='Detailed description of the rule. Typically written in structured English or pseudo code. Consider using a flowchart or UML activity diagram to depict procedural logic.') t.rules.append(r) db.session.add(r) print("Rule", r.identifier, "created") db.session.commit()