Esempio n. 1
0
 def test_add_rule(self):
     rule = Rule(start_sort='100000', end_sort='100001', mod_rule='MOD10',
                 weight=[0,1,2,3,4,5,6,7,8,9,10,11,12,13], mod_exception='')
     rule.save()
     record = Rule.objects.get(pk=rule.id)
     assert record == rule
     assert '/rules/detail/' in str(rule.get_absolute_url())
Esempio n. 2
0
 def test_summary(self):
     now = datetime.now()
     rule = Rule()
     rule.populate({
         'policy': 'block',
         'environment': 'prod',
         'surt': 'https://(org,',
         'neg_surt': 'https://(org,archive,api,)',
         'capture_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'retrieve_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'ip_range': {
             'start': '4.4.4.4',
             'end': '8.8.8.8',
         },
         'seconds_since_capture': 256,
         'collection': 'Planets',
         'partner': 'Holst',
         'warc_match': 'jupiter',
         'rewrite_from': 'zeus',
         'rewrite_to': 'jupiter',
         'public_comment': 'initial creation',
         'private_comment': 'going roman',
         'enabled': True,
     })
     self.assertEqual(
         rule.summary(),
         {
             'id':
             None,  # The rule wasn't saved, so this won't be populated.
             'policy': 'block',
             'environment': 'prod',
             'surt': 'https://(org,',
             'neg_surt': 'https://(org,archive,api,)',
             'capture_date': {
                 'start': now.isoformat(),
                 'end': now.isoformat(),
             },
             'retrieve_date': {
                 'start': now.isoformat(),
                 'end': now.isoformat(),
             },
             'ip_range': {
                 'start': '4.4.4.4',
                 'end': '8.8.8.8',
             },
             'seconds_since_capture': 256,
             'collection': 'Planets',
             'partner': 'Holst',
             'warc_match': 'jupiter',
             'rewrite_from': 'zeus',
             'rewrite_to': 'jupiter',
             'public_comment': 'initial creation',
             'enabled': True,
         })
Esempio n. 3
0
 def setUp(self):
     self.rule = Rule()
     now = datetime.now(timezone.utc)
     self.rule.populate({
         'policy': 'block',
         'environment': 'prod',
         'surt': 'https://(org,archive,',
         'capture_date': {
             'start': now.isoformat(),
             'end': (now + timedelta(days=365)).isoformat(),
         },
         'retrieve_date': {
             'start': now.isoformat(),
             'end': (now + timedelta(days=365)).isoformat(),
         },
         'seconds_since_capture': 256,
         'collection': 'Planets',
         'partner': 'Holst',
         'warc_match': 'jupiter',
         'rewrite_from': 'zeus',
         'rewrite_to': 'jupiter',
         'public_comment': 'initial creation',
         'private_comment': 'going roman',
         'enabled': True,
     })
     self.rule.save()
     self.client = Client()
Esempio n. 4
0
 def on_rule_trigger(self):
     # 设置规则模式 步骤 2 输入 触发语句
     # status: rule_trigger
     # next_status: rule_response
     new_rule = Rule(trigger_text=self.message, added_by=self.user)
     new_rule.save()
     self.user.status = 'rule_response'
     self.user.save()
     return "请输入响应语句"
Esempio n. 5
0
 def setUp(self):
     x = lambda y: TRUE if y%2 else None
     r = []
     for i in range(7):
         weight = -i if i%3 else i
         rule = Rule(weight=weight, trigger='hello')
         rule.save()
         rule.conditions = x(i)
         r.append(rule)
     self.rules = r
Esempio n. 6
0
 def test_save_adds_rule_change(self):
     now = datetime.now(timezone.utc)
     rule = Rule()
     rule.populate({
         'policy': 'block',
         'environment': 'prod',
         'surt': 'https://(org,',
         'capture_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'retrieve_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'seconds_since_capture': 256,
         'collection': 'Planets',
         'partner': 'Holst',
         'warc_match': 'jupiter',
         'rewrite_from': 'zeus',
         'rewrite_to': 'jupiter',
         'public_comment': 'initial creation',
         'private_comment': 'going roman',
         'enabled': True,
     })
     rule.save()
     last_change = RuleChange.objects.all().order_by('-pk')[0]
     self.assertEqual(last_change.change_type, 'c')
     rule.policy = 'allow'
     rule.save()
     last_change = RuleChange.objects.all().order_by('-pk')[0]
     self.assertEqual(last_change.change_type, 'u')
     self.assertEqual(last_change.policy, 'block')
