def setUp(self): app = Flask(__name__) app.testing = True self.app = app self.client = app.test_client(use_cookies=True) self.lang = Language(app) self.allowed_languages = ['en', 'fr'] self.default_language = 'en' @self.lang.allowed_languages def allowed_languages(): return self.allowed_languages @self.lang.default_language def default_language(): return self.default_language @self.app.route('/language') def get_language(): return jsonify({'language': str(current_language)}), 200 @self.app.route('/language', methods=['POST']) def set_language(): req = request.get_json() language = req.get('language', None) self.lang.change_language(language) return jsonify({ 'language': str(current_language), }), 201
from oauthlib.oauth2 import WebApplicationClient from contentagregator.config import google_config app = Flask(__name__) app.debug=True app.config.from_object('contentagregator.config.DevelopmentConfig') db = SQLAlchemy(app) assets = Environment(app) migrate = Migrate(app, db) api = Api(app) jwt = JWTManager(app) mail = Mail(app) seeder = FlaskSeeder(app, db) lang = Language(app) client = WebApplicationClient(google_config['google_client_id']) # celery setup def make_celery(app): celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery
from config import config # from keycloak import KeycloakOpenID, KeycloakAdmin # from twilio.rest import Client as TwilioClient # Library declarative. db = SQLAlchemy() ma = Marshmallow() migrate = Migrate() manager = Manager() alembic = Alembic() mail = Mail() cache = Cache() # celery = Celery() # redis = Redis() language = Language() compress = Compress() # apm = ElasticAPM() Config = config[os.getenv('APP_ENV', "DEVELOPMENT")] def create_app(environment): # Init flask app = Flask(__name__, static_folder='./static', template_folder='./templates') # Init flask configurations. app.config.from_object(config[environment]) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) # Enabling the cross origin using the cors.
class TestLanguage(unittest.TestCase): def assert_language_response(self, response, lang): cookies = get_cookies(response) cookie_lang = cookies.get(self.lang._cookie_name) self.assertNotEqual(cookie_lang, None) self.assertEqual(cookie_lang.value, lang) data = get_json_data(response) self.assertEqual(data.get('language'), lang) def setUp(self): app = Flask(__name__) app.testing = True self.app = app self.client = app.test_client(use_cookies=True) self.lang = Language(app) self.allowed_languages = ['en', 'fr'] self.default_language = 'en' @self.lang.allowed_languages def allowed_languages(): return self.allowed_languages @self.lang.default_language def default_language(): return self.default_language @self.app.route('/language') def get_language(): return jsonify({'language': str(current_language)}), 200 @self.app.route('/language', methods=['POST']) def set_language(): req = request.get_json() language = req.get('language', None) self.lang.change_language(language) return jsonify({ 'language': str(current_language), }), 201 def test_get_default_language(self): rv = self.client.get('/language') self.assertEqual(rv.status_code, 200) self.assertEqual(rv.mimetype, 'application/json') self.assert_language_response(rv, self.default_language) def test_change_allowed_language(self): new_lang = 'fr' rq = JSONRequest({'language': new_lang}) rv = self.client.post('/language', data=rq.json, content_type=rq.content_type) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.mimetype, 'application/json') self.assert_language_response(rv, new_lang) def test_change_not_allowed_language(self): new_lang = 'dk' rq = JSONRequest({'language': new_lang}) rv = self.client.post('/language', data=rq.json, content_type=rq.content_type) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.mimetype, 'application/json') self.assert_language_response(rv, self.default_language)
For more information, please visit: https://flask-language.readthedocs.io/en/latest/ """ from os import listdir from os.path import isfile, join import xml.etree.ElementTree as ET from flask import current_app from flask_language import Language from flask_language import current_language from utils.constants import LANGUAGE_RESOURCE_PATH lang = Language() allowed_langs = [] lang_dict = {} def init_lang(app): """The function initializes `lang` with the provided context. Args: app (flask.app.Flask): A Flask application. """ lang.init_app(app) # Load language packs from LANGUAGE_RESOURCE_PATH langs_list = [f for f in listdir(LANGUAGE_RESOURCE_PATH) \