'Remove any duplicate memberships and update affiliations
@param form: the FORM'

@staticmethod
def group_membership_onaccept(form):

    if hasattr(form, 'vars'):
        record_id = form.vars.id
    elif (isinstance(form, Row) and ('id' in form)):
        record_id = form.id
    else:
        return
    if (not record_id):
        return
    db = current.db
    settings = current.deployment_settings
    table = db.pr_group_membership
    gtable = db.pr_group
    left = gtable.on((gtable.id == table.group_id))
    row = db((table.id == record_id)).select(table.id, table.person_id, table.group_id, table.group_head, table.deleted, table.deleted_fk, gtable.id, gtable.group_type, left=left, limitby=(0, 1)).first()
    record = row.pr_group_membership
    if (not record):
        return
    group_id = record.group_id
    person_id = record.person_id
    if (record.deleted and record.deleted_fk):
        try:
            deleted_fk = json.loads(record.deleted_fk)
        except ValueError:
            pass
        else:
            person_id = deleted_fk.get('person_id', person_id)
            group_id = deleted_fk.get('group_id', group_id)
    if (person_id and group_id and (not record.deleted)):
        query = ((((table.person_id == person_id) & (table.group_id == group_id)) & (table.id != record.id)) & (table.deleted != True))
        deleted_fk = {'person_id': person_id, 'group_id': group_id}
        db(query).update(deleted=True, person_id=None, group_id=None, deleted_fk=json.dumps(deleted_fk))
    pr_update_affiliations(table, record)
    s3db = current.s3db
    ctable = s3db.table('dvr_case')
    if (not ctable):
        return
    response = current.response
    s3 = response.s3
    if (not s3.purge_case_groups):
        group = row.pr_group
        if ((group.id is None) and group_id):
            query = ((gtable.id == group_id) & (gtable.deleted != True))
            row = db(query).select(gtable.id, gtable.group_type, limitby=(0, 1)).first()
            if row:
                group = row
        if (group.group_type == 7):
            if ((not record.deleted) and record.group_head):
                query = (((table.group_id == group_id) & (table.id != record.id)) & (table.group_head == True))
                db(query).update(group_head=False)
            update_household_size = (settings.get_dvr_household_size() == 'auto')
            recount = s3db.dvr_case_household_size
            if (update_household_size and record.deleted and person_id):
                query = (((((table.person_id == person_id) & (table.group_id != group_id)) & (table.deleted != True)) & (gtable.id == table.group_id)) & (gtable.group_type == 7))
                row = db(query).select(table.group_id, limitby=(0, 1)).first()
                if row:
                    recount(row.group_id)
                else:
                    ctable = s3db.dvr_case
                    cquery = (ctable.person_id == person_id)
                    db(cquery).update(household_size=1)
            if (not s3.bulk):
                query = ((table.group_id == group_id) & (table.deleted != True))
                rows = db(query).select(table.id, limitby=(0, 2))
                if (len(rows) < 2):
                    if update_household_size:
                        recount(group_id)
                        update_household_size = False
                    s3.purge_case_groups = True
                    resource = s3db.resource('pr_group', id=group_id)
                    resource.delete()
                    s3.purge_case_groups = False
                elif (not record.deleted):
                    query = ((ctable.person_id == person_id) & (ctable.deleted != True))
                    row = db(query).select(ctable.id, limitby=(0, 1)).first()
                    if (not row):
                        r = S3Request('dvr', 'case', current.request)
                        r.customise_resource('dvr_case')
                        s3db.dvr_case_default_status()
                        cresource = s3db.resource('dvr_case')
                        try:
                            cresource.insert(person_id=person_id)
                        except S3PermissionError:
                            response.warning = current.T('No    permission    to    create    a    case    record    for    new    group    member')
            if update_household_size:
                recount(group_id)