Esempio n. 7
0
 def test_populate(self):
     now = datetime.now()
     base = Rule()
     base.populate({
         'policy': 'block',
         'environment': 'prod',
         'surt': 'https://(org,',
         'neg_surt': 'https://(org,archive,',
         'protocol': 'http',
         'subdomain': '',
         'capture_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'retrieve_date': {
             'start': now.isoformat(),
             'end': now.isoformat(),
         },
         'ip_range': {
             'start': '4.4.4.4',
             'end': '8.8.8.8',
         },
         'seconds_since_capture': 256,
         'collection': 'Planets',
         'partner': 'Holst',
         'warc_match': 'jupiter',
         'rewrite_from': 'zeus',
         'rewrite_to': 'jupiter',
         'public_comment': 'initial creation',
         'private_comment': 'going roman',
         'enabled': True,
     })
     self.assertEqual(base.policy, 'block')
     self.assertEqual(base.environment, 'prod')
     self.assertEqual(base.surt, 'https://(org,')
     self.assertEqual(base.neg_surt, 'https://(org,archive,')
     self.assertEqual(base.protocol, 'http')
     self.assertEqual(base.subdomain, '')
     self.assertEqual(base.capture_date_start, now)
     self.assertEqual(base.capture_date_end, now)
     self.assertEqual(base.retrieve_date_start, now)
     self.assertEqual(base.retrieve_date_end, now)
     self.assertEqual(base.ip_range_start, '4.4.4.4')
     self.assertEqual(base.ip_range_end, '8.8.8.8')
     self.assertEqual(base.seconds_since_capture, 256)
     self.assertEqual(base.collection, 'Planets')
     self.assertEqual(base.partner, 'Holst')
     self.assertEqual(base.warc_match, 'jupiter')
     self.assertEqual(base.rewrite_from, 'zeus')
     self.assertEqual(base.rewrite_to, 'jupiter')
     self.assertEqual(base.public_comment, 'initial creation')
     self.assertEqual(base.private_comment, 'going roman')
     self.assertEqual(base.enabled, True)
Esempio n. 8
0
    def test_rule(self):
        self.assertEqual(Rule.get_by_id(1).rev, 0)
        self.assertEqual(Rule.get_by_id(1).reference, "http://www.exemple.com")
        self.assertEqual(Rule.get_by_id(1).rule_full, """test""")
        self.assertTrue(Rule.get_by_id(1).enabled)

        all_rule = Rule.get_all()
        rule = Rule.get_by_id(1)
        self.assertEqual(len(all_rule), 1)
        self.assertEqual(rule.reference, "http://www.exemple.com")
        rule = Rule.get_by_id(99)
        self.assertEqual(rule, None)
        with self.assertRaises(AttributeError):
            rule.reference
        with self.assertLogs('rules.models', level='DEBUG'):
            Rule.get_by_id(99)

        rules = Rule.find("tes")
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].reference, "http://www.exemple.com")
Esempio n. 9
0
 def handle(self, *args, **kwargs):
     # Require the user to go through a step before fuzzing the database.
     if os.environ.get(self.UNDERSTOOD) != 'understood':
         self.stderr.write(
             'This command adds fuzzed data to the database, no matter '
             'the environment!')
         self.stderr.write(
             'Please run this command with the environment variable ' +
             self.UNDERSTOOD + ' set to the value `understood`.')
         sys.exit(1)
     # Generate the requested number of unique SURTs.
     surts = []
     while len(surts) < kwargs['records'][0]:
         surt = self.build_surt()
         if surt not in surts:
             surts.append(surt)
             self.stdout.write('{}: {}'.format(len(surts), surt))
     # Generate and save a rule for each SURT.
     for surt in surts:
         rule = Rule(surt=surt)
         self.add_constraints(rule)
         rule.save()
