'Remove duplicate affiliations and clear descendant paths
(to trigger lazy rebuild)
@param form: the CRUD form'

@staticmethod
def pr_affiliation_onaccept(form):

    form_vars = form.vars
    role_id = form_vars['role_id']
    pe_id = form_vars['pe_id']
    record_id = form_vars['id']
    if (role_id and pe_id and record_id):
        db = current.db
        atable = db.pr_affiliation
        query = (((atable.id != record_id) & (atable.role_id == role_id)) & (atable.pe_id == pe_id))
        deleted_fk = {'role_id': role_id, 'pe_id': pe_id}
        data = {'deleted': True, 'role_id': None, 'pe_id': None, 'deleted_fk': json.dumps(deleted_fk)}
        db(query).update(**data)
        current.s3db.pr_rebuild_path(pe_id, clear=True)
    return
