code: def get_parent_of_type(typ, obj):
      
      if type(typ) is not text:
          typ = typ.__name__
  
      while hasattr(obj, 'parent'):
          obj = obj.parent
          if obj.__class__.__name__ == typ:
              return obj
docs: Finds first object up the parent chain of the given type.
      If no parent of the given type exists None is returned.
  
      Args:
          typ(str or python class): The type of the model object we are
              looking for.
          obj (model object): Python model object which is the start of the
              search process.