Esempio n. 10
0
 def test_populate_no_dates(self):
     base = Rule()
     base.populate({
         'policy': 'block',
         'environment': 'prod',
         'surt': 'https://(org,',
         'neg_surt': 'https://(org,archive,',
         'protocol': 'http',
         'subdomain': '',
         'seconds_since_capture': 256,
         'collection': 'Planets',
         'partner': 'Holst',
         'warc_match': 'jupiter',
         'rewrite_from': 'zeus',
         'rewrite_to': 'jupiter',
         'public_comment': 'initial creation',
         'private_comment': 'going roman',
         'enabled': True,
     })
     self.assertEqual(base.capture_date_start, None)
     self.assertEqual(base.capture_date_end, None)
     self.assertEqual(base.retrieve_date_start, None)
     self.assertEqual(base.retrieve_date_end, None)
Esempio n. 11
0
class ViewsTestCase(TestCase):

    def setUp(self):
        self.rule = Rule()
        now = datetime.now(timezone.utc)
        self.rule.populate({
            'policy': 'block',
            'environment': 'prod',
            'surt': 'https://(org,archive,',
            'capture_date': {
                'start': now.isoformat(),
                'end': (now + timedelta(days=365)).isoformat(),
            },
            'retrieve_date': {
                'start': now.isoformat(),
                'end': (now + timedelta(days=365)).isoformat(),
            },
            'seconds_since_capture': 256,
            'collection': 'Planets',
            'partner': 'Holst',
            'warc_match': 'jupiter',
            'rewrite_from': 'zeus',
            'rewrite_to': 'jupiter',
            'public_comment': 'initial creation',
            'private_comment': 'going roman',
            'enabled': True,
        })
        self.rule.save()
        self.client = Client()

    def test_rules_get_surt_exact(self):
        response = self.client.get(
            '/rules', {'surt-exact': 'https://(org,archive,'})
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        self.assertEqual(len(parsed['result']), 1)

    def test_rules_get_surt_starts_with(self):
        response = self.client.get(
            '/rules', {'surt-start': 'https://(org,archive,'})
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        for result in parsed['result']:
            self.assertEqual(
                result['surt'].startswith('https://(org,archive,'), True)

    def test_rules_get_all(self):
        response = self.client.get('/rules')
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        self.assertEqual(len(parsed['result']), Rule.objects.count())

    def test_rules_new_success(self):
        response = self.client.post(
            '/rules',
            content_type='application/json',
            data=json.dumps({
                'surt': 'http://(doctor,who,)/companions/rose',
                'enabled': False,
                'environment': 'prod',
                'policy': 'allow',
            }))
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        new_rule = Rule.objects.get(
            surt='http://(doctor,who,)/companions/rose')
        self.assertEqual(new_rule.policy, 'allow')

    def test_rules_new_fail_json(self):
        response = self.client.post('/rules', {'bad': 'wolf'})
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(parsed['message'], 'unable to marshal json')

    def test_rules_new_fail_validation(self):
        response = self.client.post(
            '/rules',
            content_type='application/json',
            data=json.dumps({'bad': 'wolf'}))
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(parsed['message'], 'error validating json')

    def test_rule_get_success(self):
        response = self.client.get('/rule/1')
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        self.assertEqual(parsed['result']['id'], 1)

    def test_rule_update_success(self):
        response1 = self.client.get('/rule/1')
        self.assertEqual(response1.status_code, 200)
        parsed = json.loads(response1.content.decode('utf-8'))
        payload = dict(parsed['result'])
        payload['public_comment'] = 'UPDATE'
        response2 = self.client.put(
            '/rule/1',
            content_type='application/json',
            data=json.dumps(payload))
        self.assertEqual(response2.status_code, 200)
        update = json.loads(response2.content.decode('utf-8'))
        self.assertEqual(update['result']['rule']['public_comment'], 'UPDATE')
        self.assertEqual(
            update['result']['change']['rule'],
            parsed['result'])

    def test_rule_update_fail_json(self):
        response = self.client.put('/rule/1', {'bad': 'wolf'})
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(parsed['message'], 'unable to marshal json')

    def test_rule_update_fail_validation(self):
        response = self.client.put(
            '/rule/1',
            content_type='application/json',
            data=json.dumps({'bad': 'wolf'}))
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(parsed['message'], 'error validating json')

    def test_rule_delete(self):
        response = self.client.delete('/rule/1')
        self.assertEqual(response.status_code, 200)
        response = self.client.get('/rule/1')
        self.assertEqual(response.status_code, 404)

    def test_tree_for_surt(self):
        response = self.client.get('/rules/tree/https://(org,archive,')
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        self.assertEqual(len(parsed['result']) >= 1, True)

    def test_rules_for_request_success(self):
        response = self.client.get(
            '/rules/for-request',
            {
                'surt': 'https://(org,archive,',
                'capture-date': datetime.now(timezone.utc),
            })
        self.assertEqual(response.status_code, 200)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'success')
        self.assertEqual(parsed['message'], 'ok')
        self.assertEqual(len(parsed['result']) >= 1, True)

    def test_rules_for_request_missing_params(self):
        response = self.client.get(
            '/rules/for-request',
            {
                'capture-date': datetime.now(timezone.utc),
            })
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(
            parsed['message'], 'surt and capture-date query string params'
            ' are both required')

    def test_rules_for_request_malformed_date(self):
        response = self.client.get(
            '/rules/for-request',
            {
                'surt': 'https://(org,archive,',
                'capture-date': 'bad-wolf',
            })
        self.assertEqual(response.status_code, 400)
        parsed = json.loads(response.content.decode('utf-8'))
        self.assertEqual(parsed['status'], 'error')
        self.assertEqual(
            parsed['message'], 'capture-date query string param must be '
            'a datetime')
