This commit is contained in:
Administrator 2023-09-23 17:59:26 +03:00
parent b2a41e7a80
commit 4991376d93
16 changed files with 262 additions and 2 deletions

View File

@ -45,7 +45,8 @@ INSTALLED_APPS = [
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'web.apps.WebConfig'
'web.apps.WebConfig',
'configs.apps.ConfigsConfig'
]
MIDDLEWARE = [

View File

@ -18,5 +18,6 @@ from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path('configs/', include('configs.urls')),
path('', include('web.urls'))
]

0
configs/__init__.py Normal file
View File

3
configs/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
configs/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ConfigsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'configs'

View 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'),
),
]

View 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'),
),
]

View File

@ -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'),
),
]

View File

22
configs/models.py Normal file
View 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
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
configs/urls.py Normal file
View 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
View 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

70
templates/configs.html Normal file
View File

@ -0,0 +1,70 @@
{% extends 'layouts/base.html' %}
{% block content %}
<h1>Конфиги <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#create_config">+</button></h1>
<select class="form-select" style="width: 150px;" onchange="window.location.href='/configs/?stage=' + this.value;">
<option {% if stage == 'production' %}selected{% endif %}>production</option>
<option {% if stage == 'development' %}selected{% endif %}>development</option>
</select>
<div class="modal fade" id="create_config" tabindex="-1" aria-labelledby="modal-default" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="POST">
{% csrf_token %}
<div class="modal-header">
<h2 class="h6 modal-title">Создать конфиг</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="text" name="name" placeholder="Название конфига" style="width: 100%;" />
</div>
<div class="modal-footer">
<button type="submit" name="action" value="create_config" class="btn btn-secondary">Создать</button>
<button type="button" class="btn btn-link text-gray-600 ms-auto" data-bs-dismiss="modal">Закрыть</button>
</div>
</form>
</div>
</div>
</div>
{% if error %}
<div class="alert alert-danger" role="alert">
Конфиг не имеет вид JSON!
</div>
{% endif %}
<table class="table">
<thead>
<th>Название</th>
<th>Действие</th>
</thead>
<tbody>
{% for config in configs %}
<tr>
<td>{{ config.name }}</td>
<td><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#edit_{{ config.id }}">Редактировать</button></td>
<div class="modal fade" id="edit_{{ config.id }}" tabindex="-1" aria-labelledby="modal-default" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="POST">
{% csrf_token %}
<input type="hidden" name="config" value="{{ config.id }}" />
<div class="modal-header">
<h2 class="h6 modal-title">Редактировать конфиг {{ config.name }}</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<textarea style="width: 100%; height: 800px;" name="data">{{ config.data_pretty }}</textarea>
</div>
<div class="modal-footer">
<button type="submit" name="action" value="save" class="btn btn-secondary">Сохранить</button>
<button type="submit" name="action" value="delete" class="btn btn-danger">Удалить</button>
<button type="button" class="btn btn-link text-gray-600 ms-auto" data-bs-dismiss="modal">Закрыть</button>
</div>
</form>
</div>
</div>
</div>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -75,7 +75,7 @@
{% endif %}
<li role="separator" class="dropdown-divider mt-4 mb-3 border-gray-700"></li>
<li class="nav-item">
<a href="/profile" class="nav-link">
<a href="/configs" class="nav-link">
<span class="sidebar-icon">
<i class="fa fa-cogs"></i>
</span>

View File

@ -0,0 +1,23 @@
# Generated by Django 4.1.7 on 2023-09-23 13:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0003_customuser_selected_project'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='vk_id',
field=models.IntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='customuser',
name='yandex_id',
field=models.BigIntegerField(blank=True, null=True),
),
]