|
1 | 1 | from django.http import Http404 |
| 2 | +from django.utils.translation import gettext_lazy as _ |
2 | 3 | from drf_spectacular.utils import extend_schema_view, extend_schema |
3 | 4 | from rest_framework.routers import APIRootView |
4 | 5 | from rest_framework.viewsets import ModelViewSet |
| 6 | +from rest_framework.exceptions import ValidationError |
5 | 7 |
|
6 | 8 | from netbox_custom_objects.filtersets import get_filterset_class |
7 | 9 | from netbox_custom_objects.models import CustomObjectType, CustomObjectTypeField |
| 10 | +from netbox_custom_objects.utilities import is_in_branch |
8 | 11 |
|
9 | 12 | from . import serializers |
10 | 13 |
|
| 14 | +# Constants |
| 15 | +BRANCH_ACTIVE_ERROR_MESSAGE = _("Please switch to the main branch to perform this operation.") |
| 16 | + |
11 | 17 |
|
12 | 18 | class RootView(APIRootView): |
13 | 19 | def get_view_name(self): |
@@ -60,6 +66,21 @@ def filterset_class(self): |
60 | 66 | def list(self, request, *args, **kwargs): |
61 | 67 | return super().list(request, *args, **kwargs) |
62 | 68 |
|
| 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 | + |
63 | 84 |
|
64 | 85 | class CustomObjectTypeFieldViewSet(ModelViewSet): |
65 | 86 | queryset = CustomObjectTypeField.objects.all() |
|
0 commit comments