feat: banner docker RBD
This commit is contained in:
+2
-1
@@ -18,6 +18,7 @@ from rest_framework.permissions import IsAuthenticated
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from apps.user.models import User, Institution, Department, TeacherStudentRelation
|
from apps.user.models import User, Institution, Department, TeacherStudentRelation
|
||||||
|
from apps.user.views import _build_banner_url
|
||||||
from apps.case.models import CaseBase
|
from apps.case.models import CaseBase
|
||||||
from apps.training.models import TrainingRecord, TrainingSession
|
from apps.training.models import TrainingRecord, TrainingSession
|
||||||
from apps.user.stats import STANDARD_DIMS, DIMENSION_MAP, _dimension_scores, _NORM_TOTAL_EXPR
|
from apps.user.stats import STANDARD_DIMS, DIMENSION_MAP, _dimension_scores, _NORM_TOTAL_EXPR
|
||||||
@@ -282,7 +283,7 @@ def hospital_overview(request):
|
|||||||
profile = {
|
profile = {
|
||||||
'institution_id': inst_id,
|
'institution_id': inst_id,
|
||||||
'name': inst.name if inst else '',
|
'name': inst.name if inst else '',
|
||||||
'logo': inst.banner_url if inst else '',
|
'logo': _build_banner_url(request, inst.banner_url) if inst else '', # 完整 URL(含 STATIC_PUBLIC_PREFIX)
|
||||||
'level': inst.level if inst else '',
|
'level': inst.level if inst else '',
|
||||||
'cooperation_days': (now - inst.created_at).days if inst and inst.created_at else None,
|
'cooperation_days': (now - inst.created_at).days if inst and inst.created_at else None,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,16 +102,18 @@ class CmsInstitutionViewSet(viewsets.ModelViewSet):
|
|||||||
if ext not in ALLOWED_BANNER_EXT:
|
if ext not in ALLOWED_BANNER_EXT:
|
||||||
raise AppError('CMS_BANNER_BAD_TYPE', '仅支持 png/jpg/jpeg/webp 图片', status_code=400)
|
raise AppError('CMS_BANNER_BAD_TYPE', '仅支持 png/jpg/jpeg/webp 图片', status_code=400)
|
||||||
|
|
||||||
rel_dir = 'institutions'
|
# 上传图片落在 static/uploads/ 下——该目录由 Docker 持久卷挂载(容器重启不丢),
|
||||||
|
# 与镜像内置静态(如 institutions/default_hospital.png)分开,避免被卷覆盖。
|
||||||
|
rel_dir = 'uploads/institutions'
|
||||||
base_static = settings.STATICFILES_DIRS[0]
|
base_static = settings.STATICFILES_DIRS[0]
|
||||||
target_dir = os.path.join(base_static, rel_dir)
|
target_dir = os.path.join(base_static, 'uploads', 'institutions')
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
filename = f'inst_{inst.id}_banner{ext}'
|
filename = f'inst_{inst.id}_banner{ext}'
|
||||||
with open(os.path.join(target_dir, filename), 'wb') as fh:
|
with open(os.path.join(target_dir, filename), 'wb') as fh:
|
||||||
for chunk in file.chunks():
|
for chunk in file.chunks():
|
||||||
fh.write(chunk)
|
fh.write(chunk)
|
||||||
|
|
||||||
inst.banner_url = f'{rel_dir}/{filename}'
|
inst.banner_url = f'{rel_dir}/{filename}' # 例:uploads/institutions/inst_17_banner.png
|
||||||
inst.save(update_fields=['banner_url', 'updated_at'])
|
inst.save(update_fields=['banner_url', 'updated_at'])
|
||||||
|
|
||||||
serializer = self.get_serializer(inst)
|
serializer = self.get_serializer(inst)
|
||||||
|
|||||||
@@ -205,9 +205,10 @@ class CmsInstitutionBannerTest(CacheTestCase):
|
|||||||
self.assertEqual(resp.status_code, 200, resp.content)
|
self.assertEqual(resp.status_code, 200, resp.content)
|
||||||
body = resp.json()
|
body = resp.json()
|
||||||
self.assertEqual(body['message'], '上传成功')
|
self.assertEqual(body['message'], '上传成功')
|
||||||
self.assertTrue(body['banner_url'].endswith(f'/static/institutions/inst_{self.inst.id}_banner.png'))
|
# 上传图片落在持久卷目录 static/uploads/institutions/(与镜像内置静态分开)
|
||||||
|
self.assertTrue(body['banner_url'].endswith(f'/static/uploads/institutions/inst_{self.inst.id}_banner.png'))
|
||||||
self.inst.refresh_from_db()
|
self.inst.refresh_from_db()
|
||||||
self.assertEqual(self.inst.banner_url, f'institutions/inst_{self.inst.id}_banner.png')
|
self.assertEqual(self.inst.banner_url, f'uploads/institutions/inst_{self.inst.id}_banner.png')
|
||||||
|
|
||||||
def test_upload_no_file(self):
|
def test_upload_no_file(self):
|
||||||
with override_settings(STATICFILES_DIRS=[self._tmp]):
|
with override_settings(STATICFILES_DIRS=[self._tmp]):
|
||||||
|
|||||||
Reference in New Issue
Block a user