def get_or_create(session, query_class, **kwargs):
    """Query the query_class, filtering by the given keyword arguments. If no result
    is found, then add a new row to the database.

    Returns a tuple: (result of the query, Boolean: the result already existed)

    Arguments
    ---------

    session: The SQLAlchemy session.

    query_class: The class to query.

    """
    res = session.query(query_class).filter_by(**kwargs).first()
    if res is not None:
        return res, True
    res = query_class(**kwargs)
    session.add(res)
    session.commit()
    return res, False