configs
This commit is contained in:
0
configs/__init__.py
Normal file
0
configs/__init__.py
Normal file
3
configs/admin.py
Normal file
3
configs/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
configs/apps.py
Normal file
6
configs/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ConfigsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'configs'
|
33
configs/migrations/0001_initial.py
Normal file
33
configs/migrations/0001_initial.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 4.1.7 on 2023-09-23 13:56
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('web', '0004_alter_customuser_vk_id_alter_customuser_yandex_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Config',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.TextField()),
|
||||
('data', models.JSONField(default=dict)),
|
||||
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web.project')),
|
||||
],
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='config',
|
||||
index=models.Index(fields=['project'], name='configs_con_project_8236d6_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='config',
|
||||
index=models.Index(fields=['project', 'name'], name='configs_con_project_2e12ee_idx'),
|
||||
),
|
||||
]
|
18
configs/migrations/0002_config_stage.py
Normal file
18
configs/migrations/0002_config_stage.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.1.7 on 2023-09-23 14:51
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('configs', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='config',
|
||||
name='stage',
|
||||
field=models.TextField(default='production'),
|
||||
),
|
||||
]
|
@@ -0,0 +1,29 @@
|
||||
# Generated by Django 4.1.7 on 2023-09-23 14:57
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('configs', '0002_config_stage'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveIndex(
|
||||
model_name='config',
|
||||
name='configs_con_project_8236d6_idx',
|
||||
),
|
||||
migrations.RemoveIndex(
|
||||
model_name='config',
|
||||
name='configs_con_project_2e12ee_idx',
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='config',
|
||||
index=models.Index(fields=['project', 'stage'], name='configs_con_project_19e8a6_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='config',
|
||||
index=models.Index(fields=['project', 'name', 'stage'], name='configs_con_project_a9e1de_idx'),
|
||||
),
|
||||
]
|
0
configs/migrations/__init__.py
Normal file
0
configs/migrations/__init__.py
Normal file
22
configs/models.py
Normal file
22
configs/models.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from json import dumps
|
||||
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Config(models.Model):
|
||||
name = models.TextField()
|
||||
data = models.JSONField(default=dict)
|
||||
project = models.ForeignKey('web.Project', on_delete=models.CASCADE)
|
||||
stage = models.TextField(default='production')
|
||||
|
||||
@property
|
||||
def data_pretty(self):
|
||||
return dumps(self.data, indent=4)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=['project', 'stage']),
|
||||
models.Index(fields=['project', 'name', 'stage'])
|
||||
]
|
3
configs/tests.py
Normal file
3
configs/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
8
configs/urls.py
Normal file
8
configs/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
|
||||
from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path(*ConfigsView.as_path())
|
||||
]
|
43
configs/views.py
Normal file
43
configs/views.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from json import loads
|
||||
|
||||
from BaseLib.BaseView import BaseView
|
||||
from configs.models import Config
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class ConfigsView(BaseView):
|
||||
required_login = True
|
||||
endpoint = ''
|
||||
view_file = 'configs.html'
|
||||
|
||||
def pre_handle(self):
|
||||
if 'stage' not in self.request.GET or self.request.GET['stage'] not in ['development', 'production']:
|
||||
return '/configs/?stage=production'
|
||||
self.stage = self.request.GET['stage']
|
||||
self.context['stage'] = self.stage
|
||||
|
||||
def get(self):
|
||||
self.context['configs'] = Config.objects.filter(project=self.request.user.selected_project, stage=self.stage).order_by('name')
|
||||
self.context['error'] = 'error' in self.request.GET
|
||||
|
||||
def post_create_config(self):
|
||||
Config.objects.create(name=self.request.POST['name'], project=self.request.user.selected_project, stage=self.stage)
|
||||
return '/configs/?stage=' + self.stage
|
||||
|
||||
def post_delete(self):
|
||||
config = Config.objects.get(id=self.request.POST['config'])
|
||||
config.delete()
|
||||
return '/configs/?stage=' + config.stage
|
||||
|
||||
def post_save(self):
|
||||
data = self.request.POST['data']
|
||||
try:
|
||||
data = loads(data)
|
||||
except:
|
||||
return '/configs?error=config_is_not_json'
|
||||
config = Config.objects.get(id=self.request.POST['config'])
|
||||
config.data = data
|
||||
config.save()
|
||||
return '/configs/?stage=' + config.stage
|
Reference in New Issue
Block a user