def add_agency():
    from ..agency.models_agency import AgencyModel, DestinationModel

    token = request.form['token']
    if not token:
        flash("token empty", "danger")
        return redirect(url_for('Dashboard'))

    local_active_agency = AgencyModel.query(
        AgencyModel.local_status == True
    ).get()

    url_config = ConfigModel.query(
        ConfigModel.local_ref == local_active_agency.key
    ).get()

    url = ""+url_config.url_server+"/agency/get/"+token
    result = urlfetch.fetch(url)
    result = result.content
    result = json.loads(result)

    if result['status'] and result['status'] == 404:
        flash(result['message'], "danger")
    else:
        if result['agency']:
            old_data = AgencyModel.get_by_id(result['agency']['agency_id'])

            agency_new = None

            if not old_data:
                new_data = AgencyModel(id=result['agency']['agency_id'])
                new_data.name = result['agency']['agency_name']
                new_data.country = result['agency']['agency_country']
                new_data.phone = result['agency']['agency_phone']
                new_data.fax = result['agency']['agency_fax']
                new_data.address = result['agency']['agency_address']
                new_data.reduction = float(result['agency']['agency_reduction'])
                new_data.status = result['agency']['agency_status']
                new_data.is_achouka = result['agency']['agency_is_achouka']

                destination = DestinationModel.get_by_id(result['agency']['agency_destination'])
                new_data.destination = destination.key

                agency_new = new_data.put()
            else:
                flash("This token exist", "danger")

            if agency_new:
                conf = ConfigModel()
                conf.url_server = url_config.url_server
                conf.local_ref = agency_new
                conf.token_agency = token
                conf.put()

                flash("Agency added", "success")

    return redirect(url_for('Dashboard'))