Esempio n. 12
0
     rule = Rule.objects.get(rule_id=r['rule_id'])
     print "  Updating Rule: " + r['nls_rule_id']
     rule.scope=r['scope']
     rule.language_dependancy=r['language_dependency']
     rule.cache_dependency=r['cache_dependency']
     rule.resource_properties=resource_properties
     rule.validation=r['validate']
     rule.wcag_primary = WCAG20_SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.rule_category = RuleCategory.objects.get(category_num=r['rule_category'])
     rule.updated_date=r['last_updated']
     rule.updated_editor=u
   
   except ObjectDoesNotExist:  
     print "  Creating Rule: " + r['nls_rule_id']
     resource_properties = ",".join(r['resource_properties'])
     rule = Rule(rule_id=r['rule_id'],scope=r['scope'],language_dependancy=r['language_dependency'],cache_dependency=r['cache_dependency'],resource_properties=resource_properties,validation=r['validate'],updated_date=r['last_updated'],updated_editor=u)
     rule.wcag_primary = WCAG20_SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.rule_category = RuleCategory.objects.get(category_num=r['rule_category'])
     rule.save()

   rule.wcag_related.clear() 
   for related in r['wcag_related']:
      rule.wcag_related.add(WCAG20_SuccessCriterion.get_by_wcag_number(related)) 

   rule.target_resources.clear()  
   for m in r['target_resources']:
     try: 
       rule.target_resources.add(ElementDefinition.get_by_title(m))
     except:
       print "  target resources exception for element definition: " + m  
Esempio n. 13
0
     rule.scope=scope
     rule.category = RuleCategory.objects.get(rule_category_code=r['rule_category'])
     rule.group=group
     rule.language_dependancy=r['language_dependency']
     rule.primary_property=r['primary_property']
     rule.resource_properties=resource_properties
     rule.validation=r['validate']
     rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.updated_date=r['last_updated']
     
     NodeResultMessage.objects.filter(rule=rule).delete()  
     
   except ObjectDoesNotExist:  
     print("  Creating Rule: " + r['nls_rule_id'])
     resource_properties = ",".join(r['resource_properties'])
     rule = Rule(rule_id=r['rule_id'],scope=scope,group=group,language_dependancy=r['language_dependency'],primary_property=r['primary_property'],resource_properties=resource_properties,validation=r['validate'],updated_date=r['last_updated'])
     rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.category = RuleCategory.objects.get(rule_category_code=r['rule_category'])
     
   rule.slug = r['rule_id'].lower().replace('_', '')
   rule.save()

   rule.wcag_related.clear();  
   for related in r['wcag_related']:
      rule.wcag_related.add(SuccessCriterion.get_by_wcag_number(related)) 

   rule.target_resources.clear();  
   for m in r['target_resources']:
     try: 
       rule.target_resources.add(ElementDefinition.get_by_title(m))
     except:
