experiments

This commit is contained in:
Administrator
2023-09-23 18:54:49 +03:00
parent 4991376d93
commit 59aa298db6
13 changed files with 200 additions and 2 deletions

0
experiments/__init__.py Normal file
View File

3
experiments/admin.py Normal file
View File

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

6
experiments/apps.py Normal file
View File

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

View File

@@ -0,0 +1,35 @@
# Generated by Django 4.1.7 on 2023-09-23 15:45
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='Experiment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.TextField()),
('enabled', models.BooleanField(default=False)),
('condition', models.TextField(default='False')),
('stage', models.TextField(default='production')),
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web.project')),
],
),
migrations.AddIndex(
model_name='experiment',
index=models.Index(fields=['project', 'stage'], name='experiments_project_e2c9f6_idx'),
),
migrations.AddIndex(
model_name='experiment',
index=models.Index(fields=['project', 'name', 'stage'], name='experiments_project_dade49_idx'),
),
]

View File

29
experiments/models.py Normal file
View File

@@ -0,0 +1,29 @@
from django.db import models
# Create your models here.
class Experiment(models.Model):
name = models.TextField()
enabled = models.BooleanField(default=False)
condition = models.TextField(default='False')
project = models.ForeignKey('web.Project', on_delete=models.CASCADE)
stage = models.TextField(default='production')
class Meta:
indexes = [
models.Index(fields=['project', 'stage']),
models.Index(fields=['project', 'name', 'stage'])
]
@property
def exp_type(self):
if self.condition == 'False':
return 0
if self.condition == 'user.is_superuser':
return 1
if self.condition == 'user.is_staff':
return 2
if self.condition == 'True':
return 3
return 4

3
experiments/tests.py Normal file
View File

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

8
experiments/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(*ExperimentsView.as_path())
]

42
experiments/views.py Normal file
View File

@@ -0,0 +1,42 @@
from BaseLib.BaseView import BaseView
from experiments.models import Experiment
class ExperimentsView(BaseView):
required_login = True
endpoint = ''
view_file = 'experiments.html'
def pre_handle(self):
if 'stage' not in self.request.GET or self.request.GET['stage'] not in ['development', 'production']:
return '/experiments/?stage=production'
self.stage = self.request.GET['stage']
self.context['stage'] = self.stage
def get(self):
self.context['experiments'] = Experiment.objects.filter(project=self.request.user.selected_project,
stage=self.stage).order_by('name')
def post_create(self):
Experiment.objects.create(project=self.request.user.selected_project, stage=self.stage, name=self.request.POST['name'])
return '/experiments/?stage=' + self.stage
def post_delete(self):
Experiment.objects.get(id=self.request.POST['experiment_id']).delete()
return '/experiments/?stage=' + self.stage
def post_change(self):
exp = Experiment.objects.get(id=self.request.POST['experiment_id'])
exp.enabled = 'enabled' in self.request.POST
condition = self.request.POST['condition_select']
if condition == 'Другое (только для техлидов)':
exp.condition = self.request.POST['condition']
elif condition == 'Никому':
exp.condition = 'False'
elif condition == 'Только админам':
exp.condition = 'user.is_superuser'
elif condition == 'Только сотрудникам':
exp.condition = 'user.is_staff'
else:
exp.condition = 'True'
exp.save()
return '/experiments/?stage=' + self.stage