Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
91d7e7bb
Commit
91d7e7bb
authored
Nov 07, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
04609416
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
129 additions
and
7 deletions
+129
-7
application/extensions/init_celery.py
application/extensions/init_celery.py
+52
-0
application/extensions/init_sqlalchemy.py
application/extensions/init_sqlalchemy.py
+2
-2
application/utils/__init__.py
application/utils/__init__.py
+1
-1
application/utils/dsn/__init__.py
application/utils/dsn/__init__.py
+2
-1
application/utils/dsn/redis.py
application/utils/dsn/redis.py
+69
-0
application/utils/dsn/sql.py
application/utils/dsn/sql.py
+3
-3
No files found.
application/extensions/init_celery.py
0 → 100644
View file @
91d7e7bb
#!/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
application/extensions/init_sqlalchemy.py
View file @
91d7e7bb
...
...
@@ -13,7 +13,7 @@
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
application.utils
import
DatabaseU
RI
from
application.utils
import
DatabaseU
ri
db
=
SQLAlchemy
()
...
...
@@ -26,7 +26,7 @@ def init_database(app: Flask) -> None:
:return: None
"""
cfg
=
app
.
config
.
Database
uri
=
DatabaseU
RI
(
uri
=
DatabaseU
ri
(
db_type
=
cfg
.
Type
,
username
=
cfg
.
Username
,
password
=
cfg
.
Password
,
...
...
application/utils/__init__.py
View file @
91d7e7bb
...
...
@@ -10,5 +10,5 @@
# @Description :
"""
from
.dsn
import
DatabaseU
RI
from
.dsn
import
DatabaseU
ri
from
.elasticsearch
import
ElasticsearchUtil
application/utils/dsn/__init__.py
View file @
91d7e7bb
...
...
@@ -10,4 +10,5 @@
# @Description :
"""
from
.dsn
import
DatabaseURI
from
.redis
import
RedisUri
from
.sql
import
DatabaseUri
application/utils/dsn/redis.py
0 → 100644
View file @
91d7e7bb
#!/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
()
application/utils/dsn/
dsn
.py
→
application/utils/dsn/
sql
.py
View file @
91d7e7bb
...
...
@@ -6,12 +6,12 @@
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/29 00:36
# @File :
dsn
.py
# @File :
sql
.py
# @Description :
"""
class
DatabaseU
RI
:
class
DatabaseU
ri
:
"""
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
'<DatabaseU
RI
(
{
self
.
db_type
}
)>'
return
f
'<DatabaseU
ri
(
{
self
.
db_type
}
)>'
def
__str__
(
self
):
return
self
.
create
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment