Commit e002d7855c9070cde649c87d50689cb9261569aa
1 parent
821570955a
Exists in
master
add get menu-items
Showing 10 changed files with 17 additions and 2 deletions Inline Diff
- __pycache__/main.cpython-310.pyc
- __pycache__/routes.cpython-310.pyc
- components/k2test/__pycache__/__init__.cpython-310.pyc
- components/k2test/k2test/__pycache__/__init__.cpython-310.pyc
- components/k2test/k2test/__pycache__/views.cpython-310.pyc
- components/k2test/setup.py
- db/database.db
- k2/__pycache__/k2cfg.cpython-310.pyc
- k2/k2cfg.py
- main.py
__pycache__/main.cpython-310.pyc
No preview for this file type
__pycache__/routes.cpython-310.pyc
No preview for this file type
components/k2test/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/k2test/k2test/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/k2test/k2test/__pycache__/views.cpython-310.pyc
No preview for this file type
components/k2test/setup.py
| File was created | 1 | from setuptools import setup, find_packages | |
| 2 | |||
| 3 | setup( | ||
| 4 | name='k2test', | ||
| 5 | version='1.0.0', | ||
| 6 | description='Description of my project', | ||
| 7 | author='Your Name', | ||
| 8 | author_email='yourname@example.com', | ||
| 9 | keywords="k2test, k2", | ||
| 10 | python_requires=">=3.7, <=3.11.2", | ||
| 11 | packages=['k2test'], | ||
| 12 | package_data={ # Optional | ||
| 13 | }, | ||
| 14 | install_requires=[""], | ||
| 15 | ) | ||
| 16 |
db/database.db
No preview for this file type
k2/__pycache__/k2cfg.cpython-310.pyc
No preview for this file type
k2/k2cfg.py
| 1 | from flask_sqlalchemy import SQLAlchemy | 1 | from flask_sqlalchemy import SQLAlchemy |
| 2 | from sqlalchemy import text | 2 | from sqlalchemy import text |
| 3 | from .k2obj import k2obj | 3 | from .k2obj import k2obj |
| 4 | import yaml | 4 | import yaml |
| 5 | import os | 5 | import os |
| 6 | import requests | 6 | import requests |
| 7 | import pytz | 7 | import pytz |
| 8 | import os | 8 | import os |
| 9 | import sys | 9 | import sys |
| 10 | from flask import session | 10 | from flask import session |
| 11 | 11 | ||
| 12 | class k2(k2obj): | 12 | class k2(k2obj): |
| 13 | 13 | ||
| 14 | version = '2.5.1.27' | 14 | version = '2.5.1.27' |
| 15 | db = SQLAlchemy() | 15 | db = SQLAlchemy() |
| 16 | domain = 'http://127.0.0.1:5000/' | 16 | domain = 'http://127.0.0.1:5000/' |
| 17 | update_domain ='http://127.0.0.1:8001/' | 17 | update_domain ='http://127.0.0.1:8001/' |
| 18 | venv_bin_path = os.path.join(os.path.dirname(sys.prefix), 'venv/Scripts') | 18 | venv_bin_path = os.path.join(os.path.dirname(sys.prefix), 'venv/Scripts') |
| 19 | json_sort_keys = False | 19 | json_sort_keys = False |
| 20 | timezone = pytz.timezone('Europe/Kiev') | 20 | timezone = pytz.timezone('Europe/Kiev') |
| 21 | secret_key = 'JH&INH987gFDHdsagh&8dwbjdw8ckw' | 21 | secret_key = 'JH&INH987gFDHdsagh&8dwbjdw8ckw' |
| 22 | default_language = 'en' | 22 | default_language = 'en' |
| 23 | current_language = '' | 23 | current_language = '' |
| 24 | babel_translation_directories = '' | 24 | babel_translation_directories = '' |
| 25 | 25 | ||
| 26 | def __init__(self): | 26 | def __init__(self): |
| 27 | super().__init__() | 27 | super().__init__() |
| 28 | 28 | ||
| 29 | @classmethod | 29 | @classmethod |
| 30 | def load_babel_translation_directories(cls): | 30 | def load_babel_translation_directories(cls): |
| 31 | with open('languages/babel_translation_directories.yml', 'r') as f: | 31 | with open('languages/babel_translation_directories.yml', 'r') as f: |
| 32 | data = yaml.safe_load(f) | 32 | data = yaml.safe_load(f) |
| 33 | cls.babel_translation_directories = data['babel_translation_directories'] | 33 | cls.babel_translation_directories = data['babel_translation_directories'] |
| 34 | 34 | ||
| 35 | @classmethod | 35 | @classmethod |
| 36 | def get_locale(cls): | 36 | def get_locale(cls): |
| 37 | if 'lang' in session: | 37 | if 'lang' in session: |
| 38 | # Отримати обрану мову з сеансу користувача | 38 | # Отримати обрану мову з сеансу користувача |
| 39 | return session['lang'] | 39 | return session['lang'] |
| 40 | else: | 40 | else: |
| 41 | # Повернути мову за замовчуванням з налаштувань додатку | 41 | # Повернути мову за замовчуванням з налаштувань додатку |
| 42 | return cls.default_language #app.config['DEFAULT_LANGUAGE'] | 42 | return cls.default_language #app.config['DEFAULT_LANGUAGE'] |
| 43 | 43 | ||
| 44 | @classmethod | 44 | @classmethod |
| 45 | def search_babel_translation_directories(cls): | 45 | def search_babel_translation_directories(cls): |
| 46 | response = requests.get(f'{cls.domain}api/languages') | 46 | response = requests.get(f'{cls.domain}api/languages') |
| 47 | if response.status_code == 200: | 47 | if response.status_code == 200: |
| 48 | cls.babel_translation_directories = response.text | 48 | cls.babel_translation_directories = response.text |
| 49 | data = {'babel_translation_directories': response.text} | 49 | data = {'babel_translation_directories': response.text} |
| 50 | else: | 50 | else: |
| 51 | cls.babel_translation_directories = 'languages' | 51 | cls.babel_translation_directories = 'languages' |
| 52 | data = {'babel_translation_directories': 'languages'} | 52 | data = {'babel_translation_directories': 'languages'} |
| 53 | 53 | ||
| 54 | with open('languages/babel_translation_directories.yml', 'w') as f: | 54 | with open('languages/babel_translation_directories.yml', 'w') as f: |
| 55 | yaml.dump(data, f) | 55 | yaml.dump(data, f) |
| 56 | @classmethod | 56 | @classmethod |
| 57 | def search_menu_elenents(cls): | 57 | def get_menu_elenents(cls): |
| 58 | with open('components/menu.yml', 'r') as f: | 58 | with open('components/menu.yml', 'r') as f: |
| 59 | yaml_data = yaml.load(f, Loader=yaml.FullLoader) | 59 | yaml_data = yaml.load(f, Loader=yaml.FullLoader) |
| 60 | cls.menu = yaml_data | 60 | cls.menu = yaml_data |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | # load database settings from yml file | 65 | # load database settings from yml file |
| 66 | def init_db_uri(self): | 66 | def init_db_uri(self): |
| 67 | with open('db/db.yml', 'r') as f: | 67 | with open('db/db.yml', 'r') as f: |
| 68 | db_config = yaml.load(f, Loader=yaml.FullLoader) | 68 | db_config = yaml.load(f, Loader=yaml.FullLoader) |
| 69 | return db_config | 69 | return db_config |
| 70 | 70 | ||
| 71 | # creating a connection | 71 | # creating a connection |
| 72 | def init_db(self, app): | 72 | def init_db(self, app): |
| 73 | db_config = self.init_db_uri() | 73 | db_config = self.init_db_uri() |
| 74 | db_uri = f"mysql+{db_config['driver']}://{db_config['user']}:{db_config['password']}@{db_config['host']}/{db_config['dbname']}" | 74 | db_uri = f"mysql+{db_config['driver']}://{db_config['user']}:{db_config['password']}@{db_config['host']}/{db_config['dbname']}" |
| 75 | app.config['SQLALCHEMY_DATABASE_URI'] = db_uri | 75 | app.config['SQLALCHEMY_DATABASE_URI'] = db_uri |
| 76 | db_path = '../db/database.db' | 76 | db_path = '../db/database.db' |
| 77 | app.config['SQLALCHEMY_BINDS'] = {'db2': f'sqlite:///{db_path}'} | 77 | app.config['SQLALCHEMY_BINDS'] = {'db2': f'sqlite:///{db_path}'} |
| 78 | self.db.init_app(app) | 78 | self.db.init_app(app) |
| 79 | 79 | ||
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | 83 |
main.py
| 1 | from flask_cors import CORS | 1 | from flask_cors import CORS |
| 2 | from flask_babel import Babel | 2 | from flask_babel import Babel |
| 3 | #from k2.k2inst import * | 3 | #from k2.k2inst import * |
| 4 | from flask import Flask | 4 | from flask import Flask |
| 5 | from k2.k2cfg import k2 | 5 | from k2.k2cfg import k2 |
| 6 | 6 | ||
| 7 | # configure flask app | 7 | # configure flask app |
| 8 | app = Flask(__name__) | 8 | app = Flask(__name__) |
| 9 | app.config['SECRET_KEY'] = k2.secret_key | 9 | app.config['SECRET_KEY'] = k2.secret_key |
| 10 | #app.config.from_object(k2) | 10 | #app.config.from_object(k2) |
| 11 | 11 | ||
| 12 | # init db | 12 | # init db |
| 13 | k2().init_db(app) | 13 | k2().init_db(app) |
| 14 | CORS(app) | 14 | CORS(app) |
| 15 | 15 | ||
| 16 | #translate | 16 | #translate |
| 17 | k2.load_babel_translation_directories() | 17 | k2.load_babel_translation_directories() |
| 18 | app.config['BABEL_TRANSLATION_DIRECTORIES'] = k2.babel_translation_directories | 18 | app.config['BABEL_TRANSLATION_DIRECTORIES'] = k2.babel_translation_directories |
| 19 | babel = Babel(app) | 19 | babel = Babel(app) |
| 20 | babel.init_app(app, locale_selector=k2.get_locale) | 20 | babel.init_app(app, locale_selector=k2.get_locale) |
| 21 | 21 | ||
| 22 | #menu | 22 | #menu |
| 23 | k2.search_menu_elenents() | 23 | k2.get_menu_elenents() |
| 24 | 24 | ||
| 25 | # flask routing | 25 | # flask routing |
| 26 | from routes import * | 26 | from routes import * |
| 27 | 27 | ||
| 28 | if __name__ == '__main__': | 28 | if __name__ == '__main__': |
| 29 | app.run(debug=True, port=5000) | 29 | app.run(debug=True, port=5000) |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | 33 |