'Remove any duplicate memberships and update affiliations'

@staticmethod
def group_membership_onaccept(form):

    if hasattr(form, 'vars'):
        _id = form.vars.id
    elif (isinstance(form, Row) and ('id' in form)):
        _id = form.id
    else:
        return
    db = current.db
    mtable = db.org_group_membership
    if _id:
        record = db((mtable.id == _id)).select(limitby=(0, 1)).first()
    else:
        return
    if record:
        organisation_id = record.organisation_id
        group_id = record.group_id
        if (organisation_id and group_id and (not record.deleted)):
            query = ((((mtable.organisation_id == organisation_id) & (mtable.group_id == group_id)) & (mtable.id != record.id)) & (mtable.deleted != True))
            deleted_fk = {'organisation_id': organisation_id, 'group_id': group_id}
            db(query).update(deleted=True, organisation_id=None, group_id=None, deleted_fk=json.dumps(deleted_fk))
        org_update_affiliations('org_group_membership', record)
