it seems like in every call to this function, username and user_id are pulled from the same record. why not:

def assign_case(case, new_owner, user=None):
    user=user or new_owner
    ...