Skip to content

Commit 4ef1bf0

Browse files
authored
Merge pull request #84 from nineaiyu/dev
优化代码
2 parents ae1a6df + b0b22d8 commit 4ef1bf0

File tree

12 files changed

+97
-37
lines changed

12 files changed

+97
-37
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM nineaiyu/xadmin-server-base:20241225_013140 AS stage-build
1+
FROM nineaiyu/xadmin-server-base:20250224_065028 AS stage-build
22
ARG VERSION
33

44
WORKDIR /data/xadmin-server
@@ -11,7 +11,7 @@ RUN echo > config.yml \
1111
sed -i "s@VERSION = .*@VERSION = '${VERSION}'@g" server/const.py; \
1212
fi
1313

14-
FROM python:3.13.1-slim
14+
FROM python:3.13.2-slim
1515

1616
ENV LANG=en_US.UTF-8 \
1717
PATH=/data/py3/bin:$PATH

Dockerfile-base

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.13.1-slim
1+
FROM python:3.13.2-slim
22

33
# Install APT dependencies
44
ARG DEPENDENCIES=" \

Dockerfile-dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM registry.cn-beijing.aliyuncs.com/nineaiyu/python:3.13.1-slim
1+
FROM registry.cn-beijing.aliyuncs.com/nineaiyu/python:3.13.2-slim
22

33
# add pip cn mirrors
44
ARG PIP_MIRROR=https://pypi.tuna.tsinghua.edu.cn/simple

common/core/db/utils.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
from contextlib import contextmanager
1010

11-
from django.db import connections, transaction
11+
from django.db import connections, transaction, connection
1212
from django.db.models import Q
1313

1414

@@ -114,13 +114,25 @@ def close_old_connections():
114114

115115
@contextmanager
116116
def safe_db_connection():
117-
close_old_connections()
118-
yield
119-
close_old_connections()
117+
in_atomic_block = connection.in_atomic_block # 当前是否处于事务中
118+
autocommit = transaction.get_autocommit() # 是否启用了自动提交
119+
created = False
120+
121+
try:
122+
if not connection.is_usable():
123+
connection.close()
124+
connection.connect()
125+
created = True
126+
yield
127+
finally:
128+
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
129+
if created and not in_atomic_block and autocommit:
130+
print("close connection in safe_db_connection")
131+
close_old_connections()
120132

121133

122134
@contextmanager
123-
def open_db_connection(alias='default'):
135+
def open_db_connection(alias="default"):
124136
connection = transaction.get_connection(alias)
125137
try:
126138
connection.connect()

common/core/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ def upload_directory_path(instance, filename):
102102
tmp_name = f"{filename}_{time.time()}"
103103
new_filename = f"{uuid.uuid5(uuid.NAMESPACE_DNS, tmp_name).__str__().replace('-', '')}.{prefix}"
104104
labels = instance._meta.label_lower.split('.')
105-
return os.path.join(labels[0], labels[1], str(instance.pk), new_filename)
105+
if creator := getattr(instance, "creator", None):
106+
creator_pk = creator.pk
107+
else:
108+
creator_pk = 0
109+
return os.path.join(labels[0], labels[1], str(creator_pk), new_filename)

common/fields/char.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from common.base.utils import AESCipher
1313

1414

15-
class AESCharField(models.CharField):
15+
class AESField(models.Field):
1616

1717
def __init__(self, *args, **kwargs):
1818
if 'prefix' in kwargs:
@@ -21,10 +21,10 @@ def __init__(self, *args, **kwargs):
2121
else:
2222
self.prefix = "aes:::"
2323
self.cipher = AESCipher(settings.SECRET_KEY)
24-
super(AESCharField, self).__init__(*args, **kwargs)
24+
super(AESField, self).__init__(*args, **kwargs)
2525

2626
def deconstruct(self):
27-
name, path, args, kwargs = super(AESCharField, self).deconstruct()
27+
name, path, args, kwargs = super(AESField, self).deconstruct()
2828
if self.prefix != "aes:::":
2929
kwargs['prefix'] = self.prefix
3030
return name, path, args, kwargs
@@ -58,3 +58,11 @@ def get_prep_value(self, value):
5858
elif value is not None:
5959
raise TypeError(_("{} is not a valid value for AESCharField").format(value))
6060
return value
61+
62+
63+
class AESCharField(AESField, models.CharField):
64+
pass
65+
66+
67+
class AESTextField(AESField, models.TextField):
68+
pass

common/fields/image.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# date : 1/17/2024
77
import os
88

9+
from django.core.files.storage import default_storage, FileSystemStorage
910
from django.db import models
1011
from django.db.models.fields.files import ImageFieldFile
1112
from imagekit.cachefiles import ImageCacheFile
@@ -38,7 +39,10 @@ def get_thumbnail(source, index, force=False):
3839
return file.name
3940

4041

