Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing team for escalation chains #1658

Merged
merged 10 commits into from
Mar 30, 2023
4 changes: 2 additions & 2 deletions engine/apps/alerts/models/escalation_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class Meta:
def __str__(self):
return f"{self.pk}: {self.name}"

def make_copy(self, copy_name: str):
def make_copy(self, copy_name: str, team=None):
with transaction.atomic():
copied_chain = EscalationChain.objects.create(
organization=self.organization,
team=self.team,
team=self.team if team is None else team,
name=copy_name,
)
for escalation_policy in self.escalation_policies.all():
Expand Down
10 changes: 8 additions & 2 deletions engine/apps/api/views/escalation_chain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models import Count, Q
from django_filters import rest_framework as filters
from emoji import emojize
from rest_framework import viewsets
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
Expand All @@ -15,6 +15,7 @@
FilterEscalationChainSerializer,
)
from apps.auth_token.auth import PluginAuthentication
from apps.user_management.models import Team
from common.api_helpers.exceptions import BadRequest
from common.api_helpers.filters import ByTeamModelFieldFilterMixin, ModelFieldFilterMixin, TeamModelMultipleChoiceFilter
from common.api_helpers.mixins import (
Expand Down Expand Up @@ -120,14 +121,19 @@ def perform_update(self, serializer):
@action(methods=["post"], detail=True)
def copy(self, request, pk):
name = request.data.get("name")
team_id = request.data.get("team")
if name is None:
raise BadRequest(detail={"name": ["This field may not be null."]})
else:
if EscalationChain.objects.filter(organization=request.auth.organization, name=name).exists():
raise BadRequest(detail={"name": ["Escalation chain with this name already exists."]})

obj = self.get_object()
copy = obj.make_copy(name)
try:
team = request.user.available_teams.get(public_primary_key=team_id)
vstpme marked this conversation as resolved.
Show resolved Hide resolved
except Team.DoesNotExist:
return Response(data={"error_code": "wrong_team"}, status=status.HTTP_403_FORBIDDEN)
copy = obj.make_copy(name, team)
serializer = self.get_serializer(copy)
write_resource_insight_log(
instance=copy,
Expand Down