Skip to content

Commit 268f120

Browse files
committed
check for branching in REST API
1 parent bc553af commit 268f120

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

netbox_custom_objects/api/views.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from django.http import Http404
2+
from django.utils.translation import gettext_lazy as _
23
from drf_spectacular.utils import extend_schema_view, extend_schema
34
from rest_framework.routers import APIRootView
45
from rest_framework.viewsets import ModelViewSet
6+
from rest_framework.exceptions import ValidationError
57

68
from netbox_custom_objects.filtersets import get_filterset_class
79
from netbox_custom_objects.models import CustomObjectType, CustomObjectTypeField
10+
from netbox_custom_objects.utilities import is_in_branch
811

912
from . import serializers
1013

14+
# Constants
15+
BRANCH_ACTIVE_ERROR_MESSAGE = _("Please switch to the main branch to perform this operation.")
16+
1117

1218
class RootView(APIRootView):
1319
def get_view_name(self):
@@ -60,6 +66,21 @@ def filterset_class(self):
6066
def list(self, request, *args, **kwargs):
6167
return super().list(request, *args, **kwargs)
6268

69+
def create(self, request, *args, **kwargs):
70+
if is_in_branch():
71+
raise ValidationError(BRANCH_ACTIVE_ERROR_MESSAGE)
72+
return super().create(request, *args, **kwargs)
73+
74+
def update(self, request, *args, **kwargs):
75+
if is_in_branch():
76+
raise ValidationError(BRANCH_ACTIVE_ERROR_MESSAGE)
77+
return super().update(request, *args, **kwargs)
78+
79+
def partial_update(self, request, *args, **kwargs):
80+
if is_in_branch():
81+
raise ValidationError(BRANCH_ACTIVE_ERROR_MESSAGE)
82+
return super().partial_update(request, *args, **kwargs)
83+
6384

6485
class CustomObjectTypeFieldViewSet(ModelViewSet):
6586
queryset = CustomObjectTypeField.objects.all()

0 commit comments

Comments
 (0)