Esempio n. 14
0
        rule.primary_property = r["primary_property"]
        rule.resource_properties = resource_properties
        rule.validation = r["validate"]
        rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r["wcag_primary"])
        rule.updated_date = r["last_updated"]

        NodeResultMessage.objects.filter(rule=rule).delete()

    except ObjectDoesNotExist:
        print("  Creating Rule: " + r["nls_rule_id"])
        resource_properties = ",".join(r["resource_properties"])
        rule = Rule(
            rule_id=r["rule_id"],
            scope=scope,
            group=group,
            language_dependancy=r["language_dependency"],
            primary_property=r["primary_property"],
            resource_properties=resource_properties,
            validation=r["validate"],
            updated_date=r["last_updated"],
        )
        rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r["wcag_primary"])
        rule.category = RuleCategory.objects.get(rule_category_code=r["rule_category"])

    rule.slug = r["rule_id"].lower().replace("_", "")
    rule.save()

    rule.wcag_related.clear()
    for related in r["wcag_related"]:
        rule.wcag_related.add(SuccessCriterion.get_by_wcag_number(related))

    rule.target_resources.clear()
Esempio n. 15
0
def admin_rules(request, model_admin):
    if request.method == 'POST':
        # get information that was submitted
        part1 = request.POST['part1']
        boolpart = request.POST['boolpart']
        part2 = request.POST['part2']

        # build up the boolean part of the rule
        boolstring = ""
        if part1:
            boolstring += "ans"+part1
            if boolpart != "None":
                if part2:
                    boolstring += " "+boolpart+" ans"+part2

        # results part
        rec_result = request.POST['result1']
        ans_result = request.POST['result2']
        red_result = request.POST['result3']
        resultstring = ""
        
        if rec_result:
            rec_result="rec"+rec_result
        if ans_result:
            ans_result="ans"+ans_result
        if red_result:
            red_result="red"+red_result

        temp_result_list = [rec_result, ans_result, red_result]
        for r in temp_result_list:
                if len(r) > 2:
                    resultstring = resultstring+r+" "
        
        if (len(boolstring) > 1) and (len(resultstring) > 2):
            rule = Rule()
            rule.rule = boolstring+" : "+resultstring
            rule.save()

        #return render_to_response('admin/rules/rule/rules.html', {}, context_instance=RequestContext(request))

    opts = model_admin.model._meta
    admin_site = model_admin.admin_site
    has_perm = request.user.has_perm(opts.app_label+'.'+opts.get_change_permission())
    
    # To show rules already in system
    rule_list = []
    for ru in Rule.objects.all():
        rule = "rule id:"+str(ru.id)+" "
        result_str = ""
        bool_rule = re.compile(":").split(ru.rule)[0]
        result_part = re.compile(":").split(ru.rule)[1]
        tokens = bool_rule.split(' ')
        result_tokens = result_part.split(" ")
        for t in tokens:
            if "ans" in t:
                temp_ans = get_or_none(Answer, pk=(int(float(t[3:]))))
                if temp_ans != None:
                    rule += str(temp_ans)
            if t == "and" or t == "or":
                rule += " <strong>"+t+"</strong> "
        
        result_str += "<br /><strong>triggers the following:</strong><ul> "
        for rt in result_tokens:
            if "rec" in rt:
                temp_rec = get_or_none(Recommendation, pk=(int(float(rt[3:]))))
                if temp_rec != None:
                    result_str += "<li>A recommendation: "+str(temp_rec)+"</li>"
            
            if "ans" in rt:
                temp_ans_result = get_or_none(Answer, pk=(int(float(rt[3:]))))
                if temp_ans_result != None:
                    result_str += "<li>A question being implicitly answered: "+str(temp_ans_result)+"</li>"
            
            if "red" in rt:
                temp_red = get_or_none(Question, pk=(int(float(rt[3:]))))
                if temp_red != None:
                    result_str += "<li>A question being made redundant: "+str(temp_red)+"</li>"


        rule_list.append(rule+result_str+"</ul>")
    
    # To display answers to choose from
    answer_list = []
    for a in Answer.objects.all():
        answer_list.append(a)
    answer_list.append("None")
    
    # To display recommendations to choose from
    rec_list = []
    for r in Recommendation.objects.all():
        rec_list.append(r)
    rec_list.append("None")
    
    # To display questions to choose from (to make redundant)
    question_list = []
    for q in Question.objects.all():
        question_list.append(q)
    question_list.append("None")

    # To choose bool operator to choose from
    bool_list = ["and", "or", "None"]
    
    context = {'admin_site': admin_site.name,
                'title': "Create Custom Rules",
                'opts': opts,
                'root_path': '/%s' % admin_site.root_path,
                'app_label': opts.app_label,
                'has_change_permission': has_perm,
                'question_list': question_list,
                'rec_list': rec_list,
                'rule_list': rule_list,
                'answer_list': answer_list,
                'bool_list': bool_list
                }
    template = 'admin/rules/rule/rules.html'
    return render_to_response(template, context, context_instance=RequestContext(request))
