Ejemplo n.º 1
0
def telmap_login():
    """
    Perform a login request to Telmap's servers
    Returns (server_id, session_id) tuple that can be used in subsequent calls and stores it in memcache
    """

    logging.info("Telmap login")
    c = Context({
        "app_name": "waybetter",
        "username": settings.TELMAP_API_USER,
        "password": settings.TELMAP_API_PASSWORD,
        "language_code": "he",
        "sec_language_code": "en"
    })

    t = get_template("telmap_login.xml")
    #    logging.info(unicode("requestXML="+(t.render(c))))

    res = fetch(TELMAP_XML_API_URL,
                method="POST",
                payload=str("requestXML=" + urlquote_plus(t.render(c))))
    cleaned_response = clean_telmap_response(res.content)
    login_response = minidom.parseString(cleaned_response)

    server_id = get_text_from_element(login_response, "serverId")
    session_id = get_text_from_element(login_response, "sessionId")

    if server_id and session_id:
        telmap_login_credentials = (server_id, session_id)
        memcache.set(TELMAP_LOGIN_CREDENTIALS, telmap_login_credentials)
        return telmap_login_credentials
    else:
        logging.error('Could not login to Telmap servers')
        #        logging.info(res.content)
        #        telmap_logout(session_id)
        return None
Ejemplo n.º 2
0
def telmap_login():
    """
    Perform a login request to Telmap's servers
    Returns (server_id, session_id) tuple that can be used in subsequent calls and stores it in memcache
    """

    logging.info("Telmap login")
    c = Context(
        {
            "app_name": "waybetter",
            "username": settings.TELMAP_API_USER,
            "password": settings.TELMAP_API_PASSWORD,
            "language_code": "he",
            "sec_language_code": "en",
        }
    )

    t = get_template("telmap_login.xml")
    #    logging.info(unicode("requestXML="+(t.render(c))))

    res = fetch(TELMAP_XML_API_URL, method="POST", payload=str("requestXML=" + urlquote_plus(t.render(c))))
    cleaned_response = clean_telmap_response(res.content)
    login_response = minidom.parseString(cleaned_response)

    server_id = get_text_from_element(login_response, "serverId")
    session_id = get_text_from_element(login_response, "sessionId")

    if server_id and session_id:
        telmap_login_credentials = (server_id, session_id)
        memcache.set(TELMAP_LOGIN_CREDENTIALS, telmap_login_credentials)
        return telmap_login_credentials
    else:
        logging.error("Could not login to Telmap servers")
        #        logging.info(res.content)
        #        telmap_logout(session_id)
        return None
Ejemplo n.º 3
0
def telmap_request(data, request_template):
    """
    Wrapper for Telmap requests.
    Handles session login
    """
    if memcache.get(TELMAP_LOGIN_CREDENTIALS):  # lazy login
        server_id, session_id = memcache.get(TELMAP_LOGIN_CREDENTIALS)
    else:
        server_id, session_id = telmap_login()

    data["server_id"] = server_id
    data["session_id"] = session_id
    response = telmap_fetch_results(data, request_template)
    if get_text_from_element(response, 'errorCode') == '1150':
        logging.info("Invalid session, login again")
        server_id, session_id = telmap_login()
        data["server_id"] = server_id
        data["session_id"] = session_id
        response = telmap_fetch_results(data, request_template)

    return response
Ejemplo n.º 4
0
def get_address_from_telmap_location(location):
    result = {}
    lat = get_text_from_element(location, "geoPoint", "latitude")
    result["lat"] = float(lat) if lat else lat
    lon = get_text_from_element(location, "geoPoint", "longitude")
    result["lon"] = float(lon) if lon else lon
    result["description"] = get_text_from_element(location, "description", "text")
    result["country"] = get_text_from_element(location, "countryCode", "code")
    result["city"] = get_text_from_element(location, "city", "text")
    result["street_address"] = get_text_from_element(location, "street", "text")
    result["house_number"] = get_text_from_element(location, "houseNumber")
    if result["house_number"]:
        result["name"] = "%s %s, %s" % (result["street_address"], result["house_number"], result["city"])
    else:
        result["name"] = result["description"]

    result["raw"] = result["name"]

    return result
Ejemplo n.º 5
0
def telmap_request(data, request_template):
    """
    Wrapper for Telmap requests.
    Handles session login
    """
    if memcache.get(TELMAP_LOGIN_CREDENTIALS):  # lazy login
        server_id, session_id = memcache.get(TELMAP_LOGIN_CREDENTIALS)
    else:
        server_id, session_id = telmap_login()

    data["server_id"] = server_id
    data["session_id"] = session_id
    response = telmap_fetch_results(data, request_template)
    if get_text_from_element(response, "errorCode") == "1150":
        logging.info("Invalid session, login again")
        server_id, session_id = telmap_login()
        data["server_id"] = server_id
        data["session_id"] = session_id
        response = telmap_fetch_results(data, request_template)

    return response
Ejemplo n.º 6
0
def get_address_from_telmap_location(location):
    result = {}
    lat = get_text_from_element(location, "geoPoint", "latitude")
    result["lat"] = float(lat) if lat else lat
    lon = get_text_from_element(location, "geoPoint", "longitude")
    result["lon"] = float(lon) if lon else lon
    result["description"] = get_text_from_element(location, "description",
                                                  "text")
    result["country"] = get_text_from_element(location, "countryCode", "code")
    result["city"] = get_text_from_element(location, "city", "text")
    result["street_address"] = get_text_from_element(location, "street",
                                                     "text")
    result["house_number"] = get_text_from_element(location, "houseNumber")
    if result["house_number"]:
        result["name"] = "%s %s, %s" % (result["street_address"],
                                        result["house_number"], result["city"])
    else:
        result["name"] = result["description"]

    result["raw"] = result["name"]

    return result