From c7e7528d13ba5863a0bd2f7665cf25d3bc2769f4 Mon Sep 17 00:00:00 2001 From: hhyo Date: Fri, 3 May 2019 12:42:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9E=E4=BE=8B=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/admin.py | 30 +++++++++++++++++++++------ sql/fixtures/initial_data.json | 22 ++++++++++++++++++++ sql/instance.py | 12 ++++++++--- sql/models.py | 31 ++++++++++++++++++++++++++++ sql/templates/instance.html | 35 ++++++++++++++++++++++++-------- sql/views.py | 6 ++++-- src/init_sql/v1.5.3_v1.5.4.sql | 37 +++++++++++++++++++++++++++++++++- 7 files changed, 153 insertions(+), 20 deletions(-) create mode 100644 sql/fixtures/initial_data.json diff --git a/sql/admin.py b/sql/admin.py index 1e087cd8ea..399fb0f7bd 100644 --- a/sql/admin.py +++ b/sql/admin.py @@ -5,7 +5,7 @@ # Register your models here. from .models import Users, Instance, SqlWorkflow, SqlWorkflowContent, QueryLog, DataMaskingColumns, DataMaskingRules, \ AliyunAccessKey, AliyunRdsConfig, ResourceGroup, ResourceGroupRelations, QueryPrivilegesApply, QueryPrivileges, \ - WorkflowAudit, WorkflowLog, ParamTemplate, ParamHistory + WorkflowAudit, WorkflowLog, ParamTemplate, ParamHistory, InstanceTag, InstanceTagRelations # 用户管理 @@ -39,9 +39,18 @@ class ResourceGroupRelationsAdmin(admin.ModelAdmin): list_display = ('object_type', 'object_id', 'object_name', 'group_id', 'group_name', 'create_time') -# 阿里云实例配置 -class AliRdsConfigInline(admin.TabularInline): - model = AliyunRdsConfig +# 实例标签配置 +@admin.register(InstanceTag) +class InstanceTagAdmin(admin.ModelAdmin): + list_display = ('id', 'tag_code', 'tag_name', 'active', 'create_time') + list_display_links = ('id', 'tag_code',) + + +# 实例标签关系配置 +@admin.register(InstanceTagRelations) +class InstanceTagRelationsAdmin(admin.ModelAdmin): + list_display = ('instance', 'instance_tag', 'active', 'create_time') + list_filter = ('instance', 'instance_tag', 'active') # 实例管理 @@ -49,8 +58,17 @@ class AliRdsConfigInline(admin.TabularInline): class InstanceAdmin(admin.ModelAdmin): list_display = ('id', 'instance_name', 'db_type', 'type', 'host', 'port', 'user', 'create_time') search_fields = ['instance_name', 'host', 'port', 'user'] - list_filter = ('db_type', 'type',) - inlines = [AliRdsConfigInline] + list_filter = ('db_type', 'type') + + # 阿里云实例关系配置 + class AliRdsConfigInline(admin.TabularInline): + model = AliyunRdsConfig + + # 实例标签关系配置 + class InstanceTagRelationsInline(admin.TabularInline): + model = InstanceTagRelations + + inlines = [InstanceTagRelationsInline, AliRdsConfigInline] # SQL工单内容 diff --git a/sql/fixtures/initial_data.json b/sql/fixtures/initial_data.json new file mode 100644 index 0000000000..0353c5ea53 --- /dev/null +++ b/sql/fixtures/initial_data.json @@ -0,0 +1,22 @@ +[ + { + "model": "sql.InstanceTag", + "pk": 1, + "fields": { + "tag_code": "write", + "tag_name": "支持上线", + "active": true, + "create_time": "2019-05-03" + } + }, + { + "model": "sql.InstanceTag", + "pk": 2, + "fields": { + "tag_code": "read", + "tag_name": "支持查询", + "active": true, + "create_time": "2019-05-03" + } + } +] diff --git a/sql/instance.py b/sql/instance.py index c91b96f92a..0c7d527eac 100644 --- a/sql/instance.py +++ b/sql/instance.py @@ -14,13 +14,14 @@ from .models import Instance, ParamTemplate, ParamHistory -# 获取实例列表 @permission_required('sql.menu_instance', raise_exception=True) def lists(request): + """获取实例列表""" limit = int(request.POST.get('limit')) offset = int(request.POST.get('offset')) type = request.POST.get('type') db_type = request.POST.get('db_type') + tags = request.POST.getlist('tags[]') limit = offset + limit search = request.POST.get('search', '') @@ -34,6 +35,10 @@ def lists(request): # 过滤数据库类型 if db_type: instances = instances.filter(db_type=db_type) + # 过滤标签,返回同时包含全部标签的实例,循环会生成多表JOIN,如果数据量大会存在效率问题 + if tags: + for tag in tags: + instances = instances.filter(instancetagrelations__instance_tag=tag) count = instances.count() instances = instances[offset:limit].values("id", "instance_name", "db_type", "type", "host", "port", "user") @@ -45,9 +50,9 @@ def lists(request): content_type='application/json') -# 获取实例用户列表 @permission_required('sql.menu_instance', raise_exception=True) def users(request): + """获取实例用户列表""" instance_id = request.POST.get('instance_id') try: instance = Instance.objects.get(id=instance_id) @@ -184,9 +189,9 @@ def param_edit(request): return HttpResponse(json.dumps(result), content_type='application/json') -# 对比实例schema信息 @permission_required('sql.menu_schemasync', raise_exception=True) def schemasync(request): + """对比实例schema信息""" instance_name = request.POST.get('instance_name') db_name = request.POST.get('db_name') target_instance_name = request.POST.get('target_instance_name') @@ -317,6 +322,7 @@ def instance_resource(request): def describe(request): + """获取表结构""" instance_name = request.POST.get('instance_name') try: instance = Instance.objects.get(instance_name=instance_name) diff --git a/sql/models.py b/sql/models.py index d9e8b3a1c0..3d0ecefc84 100644 --- a/sql/models.py +++ b/sql/models.py @@ -117,6 +117,37 @@ def save(self, *args, **kwargs): super(Instance, self).save(*args, **kwargs) +class InstanceTag(models.Model): + """实例标签配置""" + tag_code = models.CharField('标签代码', max_length=20, unique=True) + tag_name = models.CharField('标签名称', max_length=20, unique=True) + active = models.BooleanField('激活状态', default=True) + create_time = models.DateTimeField('创建时间', auto_now_add=True) + + def __str__(self): + return self.tag_name + + class Meta: + managed = True + db_table = 'sql_instance_tag' + verbose_name = u'实例标签' + verbose_name_plural = u'实例标签' + + +class InstanceTagRelations(models.Model): + """实例标签关系""" + instance = models.ForeignKey(Instance, on_delete=models.CASCADE) + instance_tag = models.ForeignKey(InstanceTag, on_delete=models.CASCADE) + active = models.BooleanField('激活状态', default=True) + create_time = models.DateTimeField('创建时间', auto_now_add=True) + + class Meta: + managed = True + db_table = 'sql_instance_tag_relations' + verbose_name = u'实例标签关系' + verbose_name_plural = u'实例标签关系' + + SQL_WORKFLOW_CHOICES = ( ('workflow_finish', _('workflow_finish')), ('workflow_abort', _('workflow_abort')), diff --git a/sql/templates/instance.html b/sql/templates/instance.html index 9ef58b1918..451fbed9cc 100644 --- a/sql/templates/instance.html +++ b/sql/templates/instance.html @@ -2,23 +2,31 @@ {% block content %} -
-
- +
-
- +
+
+ +