From 91d7e7bb246dd285dc6fa7a18031422c5dc3c0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Tue, 7 Nov 2023 16:14:33 +0800 Subject: [PATCH] Update project --- application/extensions/init_celery.py | 52 +++++++++++++++++ application/extensions/init_sqlalchemy.py | 4 +- application/utils/__init__.py | 2 +- application/utils/dsn/__init__.py | 3 +- application/utils/dsn/redis.py | 69 +++++++++++++++++++++++ application/utils/dsn/{dsn.py => sql.py} | 6 +- 6 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 application/extensions/init_celery.py create mode 100644 application/utils/dsn/redis.py rename application/utils/dsn/{dsn.py => sql.py} (96%) diff --git a/application/extensions/init_celery.py b/application/extensions/init_celery.py new file mode 100644 index 0000000..ee4e5a3 --- /dev/null +++ b/application/extensions/init_celery.py @@ -0,0 +1,52 @@ +#!/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 import Celery +from celery.schedules import crontab + + +def init_celery(app: Flask) -> None: + """ + Initialize Celery for the Flask application. + + :param app: The Flask application. + :type app: Flask + """ + celery = Celery( + app.import_name, + backend=app.config['CELERY_RESULT_BACKEND'], + broker=app.config['CELERY_BROKER_URL'] + ) + celery.conf.update(app.config) + + class ContextTask(celery.Task): + def __call__(self, *args, **kwargs): + with app.app_context(): + return self.run(*args, **kwargs) + + celery.Task = ContextTask + + @celery.task() + def update_database(): + # Add your database operations here + pass + + celery.conf.beat_schedule = { + 'update-database-every-day': { + 'task': 'update_database', + 'schedule': crontab(hour=0, minute=0) # Execute daily at midnight + } + } + + app.celery = celery diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index f09733f..e4225b4 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -13,7 +13,7 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from application.utils import DatabaseURI +from application.utils import DatabaseUri db = SQLAlchemy() @@ -26,7 +26,7 @@ def init_database(app: Flask) -> None: :return: None """ cfg = app.config.Database - uri = DatabaseURI( + uri = DatabaseUri( db_type=cfg.Type, username=cfg.Username, password=cfg.Password, diff --git a/application/utils/__init__.py b/application/utils/__init__.py index 734a651..8c88e8a 100644 --- a/application/utils/__init__.py +++ b/application/utils/__init__.py @@ -10,5 +10,5 @@ # @Description : """ -from .dsn import DatabaseURI +from .dsn import DatabaseUri from .elasticsearch import ElasticsearchUtil diff --git a/application/utils/dsn/__init__.py b/application/utils/dsn/__init__.py index de7c835..edbad07 100644 --- a/application/utils/dsn/__init__.py +++ b/application/utils/dsn/__init__.py @@ -10,4 +10,5 @@ # @Description : """ -from .dsn import DatabaseURI +from .redis import RedisUri +from .sql import DatabaseUri diff --git a/application/utils/dsn/redis.py b/application/utils/dsn/redis.py new file mode 100644 index 0000000..0dbc380 --- /dev/null +++ b/application/utils/dsn/redis.py @@ -0,0 +1,69 @@ +#!/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 15:47 +# @File : redis.py +# @Description : +""" + + +class RedisUri: + """ + A class to generate a Redis connection URI. + """ + + def __init__(self, host='localhost', port=6379, db=0, username=None, password=None): + """ + Initializes a new instance of the RedisUri class. + + :param host: The hostname of the Redis server. Default is 'localhost'. + :type host: str + :param port: The port number to connect to the Redis server. Default is 6379. + :type port: int + :param db: The database number to connect to on the Redis server. Default is 0. + :type db: int + :param username: The username for authentication to the Redis server. Default is None. + :type username: str + :param password: The password for authentication to the Redis server. Default is None. + :type password: str + """ + self.host = host + self.port = port + self.db = db + self.username = username + self.password = password + + def create(self) -> str: + """ + Creates and returns the Redis connection URI. + + :return: The Redis connection URI. + :rtype: str + """ + if self.username and self.password: + return f'redis://{self.username}:{self.password}@{self.host}:{self.port}/{self.db}' + else: + return f'redis://{self.host}:{self.port}/{self.db}' + + def __repr__(self): + """ + Returns a formal string representation of the RedisUri object. + + :return: The formal string representation of the RedisUri object. + :rtype: str + """ + return (f'RedisUri(host={self.host}, port={self.port}, db={self.db}, username={self.username}, ' + f'password={self.password})') + + def __str__(self): + """ + Returns a string representation of the RedisUri object. + + :return: The string representation of the RedisUri object. + :rtype: str + """ + return self.create() diff --git a/application/utils/dsn/dsn.py b/application/utils/dsn/sql.py similarity index 96% rename from application/utils/dsn/dsn.py rename to application/utils/dsn/sql.py index 8fb56fd..ef2d914 100644 --- a/application/utils/dsn/dsn.py +++ b/application/utils/dsn/sql.py @@ -6,12 +6,12 @@ # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/29 00:36 -# @File : dsn.py +# @File : sql.py # @Description : """ -class DatabaseURI: +class DatabaseUri: """ The DatabaseURI class is a utility for generating SQLAlchemy database connection strings for various types of databases. @@ -60,7 +60,7 @@ class DatabaseURI: return f'{self.db_type}://{self.username}:{self.password}@{self.host}:{self.port}/{self.db}' def __repr__(self): - return f'' + return f'' def __str__(self): return self.create() -- GitLab