parameters: (user, klass, attrs, **kwargs)
return_statement: return obj
docstring: Creates a new instance, saves it, and records the history
      :param klass: The class we are instantiating
      :param attrs: Dictionary with the attributes of the class that we are instantiating
      :param user:  Integer user id
      :return:
docstring_summary: Creates a new instance, saves it, and records the history
      :param klass: The class we are instantiating
      :param attrs: Dictionary with the attributes of the class that we are instantiating
      :param user:  Integer user id
      :return:
docstring_tokens: Creates a new instance saves it and records the history : param klass : The class we are instantiating : param attrs : Dictionary with the attributes of the class that we are instantiating : param user : Integer user id : return :
function: def add(user, klass, attrs, **kwargs):
      """
      Creates a new instance, saves it, and records the history
      :param klass: The class we are instantiating
      :param attrs: Dictionary with the attributes of the class that we are instantiating
      :param user:  Integer user id
      :return:
      """
      assert issubclass(klass, model.abstract.AbstractMongoRecord)
      obj = None
      if getattr(klass, "criteria_override_field", None) and attrs.get(klass.criteria_override_field):
          obj = klass().load({klass.criteria_field: attrs[klass.criteria_override_field]})
      elif attrs.get(klass.criteria_field):
          if klass.criteria_field == klass.id_field:  # a clumsy way of pushing _id through ObjectId
              obj = klass().load_by_id(attrs[klass.id_field])
          else:
              obj = klass().load({klass.criteria_field: attrs[klass.criteria_field]})
      if obj:
          old_dict = obj.contents(**kwargs)
          obj.load_from_dict(attrs).save()
          model.log_update(user, klass, old_dict, obj.contents(**kwargs), **kwargs)
          return obj
      obj = klass(attrs).save()
      model.log_add(user, klass, obj.contents(**kwargs), **kwargs)
      return obj
function_tokens: def add ( user , klass , attrs , * * kwargs ) : assert issubclass ( klass , model . abstract . AbstractMongoRecord ) obj = None if getattr ( klass , "criteria_override_field" , None ) and attrs . get ( klass . criteria_override_field ) : obj = klass ( ) . load ( { klass . criteria_field : attrs [ klass . criteria_override_field ] } ) elif attrs . get ( klass . criteria_field ) : if klass . criteria_field == klass . id_field : # a clumsy way of pushing _id through ObjectId obj = klass ( ) . load_by_id ( attrs [ klass . id_field ] ) else : obj = klass ( ) . load ( { klass . criteria_field : attrs [ klass . criteria_field ] } ) if obj : old_dict = obj . contents ( * * kwargs ) obj . load_from_dict ( attrs ) . save ( ) model . log_update ( user , klass , old_dict , obj . contents ( * * kwargs ) , * * kwargs ) return obj obj = klass ( attrs ) . save ( ) model . log_add ( user , klass , obj . contents ( * * kwargs ) , * * kwargs ) return obj