Esempio n. 16
0
class RuleChangeTestCase(TestCase):
    def setUp(self):
        self.now = datetime.now(timezone.utc)
        self.rule = Rule()
        self.rule_data = {
            'policy': 'block',
            'environment': 'prod',
            'surt': 'https://(org,',
            'capture_date': {
                'start': self.now.isoformat(),
                'end': self.now.isoformat(),
            },
            'retrieve_date': {
                'start': self.now.isoformat(),
                'end': self.now.isoformat(),
            },
            'seconds_since_capture': 256,
            'collection': 'Planets',
            'partner': 'Holst',
            'warc_match': 'jupiter',
            'rewrite_from': 'zeus',
            'rewrite_to': 'jupiter',
            'public_comment': 'initial creation',
            'private_comment': 'going roman',
            'enabled': True,
        }
        self.rule.populate(self.rule_data)
        self.rule.save()

    def test_summary(self):
        change = RuleChange(rule=self.rule,
                            change_date=self.now,
                            change_user='******',
                            change_comment='composed',
                            change_type='c')
        self.assertEqual(
            change.change_summary(), {
                'id': None,
                'rule_id': self.rule.id,
                'date': self.now,
                'user': '******',
                'comment': 'composed',
                'type': 'created',
            })

    def test_full_change(self):
        self.maxDiff = None
        change = RuleChange(rule=self.rule,
                            change_date=self.now,
                            change_user='******',
                            change_comment='composed',
                            change_type='c')
        change.populate({
            'policy': 'block',
            'environment': 'prod',
            'surt': 'https://(org,',
            'capture_date': {
                'start': self.now.isoformat(),
                'end': self.now.isoformat(),
            },
            'retrieve_date': {
                'start': self.now.isoformat(),
                'end': self.now.isoformat(),
            },
            'seconds_since_capture': 256,
            'collection': 'Planets',
            'partner': 'Holst',
            'warc_match': 'jupiter',
            'rewrite_from': 'zeus',
            'rewrite_to': 'jupiter',
            'public_comment': 'initial creation',
            'private_comment': 'going roman',
            'enabled': True,
        })
        self.assertEqual(
            change.full_change(include_private=True), {
                'id': None,
                'rule_id': self.rule.id,
                'date': self.now,
                'user': '******',
                'comment': 'composed',
                'type': 'created',
                'rule': {
                    'id': 1,
                    'policy': 'block',
                    'environment': 'prod',
                    'surt': 'https://(org,',
                    'capture_date': {
                        'start': self.now.isoformat(),
                        'end': self.now.isoformat(),
                    },
                    'retrieve_date': {
                        'start': self.now.isoformat(),
                        'end': self.now.isoformat(),
                    },
                    'seconds_since_capture': 256,
                    'collection': 'Planets',
                    'partner': 'Holst',
                    'warc_match': 'jupiter',
                    'rewrite_from': 'zeus',
                    'rewrite_to': 'jupiter',
                    'public_comment': 'initial creation',
                    'private_comment': 'going roman',
                    'enabled': True,
                },
            })
Esempio n. 17
0
 def test_str(self):
     rule = Rule(policy='block', surt='https://(org,')
     self.assertEqual(str(rule), 'BLOCK PLAYBACK (https://(org,)')
