diff --git a/service_catalog/migrations/0038_alter_towersurveyfield_unique_together_and_more.py b/service_catalog/migrations/0038_alter_towersurveyfield_unique_together_and_more.py new file mode 100644 index 000000000..458409115 --- /dev/null +++ b/service_catalog/migrations/0038_alter_towersurveyfield_unique_together_and_more.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.6 on 2023-12-08 14:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('service_catalog', '0037_alter_request_options_remove_approvalstep_next_and_more'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='towersurveyfield', + unique_together=set(), + ), + migrations.AddField( + model_name='towersurveyfield', + name='position', + field=models.IntegerField(default=0), + ), + migrations.AlterUniqueTogether( + name='towersurveyfield', + unique_together={('operation', 'position', 'variable')}, + ), + ] diff --git a/service_catalog/models/operations.py b/service_catalog/models/operations.py index 4d46aa652..6019e4c5d 100644 --- a/service_catalog/models/operations.py +++ b/service_catalog/models/operations.py @@ -64,9 +64,11 @@ def update_survey(self): if self.job_template is not None: spec_list = self.job_template.survey.get("spec", []) from service_catalog.models.tower_survey_field import TowerSurveyField + position = 0 for field in spec_list: squest_field, created = TowerSurveyField.objects.get_or_create( variable=field['variable'], + position=position, operation=self, defaults={ 'is_customer_field': True, @@ -84,6 +86,7 @@ def update_survey(self): ) if not created: squest_field.name = field['question_name'] + squest_field.position = position squest_field.description = field['question_description'] squest_field.type = field['type'] squest_field.required = field['required'] @@ -94,6 +97,7 @@ def update_survey(self): "default": field.get('default', '') } squest_field.save() + position += 1 self.tower_survey_fields.exclude( variable__in=[survey_spec["variable"] for survey_spec in spec_list]).delete() @@ -113,9 +117,11 @@ def add_job_template_survey_as_default_survey(cls, sender, instance, created, *a # copy the default survey and add a flag 'is_visible' default_survey = instance.job_template.survey if "spec" in default_survey: + position = 0 for field in default_survey["spec"]: TowerSurveyField.objects.create( variable=field['variable'], + position=position, is_customer_field=True, operation=instance, name=field['question_name'], @@ -129,6 +135,7 @@ def add_job_template_survey_as_default_survey(cls, sender, instance, created, *a "default": field.get('default', '') } ) + position += 1 post_save.connect(Operation.add_job_template_survey_as_default_survey, sender=Operation) diff --git a/service_catalog/models/tower_survey_field.py b/service_catalog/models/tower_survey_field.py index c5eeaa85a..04e0b037b 100644 --- a/service_catalog/models/tower_survey_field.py +++ b/service_catalog/models/tower_survey_field.py @@ -1,7 +1,7 @@ import logging import sys -from django.db.models import CharField, BooleanField, ForeignKey, CASCADE, SET_NULL, JSONField +from django.db.models import CharField, BooleanField, ForeignKey, CASCADE, SET_NULL, JSONField, IntegerField from django.db.models.signals import pre_save from django.dispatch import receiver from django.forms import CharField as FormsCharField @@ -47,9 +47,10 @@ def __init__(self, quota=None, *args, **kwargs): class TowerSurveyField(SquestModel): class Meta(SquestModel.Meta): - unique_together = ('operation', 'variable',) + unique_together = ('operation', 'position', 'variable',) variable = CharField(null=False, blank=False, max_length=200) + position = IntegerField(default=0) is_customer_field = BooleanField(default=True, null=False, blank=False, help_text="Display for non approver user") default = CharField(null=True, blank=True, max_length=200, verbose_name="Default value") operation = ForeignKey(Operation,