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()
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()
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')
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')
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])
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()
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()
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)
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)
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])