From 655ba6b4fb8ba78a570884f9a4695c2234401b9e Mon Sep 17 00:00:00 2001 From: Mateusz Kurek Date: Fri, 3 Mar 2017 08:59:34 +0100 Subject: [PATCH] Disable editing of configuration module and class details (#2970) * ConfigurationModule - disable name and parent * ConfigurationClass - disable module and class_name Changing these fields could be dissruptive for configuration of hosts. --- src/ralph/assets/admin.py | 10 ++++++++++ src/ralph/assets/api/views.py | 6 ++++++ src/ralph/assets/tests/test_api.py | 12 ++++++------ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/ralph/assets/admin.py b/src/ralph/assets/admin.py index cafa1ab0f5..3229175e6e 100644 --- a/src/ralph/assets/admin.py +++ b/src/ralph/assets/admin.py @@ -59,6 +59,11 @@ def objects_count(self, instance): objects_count.short_description = _('Objects count') objects_count.admin_order_field = 'objects_count' + def get_readonly_fields(self, request, obj=None): + if obj: + return self.readonly_fields + ['class_name', 'module'] + return self.readonly_fields + @register(ConfigurationModule) class ConfigurationModuleAdmin(CustomFieldValueAdminMixin, RalphMPTTAdmin): @@ -104,6 +109,11 @@ def show_children_classes(self, module): show_children_classes.allow_tags = True show_children_classes.short_description = _('Children classes') + def get_readonly_fields(self, request, obj=None): + if obj: + return self.readonly_fields + ['name', 'parent'] + return self.readonly_fields + @register(ServiceEnvironment) class ServiceEnvironmentAdmin(CustomFieldValueAdminMixin, RalphAdmin): diff --git a/src/ralph/assets/api/views.py b/src/ralph/assets/api/views.py index e1b2227fb6..ef94f43143 100644 --- a/src/ralph/assets/api/views.py +++ b/src/ralph/assets/api/views.py @@ -198,6 +198,9 @@ class ConfigurationModuleViewSet(RalphAPIViewSet): serializer_class = serializers.ConfigurationModuleSerializer save_serializer_class = serializers.ConfigurationModuleSimpleSerializer filter_fields = ('parent', 'name') + # don't allow for ConfigurationModule updating or deleting as it might + # dissrupt configuration of many hosts! + http_method_names = ['get', 'post', 'options', 'head'] class ConfigurationClassViewSet(RalphAPIViewSet): @@ -206,6 +209,9 @@ class ConfigurationClassViewSet(RalphAPIViewSet): filter_fields = ('module', 'module__name', 'class_name', 'path') select_related = ['module'] prefetch_related = ['tags'] + # don't allow for ConfigurationClass updating or deleting as it might + # dissrupt configuration of many hosts! + http_method_names = ['get', 'post', 'options', 'head'] class BaseObjectViewSetMixin(object): diff --git a/src/ralph/assets/tests/test_api.py b/src/ralph/assets/tests/test_api.py index a3b7040e35..3204cf1605 100644 --- a/src/ralph/assets/tests/test_api.py +++ b/src/ralph/assets/tests/test_api.py @@ -1039,9 +1039,9 @@ def test_patch_configuration_module(self): 'name': 'test_2' } response = self.client.patch(url, data, format='json') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.conf_module_2.refresh_from_db() - self.assertEqual(self.conf_module_2.name, 'test_2') + self.assertEqual( + response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED + ) class ConfigurationClassAPITests(RalphAPITestCase): @@ -1104,9 +1104,9 @@ def test_patch_configuration_class(self): 'class_name': 'test_2' } response = self.client.patch(url, data, format='json') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.conf_class_1.refresh_from_db() - self.assertEqual(self.conf_class_1.class_name, 'test_2') + self.assertEqual( + response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED + ) class EthernetAPITests(RalphAPITestCase):