'When an Incident is instantiated, populate defaults'

@staticmethod
def incident_create_onaccept(form):

    form_vars = form.vars
    closed = form_vars.get('closed', False)
    incident = form_vars.get('id')
    if (incident and (not closed)):
        current.session.s3.incident = incident
    event = form_vars.get('event_id')
    if (event and (not closed)):
        current.session.s3.event = event
    s3db = current.s3db
    db = current.db
    ctable = s3db.gis_config
    mapconfig = None
    scenario = form_vars.get('scenario_id')
    if scenario:
        table = s3db.scenario_scenario
        otable = s3db.scenario_organisation
        stable = s3db.scenario_site
        mtable = s3db.scenario_config
        query = (table.id == scenario)
        squery = (query & (stable.scenario_id == table.id))
        mquery = ((query & (mtable.scenario_id == table.id)) & (ctable.id == mtable.config_id))
        facilities = db(squery).select(stable.site_id)
        mapconfig = db(mquery).select(ctable.ALL).first()
        stable = s3db.event_site
        for row in facilities:
            stable.insert(incident_id=incident, site_id=row.site_id)
        htable = s3db.table('scenario_human_resource', None)
        if htable:
            hquery = (query & (htable.scenario_id == table.id))
            hrms = db(hquery).select(htable.human_resource_id)
            htable = s3db.event_human_resource
            for row in hrms:
                htable.insert(incident_id=incident, human_resource_id=row.human_resource_id)
        atable = s3db.table('scenario_asset', None)
        if atable:
            aquery = (query & (atable.scenario_id == table.id))
            assets = db(aquery).select(atable.asset_id)
            atable = s3db.event_asset
            for row in assets:
                atable.insert(incident_id=incident, asset_id=row.asset_id)
        ttable = s3db.table('scenario_task', None)
        if ttable:
            tquery = (query & (ttable.scenario_id == table.id))
            tasks = db(tquery).select(ttable.task_id)
            ttable = s3db.event_task
            for row in tasks:
                ttable.insert(incident_id=incident, task_id=row.task_id)
    if mapconfig:
        del mapconfig['id']
        del mapconfig['uuid']
        mapconfig['name'] = form_vars.name
        config = ctable.insert(**mapconfig.as_dict())
        mtable = s3db.event_config
        mtable.insert(incident_id=incident, config_id=config)
        if (not closed):
            current.gis.set_config(config)
    else:
        config = ctable.insert(name=form_vars.name)
        mtable = s3db.event_config
        mtable.insert(incident_id=incident, config_id=config)
        if (not closed):
            current.gis.set_config(config)
