def create_app(settings=None): """ Create flask application """ app = Flask(__name__) app.secret_key = 'plante website' app.register_blueprint(simple_page) if settings: app.config.update(settings) DATA_BASE.init_app(app) api.init_app(app) compress = Compress() compress.init_app(app) configure_uploads(app, uploaded_image) handler = RotatingFileHandler('ueki.log', maxBytes=100000, backupCount=1) handler.setLevel(DEBUG) app.logger.addHandler(handler) logger = getLogger('werkzeug') logger.addHandler(handler) return app
return send_from_directory('static/swaggerui/', path) @app.route('/spec') def swagger_json(): """ Endpoint that returns the swagger api using JSON :return: """ with open('static/api.json', 'r') as f: return jsonify(flask.json.loads(f.read())) # return url_for('static', filename='api.json') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--dev', action='store_true') parser.add_argument('--debug', action='store_true') options = parser.parse_args() if options.dev: _json_pretty = True _gzip = False if _gzip: c = Compress() c.init_app(app) if options.debug: logger.setLevel(logging.DEBUG) app.run(debug=options.dev)
def extension_compress(app): compress = Compress() compress.init_app(app) return app
session_options=session_options, scopefunc=_app_ctx_stack.__ident_func__) @app.teardown_appcontext def shutdown_session(response_or_exc=None): if db.session: db.session.remove() return response_or_exc #################### # SETUP EXTENSIONS # #################### gzip = Compress(app) login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "account.login" ###################### # IMPORT BLUEPRINTS # ###################### from web.modules.site import bp as site_views from web.modules.search import bp as search_views from web.modules.account import bp as account_views from web.modules.business import bp as business_views from web.modules.promotion import bp as promotion_views from web.modules.sumo_voucher import bp as sumo_voucher_views
def build_app(url=None, reaper_on=True, app_root=None, additional_templates=None, **kwargs): """ Builds :class:`flask:flask.Flask` application encapsulating endpoints for D-Tale's front-end :param url: optional parameter which sets the host & root for any internal endpoints (ex: pinging shutdown) :type url: str, optional :param reaper_on: whether to run auto-reaper subprocess :type reaper_on: bool :param app_root: Optional path to prepend to the routes of D-Tale. This is used when making use of Jupyterhub server proxy :type app_root: str, optional :param additional_templates: path(s) to any other jinja templates you would like to load. This comes into play if you're embedding D-Tale into your own Flask app :type: str, list, optional :return: :class:`flask:flask.Flask` application :rtype: :class:`dtale.app.DtaleFlask` """ app = DtaleFlask( "dtale", reaper_on=reaper_on, static_url_path="/dtale/static", url=url, instance_relative_config=False, app_root=app_root, ) app.config["SECRET_KEY"] = "Dtale" app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True if app_root is not None: app.config["APPLICATION_ROOT"] = app_root app.jinja_env.globals["url_for"] = app.url_for app.jinja_env.globals["is_app_root_defined"] = is_app_root_defined if additional_templates: loaders = [app.jinja_loader] loaders += [ jinja2.FileSystemLoader(loc) for loc in make_list(additional_templates) ] my_loader = jinja2.ChoiceLoader(loaders) app.jinja_loader = my_loader app.register_blueprint(dtale) compress = Compress() compress.init_app(app) def _root(): return redirect("/dtale/{}".format(head_endpoint())) @app.route("/") def root(): return _root() @app.route("/dtale") def dtale_base(): """ :class:`flask:flask.Flask` routes which redirect to dtale/main :return: 302 - flask.redirect('/dtale/main') """ return _root() @app.route("/favicon.ico") def favicon(): """ :class:`flask:flask.Flask` routes which returns favicon :return: image/png """ return redirect(app.url_for("static", filename="images/favicon.ico")) @app.route("/missing-js") def missing_js(): missing_js_commands = ( ">> cd [location of your local dtale repo]\n" ">> yarn install\n" ">> yarn run build # or 'yarn run watch' if you're trying to develop" ) return render_template("dtale/errors/missing_js.html", missing_js_commands=missing_js_commands) @app.errorhandler(404) def page_not_found(e=None): """ :class:`flask:flask.Flask` routes which returns favicon :param e: exception :return: text/html with exception information """ return ( render_template( "dtale/errors/404.html", page="", error=e, stacktrace=str(traceback.format_exc()), ), 404, ) @app.errorhandler(500) def internal_server_error(e=None): """ :class:`flask:flask.Flask` route which returns favicon :param e: exception :return: text/html with exception information """ return ( render_template( "dtale/errors/500.html", page="", error=e, stacktrace=str(traceback.format_exc()), ), 500, ) def shutdown_server(): global ACTIVE_HOST, ACTIVE_PORT """ This function that checks if flask.request.environ['werkzeug.server.shutdown'] exists and if so, executes that function """ logger.info("Executing shutdown...") func = request.environ.get("werkzeug.server.shutdown") if func is None: raise RuntimeError("Not running with the Werkzeug Server") func() global_state.cleanup() ACTIVE_PORT = None ACTIVE_HOST = None @app.route("/shutdown") def shutdown(): """ :class:`flask:flask.Flask` route for initiating server shutdown :return: text/html with server shutdown message """ app.clear_reaper() shutdown_server() return "Server shutting down..." @app.before_request @auth.requires_auth def before_request(): """ Logic executed before each :attr:`flask:flask.request` :return: text/html with server shutdown message """ app.build_reaper() @app.route("/site-map") def site_map(): """ :class:`flask:flask.Flask` route listing all available flask endpoints :return: JSON of all flask enpoints [ [endpoint1, function path1], ..., [endpointN, function pathN] ] """ def has_no_empty_params(rule): defaults = rule.defaults or () arguments = rule.arguments or () return len(defaults) >= len(arguments) links = [] for rule in app.url_map.iter_rules(): # Filter out rules we can't navigate to in a browser # and rules that require parameters if "GET" in rule.methods and has_no_empty_params(rule): url = app.url_for(rule.endpoint, **(rule.defaults or {})) links.append((url, rule.endpoint)) return jsonify(links) @app.route("/version-info") def version_info(): """ :class:`flask:flask.Flask` route for retrieving version information about D-Tale :return: text/html version information """ _, version = retrieve_meta_info_and_version("dtale") return str(version) @app.route("/health") def health_check(): """ :class:`flask:flask.Flask` route for checking if D-Tale is up and running :return: text/html 'ok' """ return "ok" auth.setup_auth(app) with app.app_context(): from .dash_application import views as dash_views app = dash_views.add_dash(app) return app
def create_app(): app = Flask(__name__, template_folder='../templates', static_folder='../static') app.json_encoder = CustomJSONEncoder cache = Cache(app, config={'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 0}) cache_buster = CacheBuster(config={'extensions': ['.js', '.css']}) cache_buster.init_app(app) Compress(app) if args.cors: CORS(app) db_uri = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format( args.db_user, args.db_pass, args.db_host, args.db_port, args.db_name) app.config['SQLALCHEMY_DATABASE_URI'] = db_uri app.config['SQLALCHEMY_ENGINE_OPTIONS'] = { 'pool_size' : 0 # No limit. } app.config['SQLALCHEMY_POOL_RECYCLE'] = args.db_pool_recycle app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) if args.client_auth: app.config['SESSION_TYPE'] = 'redis' r = redis.Redis(args.redis_host, args.redis_port) app.config['SESSION_REDIS'] = r app.config['SESSION_USE_SIGNER'] = True app.secret_key = args.secret_key Session(app) if args.basic_auth: accepted_auth_types.append('basic') for config in args.basic_auth_access_configs: name = config.split(':')[1] if name not in valid_access_configs: valid_access_configs.append(name) if args.discord_auth: accepted_auth_types.append('discord') for config in args.discord_access_configs: length = len(config.split(':')) name = (config.split(':')[2] if length == 3 else config.split(':')[1]) if name not in valid_access_configs: valid_access_configs.append(name) if args.telegram_auth: accepted_auth_types.append('telegram') for config in args.telegram_access_configs: name = config.split(':')[1] if name not in valid_access_configs: valid_access_configs.append(name) if not args.disable_blacklist: log.info('Retrieving blacklist...') ip_blacklist = get_ip_blacklist() # Sort & index for binary search ip_blacklist.sort(key=lambda r: r[0]) ip_blacklist_keys = [ dottedQuadToNum(r[0]) for r in ip_blacklist ] else: log.info('Blacklist disabled for this session.') @app.before_request def validate_request(): # Get real IP behind trusted reverse proxy. ip_addr = request.remote_addr if ip_addr in args.trusted_proxies: ip_addr = request.headers.get('X-Forwarded-For', ip_addr) # Make sure IP isn't blacklisted. if ip_is_blacklisted(ip_addr): log.debug('Denied access to %s: blacklisted IP.', ip_addr) abort(403) if args.client_auth: session['ip'] = ip_addr session['last_active'] = datetime.utcnow() @app.route('/') @auth_required def map_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) settings = { 'centerLat': user_args.center_lat, 'centerLng': user_args.center_lng, 'maxZoomLevel': user_args.max_zoom_level, 'showAllZoomLevel': user_args.show_all_zoom_level, 'clusterZoomLevel': user_args.cluster_zoom_level, 'clusterZoomLevelMobile': user_args.cluster_zoom_level_mobile, 'maxClusterRadius': user_args.max_cluster_radius, 'spiderfyClusters': user_args.spiderfy_clusters, 'removeMarkersOutsideViewport': ( not user_args.markers_outside_viewport), 'geocoder': not user_args.no_geocoder, 'isStartMarkerMovable': not user_args.lock_start_marker, 'generateImages': user_args.generate_images, 'statsSidebar': not user_args.no_stats_sidebar, 'twelveHourClock': user_args.twelve_hour_clock, 'mapUpdateInverval': user_args.map_update_interval, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always, 'pokemons': not user_args.no_pokemon, 'upscaledPokemon': ( [int(i) for i in user_args.upscaled_pokemon.split(',')] if user_args.upscaled_pokemon is not None else []), 'pokemonValues': (not user_args.no_pokemon and not user_args.no_pokemon_values), 'catchRates': user_args.catch_rates, 'rarity': (not user_args.no_pokemon and user_args.rarity and user_args.rarity_update_frequency), 'rarityFileName': user_args.rarity_filename, 'pokemonCries': (not user_args.no_pokemon and user_args.pokemon_cries), 'gyms': not user_args.no_gyms, 'gymSidebar': ((not user_args.no_gyms or not user_args.no_raids) and not user_args.no_gym_sidebar), 'gymFilters': (not user_args.no_gyms and not user_args.no_gym_filters), 'raids': not user_args.no_raids, 'raidFilters': (not user_args.no_raids and not user_args.no_raid_filters), 'pokestops': not user_args.no_pokestops, 'quests': not user_args.no_pokestops and not user_args.no_quests, 'invasions': (not user_args.no_pokestops and not user_args.no_invasions), 'lures': not user_args.no_pokestops and not user_args.no_lures, 'weather': not user_args.no_weather, 'spawnpoints': not user_args.no_spawnpoints, 'scannedLocs': not user_args.no_scanned_locs, 's2Cells': not user_args.no_s2_cells, 'ranges': not user_args.no_ranges, 'nestParks': user_args.nest_parks, 'nestParksFileName': user_args.nest_parks_filename, 'exParks': user_args.ex_parks, 'exParksFileName': user_args.ex_parks_filename } return render_template( 'map.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, pokemon_history_page=(settings['pokemons'] and not user_args.no_pokemon_history_page), quest_page=settings['quests'] and not user_args.no_quest_page, analytics_id=user_args.analytics_id, settings=settings, i18n=i8ln ) @app.route('/pokemon-history') @auth_required def pokemon_history_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) if user_args.no_pokemon or user_args.no_pokemon_history_page: if args.client_auth: if is_logged_in(): abort(403) else: return redirect(url_for('login_page')) else: abort(404) settings = { 'centerLat': user_args.center_lat, 'centerLng': user_args.center_lng, 'generateImages': user_args.generate_images, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template( 'pokemon-history.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, quest_page=(not user_args.no_pokestops and not user_args.no_quests and not user_args.no_quest_page), analytics_id=user_args.analytics_id, settings=settings ) @app.route('/quests') @auth_required def quest_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) if (user_args.no_pokestops or user_args.no_quests or user_args.no_quest_page): if args.client_auth: if is_logged_in(): abort(403) else: return redirect(url_for('login_page')) else: abort(404) settings = { 'generateImages': user_args.generate_images, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template( 'quest.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, pokemon_history_page=(not user_args.no_pokemon and not user_args.no_pokemon_history_page), analytics_id=user_args.analytics_id, settings=settings ) @app.route('/mobile') @auth_required def mobile_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) # todo: Check if client is Android/iOS/Desktop for geolink, currently # only supports Android. pokemon_list = [] settings = { 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } # Allow client to specify location. lat = request.args.get('lat', user_args.center_lat, type=float) lon = request.args.get('lon', user_args.center_lng, type=float) origin_point = LatLng.from_degrees(lat, lon) for pokemon in convert_pokemon_list( Pokemon.get_active(None, None, None, None)): pokemon_point = LatLng.from_degrees(pokemon['latitude'], pokemon['longitude']) diff = pokemon_point - origin_point diff_lat = diff.lat().degrees diff_lng = diff.lng().degrees direction = (('N' if diff_lat >= 0 else 'S') if abs(diff_lat) > 1e-4 else '') +\ (('E' if diff_lng >= 0 else 'W') if abs(diff_lng) > 1e-4 else '') entry = { 'id': pokemon['pokemon_id'], 'name': get_pokemon_name(pokemon['pokemon_id']), 'card_dir': direction, 'distance': int(origin_point.get_distance( pokemon_point).radians * 6366468.241830914), 'time_to_disappear': '%d min %d sec' % (divmod( (pokemon['disappear_time'] - datetime.utcnow()).seconds, 60)), 'disappear_time': pokemon['disappear_time'], 'disappear_sec': ( pokemon['disappear_time'] - datetime.utcnow()).seconds, 'latitude': pokemon['latitude'], 'longitude': pokemon['longitude'] } pokemon_list.append((entry, entry['distance'])) pokemon_list = [y[0] for y in sorted(pokemon_list, key=lambda x: x[1])] return render_template( 'mobile.html', version=version, custom_favicon=user_args.custom_favicon, pokemon_list=pokemon_list, origin_lat=lat, origin_lng=lon, analytics_id=user_args.analytics_id, settings=settings ) @app.route('/login') def login_page(): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'login.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, analytics_id=args.analytics_id, basic_auth=args.basic_auth, discord_auth=args.discord_auth, telegram_auth=args.telegram_auth, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), settings=settings ) @app.route('/login/<auth_type>') def login(auth_type): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) if auth_type not in accepted_auth_types: abort(404) authenticator = auth_factory.get_authenticator(auth_type) auth_uri = authenticator.get_authorization_url() return redirect(auth_uri) @app.route('/login/basic') def basic_login_page(): if not args.basic_auth: abort(404) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'basic-login.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), analytics_id=args.analytics_id, settings=settings ) @app.route('/login/telegram') def telegram_login_page(): if not args.telegram_auth: abort(404) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'telegram.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), analytics_id=args.analytics_id, telegram_bot_username=args.telegram_bot_username, server_uri=args.server_uri, settings=settings ) @app.route('/auth/<auth_type>') def auth(auth_type): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) if auth_type not in accepted_auth_types: abort(404) success = auth_factory.get_authenticator(auth_type).authorize() if not success: if auth_type == 'basic': return redirect(url_for('basic_login_page') + '?success=false') elif auth_type == 'discord': return redirect(url_for('login_page')) elif auth_type == 'telegram': return redirect(url_for('telegram_login_page')) if args.no_multiple_logins: r = app.config['SESSION_REDIS'] sessions = get_sessions(r) for s in sessions: if ('auth_type' in s and s['auth_type'] == session['auth_type'] and s['id'] == session['id']): r.delete('session:' + s['session_id']) return redirect(url_for('map_page')) @app.route('/logout') def logout(): if not args.client_auth: abort(404) if is_logged_in(): if session['auth_type'] in accepted_auth_types: a = auth_factory.get_authenticator(session['auth_type']) a.end_session() else: session.clear() return redirect(url_for('map_page')) @app.route('/admin') def admin_page(): return redirect(url_for('users_page')) @app.route('/admin/users') @auth_required def users_page(*_args, **kwargs): if not args.client_auth: abort(404) if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) if not is_admin(): abort(403) user_args = get_args(kwargs['access_config']) settings = { 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template( 'users.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, analytics_id=user_args.analytics_id, pokemon_history_page=(not user_args.no_pokemon and not user_args.no_pokemon_history_page), quest_page=(not user_args.no_pokestops and not user_args.no_quests and not user_args.no_quest_page), settings=settings ) @app.route('/raw-data') @auth_required def raw_data(*_args, **kwargs): if not kwargs['has_permission']: abort(401) user_args = get_args(kwargs['access_config']) # Make sure fingerprint isn't blacklisted. fingerprint_blacklisted = any([ fingerprints['no_referrer'](request), fingerprints['iPokeGo'](request) ]) if fingerprint_blacklisted: log.debug('User denied access: blacklisted fingerprint.') abort(403) d = {} # Request time of this request. d['timestamp'] = datetime.utcnow() # Request time of previous request. if request.args.get('timestamp'): timestamp = int(request.args.get('timestamp')) timestamp -= 1000 # Overlap, for rounding errors. else: timestamp = 0 swLat = request.args.get('swLat') swLng = request.args.get('swLng') neLat = request.args.get('neLat') neLng = request.args.get('neLng') oSwLat = request.args.get('oSwLat') oSwLng = request.args.get('oSwLng') oNeLat = request.args.get('oNeLat') oNeLng = request.args.get('oNeLng') # Previous switch settings. lastpokemon = request.args.get('lastpokemon') lastgyms = request.args.get('lastgyms') lastpokestops = request.args.get('lastpokestops') lastspawns = request.args.get('lastspawns') lastscannedlocs = request.args.get('lastscannedlocs') lastweather = request.args.get('lastweather') # Current switch settings saved for next request. if request.args.get('pokemon', 'true') == 'true': d['lastpokemon'] = True if (request.args.get('gyms', 'true') == 'true' or request.args.get('raids', 'true') == 'true'): d['lastgyms'] = True if (request.args.get('pokestops', 'true') == 'true' and ( request.args.get('pokestopsNoEvent', 'true') == 'true' or request.args.get('quests', 'true') == 'true' or request.args.get('invasions', 'true') == 'true' or request.args.get('lures', 'true') == 'true')): d['lastpokestops'] = True if request.args.get('spawnpoints', 'false') == 'true': d['lastspawns'] = True if request.args.get('scannedLocs', 'false') == 'true': d['lastscannedlocs'] = True if request.args.get('weather', 'false') == 'true': d['lastweather'] = True if (oSwLat is not None and oSwLng is not None and oNeLat is not None and oNeLng is not None): # If old coords are not equal to current coords we have # moved/zoomed! if (oSwLng < swLng and oSwLat < swLat and oNeLat > neLat and oNeLng > neLng): newArea = False # We zoomed in no new area uncovered. elif not (oSwLat == swLat and oSwLng == swLng and oNeLat == neLat and oNeLng == neLng): newArea = True else: newArea = False # Pass current coords as old coords. d['oSwLat'] = swLat d['oSwLng'] = swLng d['oNeLat'] = neLat d['oNeLng'] = neLng if (request.args.get('pokemon', 'true') == 'true' and not user_args.no_pokemon): verified_despawn = user_args.verified_despawn_time eids = None ids = None if (request.args.get('eids') and request.args.get('prionotif', 'false') == 'false'): request_eids = request.args.get('eids').split(',') eids = [int(i) for i in request_eids] elif not request.args.get('eids') and request.args.get('ids'): request_ids = request.args.get('ids').split(',') ids = [int(i) for i in request_ids] if lastpokemon != 'true': # If this is first request since switch on, load # all pokemon on screen. d['pokemons'] = convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, eids=eids, ids=ids, verified_despawn_time=verified_despawn)) else: # If map is already populated only request modified Pokemon # since last request time. d['pokemons'] = convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, timestamp=timestamp, eids=eids, ids=ids, verified_despawn_time=verified_despawn)) if newArea: # If screen is moved add newly uncovered Pokemon to the # ones that were modified since last request time. d['pokemons'] += ( convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, eids=eids, ids=ids, verified_despawn_time=verified_despawn))) if request.args.get('reids'): request_reids = request.args.get('reids').split(',') reids = [int(x) for x in request_reids] d['pokemons'] += convert_pokemon_list( Pokemon.get_active(swLat, swLng, neLat, neLng, ids=ids, verified_despawn_time=verified_despawn)) d['reids'] = reids if request.args.get('seen', 'false') == 'true': d['seen'] = Pokemon.get_seen(int(request.args.get('duration'))) if request.args.get('appearances', 'false') == 'true': d['appearances'] = Pokemon.get_appearances( request.args.get('pokemonid'), request.args.get('formid'), int(request.args.get('duration'))) if request.args.get('appearancesDetails', 'false') == 'true': d['appearancesTimes'] = ( Pokemon.get_appearances_times_by_spawnpoint( request.args.get('pokemonid'), request.args.get('spawnpoint_id'), request.args.get('formid'), int(request.args.get('duration')))) gyms = (request.args.get('gyms', 'true') == 'true' and not user_args.no_gyms) raids = (request.args.get('raids', 'true') == 'true' and not user_args.no_raids) if gyms or raids: if lastgyms != 'true': d['gyms'] = Gym.get_gyms(swLat, swLng, neLat, neLng, raids=raids) else: d['gyms'] = Gym.get_gyms(swLat, swLng, neLat, neLng, timestamp=timestamp, raids=raids) if newArea: d['gyms'].update( Gym.get_gyms(swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, raids=raids)) pokestops = (request.args.get('pokestops', 'true') == 'true' and not user_args.no_pokestops) pokestopsNoEvent = (request.args.get( 'pokestopsNoEvent', 'true') == 'true') quests = (request.args.get('quests', 'true') == 'true' and not user_args.no_quests) invasions = (request.args.get('invasions', 'true') == 'true' and not user_args.no_invasions) lures = (request.args.get('lures', 'true') == 'true' and not user_args.no_lures) if (pokestops and (pokestopsNoEvent or quests or invasions or lures)): if lastpokestops != 'true': d['pokestops'] = Pokestop.get_pokestops( swLat, swLng, neLat, neLng, eventless_stops=pokestopsNoEvent, quests=quests, invasions=invasions, lures=lures ) else: d['pokestops'] = Pokestop.get_pokestops( swLat, swLng, neLat, neLng, timestamp=timestamp, eventless_stops=pokestopsNoEvent, quests=quests, invasions=invasions, lures=lures ) if newArea: d['pokestops'].update(Pokestop.get_pokestops( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, eventless_stops=pokestopsNoEvent, quests=quests, invasions=invasions, lures=lures )) if (request.args.get('weather', 'false') == 'true' and not user_args.no_weather): if lastweather != 'true': d['weather'] = Weather.get_weather(swLat, swLng, neLat, neLng) else: d['weather'] = Weather.get_weather(swLat, swLng, neLat, neLng, timestamp=timestamp) if newArea: d['weather'] += Weather.get_weather( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng) if (request.args.get('spawnpoints', 'false') == 'true' and not user_args.no_spawnpoints): if lastspawns != 'true': d['spawnpoints'] = TrsSpawn.get_spawnpoints( swLat=swLat, swLng=swLng, neLat=neLat, neLng=neLng) else: d['spawnpoints'] = TrsSpawn.get_spawnpoints( swLat=swLat, swLng=swLng, neLat=neLat, neLng=neLng, timestamp=timestamp) if newArea: d['spawnpoints'] += TrsSpawn.get_spawnpoints( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng) if (request.args.get('scannedLocs', 'false') == 'true' and not user_args.no_scanned_locs): if lastscannedlocs != 'true': d['scannedlocs'] = ScannedLocation.get_recent( swLat, swLng, neLat, neLng) else: d['scannedlocs'] = ScannedLocation.get_recent( swLat, swLng, neLat, neLng, timestamp=timestamp) if newArea: d['scannedlocs'] += ScannedLocation.get_recent( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng) return jsonify(d) @app.route('/raw-data/users') def users_data(): if not args.client_auth: abort(404) # Make sure fingerprint isn't blacklisted. fingerprint_blacklisted = any([ fingerprints['no_referrer'](request), fingerprints['iPokeGo'](request) ]) if fingerprint_blacklisted: log.debug('User denied access: blacklisted fingerprint.') abort(403) if not is_admin(): abort(403) sessions = get_sessions(app.config['SESSION_REDIS']) users = [] for s in sessions: if 'auth_type' in s: del s['_permanent'] users.append(s) return jsonify(users) @app.route('/pkm_img') def pokemon_img(): raw = 'raw' in request.args pkm = int(request.args.get('pkm')) weather = int( request.args.get('weather')) if 'weather' in request.args else 0 gender = int( request.args.get('gender')) if 'gender' in request.args else None form = int( request.args.get('form')) if 'form' in request.args else None costume = int( request.args.get('costume')) if 'costume' in request.args else None shiny = 'shiny' in request.args if raw: filename = get_pokemon_raw_icon( pkm, gender=gender, form=form, costume=costume, weather=weather, shiny=shiny) else: filename = get_pokemon_map_icon( pkm, weather=weather, gender=gender, form=form, costume=costume) return send_file(filename, mimetype='image/png') @app.route('/gym_img') def gym_img(): team = request.args.get('team') level = request.args.get('level') raidlevel = request.args.get('raidlevel') pkm = request.args.get('pkm') form = request.args.get('form') costume = int( request.args.get('costume')) if 'costume' in request.args else None is_in_battle = 'in_battle' in request.args is_ex_raid_eligible = 'is_ex_raid_eligible' in request.args if level is None or raidlevel is None: return send_file( get_gym_icon(team, level, raidlevel, pkm, is_in_battle, form, costume, is_ex_raid_eligible), mimetype='image/png' ) elif (int(level) < 0 or int(level) > 6 or int(raidlevel) < 0 or int(raidlevel) > 5): return abort(416) else: return send_file( get_gym_icon(team, level, raidlevel, pkm, is_in_battle, form, costume, is_ex_raid_eligible), mimetype='image/png' ) @app.route('/robots.txt') def render_robots_txt(): return render_template('robots.txt') @app.route('/serviceWorker.min.js') def render_service_worker_js(): return send_from_directory('../static/dist/js', 'serviceWorker.min.js') return app
def _conf_compress(app): from flask_compress import Compress Compress(app)
#Initializing Flask app = Flask(__name__) app.config.from_object(server_config) app.wsgi_app = StreamConsumingMiddleware(app.wsgi_app) app.logger.handlers = [] app.logger.addHandler(create_logging_handler(app.config)) app.logger.addHandler(create_console_logger_handler()) app.userstorage = UserStorage() load_users(app.userstorage) #Enables GZIP compression compressor = Compress() compressor.init_app(app) #Expose markdown trough application object app.markdown = Markdown() app.storage = RecipeStorage(directory=app.config['RECIPE_DIRECTORY'], backup=True, logger=app.logger.info) #Jinja Context Processor @app.context_processor def inject_template_variables(): return dict(base_path=app.config['BASE_PATH'], upload_directory=app.config['UPLOAD_DIRECTORY']) @app.context_processor
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from gevent import monkey monkey.patch_all() import pymongo from flask import Flask, json, render_template # Make things as simple as possible, but not simpler. from gevent.pywsgi import WSGIServer from flask_compress import Compress app = Flask(__name__) compressor = Compress(app) conn = pymongo.MongoClient("127.0.0.1", 27017) db = conn.aqir # 连接库 @app.route('/') def choose_name(): return render_template('main.html') @app.route("/last", methods=["GET"]) def last(): d = list( db.log.find({}, { '_id': 0 }).sort('_id', pymongo.DESCENDING).limit(1000))
def setUp(self): self.app = Flask(__name__) self.app.testing = True Compress(self.app)
def test_delayed_init(self): compress = Compress() compress.init_app(self.app)
def test_constructor_init(self): Compress(self.app)
def test_default_wildcard_quality_is_0(self): """ Tests that a wildcard has a default q-factor of 0.0 """ accept_encoding = 'br;q=0.001, *' self.app.config['COMPRESS_ALGORITHM'] = ['gzip', 'br', 'deflate'] c = Compress(self.app) self.assertEqual(c._choose_compress_algorithm(accept_encoding), 'br')
def test_multiple_algos_with_different_quality(self): """ Tests requesting multiple supported compression algorithms with different q-factors """ accept_encoding = 'zstd;q=0.8, br;q=0.9, gzip;q=0.5' self.app.config['COMPRESS_ALGORITHM'] = ['zstd', 'br', 'gzip'] c = Compress(self.app) self.assertEqual(c._choose_compress_algorithm(accept_encoding), 'br')
def test_multiple_algos_unsupported(self): """ Tests requesting multiple unsupported compression algorithms """ accept_encoding = 'future-algo, alien-algo, forbidden-algo' self.app.config['COMPRESS_ALGORITHM'] = ['zstd', 'br', 'gzip'] c = Compress(self.app) self.assertIsNone(c._choose_compress_algorithm(accept_encoding))
def server_start(): list_plans = [] port = 8000 lock_plans = threading.Lock() compress = Compress() app = Flask(__name__,template_folder=workdir+'/'+'webres',static_url_path='/static',static_folder=workdir+'/webres/static') app.config['COMPRESS_MIN_SIZE'] = 0 app.config['COMPRESS_LEVEL'] = 6 app.config['COMPRESS_MIMETYPES'] = ['text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript', 'application/octet-stream', 'image/svg+xml'] compress.init_app(app) @app.after_request def add_header(response): if response.headers['Content-Type'] == "image/png": response.headers['Cache-Control'] = 'must-revalidate, public, max-age=86400' else: response.headers['Cache-Control'] = 'must-revalidate, public, max-age=-1' return response @app.route('/_remove_plan') def remove_plan(): location = (request.args.get('lat', type=float), request.args.get('lng', type=float)) cid = CellId.from_lat_lng(LatLng.from_degrees(location[0],location[1])).parent(mapl.lvl_big) token = cid.to_token() lock_plans.acquire() if token in list_plans: list_plans.pop(list_plans.index(token)) lock_plans.release() return jsonify("") @app.route('/_write_plans') def writeplans(): subplans = request.args.get('subplans', type=int) plans = [] lock_plans.acquire() for token in list_plans: center = LatLng.from_point(Cell(CellId.from_token(token)).get_center()) center = (center.lat().degrees, center.lng().degrees) for ind_sub in range(1,subplans+1): plans.append({'type': 'seikur0_s2', 'token': token, 'location': [center[0],center[1]], 'subplans': subplans, 'subplan_index': ind_sub}) lock_plans.release() for plan in plans: filename = '{}_{}_{}.plan'.format(plan['token'],plan['subplan_index'],plan['subplans']) try: f = open(plandir+'/'+filename, 'w', 0) json.dump(plan, f, indent=1, separators=(',', ': ')) print('[+] Plan file {} was written.'.format(filename)) except Exception as e: print('[+] Error while writing plan file, error : {}'.format(e)) finally: if 'f' in vars() and not f.closed: f.close() return jsonify("") @app.route('/_add_regionplans') def regionplans(): lat_f,lat_t,lng_f,lng_t = request.args.get('lat_f', type=float),request.args.get('lat_t', type=float),request.args.get('lng_f', type=float),request.args.get('lng_t', type=float) locations = mapl.cover_region_s2((lat_f,lng_f),(lat_t,lng_t)) return jsonify(locations) @app.route('/_add_plan') def add_plan(): location = (request.args.get('lat', type=float),request.args.get('lng', type=float)) all_loc,border,cid = mapl.get_area_cell(location,True) # grid = mapl.Hexgrid() # all_loc = grid.cover_cell(cid) center = LatLng.from_point(Cell(cid).get_center()) center = (center.lat().degrees, center.lng().degrees) token = cid.to_token() lock_plans.acquire() list_plans.append(token) lock_plans.release() return jsonify((all_loc, border,[center,token],[])) @app.route("/_main") def mainfunc(): grid = mapl.Hexgrid() locations = grid.cover_region((0.1, -0.1), (-0.1, 0.1)) # even: 53.0894833975485 return jsonify(locations) @app.route("/") def mainapp(): return render_template('spawn-view.html') while True: try: app.run(host='127.0.0.1', port=port, threaded=True) except socket.error as e: if e.errno == 10048: print('[-] Error: The specified port {} is already in use.'.format(port)) break
def register_extensions(app): """ register extensions to the app """ app.jinja_env.add_extension('jinja2.ext.do') # Global values in jinja # Uncomment to enable profiler # See scripts/profile_analyzer.py to analyze output # app = setup_profiler(app) # Compress app responses with gzip compress = Compress() compress.init_app(app) # Influx db time-series database db.init_app(app) influx_db.init_app(app) # Limit authentication blueprint requests to 200 per minute limiter = Limiter(app, key_func=get_ip_address) limiter.limit("200/minute")(routes_authentication.blueprint) # Language translations babel = Babel(app) @babel.localeselector def get_locale(): try: user = User.query.filter( User.id == flask_login.current_user.id).first() if user and user.language != '': for key in LANGUAGES: if key == user.language: return key # Bypass endpoint test error "'AnonymousUserMixin' object has no attribute 'id'" except AttributeError: pass return request.accept_languages.best_match(LANGUAGES.keys()) # User login management login_manager = flask_login.LoginManager() login_manager.init_app(app) @login_manager.user_loader def user_loader(user_id): user = User.query.filter(User.id == user_id).first() if not user: return return user @login_manager.unauthorized_handler def unauthorized(): flash(gettext('Please log in to access this page'), "error") return redirect(url_for('routes_authentication.do_login')) # Create and populate database if it doesn't exist with app.app_context(): db.create_all() populate_db() # This is disabled because there's a bug that messes up user databases # The upgrade script will execute alembic to upgrade the database # alembic_upgrade_db() # Check user option to force all web connections to use SSL # Fail if the URI is empty (pytest is running) if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': with session_scope(app.config['SQLALCHEMY_DATABASE_URI']) as new_session: misc = new_session.query(Misc).first() if misc and misc.force_https: SSLify(app)
if jsonResponse is None: jsonResponse = get_live_albums_for_artist( artistID) # Function in live_albums.py responseCache.set(artistID, jsonResponse) # Return with JSON format and 200 status return Response(jsonResponse, status=200, mimetype='application/json') # Special case for when the query is empty @app.route('/search/') def emptyQuery(): js = json_dump({'success': False, 'error': 'empty-query'}) return Response(js, status=200, mimetype='application/json') # Set up connection with Spotify API # Called with AJAX on page load since auth token expires @app.route('/auth') def auth(): global authorized authorized = create_spotify_client() # Function in live_albums.py if authorized: return Response({}, status=200, mimetype='application/json') else: return Response({}, status=500, mimetype='application/json') # Run the server if __name__ == "__main__": Compress().init_app(app) app.run()
def test_one_algo_unsupported(self): """ Tests requesting single unsupported compression algorithm """ accept_encoding = 'some-alien-algorithm' self.app.config['COMPRESS_ALGORITHM'] = ['br', 'gzip'] c = Compress(self.app) self.assertIsNone(c._choose_compress_algorithm(accept_encoding))
default=default or sort_choices[0], help='Sort by attribute', ) return pagination api_app = Flask(__name__, template_folder=os.path.join(__path__[0], 'templates')) api_app.config['REMEMBER_COOKIE_NAME'] = 'flexget.token' api_app.config['DEBUG'] = True api_app.config['ERROR_404_HELP'] = False api_app.url_map.strict_slashes = False CORS(api_app, expose_headers='Link, Total-Count, Count, ETag') Compress(api_app) api = API( api_app, title='Flexget API v{}'.format(__version__), version=__version__, description= 'View and manage flexget core operations and plugins. Open each endpoint view for usage information.' ' Navigate to http://flexget.com/API for more details.', format_checker=format_checker, ) base_message: "MessageDict" = { 'type': 'object', 'properties': { 'status_code': {
from flask import Flask from flask.ext.migrate import Migrate import os, sys from flask_cors import CORS from flask_compress import Compress sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) application = Flask(__name__) application.config.from_object('settings') CORS(application) Compress(application) from general.api import * if __name__ == "__main__": application.debug = application.config['DEBUG'] application.run(host='0.0.0.0')
def create_app(app_name=None): # Configuration settings import config if not app_name: app_name = config.APP_NAME # Check if app is created for CLI operations or Web cli_mode = False if app_name.endswith('-cli'): cli_mode = True # Only enable password related functionality in server mode. if config.SERVER_MODE is True: # Some times we need to access these config params where application # context is not available (we can't use current_app.config in those # cases even with current_app.app_context()) # So update these params in config itself. # And also these updated config values will picked up by application # since we are updating config before the application instance is # created. config.SECURITY_RECOVERABLE = True config.SECURITY_CHANGEABLE = True # Now we'll open change password page in alertify dialog # we don't want it to redirect to main page after password # change operation so we will open the same password change page again. config.SECURITY_POST_CHANGE_VIEW = 'browser.change_password' """Create the Flask application, startup logging and dynamically load additional modules (blueprints) that are found in this directory.""" app = PgAdmin(__name__, static_url_path='/static') # Removes unwanted whitespace from render_template function app.jinja_env.trim_blocks = True app.config.from_object(config) app.config.update(dict(PROPAGATE_EXCEPTIONS=True)) ########################################################################## # Setup logging and log the application startup ########################################################################## # We won't care about errors in the logging system, we are more # interested in application errors. logging.raiseExceptions = False # Add SQL level logging, and set the base logging level logging.addLevelName(25, 'SQL') app.logger.setLevel(logging.DEBUG) app.logger.handlers = [] # We also need to update the handler on the webserver in order to see # request. Setting the level prevents werkzeug from setting up it's own # stream handler thus ensuring all the logging goes through the pgAdmin # logger. logger = logging.getLogger('werkzeug') logger.setLevel(config.CONSOLE_LOG_LEVEL) # Set SQLITE_PATH to TEST_SQLITE_PATH while running test cases if ( 'PGADMIN_TESTING_MODE' in os.environ and os.environ['PGADMIN_TESTING_MODE'] == '1' ): config.SQLITE_PATH = config.TEST_SQLITE_PATH config.MASTER_PASSWORD_REQUIRED = False config.UPGRADE_CHECK_ENABLED = False if not cli_mode: # Ensure the various working directories exist from pgadmin.setup import create_app_data_directory create_app_data_directory(config) # File logging fh = logging.FileHandler(config.LOG_FILE, encoding='utf-8') fh.setLevel(config.FILE_LOG_LEVEL) fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT)) app.logger.addHandler(fh) logger.addHandler(fh) # Console logging ch = logging.StreamHandler() ch.setLevel(config.CONSOLE_LOG_LEVEL) ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT)) app.logger.addHandler(ch) logger.addHandler(ch) # Log the startup app.logger.info('########################################################') app.logger.info('Starting %s v%s...', config.APP_NAME, config.APP_VERSION) app.logger.info('########################################################') app.logger.debug("Python syspath: %s", sys.path) ########################################################################## # Setup i18n ########################################################################## # Initialise i18n babel = Babel(app) app.logger.debug('Available translations: %s' % babel.list_translations()) @babel.localeselector def get_locale(): """Get the language for the user.""" language = 'en' if config.SERVER_MODE is False: # Get the user language preference from the miscellaneous module if current_user.is_authenticated: user_id = current_user.id else: user = user_datastore.get_user(config.DESKTOP_USER) if user is not None: user_id = user.id user_language = Preferences.raw_value( 'misc', 'user_language', 'user_language', user_id ) if user_language is not None: language = user_language else: # If language is available in get request then return the same # otherwise check the session or cookie data = request.form if 'language' in data: language = data['language'] or language setattr(session, 'PGADMIN_LANGUAGE', language) elif hasattr(session, 'PGADMIN_LANGUAGE'): language = getattr(session, 'PGADMIN_LANGUAGE', language) elif hasattr(request.cookies, 'PGADMIN_LANGUAGE'): language = getattr( request.cookies, 'PGADMIN_LANGUAGE', language ) return language ########################################################################## # Setup authentication ########################################################################## app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{0}?timeout={1}' \ .format(config.SQLITE_PATH.replace('\\', '/'), getattr(config, 'SQLITE_TIMEOUT', 500) ) # Override USER_DOES_NOT_EXIST and INVALID_PASSWORD messages from flask. app.config['SECURITY_MSG_USER_DOES_NOT_EXIST'] = \ app.config['SECURITY_MSG_INVALID_PASSWORD'] = \ (gettext("Incorrect username or password."), "error") # Create database connection object and mailer db.init_app(app) ########################################################################## # Upgrade the schema (if required) ########################################################################## with app.app_context(): # Run migration for the first time i.e. create database from config import SQLITE_PATH from pgadmin.setup import db_upgrade # If version not available, user must have aborted. Tables are not # created and so its an empty db if not os.path.exists(SQLITE_PATH) or get_version() == -1: # If running in cli mode then don't try to upgrade, just raise # the exception if not cli_mode: db_upgrade(app) else: if not os.path.exists(SQLITE_PATH): raise FileNotFoundError( 'SQLite database file "' + SQLITE_PATH + '" does not exists.') raise RuntimeError('Specified SQLite database file ' 'is not valid.') else: schema_version = get_version() # Run migration if current schema version is greater than the # schema version stored in version table if CURRENT_SCHEMA_VERSION >= schema_version: db_upgrade(app) # Update schema version to the latest if CURRENT_SCHEMA_VERSION > schema_version: set_version(CURRENT_SCHEMA_VERSION) db.session.commit() if os.name != 'nt': os.chmod(config.SQLITE_PATH, 0o600) Mail(app) # Don't bother paths when running in cli mode if not cli_mode: import pgadmin.utils.paths as paths paths.init_app(app) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security = Security(None, user_datastore) ########################################################################## # Setup security ########################################################################## with app.app_context(): config.CSRF_SESSION_KEY = Keys.query.filter_by( name='CSRF_SESSION_KEY').first().value config.SECRET_KEY = Keys.query.filter_by( name='SECRET_KEY').first().value config.SECURITY_PASSWORD_SALT = Keys.query.filter_by( name='SECURITY_PASSWORD_SALT').first().value # Update the app.config with proper security keyes for signing CSRF data, # signing cookies, and the SALT for hashing the passwords. app.config.update(dict({ 'CSRF_SESSION_KEY': config.CSRF_SESSION_KEY, 'SECRET_KEY': config.SECRET_KEY, 'SECURITY_PASSWORD_SALT': config.SECURITY_PASSWORD_SALT, 'SESSION_COOKIE_DOMAIN': config.SESSION_COOKIE_DOMAIN, # CSRF Token expiration till session expires 'WTF_CSRF_TIME_LIMIT': getattr(config, 'CSRF_TIME_LIMIT', None), 'WTF_CSRF_METHODS': ['GET', 'POST', 'PUT', 'DELETE'], })) security.init_app(app, user_datastore) # register custom unauthorised handler. app.login_manager.unauthorized_handler(pga_unauthorised) # Set the permanent session lifetime to the specified value in config file. app.permanent_session_lifetime = timedelta( days=config.SESSION_EXPIRATION_TIME) if not cli_mode: app.session_interface = create_session_interface( app, config.SESSION_SKIP_PATHS ) # Make the Session more secure against XSS & CSRF when running in web mode if config.SERVER_MODE and config.ENHANCED_COOKIE_PROTECTION: paranoid = Paranoid(app) paranoid.redirect_view = 'browser.index' ########################################################################## # Load all available server drivers ########################################################################## driver.init_app(app) authenticate.init_app(app) ########################################################################## # Register language to the preferences after login ########################################################################## @user_logged_in.connect_via(app) def register_language(sender, user): # After logged in, set the language in the preferences if we get from # the login page data = request.form if 'language' in data: language = data['language'] # Set the user language preference misc_preference = Preferences.module('misc') user_languages = misc_preference.preference( 'user_language' ) if user_languages and language: language = user_languages.set(language) ########################################################################## # Register any local servers we can discover ########################################################################## @user_logged_in.connect_via(app) def on_user_logged_in(sender, user): # Keep hold of the user ID user_id = user.id # Get the first server group for the user servergroup_id = 1 servergroups = ServerGroup.query.filter_by( user_id=user_id ).order_by("id") if servergroups.count() > 0: servergroup = servergroups.first() servergroup_id = servergroup.id '''Add a server to the config database''' def add_server(user_id, servergroup_id, name, superuser, port, discovery_id, comment): # Create a server object if needed, and store it. servers = Server.query.filter_by( user_id=user_id, discovery_id=svr_discovery_id ).order_by("id") if servers.count() > 0: return svr = Server(user_id=user_id, servergroup_id=servergroup_id, name=name, host='localhost', port=port, maintenance_db='postgres', username=superuser, ssl_mode='prefer', comment=svr_comment, discovery_id=discovery_id) db.session.add(svr) db.session.commit() # Figure out what servers are present if winreg is not None: arch_keys = set() proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() try: proc_arch64 = os.environ['PROCESSOR_ARCHITEW6432'].lower() except Exception: proc_arch64 = None if proc_arch == 'x86' and not proc_arch64: arch_keys.add(0) elif proc_arch == 'x86' or proc_arch == 'amd64': arch_keys.add(winreg.KEY_WOW64_32KEY) arch_keys.add(winreg.KEY_WOW64_64KEY) for arch_key in arch_keys: for server_type in ('PostgreSQL', 'EnterpriseDB'): try: root_key = winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\" + server_type + "\\Services", 0, winreg.KEY_READ | arch_key ) for i in range(0, winreg.QueryInfoKey(root_key)[0]): inst_id = winreg.EnumKey(root_key, i) inst_key = winreg.OpenKey(root_key, inst_id) svr_name = winreg.QueryValueEx( inst_key, 'Display Name' )[0] svr_superuser = winreg.QueryValueEx( inst_key, 'Database Superuser' )[0] svr_port = winreg.QueryValueEx(inst_key, 'Port')[0] svr_discovery_id = inst_id svr_comment = gettext( "Auto-detected {0} installation with the data " "directory at {1}").format( winreg.QueryValueEx( inst_key, 'Display Name' )[0], winreg.QueryValueEx( inst_key, 'Data Directory' )[0]) add_server( user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment ) inst_key.Close() except Exception: pass else: # We use the postgres-winreg.ini file on non-Windows from configparser import ConfigParser registry = ConfigParser() try: registry.read('/etc/postgres-reg.ini') sections = registry.sections() # Loop the sections, and get the data from any that are PG or PPAS for section in sections: if ( section.startswith('PostgreSQL/') or section.startswith('EnterpriseDB/') ): svr_name = registry.get(section, 'Description') svr_superuser = registry.get(section, 'Superuser') # getint function throws exception if value is blank. # Ex: Port= # In such case we should handle the exception and continue # to read the next section of the config file. try: svr_port = registry.getint(section, 'Port') except ValueError: continue svr_discovery_id = section description = registry.get(section, 'Description') data_directory = registry.get(section, 'DataDirectory') svr_comment = gettext("Auto-detected {0} installation " "with the data directory at {1}" ).format(description, data_directory) add_server(user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment) except Exception: pass @user_logged_in.connect_via(app) @user_logged_out.connect_via(app) def force_session_write(app, user): session.force_write = True @user_logged_in.connect_via(app) def store_crypt_key(app, user): # in desktop mode, master password is used to encrypt/decrypt # and is stored in the keyManager memory if config.SERVER_MODE and 'password' in request.form: current_app.keyManager.set(request.form['password']) @user_logged_out.connect_via(app) def current_user_cleanup(app, user): from config import PG_DEFAULT_DRIVER from pgadmin.utils.driver import get_driver from flask import current_app # remove key current_app.keyManager.reset() for mdl in current_app.logout_hooks: try: mdl.on_logout(user) except Exception as e: current_app.logger.exception(e) _driver = get_driver(PG_DEFAULT_DRIVER) _driver.gc_own() ########################################################################## # Load plugin modules ########################################################################## for module in app.find_submodules('pgadmin'): app.logger.info('Registering blueprint module: %s' % module) app.register_blueprint(module) app.register_logout_hook(module) @app.before_request def limit_host_addr(): """ This function validate the hosts from ALLOWED_HOSTS before allowing HTTP request to avoid Host Header Injection attack :return: None/JSON response with 403 HTTP status code """ client_host = str(request.host).split(':')[0] valid = True allowed_hosts = config.ALLOWED_HOSTS if len(allowed_hosts) != 0: regex = re.compile( r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:/\d{1,2}|)') # Create separate list for ip addresses and host names ip_set = list(filter(lambda ip: regex.match(ip), allowed_hosts)) host_set = list(filter(lambda ip: not regex.match(ip), allowed_hosts)) is_ip = regex.match(client_host) if is_ip: ip_address = [] for ip in ip_set: ip_address.extend(list(ipaddress.ip_network(ip))) valid = ip_address.__contains__( ipaddress.ip_address(client_host) ) else: valid = host_set.__contains__(client_host) if not valid: return make_json_response( status=403, success=0, errormsg=_("403 FORBIDDEN") ) ########################################################################## # Handle the desktop login ########################################################################## @app.before_request def before_request(): """Login the default user if running in desktop mode""" # Check the auth key is valid, if it's set, and we're not in server # mode, and it's not a help file request. if not config.SERVER_MODE and app.PGADMIN_INT_KEY != '' and (( 'key' not in request.args or request.args['key'] != app.PGADMIN_INT_KEY) and request.cookies.get('PGADMIN_INT_KEY') != app.PGADMIN_INT_KEY and request.endpoint != 'help.static' ): abort(401) if not config.SERVER_MODE and not current_user.is_authenticated: user = user_datastore.get_user(config.DESKTOP_USER) # Throw an error if we failed to find the desktop user, to give # the sysadmin a hint. We'll continue to try to login anyway as # that'll through a nice 500 error for us. if user is None: app.logger.error( 'The desktop user %s was not found in the configuration ' 'database.' % config.DESKTOP_USER ) abort(401) login_user(user) elif config.SERVER_MODE and\ app.PGADMIN_EXTERNAL_AUTH_SOURCE ==\ KERBEROS and \ not current_user.is_authenticated and \ request.endpoint in ('redirects.index', 'security.login'): return authenticate.login() # if the server is restarted the in memory key will be lost # but the user session may still be active. Logout the user # to get the key again when login if config.SERVER_MODE and current_user.is_authenticated and \ app.PGADMIN_EXTERNAL_AUTH_SOURCE != \ KERBEROS and \ current_app.keyManager.get() is None and \ request.endpoint not in ('security.login', 'security.logout'): logout_user() @app.after_request def after_request(response): if 'key' in request.args: domain = dict() if config.COOKIE_DEFAULT_DOMAIN and \ config.COOKIE_DEFAULT_DOMAIN != 'localhost': domain['domain'] = config.COOKIE_DEFAULT_DOMAIN response.set_cookie('PGADMIN_INT_KEY', value=request.args['key'], path=config.COOKIE_DEFAULT_PATH, secure=config.SESSION_COOKIE_SECURE, httponly=config.SESSION_COOKIE_HTTPONLY, samesite=config.SESSION_COOKIE_SAMESITE, **domain) SecurityHeaders.set_response_headers(response) return response ########################################################################## # Cache busting ########################################################################## # Version number to be added to all static file url requests # This is used by url_for function when generating urls # This will solve caching issues when application is upgrading # This is called - Cache Busting @app.url_defaults def add_internal_version(endpoint, values): extensions = config.APP_VERSION_EXTN # Add the internal version only if it is set if config.APP_VERSION_PARAM is not None and \ config.APP_VERSION_PARAM != '': # If there is a filename, add the version if 'filename' in values \ and values['filename'].endswith(extensions): values[config.APP_VERSION_PARAM] = config.APP_VERSION_INT else: # Sometimes there may be direct endpoint for some files # There will be only one rule for such endpoints urls = [url for url in app.url_map.iter_rules(endpoint)] if len(urls) == 1 and urls[0].rule.endswith(extensions): values[config.APP_VERSION_PARAM] = \ config.APP_VERSION_INT # Strip away internal version param before sending further to app as it was # required for cache busting only @app.url_value_preprocessor def strip_version_number(endpoint, values): if values and config.APP_VERSION_PARAM in values: values.pop(config.APP_VERSION_PARAM) ########################################################################## # Minify output. Not required in desktop mode ########################################################################## if not config.DEBUG and config.SERVER_MODE: from flask_compress import Compress Compress(app) from pgadmin.misc.themes import themes themes(app) @app.context_processor def inject_blueprint(): """ Inject a reference to the current blueprint, if any. """ return { 'current_app': current_app, 'current_blueprint': current_blueprint, } @app.errorhandler(Exception) def all_exception_handler(e): current_app.logger.error(e, exc_info=True) return internal_server_error(errormsg=str(e)) # Exclude HTTPexception from above handler (all_exception_handler) # HTTPException are user defined exceptions and those should be returned # as is @app.errorhandler(HTTPException) def http_exception_handler(e): current_app.logger.error(e, exc_info=True) return e # Intialize the key manager app.keyManager = KeyManager() ########################################################################## # Protection against CSRF attacks ########################################################################## with app.app_context(): pgCSRFProtect.init_app(app) ########################################################################## # All done! ########################################################################## return app
def create_app(): """Flask application factory.""" app = Flask(__name__, static_folder='js') app.config['PROPAGATE_EXCEPTIONS'] = True app.config['TREE'] = os.getenv('TREE') app.config['GRAMPS_EXCLUDE_PRIVATE'] = Boolean( os.getenv('GRAMPS_EXCLUDE_PRIVATE')) app.config['GRAMPS_EXCLUDE_LIVING'] = Boolean( os.getenv('GRAMPS_EXCLUDE_LIVING')) app.config['TREE'] = os.getenv('TREE') if app.config['TREE'] is None or app.config['TREE'] == '': raise ValueError("You have to set the `TREE` environment variable.") app.config['PASSWORD'] = os.getenv('PASSWORD') or '' if not app.config['PASSWORD']: logging.warn("The password is empty! The app will not be protected.") app.logger.setLevel(logging.INFO) app.logger.info("Opening family tree '{}'".format(app.config['TREE'])) # called once here in case Db's constructor raises Db(app.config['TREE']) CORS(app) Compress(app) api = Api(app) cache = Cache(app, config={ 'CACHE_TYPE': 'filesystem', 'CACHE_DIR': 'appcache' }) app.config['JWT_TOKEN_LOCATION'] = ['headers', 'query_string'] jwt_secret_key = os.getenv('JWT_SECRET_KEY') if jwt_secret_key is None: if os.path.exists('jwt_secret_key'): with open('jwt_secret_key', 'r') as f: jwt_secret_key = f.read() else: jwt_secret_key = secrets.token_urlsafe(64) with open('jwt_secret_key', 'w') as f: f.write(jwt_secret_key) app.config['JWT_SECRET_KEY'] = jwt_secret_key jwt = JWTManager(app) @app.route('/') def send_js_index(): return send_from_directory(app.static_folder, 'index.html') @app.route('/env.js') def show_env_js(): return Response("window.APIHOST = '';\n", content_type='text/javascript') @app.route('/<path:path>') def send_js(path): if path and os.path.exists(os.path.join(app.static_folder, path)): return send_from_directory(app.static_folder, path) else: return send_from_directory(app.static_folder, 'index.html') @app.route('/api/login', methods=['POST']) def login(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 password = request.json.get('password', None) if password != app.config['PASSWORD']: return jsonify({"msg": "Wrong password"}), 401 expires = datetime.timedelta(days=365) access_token = create_access_token(identity='user', expires_delta=expires) return jsonify(access_token=access_token), 200 parser = reqparse.RequestParser() parser.add_argument('strings', type=str) parser.add_argument('fmt', type=str) @app.before_request def before_request(): if not get_db().dbstate.is_open(): get_db().open() app.teardown_appcontext(close_db) class ProtectedResource(Resource): method_decorators = [jwt_required] class People(ProtectedResource): @cache.cached() def get(self): return get_people(get_db()) class Families(ProtectedResource): @cache.cached() def get(self): return get_families(get_db()) class Events(ProtectedResource): @cache.cached() def get(self): return get_events(get_db()) class Places(ProtectedResource): @cache.cached() def get(self): return get_places(get_db()) class Citations(ProtectedResource): @cache.cached() def get(self): return get_citations(get_db()) class Sources(ProtectedResource): @cache.cached() def get(self): return get_sources(get_db()) class Repositories(ProtectedResource): @cache.cached() def get(self): return get_repositories(get_db()) class MediaObjects(ProtectedResource): @cache.cached() def get(self): return get_media(get_db()) class DbInfo(ProtectedResource): @cache.cached() def get(self): return get_db_info(get_db()) class FullTree(ProtectedResource): @cache.cached() def get(self): return { 'people': get_people(get_db()), 'families': get_families(get_db()), 'events': get_events(get_db()), 'places': get_places(get_db()), 'citations': get_citations(get_db()), 'sources': get_sources(get_db()), 'repositories': get_repositories(get_db()), 'media': get_media(get_db()), 'dbinfo': get_db_info(get_db()), } class Translate(Resource): @cache.cached() def get(self): args = parser.parse_args() try: strings = json.loads(args['strings']) except (json.decoder.JSONDecodeError, TypeError, ValueError) as e: return {"error": str(e)} return {"data": get_translation(strings)} class Note(ProtectedResource): @cache.cached(query_string=True) def get(self, gramps_id): args = parser.parse_args() fmt = args.get('fmt') or 'html' return get_note(get_db(), gramps_id, fmt=fmt) api.add_resource(People, '/api/people') api.add_resource(Families, '/api/families') api.add_resource(Events, '/api/events') api.add_resource(Places, '/api/places') api.add_resource(Citations, '/api/citations') api.add_resource(Sources, '/api/sources') api.add_resource(MediaObjects, '/api/mediaobjects') api.add_resource(Repositories, '/api/repositories') api.add_resource(Translate, '/api/translate') api.add_resource(DbInfo, '/api/dbinfo') api.add_resource(FullTree, '/api/tree') api.add_resource(Note, '/api/note/<string:gramps_id>') @app.route('/api/media/<string:handle>') @jwt_required def show_image(handle): path = get_media_info(get_db(), handle)['full_path'] return send_file(path) @app.route('/api/thumbnail/<string:handle>/<int:size>') @jwt_required @cache.cached() def show_thumbnail_square(handle, size): info = get_media_info(get_db(), handle) tn = get_thumbnail(info['full_path'], size, square=True, mime=info['mime']) return send_file(tn, info['mime']) @app.route( '/api/thumbnail/<string:handle>/<int:size>/<int:x1>/<int:y1>/<int:x2>/<int:y2>' ) @jwt_required @cache.cached() def show_thumbnail_square_cropped(handle, size, x1, y1, x2, y2): info = get_media_info(get_db(), handle) tn = get_thumbnail_cropped(info['full_path'], size, x1, y1, x2, y2, square=True, mime=info['mime']) return send_file(tn, info['mime']) return app
def setup_compress(app, development=False): compress = Compress() if not development: app.config['COMPRESS_CACHE_BACKEND'] = lambda: app.config['CACHE'] app.config['COMPRESS_CACHE_KEY'] = lambda r: f'{r.full_path}-compress' compress.init_app(app)
from accounts.download_acount_ids import update_pro_accounts from helper_funcs.compute_engine import check_last_day from helper_funcs.helper_imports import * from opendota_api import main # TODO # show alex ads # make levels accurate COMPRESS_MIMETYPES = [ 'text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript' ] COMPRESS_LEVEL = 6 COMPRESS_MIN_SIZE = 500 compress = Compress() app = Flask(__name__) cache = Cache(config={'CACHE_TYPE': 'simple'}) cache.init_app(app) compress.init_app(app) # minify(app=app, html=True, js=False, cssless=False) # classes @app.route('/', methods=['GET']) @cache.cached(timeout=600) def index(): with open('json_files/hero_ids.json', 'r') as f: data = json.load(f) links = sorted(data['heroes'], key=itemgetter('name'))
STATIC_DIR = os.path.join(BASE_PATH, 'static') DEFAULT_HOST = '127.0.0.1' DEFAULT_PORT = 5000 IS_A_TTY = sys.stdout.isatty() DEFAULT_GDB_EXECUTABLE = 'gdb' # create dictionary of signal names SIGNAL_NAME_TO_OBJ = {} for n in dir(signal): if n.startswith('SIG') and '_' not in n: SIGNAL_NAME_TO_OBJ[n.upper()] = getattr(signal, n) # Create flask application and add some configuration keys to be used in various callbacks app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR) Compress( app ) # add gzip compression to Flask. see https://github.com/libwilliam/flask-compress app.config['initial_binary_and_args'] = [] app.config['gdb_path'] = DEFAULT_GDB_EXECUTABLE app.config['gdb_cmd_file'] = None app.config['show_gdbgui_upgrades'] = True app.config['TEMPLATES_AUTO_RELOAD'] = True app.config['LLDB'] = False # assume false, okay to change later socketio = SocketIO() _state = StateManager(app.config) def setup_backend(serve=True, host=DEFAULT_HOST,
# -*- coding: utf-8 -*- from flask import Flask, render_template from flask_via import Via from flask_compress import Compress from flask_babel import Babel from models import db compress = Compress() via = Via() babel = Babel() def create_app(): """ Factory app. """ # Create the app. app = Flask(__name__) # File configuration from a file. app.config.from_pyfile('../config.py') # Initializes. via.init_app(app) db.init_app(app) compress.init_app(app) babel.init_app(app)
from flexget.webserver import register_app, register_home logger = logger.bind(name='webui') manager = None debug = False app_base = None ui_base = os.path.dirname(os.path.realpath(__file__)) ui_src = os.path.join(ui_base, 'src') ui_dist = os.path.join(ui_base, 'app') bower_components = os.path.join(ui_base, 'bower_components') webui_app = Flask(__name__) Compress(webui_app) webui_app.url_path = '/v1/' HTTP_METHODS = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ] @webui_app.route('/<path:path>') def serve_app(path): if debug: if path.startswith('bower_components'): return send_from_directory( bower_components, path.lstrip('bower_components').lstrip('/'))
import os from flask import Flask, redirect, url_for, request, send_from_directory, jsonify from pymongo import MongoClient from flask_cors import CORS from flask_compress import Compress from bson.objectid import ObjectId app = Flask(__name__) Compress(app) CORS(app) client = MongoClient('db', 27017) db = client.tododb def clean_entries(): entries = [item for item in db.tododb.find()] for entry in entries: if "name" not in entry or entry["name"] == '': db.tododb.delete_one({"_id": ObjectId(entry["_id"])}).deleted_count @app.route('/js/<path:path>') def send_js(path): return send_from_directory('static/js', path) @app.route('/css/<path:path>') def send_css(path): return send_from_directory('static/css', path)
def __init__(self, name=None, server=None, static_folder=None, url_base_pathname='/', **kwargs): if 'csrf_protect' in kwargs: warnings.warn( ''' `csrf_protect` is no longer used, CSRF protection has been removed as it is no longer necessary. See https://github.com/plotly/dash/issues/141 for details. ''', DeprecationWarning) # allow users to supply their own flask server if server is not None: self.server = server else: if name is None: name = 'dash' self.server = Flask(name, static_folder=static_folder) self.url_base_pathname = url_base_pathname self.config = _AttributeDict({ 'suppress_callback_exceptions': False, 'routes_pathname_prefix': url_base_pathname, 'requests_pathname_prefix': url_base_pathname }) # list of dependencies self.callback_map = {} # gzip Compress(self.server) # static files from the packages self.css = Css() self.scripts = Scripts() self.registered_paths = {} # urls def add_url(name, view_func, methods=['GET']): self.server.add_url_rule(name, view_func=view_func, endpoint=name, methods=methods) add_url('{}_dash-layout'.format(self.config['routes_pathname_prefix']), self.serve_layout) add_url( '{}_dash-dependencies'.format( self.config['routes_pathname_prefix']), self.dependencies) add_url( '{}_dash-update-component'.format( self.config['routes_pathname_prefix']), self.dispatch, ['POST']) add_url(('{}_dash-component-suites' '/<string:package_name>' '/<path:path_in_package_dist>').format( self.config['routes_pathname_prefix']), self.serve_component_suites) add_url('{}_dash-routes'.format(self.config['routes_pathname_prefix']), self.serve_routes) add_url(self.config['routes_pathname_prefix'], self.index) # catch-all for front-end routes add_url('{}<path:path>'.format(self.config['routes_pathname_prefix']), self.index) self.server.before_first_request(self._setup_server) self._layout = None self._cached_layout = None self.routes = []
def server_start(): global exclude_ids try: f = open(settings_file, 'r') try: allsettings = json.load(f) except ValueError as e: print('[-] Error: The settings file is not in a valid format, {}'.format(e)) f.close() sys.exit() f.close() finally: if 'f' in vars() and not f.closed: f.close() exclude_ids = allsettings['exclude_ids'] port = allsettings['port'] if allsettings['icon_set'] == 'standard': icon_set = 'icons_gen1_standard.png' elif allsettings['icon_set'] == 'shuffle': icon_set = 'icons_gen1_shuffle.png' elif allsettings['icon_set'] == 'alt': icon_set = 'icons_gen1_alt.png' elif allsettings['icon_set'] == 'toon': icon_set = 'icons_gen1_toon.png' else: print('[-] Error: Icon set in settings file is invalid, possible sets are: "standard", "shuffle", "toon", "alt".') list_profiles = [] list_lats = [] list_lngs = [] for i in range(0, len(allsettings['profiles'])): if allsettings['profiles'][i]['id'] not in list_profiles: list_profiles.append(allsettings['profiles'][i]['id']) list_lats.append(allsettings['profiles'][i]['coordinates']['lat']) list_lngs.append(allsettings['profiles'][i]['coordinates']['lng']) if len(list_profiles) == 0: print('[-] Error: No profiles in settings file.') sys.exit() else: main_ind = 0 db_data = sqlite3.connect(data_file, check_same_thread=False) db_data.create_function("isnotExcluded", 1, isnotExcluded) # def patched_finish(self): # print('still') # try: # if not self.wfile.closed: # self.wfile.close() # except socket.error as e: # sys.stdout.write('socket error: {}\n'.format(e)) # self.rfile.close() # SocketServer.StreamRequestHandler.finish = patched_finish # BaseHTTPServer.HTTPServer.allow_reuse_address = False compress = Compress() app = Flask(__name__,template_folder=workdir+'/'+'webres',static_url_path='/static',static_folder=workdir+'/webres/static') app.config['COMPRESS_MIN_SIZE'] = 0 app.config['COMPRESS_LEVEL'] = 6 app.config['COMPRESS_MIMETYPES'] = ['text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript', 'application/octet-stream', 'image/svg+xml'] compress.init_app(app) @app.teardown_appcontext def close_connection(exception): db = getattr(g, '_database', None) if db is not None: db.close() @app.after_request def add_header(response): if response.headers['Content-Type'] == "image/png": response.headers['Cache-Control'] = 'must-revalidate, public, max-age=86400' else: response.headers['Cache-Control'] = 'must-revalidate, public, max-age=-1' return response @app.route('/_getdata') def add_numbers(): datatill = request.args.get('data_till', 0, type=int) profile = request.args.get('profile', -1, type=int) timenow = int(round(time.time(),0)) cursor_data = db_data.cursor() while True: try: if profile == -1: results = cursor_data.execute('SELECT spawnid, latitude, longitude, spawntype, pokeid, expiretime FROM spawns WHERE isnotExcluded(pokeid) AND (expiretime > ?) AND (fromtime >= ?)',(timenow,datatill)) else: results = cursor_data.execute('SELECT spawnid, latitude, longitude, spawntype, pokeid, expiretime FROM spawns WHERE isnotExcluded(pokeid) AND (profile == ?) AND (expiretime > ?) AND (fromtime >= ?)', (profile,timenow, datatill)) return jsonify([timenow, results.fetchall()]) except sqlite3.OperationalError as e: print('[-] Sqlite operational error: {} Retrying...'.format(e)) @app.route("/") def mainapp(): return render_template('index.html',api_key=allsettings['api_key'],icon_scalefactor=allsettings['icon_scalefactor'],mobile_scale=allsettings['mobile_scalefactor'],lat=list_lats[main_ind],lng=list_lngs[main_ind],language=allsettings['language'],icon_set = icon_set, profile=-1) @app.route("/id<int:profile>") def subapp(profile): if profile in list_profiles: sub_ind = list_profiles.index(profile) return render_template('index.html', api_key=allsettings['api_key'], icon_scalefactor=allsettings['icon_scalefactor'], mobile_scale=allsettings['mobile_scalefactor'],lat=list_lats[sub_ind],lng=list_lngs[sub_ind], language=allsettings['language'], icon_set = icon_set, profile=profile) http_server = HTTPServer(WSGIContainer(app)) try: http_server.listen(port=port,address='0.0.0.0') IOLoop.instance().start() except socket.error as e: if e.errno == 10048: print('[-] Error: The specified port {} is already in use.'.format(port))
def __init__( self, name='__main__', server=None, static_folder='static', assets_folder=None, assets_url_path='/assets', assets_ignore='', include_assets_files=True, url_base_pathname='/', assets_external_path=None, requests_pathname_prefix=None, routes_pathname_prefix=None, compress=True, meta_tags=None, index_string=_default_index, external_scripts=None, external_stylesheets=None, suppress_callback_exceptions=None, **kwargs): # pylint-disable: too-many-instance-attributes if 'csrf_protect' in kwargs: warnings.warn(''' `csrf_protect` is no longer used, CSRF protection has been removed as it is no longer necessary. See https://github.com/plotly/dash/issues/141 for details. ''', DeprecationWarning) name = name if server is None else server.name self._assets_folder = assets_folder or os.path.join( flask.helpers.get_root_path(name), 'assets' ) # allow users to supply their own flask server self.server = server or Flask(name, static_folder=static_folder) self.server.register_blueprint( flask.Blueprint('assets', 'assets', static_folder=self._assets_folder, static_url_path=assets_url_path)) env_configs = _configs.env_configs() url_base_pathname, routes_pathname_prefix, requests_pathname_prefix = \ _configs.pathname_configs( url_base_pathname, routes_pathname_prefix, requests_pathname_prefix, environ_configs=env_configs) self.url_base_pathname = url_base_pathname self.config = _AttributeDict({ 'suppress_callback_exceptions': _configs.get_config( 'suppress_callback_exceptions', suppress_callback_exceptions, env_configs, False ), 'routes_pathname_prefix': routes_pathname_prefix, 'requests_pathname_prefix': requests_pathname_prefix, 'include_assets_files': _configs.get_config( 'include_assets_files', include_assets_files, env_configs, True), 'assets_external_path': _configs.get_config( 'assets_external_path', assets_external_path, env_configs, ''), }) # list of dependencies self.callback_map = {} self._index_string = '' self.index_string = index_string self._meta_tags = meta_tags or [] self._favicon = None if compress: # gzip Compress(self.server) @self.server.errorhandler(exceptions.PreventUpdate) def _handle_error(error): """Handle a halted callback and return an empty 204 response""" print(error, file=sys.stderr) return ('', 204) # static files from the packages self.css = Css() self.scripts = Scripts() self._external_scripts = external_scripts or [] self._external_stylesheets = external_stylesheets or [] self.assets_ignore = assets_ignore self.registered_paths = {} # urls def add_url(name, view_func, methods=('GET',)): self.server.add_url_rule( name, view_func=view_func, endpoint=name, methods=list(methods) ) add_url( '{}_dash-layout'.format(self.config['routes_pathname_prefix']), self.serve_layout) add_url( '{}_dash-dependencies'.format( self.config['routes_pathname_prefix']), self.dependencies) add_url( '{}_dash-update-component'.format( self.config['routes_pathname_prefix']), self.dispatch, ['POST']) add_url(( '{}_dash-component-suites' '/<string:package_name>' '/<path:path_in_package_dist>').format( self.config['routes_pathname_prefix']), self.serve_component_suites) add_url( '{}_dash-routes'.format(self.config['routes_pathname_prefix']), self.serve_routes) add_url( self.config['routes_pathname_prefix'], self.index) # catch-all for front-end routes add_url( '{}<path:path>'.format(self.config['routes_pathname_prefix']), self.index) self.server.before_first_request(self._setup_server) self._layout = None self._cached_layout = None self.routes = []
def __init__(self, name=None, server=None, static_folder=None, url_base_pathname='/', **kwargs): # pylint-disable: too-many-instance-attributes if 'csrf_protect' in kwargs: warnings.warn( ''' `csrf_protect` is no longer used, CSRF protection has been removed as it is no longer necessary. See https://github.com/plotly/dash/issues/141 for details. ''', DeprecationWarning) name = name or 'dash' # allow users to supply their own flask server self.server = server or Flask(name, static_folder=static_folder) self.url_base_pathname = url_base_pathname self.config = _AttributeDict({ 'suppress_callback_exceptions': False, 'routes_pathname_prefix': url_base_pathname, 'requests_pathname_prefix': url_base_pathname }) # list of dependencies self.callback_map = {} # gzip Compress(self.server) @self.server.errorhandler(exceptions.PreventUpdate) def _handle_error(error): """Handle a halted callback and return an empty 204 response""" print(error, file=sys.stderr) return ('', 204) # static files from the packages self.css = Css() self.scripts = Scripts() self.registered_paths = {} # urls def add_url(name, view_func, methods=('GET', )): self.server.add_url_rule(name, view_func=view_func, endpoint=name, methods=list(methods)) add_url('{}_dash-layout'.format(self.config['routes_pathname_prefix']), self.serve_layout) add_url( '{}_dash-dependencies'.format( self.config['routes_pathname_prefix']), self.dependencies) add_url( '{}_dash-update-component'.format( self.config['routes_pathname_prefix']), self.dispatch, ['POST']) add_url(('{}_dash-component-suites' '/<string:package_name>' '/<path:path_in_package_dist>').format( self.config['routes_pathname_prefix']), self.serve_component_suites) add_url('{}_dash-routes'.format(self.config['routes_pathname_prefix']), self.serve_routes) add_url(self.config['routes_pathname_prefix'], self.index) # catch-all for front-end routes add_url('{}<path:path>'.format(self.config['routes_pathname_prefix']), self.index) self.server.before_first_request(self._setup_server) self._layout = None self._cached_layout = None self.routes = []
def register_extensions(app): """ register extensions to the app """ app.jinja_env.add_extension('jinja2.ext.do') # Global values in jinja # Compress app responses with gzip compress = Compress() compress.init_app(app) # Influx db time-series database db.init_app(app) # Limit authentication blueprint requests to 200 per minute limiter = Limiter(app, key_func=get_ip_address) limiter.limit("200/minute")(routes_authentication.blueprint) # Language translations babel = Babel(app) @babel.localeselector def get_locale(): try: user = User.query.filter( User.id == flask_login.current_user.id).first() if user and user.language != '': for key in LANGUAGES: if key == user.language: return key # Bypass endpoint test error "'AnonymousUserMixin' object has no attribute 'id'" except AttributeError: pass return request.accept_languages.best_match(LANGUAGES.keys()) # User login management login_manager = flask_login.LoginManager() login_manager.init_app(app) @login_manager.user_loader def user_loader(user_id): user = User.query.filter(User.id == user_id).first() if not user: return return user @login_manager.unauthorized_handler def unauthorized(): flash(gettext('Please log in to access this page'), "error") return redirect(url_for('routes_authentication.do_login')) # Create and populate database if it doesn't exist with app.app_context(): db.create_all() populate_db() # This is disabled because there's a bug that messes up user databases # The upgrade script will execute alembic to upgrade the database # alembic_upgrade_db() # Check user option to force all web connections to use SSL # Fail if the URI is empty (pytest is running) if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': with session_scope( app.config['SQLALCHEMY_DATABASE_URI']) as new_session: misc = new_session.query(Misc).first() if misc and misc.force_https: SSLify(app)
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_bcrypt import Bcrypt from itsdangerous import URLSafeTimedSerializer from datetime import timedelta from flask_compress import Compress compress = Compress() app = Flask(__name__) app.config.from_object('config') db = SQLAlchemy(app) app.secret_key = 'super secret key' login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "signin" login_serializer = URLSafeTimedSerializer(app.secret_key) app.config["REMEMBER_COOKIE_DURATION"] = timedelta(days=14) bcrypt = Bcrypt(app) compress.init_app(app) from app import views, models
), 'APISPEC_SWAGGER_URL': '/docs.json', 'APISPEC_SWAGGER_UI_URL': '/docs/' }) app.config["JWT_IDENTITY_CLAIM"] = "identity" app.config["JWT_BLACKLIST_ENABLED"] = True app.config["JWT_BLACKLIST_TOKEN_CHECKS"] = ["access", "refresh"] app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta( hours=app.config["JWT_ACCESS_TOKEN_EXPIRES_TIME"]) app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta( hours=app.config["JWT_REFRESH_TOKEN_EXPIRES_TIME"]) docs = FlaskApiSpec(app) db = SQLAlchemy(app) compress = Compress(app) jwt = JWTManager(app) CORS(app) api = Api(app) email_sender = Smtp("smtp.gmail.com", 587) email_sender.set_account(app.config["EMAIL_ACCOUNT"], app.config["EMAIL_PASSWORD"]) # Blueprint from .controllers import skeleton_bp from .controllers.auth import auth_bp blueprints = [skeleton_bp, auth_bp] for bp in blueprints: app.register_blueprint(bp) docs.register_existing_resources()
import geo from natsort import natsorted from operator import itemgetter app = Flask(__name__) # ################################# # config and init for flask plugins # ################################# # Flask-compress ################ # gzip compression # should be already done by apache/nginx, but just to be sure :) compress = Compress() compress.defaults = [ ('COMPRESS_MIMETYPES', ['text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript', 'application/pdf', 'image/svg+xml']), ('COMPRESS_DEBUG', True), ('COMPRESS_LEVEL', 6), ('COMPRESS_MIN_SIZE', 50)] Compress(app) ################ # personal needs ################
def test_one_algo_supported(self): """ Tests requesting a single supported compression algorithm """ accept_encoding = 'gzip' self.app.config['COMPRESS_ALGORITHM'] = ['br', 'gzip'] c = Compress(self.app) self.assertEqual(c._choose_compress_algorithm(accept_encoding), 'gzip')