Flask-Exts 0.1.2__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. flask_exts-0.1.3/.readthedocs.yaml +39 -0
  2. {flask_exts-0.1.2 → flask_exts-0.1.3}/CHANGES.rst +14 -0
  3. {flask_exts-0.1.2 → flask_exts-0.1.3}/PKG-INFO +1 -1
  4. flask_exts-0.1.3/src/flask_exts/__about__.py +1 -0
  5. flask_exts-0.1.3/src/flask_exts/__init__.py +1 -0
  6. flask_exts-0.1.3/src/flask_exts/babel/__init__.py +1 -0
  7. flask_exts-0.1.3/src/flask_exts/babel/admin_babel.py +14 -0
  8. flask_exts-0.1.3/src/flask_exts/babel/babel_setting.py +41 -0
  9. flask_exts-0.1.3/src/flask_exts/manager.py +23 -0
  10. flask_exts-0.1.3/tests/conftest.py +19 -0
  11. flask_exts-0.1.3/tests/test_babel.py +53 -0
  12. flask_exts-0.1.3/tests/test_base.py +6 -0
  13. flask_exts-0.1.2/.readthedocs.yaml +0 -21
  14. flask_exts-0.1.2/src/flask_exts/__about__.py +0 -1
  15. {flask_exts-0.1.2 → flask_exts-0.1.3}/.github/workflows/release.yml +0 -0
  16. {flask_exts-0.1.2 → flask_exts-0.1.3}/.gitignore +0 -0
  17. {flask_exts-0.1.2 → flask_exts-0.1.3}/LICENSE.rst +0 -0
  18. {flask_exts-0.1.2 → flask_exts-0.1.3}/README.rst +0 -0
  19. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/Makefile +0 -0
  20. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/api.rst +0 -0
  21. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/changes.rst +0 -0
  22. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/conf.py +0 -0
  23. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/index.rst +0 -0
  24. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/make.bat +0 -0
  25. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/requirements.txt +0 -0
  26. {flask_exts-0.1.2 → flask_exts-0.1.3}/docs/usage.rst +0 -0
  27. {flask_exts-0.1.2 → flask_exts-0.1.3}/pyproject.toml +0 -0
  28. {flask_exts-0.1.2 → flask_exts-0.1.3}/requirements/build.in +0 -0
  29. {flask_exts-0.1.2 → flask_exts-0.1.3}/requirements/test.in +0 -0
  30. {flask_exts-0.1.2/src/flask_exts → flask_exts-0.1.3/src/flask_exts/translations}/__init__.py +0 -0
  31. {flask_exts-0.1.2 → flask_exts-0.1.3}/tests/__init__.py +0 -0
@@ -0,0 +1,39 @@
1
+ # .readthedocs.yaml
2
+ # Read the Docs configuration file
3
+ # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4
+
5
+ # Required
6
+ version: 2
7
+
8
+ # Set the OS, Python version and other tools you might need
9
+ build:
10
+ os: "ubuntu-22.04"
11
+ tools:
12
+ python: "3.12"
13
+ # You can also specify other tool versions:
14
+ # nodejs: "19"
15
+ # rust: "1.64"
16
+ # golang: "1.19"
17
+
18
+ # Build documentation in the "docs/" directory with Sphinx
19
+ sphinx:
20
+ configuration: docs/conf.py
21
+
22
+ # Optionally build your docs in additional formats such as PDF and ePub
23
+ # formats:
24
+ # - pdf
25
+ # - epub
26
+
27
+ # Optional but recommended, declare the Python requirements required
28
+ # to build your documentation
29
+ # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
30
+ # python:
31
+ # install:
32
+ # - requirements: docs/requirements.txt
33
+
34
+ python:
35
+ install:
36
+ - method: pip
37
+ path: .
38
+ - requirements: docs/requirements.txt
39
+
@@ -1,3 +1,17 @@
1
+ v0.1.4
2
+ ------
3
+
4
+ Unreleased
5
+
6
+ - form
7
+
8
+ v0.1.3
9
+ ------
10
+
11
+ Released 2024-03-25
12
+
13
+ - babel support
14
+
1
15
  v0.1.2
2
16
  ------
3
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: Flask-Exts
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Flask extensions with admin, SQLAlchemy, babel, forms, fields, widgets, and so on.
5
5
  Project-URL: Documentation, https://flask-exts.readthedocs.io
6
6
  Project-URL: Source, https://github.com/ojso/flask-exts
