From 4349e6348d9c5f0d4967e063b876dadc629c9fea Mon Sep 17 00:00:00 2001 From: cuiweizhi <560397@gree.com.cn> Date: Sat, 11 Nov 2023 14:24:31 +0800 Subject: [PATCH] Update project --- application/__init__.py | 20 ++------ application/common/__init__.py | 4 +- application/common/config.py | 44 +++------------- application/extensions/__init__.py | 2 - application/extensions/init_celery.py | 28 ----------- application/extensions/init_sqlalchemy.py | 3 ++ .../libs/flask_elasticsearch/elasticsearch.py | 2 +- application/libs/flask_loguru/logger.py | 2 +- application/libs/helper/__init__.py | 15 ++++++ application/libs/helper/config.py | 50 +++++++++++++++++++ application/{common => libs/helper}/env.py | 0 application/{common => libs/helper}/file.py | 0 application/libs/tasks/task.py | 10 ---- application/utils/loaders/consul_loader.py | 2 +- application/utils/loaders/yaml_loader.py | 2 +- application/views/user/user.py | 20 ++++---- 16 files changed, 94 insertions(+), 110 deletions(-) delete mode 100644 application/extensions/init_celery.py create mode 100644 application/libs/helper/__init__.py create mode 100644 application/libs/helper/config.py rename application/{common => libs/helper}/env.py (100%) rename application/{common => libs/helper}/file.py (100%) diff --git a/application/__init__.py b/application/__init__.py index 19dfdf1..319d05d 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -18,7 +18,7 @@ from application.extensions import init_plugs from application.views import init_views from application.script import init_script from application.utils import make_celery -from celery.schedules import crontab +from application.common import config def create_app() -> Flask: @@ -38,22 +38,8 @@ def create_app() -> Flask: # This could include things like database connectors, authentication systems, etc. init_plugs(app) - celery = make_celery(app) - celery.conf.update(app.config) - - @celery.task(bind=True) - def update_database(self): - # Add your database operations here - print("update database") - - celery.conf.beat_schedule = { - 'update-database-every-minute': { - 'task': 'application.__init__.update_database', - 'schedule': crontab(minute='*') # Execute every minute - } - } - - app.celery = celery + # Set universal config + config.Universal = app.config # Register the routes that this application will respond to. # This includes both the route URLs and the handlers for each route. diff --git a/application/common/__init__.py b/application/common/__init__.py index ab260d7..52f613c 100644 --- a/application/common/__init__.py +++ b/application/common/__init__.py @@ -10,6 +10,4 @@ # @Description : """ -from .config import ConfigHelper -from .env import EnvVarHelper -from .file import FileHelper +from .config import Config diff --git a/application/common/config.py b/application/common/config.py index 6b463d5..6da1568 100644 --- a/application/common/config.py +++ b/application/common/config.py @@ -5,46 +5,18 @@ # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com -# @Date : 2023/11/3 19:43 +# @Date : 2023/11/11 14:11 # @File : config.py # @Description : """ -from typing import Any +# config.py +class Config: + """Configuration for the application.""" + def __init__(self): + self.Universal = None -from flask import Flask -from loguru import logger -from application.libs import ConfigKeyError - - -class ConfigHelper: - """ - The ConfigHelper class is a utility for fetching configuration values - from a Flask application. - - :param app: The Flask application instance from which to fetch configuration values. - """ - - def __init__(self, app: Flask): - self.app = app - - def __getattr__(self, name: str) -> Any: - """ - Get a config value as an attribute. - - :param name: The name of the config key. - :return: The value for the provided key, or None if it does not exist. - """ - try: - return self.app.config[name] - except KeyError: - logger.error(f'Key {name} not found in configuration') - raise ConfigKeyError(name) - - def __repr__(self): - return f"" - - def __str__(self): - return f"ConfigHelper for app {self.app}" +# This creates a global instance of the Config class +config = Config() diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index 45921e7..eabf917 100644 --- a/application/extensions/__init__.py +++ b/application/extensions/__init__.py @@ -21,7 +21,6 @@ from .init_migrate import init_migrate from .init_apispec import init_apispec from .init_marshmallow import init_marshmallow from .init_elasticsearch import init_elasticsearch -# from .init_celery import init_celery def init_plugs(app: Flask) -> None: @@ -34,4 +33,3 @@ def init_plugs(app: Flask) -> None: init_marshmallow(app) init_elasticsearch(app) init_cors(app) - # init_celery(app) diff --git a/application/extensions/init_celery.py b/application/extensions/init_celery.py deleted file mode 100644 index 8df4e59..0000000 --- a/application/extensions/init_celery.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/11/7 16:10 -# @File : init_celery.py -# @Description : -""" - -from flask import Flask -from celery.schedules import crontab - -from application.utils import make_celery - - -def init_celery(app: Flask) -> None: - celery = make_celery(app) - app.celery = celery - - celery.conf.beat_schedule = { - 'update-database-every-day': { - 'task': 'application.libs.tasks.task.update_database', - 'schedule': crontab(minute='*') # Execute every minute - } - } diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index e4225b4..e9eaa5b 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -36,4 +36,7 @@ def init_database(app: Flask) -> None: driver=cfg.Driver, ) app.config.setdefault('SQLALCHEMY_DATABASE_URI', uri.create()) + db.app = app db.init_app(app) + with app.app_context(): + db.create_all() diff --git a/application/libs/flask_elasticsearch/elasticsearch.py b/application/libs/flask_elasticsearch/elasticsearch.py index 7be6ee4..e0f5daf 100644 --- a/application/libs/flask_elasticsearch/elasticsearch.py +++ b/application/libs/flask_elasticsearch/elasticsearch.py @@ -19,7 +19,7 @@ from flask import _app_ctx_stack as stack_context from flask_elasticsearch import FlaskElasticsearch as BaseFlaskElasticsearch from loguru import logger -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper class FlaskElasticsearch(BaseFlaskElasticsearch): diff --git a/application/libs/flask_loguru/logger.py b/application/libs/flask_loguru/logger.py index 98b57ff..95a369b 100644 --- a/application/libs/flask_loguru/logger.py +++ b/application/libs/flask_loguru/logger.py @@ -17,7 +17,7 @@ from flask import Flask, request, g from loguru import logger from .format import patching -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper class FlaskLoguru: diff --git a/application/libs/helper/__init__.py b/application/libs/helper/__init__.py new file mode 100644 index 0000000..c304e7b --- /dev/null +++ b/application/libs/helper/__init__.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/11 14:05 +# @File : __init__.py +# @Description : +""" + +from .config import ConfigHelper +from .env import EnvVarHelper +from .file import FileHelper diff --git a/application/libs/helper/config.py b/application/libs/helper/config.py new file mode 100644 index 0000000..6b463d5 --- /dev/null +++ b/application/libs/helper/config.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/3 19:43 +# @File : config.py +# @Description : +""" + + +from typing import Any + +from flask import Flask +from loguru import logger + +from application.libs import ConfigKeyError + + +class ConfigHelper: + """ + The ConfigHelper class is a utility for fetching configuration values + from a Flask application. + + :param app: The Flask application instance from which to fetch configuration values. + """ + + def __init__(self, app: Flask): + self.app = app + + def __getattr__(self, name: str) -> Any: + """ + Get a config value as an attribute. + + :param name: The name of the config key. + :return: The value for the provided key, or None if it does not exist. + """ + try: + return self.app.config[name] + except KeyError: + logger.error(f'Key {name} not found in configuration') + raise ConfigKeyError(name) + + def __repr__(self): + return f"" + + def __str__(self): + return f"ConfigHelper for app {self.app}" diff --git a/application/common/env.py b/application/libs/helper/env.py similarity index 100% rename from application/common/env.py rename to application/libs/helper/env.py diff --git a/application/common/file.py b/application/libs/helper/file.py similarity index 100% rename from application/common/file.py rename to application/libs/helper/file.py diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index 2116426..bc0c3bb 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -9,13 +9,3 @@ # @File : task.py # @Description : """ - - -from flask import current_app - - -@current_app.celery.task -def update_database(): - # Add your database operations here - print(123312312) - pass diff --git a/application/utils/loaders/consul_loader.py b/application/utils/loaders/consul_loader.py index d0ffa44..10f09b5 100644 --- a/application/utils/loaders/consul_loader.py +++ b/application/utils/loaders/consul_loader.py @@ -16,8 +16,8 @@ import requests from dynaconf.base import LazySettings from dynaconf.utils.parse_conf import parse_conf_data -from application.common import EnvVarHelper from application.libs import ConsulConfig +from application.libs.helper import EnvVarHelper IDENTIFIER = "consul_loader" diff --git a/application/utils/loaders/yaml_loader.py b/application/utils/loaders/yaml_loader.py index 0ddcd23..fe92c6e 100644 --- a/application/utils/loaders/yaml_loader.py +++ b/application/utils/loaders/yaml_loader.py @@ -17,8 +17,8 @@ from typing import Union from dynaconf.base import LazySettings from dynaconf.utils.parse_conf import parse_conf_data -from application.common import FileHelper from application.libs import LocalConfig +from application.libs.helper import FileHelper IDENTIFIER = "yaml_loader" diff --git a/application/views/user/user.py b/application/views/user/user.py index dd947cb..b5ed835 100644 --- a/application/views/user/user.py +++ b/application/views/user/user.py @@ -18,7 +18,7 @@ from marshmallow import ValidationError from application.schemas import UserSchema from application.utils import ElasticsearchUtil -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper user_api = Blueprint('user_api', __name__) api_users = Api(user_api) @@ -54,16 +54,16 @@ def create_user(): @user_api.route('/tests', methods=['POST']) def test_user(): - data = request.get_json() - start = data.get('start') - end = data.get('end') - dsl = ElasticsearchUtil.dsl(start, end) - index = current_app.config.Elasticsearch.Index - res = ElasticsearchUtil.search(index, dsl) - print(len(res)) - data = { + # data = request.get_json() + # start = data.get('start') + # end = data.get('end') + # dsl = ElasticsearchUtil.dsl(start, end) + # index = current_app.config.Elasticsearch.Index + # res = ElasticsearchUtil.search(index, dsl) + # print(len(res)) + res = { "user_name": "libai", "user_age": 18, } - res_json = json.dumps(data) + res_json = json.dumps(res) return res_json, 200 -- GitLab