def test_get_geoip_data_for_x_forwarded_for(self): with self.app.test_request_context( environ_base={'HTTP_X_FORWARDED_FOR': '1.2.3.4'}): simple_geoip = SimpleGeoIP(self.app) data = simple_geoip.get_geoip_data() self.assertIsInstance(data, dict) self.assertEqual(data['ip'], '1.2.3.4')
def test_get_geoip_data_with_multiple_proxies(self): with self.app.test_request_context(environ_base={ 'HTTP_X_FORWARDED_FOR': '1.2.3.4, 1.2.3.5, 1.2.3.6, 1.2.3.7' }): simple_geoip = SimpleGeoIP(self.app) data = simple_geoip.get_geoip_data() self.assertIsInstance(data, dict) self.assertEqual(data['ip'], '1.2.3.4')
def test_get_geoip_data_with_malformed_x_forwarded_for(self): environ_base = { 'REMOTE_ADDR': '127.0.0.1', 'HTTP_X_FORWARDED_FOR': '<script> alert(1) </script' } with self.app.test_request_context(environ_base=environ_base): simple_geoip = SimpleGeoIP(self.app) data = simple_geoip.get_geoip_data() self.assertIsInstance(data, dict) self.assertEqual(data['ip'], request.remote_addr)
def get_forecast_data(): session['sequence'] = 0 seg = session.get('sequence') simple_geoip = SimpleGeoIP(current_app) geoip_data = simple_geoip.get_geoip_data() depart = 'Val-de-Marne' currentdate = datetime.today().strftime('%Y-%m-%d') last = datetime.strptime('2021-01-31', "%Y-%m-%d") d2 = datetime.strptime(datetime.today().strftime('%Y-%m-%d'), "%Y-%m-%d") days_current = abs((d2 - last).days) current_forecast = create_dates('2021-01-31', days_current) demain_forcast = create_dates('2021-01-31', (days_current + 1)) seven_forcats = create_dates('2021-01-31', (days_current + 7)) quinze_forcats = create_dates('2021-01-31', (days_current + 15)) tente_forcats = create_dates('2021-01-31', (days_current + 30)) for i in range(len(departments)): seven_days_svr = forecast_svr(x_test[i], days_current, model[i], 60, scaler[i]) current_forecast[departments[i]] = np.array(seven_days_svr) for i in range(len(departments)): seven_days_svr = forecast_svr(x_test[i], days_current + 1, model[i], 60, scaler[i]) demain_forcast[departments[i]] = np.array(seven_days_svr) for i in range(len(departments)): seven_days_svr = forecast_svr(x_test[i], days_current + 7, model[i], 60, scaler[i]) seven_forcats[departments[i]] = np.array(seven_days_svr) for i in range(len(departments)): seven_days_svr = forecast_svr(x_test[i], days_current + 15, model[i], 60, scaler[i]) quinze_forcats[departments[i]] = np.array(seven_days_svr) for i in range(len(departments)): seven_days_svr = forecast_svr(x_test[i], days_current + 30, model[i], 60, scaler[i]) tente_forcats[departments[i]] = np.array(seven_days_svr) tente_forcats = tente_forcats.tail(30) quinze_forcats = quinze_forcats.tail(15) seven_forcats = seven_forcats.tail(7) demain_forcast = demain_forcast.tail(1) current_forecast = current_forecast.tail(1) print(current_forecast) #print(seven_forcats.tail(7)) return { 'current': current_forecast.to_json(), 'tomorrow': demain_forcast.to_json(), 'seven_days': seven_forcats.to_json(), 'fifteen_days': quinze_forcats.to_json(), 'thirty_days': tente_forcats.to_json() }
def test_init_fails_with_no_config(self): api_key = environ.get("GEOIPIFY_API_KEY") if api_key: del environ["GEOIPIFY_API_KEY"] with self.assertRaises(Exception): SimpleGeoIP(app) if api_key: environ["GEOIPIFY_API_KEY"] = api_key
Also added a geolocation pulled from the user's IP. """ import json import os import requests from flask_simple_geoip import SimpleGeoIP from flask import Flask, render_template, jsonify app = Flask(__name__) OPENWEATHERMAP_API_KEY = os.environ.get('OPENWEATHERMAP_API_KEY') app.config["GEOIPIFY_API_KEY"] = "at_YSjTHe0GUeE7GfDmTWwGbZpw3u760" simple_geoip = SimpleGeoIP(app) nice_out_dict = { 'Yes': { 'value': 'Yes', 'text_color': '#f0eec9', 'background_color': '#7aed70' }, 'No': { 'value': 'No', 'text_color': '#6a6475', 'background_color': '#ff798b' } } def user_loc():
def test_get_geoip_data_for_custom_addr(self): with self.app.test_request_context(): simple_geoip = SimpleGeoIP(self.app) data = simple_geoip.get_geoip_data('8.8.8.8') self.assertIsInstance(data, dict) self.assertEqual(data['ip'], '8.8.8.8')
def test_get_geoip_data(self): with self.app.test_request_context(): simple_geoip = SimpleGeoIP(self.app) data = simple_geoip.get_geoip_data() self.assertIsInstance(data, dict)
def test_init_works_with_environment_variable_config(self): self.app.config["GEOIPIFY_API_KEY"] = "test" SimpleGeoIP(self.app) del self.app.config["GEOIPIFY_API_KEY"]