'Update the shelter population, onaccept
@param form: the FORM
@param tablename: the table name
@param unit_id: the shelter unit ID (to warn if full)'

@staticmethod
def shelter_population_onaccept(form, tablename=None, unit_id=None):

    db = current.db
    s3db = current.s3db
    if (not tablename):
        return
    table = s3db[tablename]
    try:
        if (type(form) is Row):
            record_id = form.id
        else:
            record_id = form.vars.id
    except AttributeError:
        return
    if (tablename == 'cr_shelter_unit'):
        unit_id = record_id
    row = db((table._id == record_id)).select(table._id, table.shelter_id, table.deleted, table.deleted_fk, limitby=(0, 1)).first()
    if row:
        if row.deleted:
            if row.deleted_fk:
                deleted_fk = json.loads(row.deleted_fk)
            else:
                return
            shelter_id = deleted_fk.get('shelter_id')
        else:
            shelter_id = row.shelter_id
        if shelter_id:
            if current.deployment_settings.get_cr_shelter_housing_unit_management():
                cr_update_capacity_from_housing_units(shelter_id)
            cr_update_shelter_population(shelter_id)
            if unit_id:
                cr_check_population_availability(unit_id, table=s3db.cr_shelter_unit)
            cr_check_population_availability(shelter_id, table=s3db.cr_shelter)
