Skip to content

Commit

Permalink
Merge pull request #816 from longguikeji/feature-168
Browse files Browse the repository at this point in the history
feat: 🎸 增加frontend_url 查看和修改接口, 修改get_frontend_host逻辑
  • Loading branch information
fanhe-lg authored May 9, 2022
2 parents f347df0 + 55d5b3b commit af44d47
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/v1/urls/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

urlpatterns = [
path('get_frontendurl/', views_setup.GetFrontendUrlAPIView.as_view(), name='get-frontendurl'),
path('set_frontendurl/', views_setup.SetFrontendUrlAPIView.as_view(), name='set-frontendurl'),
]
44 changes: 43 additions & 1 deletion api/v1/views/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from api.v1.serializers.setup import (
FrontendUrlSerializer,
)
from rest_framework.permissions import IsAuthenticated
from system.permission import IsSuperAdmin
from rest_framework_expiring_authtoken.authentication import ExpiringTokenAuthentication
from config.models import PlatformConfig
from common.code import Code
from django.utils.translation import gettext_lazy as _


@extend_schema(
Expand All @@ -18,7 +24,43 @@ class GetFrontendUrlAPIView(APIView):

@extend_schema(responses=FrontendUrlSerializer)
def get(self, request):
url = get_app_config().get_frontend_host()
# url = get_app_config().get_frontend_host()
# return JsonResponse({
# 'url': url
# }, safe=False)
url = ''
plat_config = PlatformConfig.valid_objects.filter().first()
if plat_config:
url = plat_config.frontend_url
return JsonResponse({
'url': url
}, safe=False)


class SetFrontendUrlAPIView(APIView):
permission_classes = [IsAuthenticated, IsSuperAdmin]
authentication_classes = [ExpiringTokenAuthentication]

@extend_schema(responses=FrontendUrlSerializer)
def post(self, request):
# url = get_app_config().get_frontend_host()
# return JsonResponse({
# 'url': url
# }, safe=False)
url = request.data.get('url')
if not url:
return JsonResponse(
data={
'error': Code.POST_DATA_ERROR.value,
'message': _('empty url value'),
}
)
url = url.rstrip('/')
plat_config = PlatformConfig.valid_objects.filter().first()
if not plat_config:
plat_config = PlatformConfig.valid_objects.create(frontend_url=url)
else:
plat_config.frontend_url = url
plat_config.save()

return JsonResponse(data={'error': Code.OK.value, 'data': {'url': url}})
5 changes: 5 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def get_host(self, schema=True):
return self.host

def get_frontend_host(self, schema=True):
from config.models import PlatformConfig
plat_config = PlatformConfig.valid_objects.filter().first()
if plat_config:
return plat_config.frontend_url

if schema:
return '{}://{}'.format(
'https' if self.https_enabled else 'http', self.frontend_host
Expand Down
29 changes: 29 additions & 0 deletions config/migrations/0004_platformconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.10 on 2022-02-17 07:07

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('config', '0003_alter_privacynotice_tenant'),
]

operations = [
migrations.CreateModel(
name='PlatformConfig',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, unique=True, verbose_name='UUID')),
('is_del', models.BooleanField(default=False, verbose_name='是否删除')),
('is_active', models.BooleanField(default=True, verbose_name='是否可用')),
('updated', models.DateTimeField(auto_now=True, null=True, verbose_name='更新时间')),
('created', models.DateTimeField(auto_now_add=True, null=True, verbose_name='创建时间')),
('frontend_url', models.URLField(blank=True, default='', max_length=128, null=True, verbose_name='ArkId访问地址')),
],
options={
'abstract': False,
},
),
]
6 changes: 6 additions & 0 deletions config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ def check_pwd(self, pwd):
return True
else:
return False

class PlatformConfig(BaseModel):

frontend_url = models.URLField(
verbose_name='ArkId访问地址', max_length=128, blank=True, null=True, default=''
)

0 comments on commit af44d47

Please sign in to comment.