Skip to content

Commit

Permalink
Add enabled to ApprovalWorflow
Browse files Browse the repository at this point in the history
  • Loading branch information
a-belhadj committed Nov 21, 2023
1 parent b1fe863 commit a1bbb35
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion service_catalog/filters/approval_workflow_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class ApprovalWorkflowFilter(SquestFilter):
class Meta:
model = ApprovalWorkflow
fields = ["name"]
fields = ["name", "enabled"]
5 changes: 3 additions & 2 deletions service_catalog/forms/approval_workflow_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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():
Expand Down
18 changes: 18 additions & 0 deletions service_catalog/migrations/0036_approvalworkflow_enabled.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
4 changes: 2 additions & 2 deletions service_catalog/models/approval_workflow.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
Expand Down
9 changes: 6 additions & 3 deletions service_catalog/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion service_catalog/tables/approval_workflow_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions service_catalog/views/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions templates/service_catalog/approvalworkflow_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ <h3 class="card-title">{{ object.name }}</h3>
<b>Operation</b>
<span class="float-right">{{ object.operation.name }}</span>
</li>
<li class="list-group-item">
<b>Enabled</b>
<span class="float-right">{{ object.enabled | display_boolean }}</span>
</li>
{% if object.scopes.all %}
<li class="list-group-item">
<b>Restricted scopes</b>
Expand Down

0 comments on commit a1bbb35

Please sign in to comment.