Esempio n. 18
0
     rule.scope=scope
     rule.category = RuleCategory.objects.get(rule_category_code=r['rule_category'])
     rule.group=group
     rule.language_dependancy=r['language_dependency']
     rule.primary_property=r['primary_property']
     rule.resource_properties=resource_properties
     rule.validation=r['validate']
     rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.updated_date=r['last_updated']

     NodeResultMessage.objects.filter(rule=rule).delete()

   except ObjectDoesNotExist:
     print("  Creating Rule: " + r['nls_rule_id'])
     resource_properties = ",".join(r['resource_properties'])
     rule = Rule(rule_id=r['rule_id'],scope=scope,group=group,language_dependancy=r['language_dependency'],primary_property=r['primary_property'],resource_properties=resource_properties,validation=r['validate'],updated_date=r['last_updated'])
     rule.wcag_primary = SuccessCriterion.get_by_wcag_number(r['wcag_primary'])
     rule.category = RuleCategory.objects.get(rule_category_code=r['rule_category'])

   rule.slug = r['rule_id'].lower().replace('_', '')
   rule.save()

   rule.wcag_related.clear();
   for related in r['wcag_related']:
      rule.wcag_related.add(SuccessCriterion.get_by_wcag_number(related))

   rule.target_resources.clear();
   for m in r['target_resources']:
     try:
       rule.target_resources.add(ElementDefinition.get_by_title(m))
     except:
Esempio n. 19
0
    def handle(self, *args, **options):
        UTF8Writer = codecs.getwriter('utf8')
        outer = UTF8Writer(sys.stdout)
        logger = logging.getLogger(__name__)

        # Downloaded docx version of rules - http://archive.wizards.com/Magic/tcg/article.aspx?x=magic/rules
        # opened it in Word, and then saved it as htm with the encoding set to UTF-8 in the Web Options settings.
        # Make it XML so that xml.dom can play with it
        # xmllint --html --xmlout MagicCompRules_20140601.htm  > fixed.html
        infile = codecs.open('/home/jason/projects/mtgdb/rules.html', 'r', 'utf-8')
        bigstring = infile.read()
        dom = parseString(bigstring.encode('UTF-8'))
        divs = dom.getElementsByTagName('div')
        div = divs.item(0)

        # Axe all of the current data
        Example.objects.all().delete()
        Rule.objects.all().delete()
        RulesMeta.objects.all().delete()

        rulesmeta = None

        card_vals = self.init_cards()
        #sys.stdout.write("cards are " + str(card_vals) + "\n\n")

        markers_passed = list()
        header_re = re.compile('^(\d{1,2})\\.\s+(\S.+)$', re.UNICODE)
        rule_re = re.compile('^(\d\d\d)\\.?((\d{1,3})([a-z])?)?\\.?\s+(\S.+)$', re.UNICODE)
        example_re = re.compile('^\s*\\<[Bb]\\>[Ee]xample[^\\<]*\\</[Bb]\\>:?\\.?\s*(\S.+)$', re.UNICODE)
        has_letter_re = re.compile('[a-z]', re.I)
        effectivedate_re = re.compile('These rules are effective as of ([^\\.]+).', re.U)
        current_header = None
        parent_rule = None
        current_rule = None
        current_header_r = None
        parent_rule_r = None
        current_rule_r = None
        example_position = 0
        for element in div.childNodes:
            if element.nodeType == element.ELEMENT_NODE and element.tagName == 'p':
                stringback = self.cleanTextFromNode(element.childNodes)
                if stringback == 'Contents' or stringback == 'Glossary' or stringback == 'Credits':
                    markers_passed.append(stringback)
                    if 'Contents' in markers_passed and 'Glossary' in markers_passed and 'Credits' in markers_passed and stringback == u'Glossary':
                        break
                    continue
                if 'Contents' in markers_passed and 'Glossary' in markers_passed and 'Credits' in markers_passed:
                    if len(stringback) > 0:
                        header_match = header_re.match(stringback)
                        if header_match:
                            dfg = u'HEADER!!! __{}__ --{}--\n'.format(header_match.group(1), header_match.group(2)).encode('utf8')
                            current_header = header_match.group(1)
                            current_rule = None
                            #dfg = match.group(0).encode('utf8')
                            sys.stdout.write(dfg)
                            # now the real work
                            hRule = Rule(section=header_match.group(1).encode('utf8'),
                                         rule_text=header_match.group(2).encode('utf8'),
                                         sortsection=header_match.group(1).encode('utf8'),
                                         )
                            hRule.save()
                            current_header_r = hRule
                        else:
                            rule_match = rule_re.match(stringback)
                            if rule_match:
                                current_rule = rule_match.group(1)
                                dfg = u'RULE {}, 1"{}" 3"{}" 4"{}": {}\n'.format(
                                    current_header,
                                    rule_match.group(1),
                                    rule_match.group(3),
                                    rule_match.group(4),
                                    rule_match.group(5)).encode('utf8')
                                sys.stdout.write(dfg)
                                # now the real work
                                part1 = rule_match.group(1)
                                part2 = rule_match.group(3)
                                part3 = rule_match.group(4)
                                combined = part1
                                sorty = part1
                                dParent = None
                                if part2 is None and part3 is None:
                                    # parent is a header
                                    dParent = part1[0]  # hard coded to first character for now
                                else:
                                    combined = u'{}.{}'.format(part1, part2)
                                    sorty = u'{0:0>3}.{1:0>3}'.format(part1, part2)
                                    dParent = part1
                                    if part3 is not None:
                                        dParent = u'{}.{}'.format(part1, part2)
                                        combined = u'{}{}'.format(combined, part3)
                                        sorty = u'{0}{1: >2}'.format(sorty, part3)
                                sys.stdout.write('Looking for parent ' + dParent + "\n")
                                parent_q = Rule.objects.filter(section__iexact=dParent)
                                parent_r = parent_q.first()
                                if parent_r is None:
                                    sys.stdout.write("SQL: " + str(parent_q.query) + "\n")
                                    quit()
                                rule_text_html = self.markup_text(rule_match.group(5), card_vals)
                                rRule = Rule(
                                    parent=parent_r,
                                    section=combined.encode('utf8'),
                                    sortsection=sorty.encode('utf8'),
                                    rule_text=rule_match.group(5).encode('utf8'),
                                    rule_text_html=rule_text_html)
                                rRule.save()
                                current_rule_r = rRule
                                example_position = 0
                            else:
                                example_match = example_re.match(stringback)
                                if example_match:
                                    dfg = u'  EXAMPLE {}, {}: {}\n'.format(
                                        current_header,
                                        current_rule,
                                        example_match.group(1)).encode('utf8')

                                    sys.stdout.write(dfg)
                                    # Now do the real work
                                    example_db = Example(
                                        position=example_position,
                                        rule=current_rule_r,
                                        example_text=example_match.group(1).encode('utf8'),
                                        example_text_html=self.markup_text(example_match.group(1), card_vals))
                                    example_db.save()
                                    example_position = example_position + 1
                                else:
                                    sys.stdout.write(u'ACK!! {}\n'.format(stringback).encode('utf8'))
                else:
                    ed_m = effectivedate_re.match(stringback)
                    if ed_m and rulesmeta is None:
                        # more code goes in here...
                        rulesmeta = RulesMeta()
                        rulesmeta.source_url = 'http://media.wizards.com/images/magic/tcg/resources/rules/MagicCompRules_20150327.pdf'
                        rulesmeta.effective_date = datetime.date(2015, 3, 27)
                        rulesmeta.import_date = timezone.now()
                        rulesmeta.save()
Esempio n. 20
0
def send_finding_alerts_api(request, finding_id):
    if request.GET.get("raw", None) and request.GET.get("raw") == "true":
        finding = get_object_or_404(RawFinding.objects.for_user(request.user), id=finding_id)
    else:
        finding = get_object_or_404(Finding.objects.for_user(request.user), id=finding_id)

    # Create a new rule
    rule = Rule(title="manual", severity=finding.severity.capitalize(), owner_id=request.user.id)
    if request.GET.get("type", None) and request.GET.get("type") == "slack":
        rule.target = "slack"
        rule.notify(finding.title)
    elif request.GET.get("type", None) and request.GET.get("type") == "thehive":
        rule.target = "thehive"
        rule.notify(message=finding.title, asset=finding.asset, description=finding.description)
    elif request.GET.get("type", None) and request.GET.get("type") == "email":
        rule.target = "email"
        rule.notify(message=finding.title, asset=finding.asset, description=finding.description)

    rule.delete()

    return JsonResponse({"status": "success"})
Esempio n. 21
0
 def test_delete_rule(self):
     rule = Rule(start_sort='100000', end_sort='100001', mod_rule='MOD10',
                 weight=[0,1,2,3,4,5,6,7,8,9,10,11,12,13], mod_exception='')
     rule.save()
     rule.delete()
     assert rule.id is None