Example #1
0
    def get_object_by_key(self, key="slug"):
        """ Get object if the slug is in the system or return a new object """
        value = self.get_value(key)
        self.is_new = False

        if value:
            obj = self.find_by_key(key, value)

        if not value or not obj:
            # We assume that 'get_importables()' returned value contains
            # names of the objects that cannot be created via import but
            # can be updated.
            if self.block_converter.class_name.lower() not in get_importables(
            ):
                self.add_error(errors.CREATE_INSTANCE_ERROR)
            obj = self.object_class()
            self.is_new = True
        elif not permissions.is_allowed_update_for(obj):
            self.ignore = True
            self.add_error(errors.PERMISSION_ERROR)
        elif self._has_readonly_access(obj):
            self._is_obj_readonly = True

        self.initial_state = dump_attrs(obj)
        return obj
Example #2
0
  def test_query_all_aliases(self):
    def rhs(model, attr):
      attr = getattr(model, attr, None)
      if attr is not None and hasattr(attr, "_query_clause_element"):
        class_name = attr._query_clause_element().type.__class__.__name__
        if class_name == "Boolean":
          return "1"
      return "1/1/2015"

    def data(model, attr, field):
      return [{
          "object_name": model.__name__,
          "fields": "all",
          "filters": {
              "expression": {
                  "left": field.lower(),
                  "op": {"name": "="},
                  "right": rhs(model, attr)
              },
          }
      }]

    failed = set()
    for model in set(get_importables().values()):
      for attr, field in AttributeInfo(model)._aliases.items():
        if field is None:
          continue
        try:
          field = field["display_name"] if type(field) is dict else field
          res = self.export_csv(data(model, attr, field))
          self.assertEqual(res.status_code, 200)
        except Exception as e:
          failed.add((model, attr, field, e))
    self.assertEqual(sorted(failed), [])
Example #3
0
 def test_importable_only(self):
     """Test importable only objects not exists in exportables"""
     result_importables = set(get_importables().keys())
     result_exportables = set(get_exportables().keys())
     importable_only = set(GGRC_IMPORTABLE_ONLY.keys())
     self.assertTrue(importable_only.issubset(result_importables))
     self.assertFalse(importable_only.issubset(result_exportables))
Example #4
0
  def test_query_all_aliases(self):
    def rhs(model, attr):
      attr = getattr(model, attr, None)
      if attr is not None and hasattr(attr, "_query_clause_element"):
        class_name = attr._query_clause_element().type.__class__.__name__
        if class_name == "Boolean":
          return "1"
      return "1/1/2015"

    def data(model, attr, field):
      return [{
          "object_name": model.__name__,
          "fields": "all",
          "filters": {
              "expression": {
                  "left": field.lower(),
                  "op": {"name": "="},
                  "right": rhs(model, attr)
              },
          }
      }]

    failed = set()
    for model in set(get_importables().values()):
      for attr, field in AttributeInfo(model)._aliases.items():
        if field is None:
          continue
        try:
          field = field["display_name"] if type(field) is dict else field
          res = self.export_csv(data(model, attr, field))
          self.assertEqual(res.status_code, 200)
        except Exception as e:
          failed.add((model, attr, field, e))
    self.assertEqual(sorted(failed), [])
Example #5
0
  def get_object_by_key(self, key="slug"):
    """ Get object if the slug is in the system or return a new object """
    value = self.get_value(key)
    self.is_new = False

    if value:
      obj = self.find_by_key(key, value)

    if not value or not obj:
      # We assume that 'get_importables()' returned value contains
      # names of the objects that cannot be created via import but
      # can be updated.
      if self.block_converter.class_name.lower() not in get_importables() and \
              not self._check_object_is_external():
        self.add_error(errors.CREATE_INSTANCE_ERROR)
      obj = self.object_class()
      self.is_new = True
    elif not permissions.is_allowed_update_for(obj):
      self.ignore = True
      self.add_error(errors.PERMISSION_ERROR)
    elif self._has_readonly_access(obj):
      self._is_obj_readonly = True

    self.initial_state = dump_attrs(obj)
    return obj
Example #6
0
 def __init__(self, ie_job, dry_run=True, csv_data=None, bulk_import=False):
     self.user = login.get_current_user()
     self.dry_run = dry_run
     self.csv_data = csv_data or []
     self._bulk_import = bulk_import
     self.indexer = get_indexer()
     self.comment_created_notif_type = all_models.NotificationType.query. \
         filter_by(name="comment_created").one().id
     super(ImportConverter, self).__init__(ie_job)
     self.exportable.update(get_importables())
     self.bulk_import = bulk_import
     self.failed_slugs = []
Example #7
0
 def __init__(self, row_converter, key, **options):
     self.mappable = get_importables()
     self.new_slugs = row_converter.block_converter.converter.new_objects
     super(ObjectsColumnHandler, self).__init__(row_converter, key,
                                                **options)
Example #8
0
 def __init__(self, row_converter, key, **options):
   self.mappable = get_importables()
   self.new_slugs = row_converter.block_converter.converter.new_objects
   super(ObjectsColumnHandler, self).__init__(row_converter, key, **options)