mirror of https://github.com/openspug/spug
				
				
				
			A api add configure center
							parent
							
								
									5ef550160c
								
							
						
					
					
						commit
						0a6b6bc117
					
				| 
						 | 
				
			
			@ -0,0 +1,81 @@
 | 
			
		|||
from django.db import models
 | 
			
		||||
from libs import ModelMixin, human_time
 | 
			
		||||
from apps.account.models import User
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Environment(models.Model, ModelMixin):
 | 
			
		||||
    name = models.CharField(max_length=50)
 | 
			
		||||
    key = models.CharField(max_length=50)
 | 
			
		||||
    desc = models.CharField(max_length=255, null=True)
 | 
			
		||||
    created_at = models.CharField(max_length=20, default=human_time)
 | 
			
		||||
    created_by = models.ForeignKey(User, on_delete=models.PROTECT)
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return f'<Environment {self.name!r}>'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        db_table = 'environments'
 | 
			
		||||
        ordering = ('-id',)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Service(models.Model, ModelMixin):
 | 
			
		||||
    name = models.CharField(max_length=50)
 | 
			
		||||
    key = models.CharField(max_length=50)
 | 
			
		||||
    desc = models.CharField(max_length=255, null=True)
 | 
			
		||||
    created_at = models.CharField(max_length=20, default=human_time)
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return f'<Service {self.name!r}>'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        db_table = 'services'
 | 
			
		||||
        ordering = ('-id',)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigKey(models.Model, ModelMixin):
 | 
			
		||||
    TYPES = (
 | 
			
		||||
        ('app', 'App'),
 | 
			
		||||
        ('src', 'Service')
 | 
			
		||||
    )
 | 
			
		||||
    name = models.CharField(max_length=50)
 | 
			
		||||
    type = models.CharField(max_length=5, choices=TYPES)
 | 
			
		||||
    o_id = models.IntegerField()
 | 
			
		||||
    is_public = models.BooleanField()
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return f'<ConfigKey {self.name!r}>'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        db_table = 'config_keys'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigValue(models.Model, ModelMixin):
 | 
			
		||||
    env = models.ForeignKey(Environment, on_delete=models.PROTECT)
 | 
			
		||||
    key = models.ForeignKey(ConfigKey, on_delete=models.PROTECT)
 | 
			
		||||
    value = models.TextField()
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return f'<ConfigValue {self.id!r}>'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        db_table = 'config_values'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigHistory(models.Model, ModelMixin):
 | 
			
		||||
    ACTIONS = (
 | 
			
		||||
        ('1', '新增'),
 | 
			
		||||
        ('2', '更新'),
 | 
			
		||||
        ('3', '删除')
 | 
			
		||||
    )
 | 
			
		||||
    key = models.CharField(max_length=50)
 | 
			
		||||
    old_value = models.TextField(null=True)
 | 
			
		||||
    new_value = models.TextField(null=True)
 | 
			
		||||
    action = models.CharField(max_length=2, choices=ACTIONS)
 | 
			
		||||
    created_at = models.CharField(max_length=20, default=human_time)
 | 
			
		||||
    created_by = models.ForeignKey(User, on_delete=models.PROTECT)
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return f'<ConfigHistory {self.key!r}>'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        db_table = 'config_histories'
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
from django.urls import path
 | 
			
		||||
 | 
			
		||||
from .views import *
 | 
			
		||||
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
    path('environment/', EnvironmentView.as_view()),
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
from django.views.generic import View
 | 
			
		||||
from libs import json_response, JsonParser, Argument, human_time
 | 
			
		||||
from apps.config.models import Environment, ConfigValue
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EnvironmentView(View):
 | 
			
		||||
    def get(self, request):
 | 
			
		||||
        envs = Environment.objects.all()
 | 
			
		||||
        return json_response(envs)
 | 
			
		||||
 | 
			
		||||
    def post(self, request):
 | 
			
		||||
        form, error = JsonParser(
 | 
			
		||||
            Argument('id', type=int, required=False),
 | 
			
		||||
            Argument('name', help='请输入环境名称'),
 | 
			
		||||
            Argument('key', help='请输入唯一标识符'),
 | 
			
		||||
            Argument('desc', required=False)
 | 
			
		||||
        ).parse(request.body)
 | 
			
		||||
        if error is None:
 | 
			
		||||
            env = Environment.objects.filter(key=form.key).first()
 | 
			
		||||
            if env and env.id != form.id:
 | 
			
		||||
                return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试')
 | 
			
		||||
            if form.id:
 | 
			
		||||
                Environment.objects.filter(pk=form.id).update(**form)
 | 
			
		||||
            else:
 | 
			
		||||
                Environment.objects.create(created_by=request.user, **form)
 | 
			
		||||
        return json_response(error=error)
 | 
			
		||||
 | 
			
		||||
    def delete(self, request):
 | 
			
		||||
        form, error = JsonParser(
 | 
			
		||||
            Argument('id', type=int, help='请指定操作对象')
 | 
			
		||||
        ).parse(request.GET)
 | 
			
		||||
        if error is None:
 | 
			
		||||
            if ConfigValue.objects.filter(env_id=form.id).exists():
 | 
			
		||||
                return json_response(error='该环境已存在关联的配置信息,请删除相关配置后再尝试删除')
 | 
			
		||||
            Environment.objects.filter(pk=form.id).delete()
 | 
			
		||||
        return json_response(error=error)
 | 
			
		||||
| 
						 | 
				
			
			@ -37,6 +37,7 @@ INSTALLED_APPS = [
 | 
			
		|||
    'apps.schedule',
 | 
			
		||||
    'apps.monitor',
 | 
			
		||||
    'apps.alarm',
 | 
			
		||||
    'apps.config',
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
MIDDLEWARE = [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,4 +23,5 @@ urlpatterns = [
 | 
			
		|||
    path('monitor/', include('apps.monitor.urls')),
 | 
			
		||||
    path('alarm/', include('apps.alarm.urls')),
 | 
			
		||||
    path('setting/', include('apps.setting.urls')),
 | 
			
		||||
    path('config/', include('apps.config.urls')),
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue