Esempio n. 1
0
 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)
Esempio n. 2
0
 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
Esempio n. 3
0
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)
Esempio n. 4
0
 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
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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)
Esempio n. 12
0
 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'])
Esempio n. 13
0
 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'])
Esempio n. 14
0
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)
Esempio n. 15
0
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)
Esempio n. 16
0
 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)
Esempio n. 17
0
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)
Esempio n. 18
0
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
Esempio n. 19
0
 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)
Esempio n. 20
0
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
Esempio n. 21
0
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