'See if we can auto-populate the Date/Location/Event Type in imported records
@param form: the form'

@staticmethod
def deploy_mission_create_onaccept(form):

    if (not current.response.s3.bulk):
        return
    db = current.db
    s3db = current.s3db
    table = s3db.deploy_mission
    record = db((table.id == form.vars.id)).select(table.id, table.name, table.location_id, table.event_type_id, limitby=(0, 1)).first()
    name = record.name
    if ('    ' not in name):
        return
    (year, location) = record.name.split('    ', 1)
    if ('    ' in location):
        (location, event_type) = location.split('    ', 1)
    else:
        event_type = None
    update = {}
    if (not record.location_id):
        gtable = s3db.gis_location
        matches = db((gtable.name == location)).select(gtable.id)
        if (len(matches) == 1):
            update['location_id'] = matches.first().id
        elif (event_type and (len(matches) == 0)):
            matches = db((gtable.name == ('%s    %s' % (location, event_type)))).select(gtable.id)
            if (len(matches) == 1):
                update['location_id'] = matches.first().id
    if (event_type and (not record.event_type_id)):
        if (event_type == 'EQ'):
            event_type = 'Earthquake'
        elif (event_type == 'Flood'):
            event_type = 'Floods'
        etable = s3db.event_event_type
        event_type = db((etable.name == event_type)).select(etable.id, limitby=(0, 1)).first()
        if event_type:
            update['event_type_id'] = event_type.id
    if update:
        record.update_record(**update)