42+
4143
class ProcessedImageFieldFile(ImageFieldFile):
44+
is_local_storage = isinstance(default_storage, FileSystemStorage)
45+
4246
def save(self, name, content, save=True):
4347
filename, ext = os.path.splitext(name)
4448
spec = self.field.get_spec(source=content)
@@ -52,19 +56,20 @@ def delete(self, save=True):
5256
if hasattr(self, "_dimensions_cache"):
5357
del self._dimensions_cache
5458
name = self.name
55-
try:
56-
for i in self.field.scales:
57-
self.name = f"{name.split('.')[0]}_{i}.jpg"
58-
super().delete(False)
59-
except Exception as e:
60-
pass
59+
if self.is_local_storage:
60+
try:
61+
for i in self.field.scales:
62+
self.name = f"{name.split('.')[0]}_{i}.jpg"
63+
super().delete(False)
64+
except Exception as e:
65+
pass
6166
self.name = name
6267
super().delete(save)
6368

6469
@property
6570
def url(self):
6671
url: str = super().url
67-
if url.endswith('.png'):
72+
if self.is_local_storage and url.endswith('.png'):
6873
return url.replace('.png', '_1.jpg')
6974
return url
7075

config_example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
SECRET_KEY:
55

66
# Development env open this, when error occur display the full process track, Production disable it
7-
# DEBUG 模式 开启DEBUG后遇到错误时可以看到更多日志,正式服要禁用
7+
# DEBUG 模式 开启DEBUG后遇到错误时可以看到更多日志,正式服要禁用,开发阶段,需要取消该注释,否则会导致前端ws连接失败
88
# DEBUG: true
99

1010
# DEBUG, INFO, WARNING, ERROR, CRITICAL can set. See https://docs.djangoproject.com/zh-hans/5.0/topics/logging/
@@ -24,7 +24,7 @@ SECRET_KEY:
2424
# SQLite setting:
2525
# 使用单文件sqlite数据库
2626
# DB_ENGINE: sqlite3
27-
# DB_NAME:
27+
# DB_DATABASE:
2828
# MySQL or postgres setting like:
2929
# DB_ENGINE can set mysql, oracle, postgresql, sqlite3
3030

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ services:
2020
- net
2121

2222
postgresql:
23-
image: registry.cn-beijing.aliyuncs.com/nineaiyu/postgres:16.5
23+
image: registry.cn-beijing.aliyuncs.com/nineaiyu/postgres:16.7
2424
container_name: xadmin-postgresql
2525
restart: always
2626
command: [ "postgres","-c","max_connections=8000" ]
@@ -41,7 +41,7 @@ services:
4141
- net
4242

4343
redis:
44-
image: registry.cn-beijing.aliyuncs.com/nineaiyu/redis:7.4.1
44+
image: registry.cn-beijing.aliyuncs.com/nineaiyu/redis:7.4.2
4545
container_name: xadmin-redis
4646
hostname: xadmin-redis
4747
restart: always

requirements.txt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
django==5.1.4
1+
django==5.1.6
22
djangorestframework==3.15.2
3-
django-cors-headers==4.6.0
4-
django-filter==24.3
5-
mysqlclient==2.2.6
3+
django-cors-headers==4.7.0
4+
django-filter==25.1
5+
mysqlclient==2.2.7
66
psycopg2-binary==2.9.10
77
django-redis==5.4.0
88
pycryptodomex==3.21.0
9-
djangorestframework-simplejwt==5.3.1
9+
djangorestframework-simplejwt==5.4.0
1010
celery==5.4.0
1111
django-celery-beat==2.7.0
1212
django-celery-results==2.5.1
1313
flower==2.0.1
1414
python-daemon==3.1.2
1515
gunicorn==23.0.0
1616
django-proxy==1.3.0
17-
psutil==6.1.0
18-
uvicorn==0.32.1
17+
psutil==6.1.1
18+
uvicorn==0.34.0
1919
daphne==4.1.2
2020
channels==4.2.0
2121
channels-redis==4.2.1
2222
django-ranged-response==0.2.0
2323
user-agents==2.2.0
2424
aiofiles==24.1.0
25-
websockets==14.1
25+
websockets==15.0
2626
django-imagekit==5.0.0
2727
pilkit==3.0
2828
drf-spectacular==0.28.0
29-
drf-spectacular-sidecar==2024.12.1
29+
drf-spectacular-sidecar==2025.2.1
3030
openpyxl==3.2.0b1
3131
pyzipper==0.3.6
3232
unicodecsv==0.14.1
3333
chardet==5.2.0
3434
pyexcel==0.7.1
3535
pyexcel-xlsx==0.6.0
36-
alibabacloud-dysmsapi20170525==3.1.0
37-
phonenumbers==8.13.51
36+
alibabacloud-dysmsapi20170525==3.1.1
37+
phonenumbers==8.13.55
3838
pycountry==24.6.1
3939
geoip2==4.8.1
4040
ipip-ipdb==1.6.1
4141
requests==2.32.3
42-
html2text==2024.2.26
42+
html2text==2024.2.26
43+
pyotp==2.9.0

0 commit comments

Comments
 (0)