From a1bbb359698165f95395720bf9fea62a5783d776 Mon Sep 17 00:00:00 2001 From: Anthony Belhadj Date: Tue, 21 Nov 2023 21:02:53 +0100 Subject: [PATCH] Add enabled to ApprovalWorflow --- .../approval_workflow_serializer.py | 5 +++-- .../filters/approval_workflow_filter.py | 2 +- .../forms/approval_workflow_form.py | 5 +++-- .../0036_approvalworkflow_enabled.py | 18 ++++++++++++++++++ service_catalog/models/approval_workflow.py | 4 ++-- service_catalog/models/request.py | 9 ++++++--- .../tables/approval_workflow_table.py | 2 +- service_catalog/views/operation.py | 4 ++-- .../approvalworkflow_detail.html | 4 ++++ 9 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 service_catalog/migrations/0036_approvalworkflow_enabled.py diff --git a/service_catalog/api/serializers/approval_workflow_serializer.py b/service_catalog/api/serializers/approval_workflow_serializer.py index fd9eabe0f..82fae187e 100644 --- a/service_catalog/api/serializers/approval_workflow_serializer.py +++ b/service_catalog/api/serializers/approval_workflow_serializer.py @@ -7,7 +7,7 @@ class ApprovalWorkflowSerializer(ModelSerializer): class Meta: model = ApprovalWorkflow - fields = ['id', 'name', 'operation', 'scopes'] + fields = ['id', 'enabled', 'name', 'operation', 'scopes'] read_only_fields = ['id'] def validate(self, data): @@ -19,7 +19,8 @@ def validate(self, data): # check that selected scopes are not already in use by another approval workflow for the selected operation exclude_id = self.instance.id if self.instance else None if len(scopes) == 0: - if ApprovalWorkflow.objects.filter(operation=operation, scopes__isnull=True).exclude(id=exclude_id).exists(): + if ApprovalWorkflow.objects.filter(enabled=True, operation=operation, scopes__isnull=True).exclude( + id=exclude_id).exists(): raise ValidationError({"scopes": f"An approval workflow for all scopes already exists"}) for scope in scopes: if scope.approval_workflows.filter(operation=operation).exclude(id=exclude_id).exists(): diff --git a/service_catalog/filters/approval_workflow_filter.py b/service_catalog/filters/approval_workflow_filter.py index 7424ca4fd..5cb731bb5 100644 --- a/service_catalog/filters/approval_workflow_filter.py +++ b/service_catalog/filters/approval_workflow_filter.py @@ -5,4 +5,4 @@ class ApprovalWorkflowFilter(SquestFilter): class Meta: model = ApprovalWorkflow - fields = ["name"] + fields = ["name", "enabled"] diff --git a/service_catalog/forms/approval_workflow_form.py b/service_catalog/forms/approval_workflow_form.py index e53f88345..18332e336 100644 --- a/service_catalog/forms/approval_workflow_form.py +++ b/service_catalog/forms/approval_workflow_form.py @@ -7,7 +7,7 @@ class ApprovalWorkflowForm(SquestModelForm): class Meta: model = ApprovalWorkflow - fields = ['name', 'operation', 'scopes'] + fields = ['name', 'operation', 'scopes', 'enabled'] def clean(self): cleaned_data = super().clean() @@ -16,7 +16,8 @@ def clean(self): # check that selected scopes are not already in use by another approval workflow for the selected operation exclude_id = self.instance.id if self.instance else None if not scopes.exists(): - if ApprovalWorkflow.objects.filter(operation=operation, scopes__isnull=True).exclude(id=exclude_id).exists(): + if ApprovalWorkflow.objects.filter(enabled=True, operation=operation, scopes__isnull=True).exclude( + id=exclude_id).exists(): raise ValidationError({"scopes": f"An approval workflow for all scopes already exists"}) for scope in scopes: if scope.approval_workflows.filter(operation=operation).exclude(id=exclude_id).exists(): diff --git a/service_catalog/migrations/0036_approvalworkflow_enabled.py b/service_catalog/migrations/0036_approvalworkflow_enabled.py new file mode 100644 index 000000000..830d3fcb0 --- /dev/null +++ b/service_catalog/migrations/0036_approvalworkflow_enabled.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.6 on 2023-11-21 19:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('service_catalog', '0035_alter_request_options'), + ] + + operations = [ + migrations.AddField( + model_name='approvalworkflow', + name='enabled', + field=models.BooleanField(default=True, help_text='True to use the workflow'), + ), + ] diff --git a/service_catalog/models/approval_workflow.py b/service_catalog/models/approval_workflow.py index 39a023d3d..cd5f1caf8 100644 --- a/service_catalog/models/approval_workflow.py +++ b/service_catalog/models/approval_workflow.py @@ -1,4 +1,4 @@ -from django.db.models import CharField, ForeignKey, ManyToManyField, CASCADE +from django.db.models import CharField, ForeignKey, ManyToManyField, CASCADE, BooleanField from Squest.utils.squest_model import SquestModel from profiles.models import AbstractScope, GlobalScope, Scope @@ -7,7 +7,7 @@ class ApprovalWorkflow(SquestModel): name = CharField(max_length=100, blank=False, unique=True) - + enabled = BooleanField(default=True, help_text="True to use the workflow") operation = ForeignKey( 'service_catalog.Operation', related_name='approval_workflows', diff --git a/service_catalog/models/request.py b/service_catalog/models/request.py index 67e258329..16448fdec 100644 --- a/service_catalog/models/request.py +++ b/service_catalog/models/request.py @@ -342,20 +342,23 @@ def check_job_status(self): def _get_approval_workflow(self): from service_catalog.models import ApprovalWorkflow - workflow = ApprovalWorkflow.objects.filter(operation=self.operation, - scopes__id__in=[self.instance.quota_scope.id]) + workflow = ApprovalWorkflow.objects.filter( + enabled=True, + operation=self.operation, + scopes__id__in=[self.instance.quota_scope.id]) if workflow.exists(): return workflow.first() if self.instance.quota_scope.is_team: parent_workflow = ApprovalWorkflow.objects.filter( + enabled=True, operation=self.operation, scopes__id__in=[self.instance.quota_scope.get_object().org.id] ) if parent_workflow.exists(): return parent_workflow.first() - default_workflow = ApprovalWorkflow.objects.filter(operation=self.operation, scopes__isnull=True) + default_workflow = ApprovalWorkflow.objects.filter(enabled=True, operation=self.operation, scopes__isnull=True) if default_workflow.exists(): return default_workflow.first() return None diff --git a/service_catalog/tables/approval_workflow_table.py b/service_catalog/tables/approval_workflow_table.py index 675bd8178..4c69afd75 100644 --- a/service_catalog/tables/approval_workflow_table.py +++ b/service_catalog/tables/approval_workflow_table.py @@ -12,7 +12,7 @@ class ApprovalWorkflowTable(SquestTable): class Meta: model = ApprovalWorkflow attrs = {"id": "approval_workflow_table", "class": "table squest-pagination-tables "} - fields = ("name", "operation", "scopes", "actions") + fields = ("name", "operation", "scopes", "enabled", "actions") def render_scopes(self, value, record): scopes = record.scopes.all().distinct() diff --git a/service_catalog/views/operation.py b/service_catalog/views/operation.py index b850a9ab1..ef7d1d69c 100644 --- a/service_catalog/views/operation.py +++ b/service_catalog/views/operation.py @@ -56,8 +56,8 @@ def get_context_data(self, **kwargs): context['extra_html_button_path'] = "service_catalog/buttons/operation_survey_button.html" if self.request.user.has_perm('service_catalog.view_approvalworkflow'): context['workflows_table'] = ApprovalWorkflowTable( - ApprovalWorkflow.objects.filter(operation=self.get_object()), exclude=['operation'], - prefix="operation-") + ApprovalWorkflow.objects.filter(enabled=True, operation=self.get_object()), exclude=['operation'], + prefix="operation-", hide_fields=["enabled"]) config.configure(context['workflows_table']) return context diff --git a/templates/service_catalog/approvalworkflow_detail.html b/templates/service_catalog/approvalworkflow_detail.html index 59acacf23..c554003ef 100644 --- a/templates/service_catalog/approvalworkflow_detail.html +++ b/templates/service_catalog/approvalworkflow_detail.html @@ -35,6 +35,10 @@

{{ object.name }}

Operation {{ object.operation.name }} +
  • + Enabled + {{ object.enabled | display_boolean }} +
  • {% if object.scopes.all %}
  • Restricted scopes