@classmethod
def update_or_create(cls, db_session, mapper, attributes):
    """
        Updated the record if attributes contain the primary key value(s) and creates it if they don't.

        :param db_session:
        :type db_session: sqlalchemy.orm.session.Session

        :param mapper:
        :type mapper: sqlalchemy.orm.mapper.Mapper

        :param attributes:
        :type attributes: dict

        :return:
        :rtype: object
        """
    query_attrs = {}
    for key in mapper.primary_key:
        if key in attributes:
            query_attrs[key] = attributes.pop(key)
    if query_attrs:
        obj = db_session.query(mapper.class_).get(query_attrs[0] if len(
            query_attrs) == 1 else tuple(query_attrs))
    else:
        obj = mapper.class_()
    if attributes:
        return cls.save_resource(obj, attributes, db_session)
    return obj
