def test_can_get_or_create(self): # Create cat1 = KeysCategory.get_or_create(self.db, 'SEO') expect(cat1.name).to_equal('SEO') # Get cat2 = KeysCategory.get_or_create(self.db, 'SEO') expect(cat1.id).to_equal(cat2.id)
def test_get_by_id(self): no_category = KeysCategory.get_by_id(self.db, -1) expect(no_category).to_be_null() category = KeysCategoryFactory.create(name='HOLMES') loaded_category = KeysCategory.get_by_id(self.db, category.id) expect(category).to_equal(loaded_category) expect(loaded_category.name).to_equal('HOLMES') expect(loaded_category.id).to_equal(category.id)
def test_get_by_name(self): category = KeysCategoryFactory.create(name='HOLMES') loaded_category = KeysCategory.get_by_name(self.db, 'HOLMES') expect(category).to_equal(loaded_category) expect(loaded_category.name).to_equal('HOLMES')
def get_or_create(cls, db, key_name, category_name=None): key = cls.get_by_name(db, key_name) if key and key.category and key.category.name == category_name: return key if category_name is None: query_params = {'name': key_name} db.execute( 'INSERT INTO `keys` (name) ' \ 'VALUES (:name) ON DUPLICATE KEY ' \ 'UPDATE name = :name', query_params ) else: from holmes.models import KeysCategory category = KeysCategory.get_or_create(db, category_name) query_params = {'name': key_name, 'category_id': category.id} db.execute( 'INSERT INTO `keys` (name, category_id) ' \ 'VALUES (:name, :category_id) ON DUPLICATE KEY ' \ 'UPDATE name = :name, category_id = :category_id', query_params ) db.flush() return cls.get_by_name(db, key_name)
def get_or_create(cls, db, key_name, category_name=None): key = db.query(Key).filter(Key.name == key_name).scalar() if not key: if category_name is None: key = Key(name=key_name) else: from holmes.models import KeysCategory category = KeysCategory.get_or_create(db, category_name) key = Key(name=key_name, category=category) return key
def get(self, domain_name, key_category_id): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return key_category = KeysCategory.get_by_id(self.db, key_category_id) if not key_category: self.set_status( 404, self._('Key category %s not found') % key_category_id) return violation_defs = self.application.violation_definitions top_violations = self.girl.get( 'top_violations_in_category_for_domains') or {} violations = [] for top_violation in top_violations.get(domain_name, {}).get(key_category.id, []): violations.append({ 'title': self._(violation_defs[top_violation['key_name']]['title']), 'count': top_violation['count'], 'key': top_violation['key_name'], }) result = { "domainId": domain.id, 'domainName': domain.name, 'domainURL': domain.url, 'categoryId': key_category_id, 'violations': violations } self.write_json(result)
def _insert_key_category(self, key, name): category = KeysCategory.get_or_create(self.application.db, name) self.application.db.add(category) self.application.db.flush() self.application.db.commit() return category