@@ -0,0 +1 @@
1
+ __version__ = "0.1.3"
@@ -0,0 +1 @@
1
+ from .manager import Manager
@@ -0,0 +1 @@
1
+ from .admin_babel import gettext, ngettext, lazy_gettext
@@ -0,0 +1,14 @@
1
+ from flask_babel import Domain
2
+ from .. import translations
3
+
4
+ class AdminDomain(Domain):
5
+ def __init__(self):
6
+ super().__init__(translations.__path__[0], domain="admin")
7
+
8
+
9
+ admin_domain = AdminDomain()
10
+
11
+ gettext = admin_domain.gettext
12
+ ngettext = admin_domain.ngettext
13
+ lazy_gettext = admin_domain.lazy_gettext
14
+
@@ -0,0 +1,41 @@
1
+ from flask import request
2
+ from flask import session
3
+ from flask import current_app
4
+ from flask_babel import Babel
5
+
6
+ babel = Babel()
7
+
8
+
9
+ def locale_selector():
10
+ if not request:
11
+ return
12
+ # save lang to session if lang exists in args
13
+ if request.args.get("lang"):
14
+ session["lang"] = request.args.get("lang")
15
+ # from session
16
+ if session.get("lang"):
17
+ session_lang = session.get("lang")
18
+ return session_lang
19
+ # try to guess the language from the user accept header the browser transmits.
20
+ if current_app.config.get("BABEL_ACCEPT_LANGUAGES"):
21
+ accept_languages = current_app.config["BABEL_ACCEPT_LANGUAGES"].split(";")
22
+ accept_language = request.accept_languages.best_match(accept_languages)
23
+ return accept_language
24
+
25
+
26
+ def timezone_selector():
27
+ # save timezone to session if timezone exists in args
28
+ if request.args.get("timezone"):
29
+ session["timezone"] = request.args.get("timezone")
30
+ # from session
31
+ if session.get("timezone"):
32
+ return session.get("timezone")
33
+ # from app.config
34
+ if current_app.config.get("BABEL_DEFAULT_TIMEZONE"):
35
+ return current_app.config.get("BABEL_DEFAULT_TIMEZONE")
36
+
37
+
38
+ def flask_babel_init_app(app):
39
+ babel.init_app(
40
+ app, locale_selector=locale_selector, timezone_selector=timezone_selector
41
+ )
@@ -0,0 +1,23 @@
1
+ from .babel.babel_setting import flask_babel_init_app
2
+
3
+
4
+ class Manager:
5
+ """This is used to manager babel,form,admin."""
6
+
7
+ def init_flask_babel(self,app):
8
+ flask_babel_init_app(app)
9
+
10
+ def __init__(self, app=None):
11
+ if app is not None:
12
+ self.init_app(app)
13
+
14
+ def init_app(self, app):
15
+ self.app = app
16
+
17
+ if not hasattr(app, "extensions"):
18
+ app.extensions = {}
19
+
20
+ app.extensions["manager"] = self
21
+
22
+ if 'babel' not in app.extensions:
23
+ self.init_flask_babel(app)
@@ -0,0 +1,19 @@
1
+ import pytest
2
+ from flask import Flask
3
+ from flask_exts import Manager
4
+
5
+
6
+ @pytest.fixture
7
+ def app():
8
+ app = Flask(__name__)
9
+ app.secret_key = "1"
10
+ app.config["BABEL_ACCEPT_LANGUAGES"] = "en;zh;fr;de;ru"
11
+ app.config["BABEL_DEFAULT_TIMEZONE"] = "Asia/Shanghai"
12
+ manager = Manager()
13
+ manager.init_app(app)
14
+ yield app
15
+
16
+
17
+ @pytest.fixture
18
+ def client(app):
19
+ return app.test_client()
@@ -0,0 +1,53 @@
1
+ import pytest
2
+ from datetime import datetime
3
+ from flask import session
4
+ from flask_babel import get_locale
5
+ from flask_babel import get_timezone
6
+ from flask_babel import format_datetime
7
+ from flask_babel import refresh
8
+ from flask_babel import gettext
9
+ from flask_exts.babel import gettext as admin_gettext
10
+
11
+
12
+ class TestBabel:
13
+ def test_locale(self, app):
14
+ with app.test_request_context():
15
+ locale = get_locale()
16
+ timezone = get_timezone()
17
+ # print("locale:",get_locale().language)
18
+ # print("timezone:",get_timezone().zone)
19
+ # print(format_datetime(datetime.now(), "full"))
20
+ # print(format_datetime(datetime.now()))
21
+ assert "China Standard Time" in format_datetime(datetime.now(), "full")
22
+ assert get_locale().language == "en"
23
+ assert get_timezone().zone == app.config.get("BABEL_DEFAULT_TIMEZONE")
24
+ assert get_timezone().zone == "Asia/Shanghai"
25
+ # 修改语言为zh
26
+ session["lang"] = "zh"
27
+ # print("locale:",get_locale().language)
28
+ assert get_locale().language == "en"
29
+ refresh()
30
+ # print("locale:",get_locale().language)
31
+ assert get_locale().language == "zh"
32
+ # print(format_datetime(datetime.now(), "full"))
33
+ assert "中国标准时间" in format_datetime(datetime.now(), "full")
34
+ # print(format_datetime(datetime.now()))
35
+
36
+ def test_babel(self,app):
37
+ with app.test_request_context():
38
+ text = "copy"
39
+ text_zh = "copy"
40
+ assert gettext(text) == text
41
+ session["lang"] = "zh"
42
+ refresh()
43
+ assert gettext(text) == text_zh
44
+
45
+ def test_admin_translation(self, app):
46
+ with app.test_request_context():
47
+ text = "Home"
48
+ # text_zh = "首页"
49
+ text_zh = "Home"
50
+ assert admin_gettext(text) == text
51
+ session["lang"] = "zh"
52
+ refresh()
53
+ assert admin_gettext(text) == text_zh
@@ -0,0 +1,6 @@
1
+ class TestBase:
2
+ def test_extensions(self, app):
3
+ manager = app.extensions.get("manager")
4
+ assert manager is not None
5
+ babel = app.extensions.get("babel")
6
+ assert babel is not None
@@ -1,21 +0,0 @@
1
- version: "2"
2
-
3
- build:
4
- os: "ubuntu-22.04"
5
- tools:
6
- python: "3.12"
7
-
8
- python:
9
- install:
10
- - method: pip
11
- path: .
12
- - requirements: docs/requirements.txt
13
-
14
- sphinx:
15
- configuration: docs/conf.py
16
-
17
-
18
-
19
-
20
-
21
-
@@ -1 +0,0 @@
1
- __version__ = "0.1.2"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes