コード例 #1
0
    def update_by_user(cls, db, user, data):
        from holmes.models import Key

        if not user or not data:
            return

        for item in data:
            if 'key' not in item or 'is_active' not in item:
                continue

            key = Key.get_by_name(db, item.get('key'))
            if not key:
                continue

            is_active = item.get('is_active', True)

            db \
                .query(UsersViolationsPrefs) \
                .filter(
                    UsersViolationsPrefs.user_id == user.id,
                    UsersViolationsPrefs.key_id == key.id
                ) \
                .update(
                    {'is_active': is_active}
                )

        db.flush()
コード例 #2
0
    def update_by_user(cls, db, user, data):
        from holmes.models import Key

        if not user or not data:
            return

        for item in data:
            if 'key' not in item or 'is_active' not in item:
                continue

            key = Key.get_by_name(db, item.get('key'))
            if not key:
                continue

            is_active = item.get('is_active', True)

            db \
                .query(UsersViolationsPrefs) \
                .filter(
                    UsersViolationsPrefs.user_id == user.id,
                    UsersViolationsPrefs.key_id == key.id
                ) \
                .update(
                    {'is_active': is_active}
                )

        db.flush()
コード例 #3
0
    def test_get_by_name(self):
        key = KeyFactory.create(name='some.random.key')

        loadded_key = Key.get_by_name(self.db, 'some.random.key')

        expect(key).to_equal(loadded_key)
        expect(loadded_key.name).to_equal('some.random.key')
コード例 #4
0
ファイル: test_keys.py プロジェクト: diegosaouda/holmes-api
    def test_get_by_name(self):
        key = KeyFactory.create(name='some.random.key')

        loadded_key = Key.get_by_name(self.db, 'some.random.key')

        expect(key).to_equal(loadded_key)
        expect(loadded_key.name).to_equal('some.random.key')
コード例 #5
0
    def test_can_get_domain_top_category_violations(self):
        domain1 = DomainFactory.create()
        domain2 = DomainFactory.create()

        page1 = PageFactory.create(domain=domain1)
        page2 = PageFactory.create(domain=domain1)
        page3 = PageFactory.create(domain=domain2)

        ReviewFactory.create(domain=domain1,
                             page=page1,
                             is_active=True,
                             number_of_violations=5)
        ReviewFactory.create(domain=domain1,
                             page=page2,
                             is_active=True,
                             number_of_violations=7)
        ReviewFactory.create(domain=domain2,
                             page=page3,
                             is_active=True,
                             number_of_violations=9)

        self.server.application.violation_definitions = {
            'key.%s' % i: {
                'title':
                'title.%s' % i,
                'category':
                'category.%s' % (i % 3),
                'key':
                Key.get_or_create(self.db, 'key.%d' % i,
                                  'category.%d' % (i % 3))
            }
            for i in range(9)
        }

        key = Key.get_by_name(self.db, 'key.0')

        response = yield self.authenticated_fetch(
            '/domains/%s/violations/%d/' % (domain1.name, key.category_id))

        expect(response.code).to_equal(200)

        domain_top_category = loads(response.body)

        expect(domain_top_category).to_length(5)
        expect(domain_top_category.keys()).to_be_like([
            'violations', 'domainId', 'categoryId', 'domainURL', 'domainName'
        ])
        expect(domain_top_category['violations']).to_length(3)

        counts = map(lambda v: v['count'], domain_top_category['violations'])
        expect(counts).to_be_like([2, 1, 1])
コード例 #6
0
    def delete_prefs(cls, db, user, items):
        from holmes.models import Key

        for key_name in items:
            key = Key.get_by_name(db, key_name)
            if not key:
                continue

            db \
                .query(UsersViolationsPrefs) \
                .filter(
                    UsersViolationsPrefs.key_id == key.id,
                    UsersViolationsPrefs.user_id == user.id
                ) \
                .delete()

        db.flush()
コード例 #7
0
    def delete_prefs(cls, db, user, items):
        from holmes.models import Key

        for key_name in items:
            key = Key.get_by_name(db, key_name)
            if not key:
                continue

            db \
                .query(UsersViolationsPrefs) \
                .filter(
                    UsersViolationsPrefs.key_id == key.id,
                    UsersViolationsPrefs.user_id == user.id
                ) \
                .delete()

        db.flush()
コード例 #8
0
    def insert_prefs(cls, db, user, items):
        from holmes.models import Key

        if not user:
            return

        data = []
        for key_name in items:
            key = Key.get_by_name(db, key_name)

            if not key:
                continue

            data.append({
                'user_id': user.id,
                'key_id': key.id,
                'is_active': True
            })

        db.execute(UsersViolationsPrefs.__table__.insert(), data)
コード例 #9
0
    def insert_prefs(cls, db, user, items):
        from holmes.models import Key

        if not user:
            return

        data = []
        for key_name in items:
            key = Key.get_by_name(db, key_name)

            if not key:
                continue

            data.append({
                'user_id': user.id,
                'key_id': key.id,
                'is_active': True
            })

        db.execute(UsersViolationsPrefs.__table__.insert(), data)
コード例 #10
0
    def test_can_get_domain_top_category_violations(self):
        domain1 = DomainFactory.create()
        domain2 = DomainFactory.create()

        page1 = PageFactory.create(domain=domain1)
        page2 = PageFactory.create(domain=domain1)
        page3 = PageFactory.create(domain=domain2)

        ReviewFactory.create(domain=domain1, page=page1, is_active=True, number_of_violations=5)
        ReviewFactory.create(domain=domain1, page=page2, is_active=True, number_of_violations=7)
        ReviewFactory.create(domain=domain2, page=page3, is_active=True, number_of_violations=9)

        self.server.application.violation_definitions = {
            'key.%s' % i: {
                'title': 'title.%s' % i,
                'category': 'category.%s' % (i % 3),
                'key': Key.get_or_create(self.db, 'key.%d' % i, 'category.%d' % (i % 3))
            } for i in range(9)
        }

        key = Key.get_by_name(self.db, 'key.0')

        response = yield self.authenticated_fetch(
            '/domains/%s/violations/%d/' % (domain1.name, key.category_id)
        )

        expect(response.code).to_equal(200)

        domain_top_category = loads(response.body)

        expect(domain_top_category).to_length(5)
        expect(domain_top_category.keys()).to_be_like(['violations', 'domainId', 'categoryId', 'domainURL', 'domainName'])
        expect(domain_top_category['violations']).to_length(3)

        counts = map(lambda v: v['count'], domain_top_category['violations'])
        expect(counts).to_be_like([2, 1, 1])