def test_sophisticated_mixins(self): self.mock_service('ModelA') self.mock_service('ModelB') mixin = self.mock_class('Mixin', _publish_attrs=['mixin']) mixin_subclass = self.mock_class( 'MixinSubclass', (mixin,), _publish_attrs=['mixin_subclass']) model_a = self.mock_model('ModelA', bases=(mixin_subclass,), prop_a='prop_a', mixin='mixin_a', mixin_subclass='mixin_subclass_a', _publish_attrs=['prop_a']) model_b = self.mock_model('ModelB', bases=(mixin,), prop_b='prop_b', mixin='mixin_b', _publish_attrs=['prop_b']) json_obj = publish(model_a) self.assertDictContainsSubset( {'prop_a': 'prop_a', 'mixin': 'mixin_a', 'mixin_subclass': 'mixin_subclass_a'}, json_obj) json_obj = publish(model_b) self.assertDictContainsSubset( {'prop_b': 'prop_b', 'mixin': 'mixin_b'}, json_obj)
def _transform_to_json(objects, fields=None): """Make a JSON representation of objects from the list.""" objects_json = [json.publish(obj) for obj in objects] objects_json = json.publish_representation(objects_json) if fields: objects_json = [{f: o.get(f) for f in fields} for o in objects_json] return objects_json
def get_full_user_json(): """Get the full current user""" with benchmark("Get full user JSON"): from ggrc.models.person import Person current_user = get_current_user() person = Person.eager_query().filter_by(id=current_user.id).one() result = publish_representation(publish(person, (), inclusion_filter)) return as_json(result)
def get_roles_json(): """Get a list of all roles""" with benchmark("Get roles JSON"): attrs = all_models.Role.query.all() published = [] for attr in attrs: published.append(publish(attr, attribute_whitelist=('id', 'name'))) published = publish_representation(published) return as_json(published)
def get_access_control_roles_json(): """Get a list of all access control roles""" with benchmark("Get access roles JSON"): attrs = all_models.AccessControlRole.query.all() published = [] for attr in attrs: published.append(publish(attr)) published = publish_representation(published) return as_json(published)
def get_access_control_roles_json(): """Get a list of all access control roles""" with benchmark("Get access roles JSON"): attrs = all_models.AccessControlRole.query.options( sqlalchemy.orm.undefer_group("AccessControlRole_complete")).all() published = [] for attr in attrs: published.append(publish(attr)) published = publish_representation(published) return as_json(published)
def get_attributes_json(): """Get a list of all custom attribute definitions""" with benchmark("Get attributes JSON"): attrs = models.CustomAttributeDefinition.eager_query().filter( models.CustomAttributeDefinition.definition_id.is_(None)) published = [] for attr in attrs: published.append(publish(attr)) published = publish_representation(published) return as_json(published)
def test_simple_builder(self): self.mock_service('MockModel') model = self.mock_model( 'Mock_test_simple_builder', foo='bar', id=1, _publish_attrs=['foo'], ) json_obj = publish(model) self.assertIn('foo', json_obj) self.assertEqual('bar', json_obj['foo'])
def get_internal_roles_json(): """Get a list of all access control roles""" with benchmark("Get access roles JSON"): attrs = all_models.AccessControlRole.query.options( sqlalchemy.orm.undefer_group("AccessControlRole_complete") ).filter(all_models.AccessControlRole.internal == true()).all() published = [] for attr in attrs: published.append(publish(attr)) published = publish_representation(published) return as_json(published)
def get_attributes_json(): """Get a list of all custom attribute definitions""" with benchmark("Get attributes JSON"): attrs = models.CustomAttributeDefinition.eager_query().filter( models.CustomAttributeDefinition.definition_id.is_(None) ) published = [] for attr in attrs: published.append(publish(attr)) published = publish_representation(published) return as_json(published)
def test_simple_mixin_inheritance(self): self.mock_service('MockModelWithMixin') mock_mixin = self.mock_class('MockMixin', _publish_attrs=['boo']) model = self.mock_model( 'MockModelWithMixin', bases=(mock_mixin, ), foo='bar', boo='far', _publish_attrs=['foo'], ) json_obj = publish(model) self.assertDictContainsSubset({'foo': 'bar', 'boo': 'far'}, json_obj)
def get_internal_roles_json(): """Get a list of all access control roles""" with benchmark("Get access roles JSON"): attrs = models.all_models.AccessControlRole.query.options( sqlalchemy.orm.undefer_group("AccessControlRole_complete")).filter( models.all_models.AccessControlRole.internal == sqlalchemy.true()).all() published = [] for attr in attrs: published.append(builder_json.publish(attr)) published = builder_json.publish_representation(published) return services_common.as_json(published)
def get_attributes_json(): """Get a list of all custom attribute definitions""" with benchmark("Get attributes JSON"): with benchmark("Get attributes JSON: query"): # get only GCA and exclude external CADs # external GCA should be deleted from internal GCA table attrs = models.CustomAttributeDefinition.eager_query().filter( models.CustomAttributeDefinition.definition_id.is_(None), ~models.CustomAttributeDefinition.definition_type.in_( constants.GGRCQ_OBJ_TYPES_FOR_SYNC)).all() ext_attrs = models.ExternalCustomAttributeDefinition.eager_query( ).all() with benchmark("Get attributes JSON: publish"): published = [] for attr in attrs: published.append(builder_json.publish(attr)) for attr in ext_attrs: published.append(builder_json.publish(attr)) published = builder_json.publish_representation(published) with benchmark("Get attributes JSON: json"): publish_json = services_common.as_json(published) return publish_json
def test_simple_mixin_inheritance(self): self.mock_service('MockModelWithMixin') mock_mixin = self.mock_class('MockMixin', _publish_attrs=['boo']) model = self.mock_model( 'MockModelWithMixin', bases=(mock_mixin,), foo='bar', boo='far', _publish_attrs=['foo'], ) json_obj = publish(model) self.assertDictContainsSubset( {'foo': 'bar', 'boo': 'far'}, json_obj)
def get_attributes_json(): """Get a list of all custom attribute definitions""" with benchmark("Get attributes JSON"): with benchmark("Get attributes JSON: query"): attrs = models.CustomAttributeDefinition.eager_query().filter( models.CustomAttributeDefinition.definition_id.is_(None) ).all() with benchmark("Get attributes JSON: publish"): published = [] for attr in attrs: published.append(builder_json.publish(attr)) published = builder_json.publish_representation(published) with benchmark("Get attributes JSON: json"): publish_json = services_common.as_json(published) return publish_json
def get_attributes_json(): """Get a list of all custom attribute definitions""" with benchmark("Get attributes JSON"): with benchmark("Get attributes JSON: query"): attrs = models.CustomAttributeDefinition.eager_query().filter( models.CustomAttributeDefinition.definition_id.is_( None)).all() with benchmark("Get attributes JSON: publish"): published = [] for attr in attrs: published.append(builder_json.publish(attr)) published = builder_json.publish_representation(published) with benchmark("Get attributes JSON: json"): publish_json = services_common.as_json(published) return publish_json