Example #1
0
    def get_config_context(self):
        """
        Return the rendered configuration context for a device or VM.
        """

        # Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs
        data = OrderedDict()
        for context in ConfigContext.objects.get_for_object(self):
            data = deepmerge(data, context.data)

        # If the object has local config context data defined, merge it last
        if self.local_context_data:
            data = deepmerge(data, self.local_context_data)

        return data
Example #2
0
    def get_config_context(self):
        """
        Return the rendered configuration context for a device or VM.
        """

        # Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs
        data = OrderedDict()
        for context in ConfigContext.objects.get_for_object(self):
            data = deepmerge(data, context.data)

        # If the object has local config context data defined, merge it last
        if self.local_context_data is not None:
            data = deepmerge(data, self.local_context_data)

        return data
Example #3
0
    def test_patch(self):
        """
        Set user config via PATCH requests.
        """
        userconfig = self.user.config
        url = reverse('users-api:userconfig-list')

        data = {
            "a": {
                "a1": "X",
                "a2": "Y",
            },
            "b": {
                "b1": "Z",
            }
        }
        response = self.client.patch(url,
                                     data=data,
                                     format='json',
                                     **self.header)
        self.assertDictEqual(response.data, data)
        userconfig.refresh_from_db()
        self.assertDictEqual(userconfig.data, data)

        update_data = {"c": 123}
        response = self.client.patch(url,
                                     data=update_data,
                                     format='json',
                                     **self.header)
        new_data = deepmerge(data, update_data)
        self.assertDictEqual(response.data, new_data)
        userconfig.refresh_from_db()
        self.assertDictEqual(userconfig.data, new_data)
Example #4
0
    def patch(self, request):
        """
        Update the UserConfig for the currently authenticated User.
        """
        # TODO: How can we validate this data?
        userconfig = self.get_queryset().first()
        userconfig.data = deepmerge(userconfig.data, request.data)
        userconfig.save()

        return Response(userconfig.data)
Example #5
0
    def get_config_context(self):
        """
        Return the rendered configuration context for a device or VM.
        """

        # Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs
        data = OrderedDict()

        if not hasattr(self, 'config_context_data'):
            # The annotation is not available, so we fall back to manually querying for the config context objects
            config_context_data = ConfigContext.objects.get_for_object(self, aggregate_data=True)
        else:
            # The attribute may exist, but the annotated value could be None if there is no config context data
            config_context_data = self.config_context_data or []

        for context in config_context_data:
            data = deepmerge(data, context)

        # If the object has local config context data defined, merge it last
        if self.local_context_data:
            data = deepmerge(data, self.local_context_data)

        return data
Example #6
0
    def test_deepmerge(self):

        dict1 = {
            'active': True,
            'foo': 123,
            'fruits': {
                'orange': 1,
                'apple': 2,
                'pear': 3,
            },
            'vegetables': None,
            'dairy': {
                'milk': 1,
                'cheese': 2,
            },
            'deepnesting': {
                'foo': {
                    'a': 10,
                    'b': 20,
                    'c': 30,
                },
            },
        }

        dict2 = {
            'active': False,
            'bar': 456,
            'fruits': {
                'banana': 4,
                'grape': 5,
            },
            'vegetables': {
                'celery': 1,
                'carrots': 2,
                'corn': 3,
            },
            'dairy': None,
            'deepnesting': {
                'foo': {
                    'a': 100,
                    'd': 40,
                },
            },
        }

        merged = {
            'active': False,
            'foo': 123,
            'bar': 456,
            'fruits': {
                'orange': 1,
                'apple': 2,
                'pear': 3,
                'banana': 4,
                'grape': 5,
            },
            'vegetables': {
                'celery': 1,
                'carrots': 2,
                'corn': 3,
            },
            'dairy': None,
            'deepnesting': {
                'foo': {
                    'a': 100,
                    'b': 20,
                    'c': 30,
                    'd': 40,
                },
            },
        }

        self.assertEqual(
            deepmerge(dict1, dict2),
            merged
        )
Example #7
0
    def test_deepmerge(self):

        dict1 = {
            'active': True,
            'foo': 123,
            'fruits': {
                'orange': 1,
                'apple': 2,
                'pear': 3,
            },
            'vegetables': None,
            'dairy': {
                'milk': 1,
                'cheese': 2,
            },
            'deepnesting': {
                'foo': {
                    'a': 10,
                    'b': 20,
                    'c': 30,
                },
            },
        }

        dict2 = {
            'active': False,
            'bar': 456,
            'fruits': {
                'banana': 4,
                'grape': 5,
            },
            'vegetables': {
                'celery': 1,
                'carrots': 2,
                'corn': 3,
            },
            'dairy': None,
            'deepnesting': {
                'foo': {
                    'a': 100,
                    'd': 40,
                },
            },
        }

        merged = {
            'active': False,
            'foo': 123,
            'bar': 456,
            'fruits': {
                'orange': 1,
                'apple': 2,
                'pear': 3,
                'banana': 4,
                'grape': 5,
            },
            'vegetables': {
                'celery': 1,
                'carrots': 2,
                'corn': 3,
            },
            'dairy': None,
            'deepnesting': {
                'foo': {
                    'a': 100,
                    'b': 20,
                    'c': 30,
                    'd': 40,
                },
            },
        }

        self.assertEqual(deepmerge(dict1, dict2), merged)