refactor(); project refactor

This commit is contained in:
ITQ
2026-02-23 11:46:52 +03:00
parent 85923f11fc
commit ca0c456862
16 changed files with 198 additions and 194 deletions
+9 -3
View File
@@ -4,7 +4,11 @@ from uuid import UUID
from ninja import Field, ModelSchema, Schema
from apps.conflicts.models import ConflictDomain, ConflictPolicy, ExperimentConflictDomain
from apps.conflicts.models import (
ConflictDomain,
ConflictPolicy,
ExperimentConflictDomain,
)
class ConflictDomainOut(ModelSchema):
@@ -54,7 +58,8 @@ class MembershipOut(Schema):
@classmethod
def from_membership(
cls, m: ExperimentConflictDomain,
cls,
m: ExperimentConflictDomain,
) -> "MembershipOut":
return cls(
id=m.pk,
@@ -76,7 +81,8 @@ class ExperimentDomainOut(Schema):
@classmethod
def from_membership(
cls, m: ExperimentConflictDomain,
cls,
m: ExperimentConflictDomain,
) -> "ExperimentDomainOut":
return cls(
id=m.pk,
@@ -1,13 +1,18 @@
import json
import uuid
from typing import override
from django.test import Client, TestCase
from django.urls import reverse
from apps.conflicts.tests.helpers import make_domain
from apps.experiments.tests.helpers import add_two_variants, make_flag
from apps.experiments.services import experiment_create
from apps.reviews.tests.helpers import make_admin, make_experimenter, make_viewer
from apps.experiments.tests.helpers import add_two_variants, make_flag
from apps.reviews.tests.helpers import (
make_admin,
make_experimenter,
make_viewer,
)
from apps.users.tests.helpers import auth_header
@@ -23,12 +28,14 @@ class ConflictDomainAPITest(TestCase):
def test_create_domain(self) -> None:
resp = self.client.post(
reverse("api-1:create_domain"),
data=json.dumps({
"name": "checkout",
"description": "Checkout zone",
"policy": "mutual_exclusion",
"max_concurrent": 1,
}),
data=json.dumps(
{
"name": "checkout",
"description": "Checkout zone",
"policy": "mutual_exclusion",
"max_concurrent": 1,
}
),
content_type="application/json",
HTTP_AUTHORIZATION=self.admin_auth,
)
@@ -86,8 +93,6 @@ class ConflictDomainAPITest(TestCase):
self.assertEqual(resp.status_code, 204)
def test_get_nonexistent_domain(self) -> None:
import uuid
resp = self.client.get(
reverse("api-1:get_domain", args=[uuid.uuid4()]),
HTTP_AUTHORIZATION=self.admin_auth,
@@ -114,10 +119,12 @@ class DomainExperimentAPITest(TestCase):
def test_add_experiment_to_domain(self) -> None:
resp = self.client.post(
reverse("api-1:add_experiment_to_domain", args=[self.domain.pk]),
data=json.dumps({
"experiment_id": str(self.exp.pk),
"priority": 5,
}),
data=json.dumps(
{
"experiment_id": str(self.exp.pk),
"priority": 5,
}
),
content_type="application/json",
HTTP_AUTHORIZATION=self.admin_auth,
)
@@ -134,9 +141,7 @@ class DomainExperimentAPITest(TestCase):
HTTP_AUTHORIZATION=self.admin_auth,
)
resp = self.client.get(
reverse(
"api-1:list_experiment_domains", args=[self.exp.pk]
),
reverse("api-1:list_experiment_domains", args=[self.exp.pk]),
HTTP_AUTHORIZATION=self.admin_auth,
)
self.assertEqual(resp.status_code, 200)
+3 -1
View File
@@ -214,7 +214,9 @@ def update_variant(
) -> tuple[HTTPStatus, VariantOut]:
user = _get_user(request)
v = get_object_or_404(
Variant.objects.select_related("experiment__flag", "experiment__owner"),
Variant.objects.select_related(
"experiment__flag", "experiment__owner"
),
pk=variant_id,
experiment_id=experiment_id,
)
+23 -19
View File
@@ -36,11 +36,13 @@ def create_learning(
payload: LearningCreateIn,
) -> tuple[int, LearningOut]:
try:
experiment = Experiment.objects.select_related("flag").get(pk=payload.experiment_id)
experiment = Experiment.objects.select_related("flag").get(
pk=payload.experiment_id
)
except Experiment.DoesNotExist:
raise Http404 from Experiment.DoesNotExist
l = learning_create(
learning = learning_create(
experiment=experiment,
hypothesis=payload.hypothesis,
findings=payload.findings,
@@ -48,8 +50,8 @@ def create_learning(
context_summary=payload.context_summary,
user=request.auth,
)
l = learning_get(l.pk)
return HTTPStatus.CREATED, LearningOut.from_learning(l)
learning = learning_get(learning.pk)
return HTTPStatus.CREATED, LearningOut.from_learning(learning)
@router.get(
@@ -72,7 +74,9 @@ def list_learnings(
tag=tag,
search=search,
)
return HTTPStatus.OK, [LearningOut.from_learning(l) for l in learnings]
return HTTPStatus.OK, [
LearningOut.from_learning(learning) for learning in learnings
]
@router.get(
@@ -84,10 +88,10 @@ def get_learning(
request: HttpRequest,
learning_id: UUID,
) -> tuple[int, LearningOut]:
l = learning_get(learning_id)
if not l:
learning = learning_get(learning_id)
if not learning:
raise Http404
return HTTPStatus.OK, LearningOut.from_learning(l)
return HTTPStatus.OK, LearningOut.from_learning(learning)
@router.patch(
@@ -100,16 +104,16 @@ def update_learning(
learning_id: UUID,
payload: LearningUpdateIn,
) -> tuple[int, LearningOut]:
l = learning_get(learning_id)
if not l:
learning = learning_get(learning_id)
if not learning:
raise Http404
l = learning_update(
learning=l,
learning = learning_update(
learning=learning,
user=request.auth,
**payload.dict(exclude_unset=True),
)
l = learning_get(l.pk)
return HTTPStatus.OK, LearningOut.from_learning(l)
learning = learning_get(learning.pk)
return HTTPStatus.OK, LearningOut.from_learning(learning)
@router.delete(
@@ -121,10 +125,10 @@ def delete_learning(
request: HttpRequest,
learning_id: UUID,
) -> tuple[int, None]:
l = learning_get(learning_id)
if not l:
learning = learning_get(learning_id)
if not learning:
raise Http404
learning_delete(learning=l)
learning_delete(learning=learning)
return HTTPStatus.NO_CONTENT, None
@@ -137,8 +141,8 @@ def list_learning_edits(
request: HttpRequest,
learning_id: UUID,
) -> tuple[int, list[EditOut]]:
l = learning_get(learning_id)
if not l:
learning = learning_get(learning_id)
if not learning:
raise Http404
edits = learning_edit_list(learning_id)
return HTTPStatus.OK, [EditOut.from_edit(e) for e in edits]
+15 -16
View File
@@ -1,4 +1,3 @@
from datetime import datetime
from typing import Any, ClassVar
from uuid import UUID
@@ -51,26 +50,26 @@ class LearningOut(ModelSchema):
)
@classmethod
def from_learning(cls, l: Learning) -> "LearningOut":
def from_learning(cls, learning: Learning) -> "LearningOut":
created_by_out = None
if l.created_by:
if learning.created_by:
created_by_out = CreatedByOut(
id=l.created_by.pk,
username=l.created_by.username,
id=learning.created_by.pk,
username=learning.created_by.username,
)
return cls(
id=l.pk,
hypothesis=l.hypothesis,
findings=l.findings,
tags=l.tags,
context_summary=l.context_summary,
created_at=l.created_at,
updated_at=l.updated_at,
id=learning.pk,
hypothesis=learning.hypothesis,
findings=learning.findings,
tags=learning.tags,
context_summary=learning.context_summary,
created_at=learning.created_at,
updated_at=learning.updated_at,
experiment=ExperimentBriefOut(
id=l.experiment.pk,
name=l.experiment.name,
status=l.experiment.status,
flag_key=l.experiment.flag.key,
id=learning.experiment.pk,
name=learning.experiment.name,
status=learning.experiment.status,
flag_key=learning.experiment.flag.key,
),
created_by=created_by_out,
)