'Presence record validation'

@staticmethod
def presence_onvalidation(form):

    db = current.db
    table = db.pr_presence
    s3db = current.s3db
    popts = s3db.pr_presence_opts
    shelter_table = s3db.cr_shelter
    location = form.vars.location_id
    shelter = form.vars.shelter_id
    if (shelter and (shelter_table is not None)):
        set = db((shelter_table.id == shelter))
        row = set.select(shelter_table.location_id, limitby=(0, 1)).first()
        if row:
            location = form.vars.location_id = row.location_id
        else:
            shelter = None
    if (location or shelter):
        return
    condition = form.vars.presence_condition
    if condition:
        try:
            condition = int(condition)
        except ValueError:
            condition = None
    else:
        condition = table.presence_condition.default
        form.vars.presence_condition = condition
    if condition:
        if ((condition in popts.PERSISTANT_PRESENCE) or (condition in popts.ABSENCE)):
            if (not form.vars.id):
                if (table.location_id.default or table.shelter_id.default):
                    return
            else:
                record = db((table.id == form.vars.id)).select(table.location_id, table.shelter_id, limitby=(0, 1)).first()
                if ((record and record.location_id) or record.shelter_id):
                    return
        else:
            return
    else:
        return
    form.errors.location_id = form.errors.shelter_id = T('Either    a    shelter    or    a    location    must    be    specified')
    return
