From e19bb1e91ed6a4d9207220cf75ad7cf6f4b775ea Mon Sep 17 00:00:00 2001 From: gXkch <85333670+gXkch@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:45:23 +0100 Subject: [PATCH] Add custom filter to parse default value from dict to json --- service_catalog/models/tower_survey_field.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/service_catalog/models/tower_survey_field.py b/service_catalog/models/tower_survey_field.py index f4ad04d5c..4177ab461 100644 --- a/service_catalog/models/tower_survey_field.py +++ b/service_catalog/models/tower_survey_field.py @@ -15,7 +15,7 @@ from django.forms import SelectMultiple as FormsSelectMultiple from django.forms import TextInput as FormsTextInput from django.forms import Textarea as FormsTextarea -from jinja2 import Template +from jinja2 import Template, Environment from jinja2.exceptions import UndefinedError from rest_framework.serializers import CharField as DjangoRestCharField from rest_framework.serializers import ChoiceField as DjangoRestChoiceField @@ -27,9 +27,21 @@ from Squest.utils.squest_model import SquestModel from resource_tracker_v2.models import AttributeDefinition from service_catalog.models import Operation +import json logger = logging.getLogger(__name__) +custom_filters = { + "to_json": lambda value: json.dumps(value, indent=2), # Convert to JSON +} + +# Initialize Jinja2 environment +custom_env = Environment() + +# Dynamically add filters +for filter_name, filter_func in custom_filters.items(): + custom_env.filters[filter_name] = filter_func + def get_choices_as_tuples_list(choices, default=None): if default is None: @@ -83,7 +95,9 @@ def templating_default(self, instance, user): default_value = self.field_options.get('default') if self.default is None: return default_value - template = Template(self.default) + + # Use the custom environment to create a Template + template = custom_env.from_string(self.default) from service_catalog.api.serializers import InstanceSerializer from profiles.api.serializers import UserSerializer context = {