'Verify that a person isn\'t added to a group more than once
@param form: the FORM'

@staticmethod
def group_membership_onvalidation(form):

    form_vars = form.vars
    if ('id' in form_vars):
        record_id = form_vars.id
    elif hasattr(form, 'record_id'):
        record_id = form.record_id
    else:
        record_id = None
    person_id = form_vars.get('person_id')
    group_id = form_vars.get('group_id')
    db = current.db
    s3db = current.s3db
    table = s3db.pr_group_membership
    if (not record_id):
        if (not person_id):
            person_id = table.person_id.default
        if (not group_id):
            group_id = table.group_id.default
    elif ((not person_id) or (not group_id)):
        query = ((table.id == record_id) & (table.deleted != True))
        record = db(query).select(table.person_id, table.group_id, limitby=(0, 1)).first()
        if (not record):
            return
        if (not person_id):
            person_id = record.person_id
        if (not group_id):
            group_id = record.group_id
    CASE_GROUP = 7
    group_type = None
    query = (((table.person_id == person_id) & (table.deleted != True)) & (table.group_id == group_id))
    multiple_case_groups = current.deployment_settings.get_dvr_multiple_case_groups()
    if (not multiple_case_groups):
        gtable = s3db.pr_group
        gquery = (gtable.id == group_id)
        group = db(gquery).select(gtable.group_type, limitby=(0, 1)).first()
        if group:
            group_type = group.group_type
        if (group_type == CASE_GROUP):
            query = ((((table.person_id == person_id) & (table.deleted != True)) & (gtable.id == table.group_id)) & (gtable.group_type == group_type))
    if record_id:
        query = ((table.id != record_id) & query)
    duplicate = db(query).select(table.group_id, limitby=(0, 1)).first()
    if duplicate:
        if ((group_type == CASE_GROUP) and (str(duplicate.group_id) != str(group_id))):
            error = current.T('This    person    already    belongs    to    another    case    group')
        else:
            error = current.T('This    person    already    belongs    to    this    group')
        if ('person_id' in form_vars):
            form.errors['person_id'] = error
        elif ('group_id' in form_vars):
            form.errors['group_id'] = error
