Skip to content

Commit

Permalink
Prefetch related models in FPR views
Browse files Browse the repository at this point in the history
  • Loading branch information
replaceafill authored Aug 29, 2024
1 parent ee0ba15 commit 48d3c11
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 127 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module = [
"src.MCPClient.lib.clientScripts.policy_check",
"src.MCPClient.lib.clientScripts.transcribe_file",
"src.MCPClient.lib.clientScripts.validate_file",
"tests.dashboard.fpr.test_views",
"tests.MCPClient.conftest",
"tests.MCPClient.test_characterize_file",
"tests.MCPClient.test_has_packages",
Expand Down
22 changes: 19 additions & 3 deletions src/dashboard/src/fpr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ class IDRuleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Limit to only enabled formats/commands
self.fields["format"].queryset = fprmodels.FormatVersion.active.all()
self.fields["command"].queryset = fprmodels.IDCommand.active.all()
self.fields[
"format"
].queryset = fprmodels.FormatVersion.active.all().prefetch_related(
"format__group"
)
self.fields[
"command"
].queryset = fprmodels.IDCommand.active.all().prefetch_related("tool")

class Meta:
model = fprmodels.IDRule
Expand Down Expand Up @@ -110,7 +116,11 @@ def __init__(self, *args, **kwargs):
self.fields["command"].initial = self.instance.command.uuid

# Show only active format versions in the format dropdown
self.fields["format"].queryset = fprmodels.FormatVersion.active.all()
self.fields[
"format"
].queryset = fprmodels.FormatVersion.active.all().prefetch_related(
"format__group"
)

def clean(self):
cleaned_data = super().clean()
Expand Down Expand Up @@ -168,6 +178,12 @@ class FPCommandForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields[
"output_format"
].queryset = fprmodels.FormatVersion.active.all().prefetch_related(
"format__group"
)

verification_commands = fprmodels.FPCommand.active.filter(
command_usage="verification"
)
Expand Down
22 changes: 16 additions & 6 deletions src/dashboard/src/fpr/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ def idrule_list(request):
"replaces_id"
)
]
idrules = fprmodels.IDRule.objects.exclude(uuid__in=replacing_rules)
idrules = fprmodels.IDRule.objects.exclude(
uuid__in=replacing_rules
).prefetch_related("format__format__group", "command")
return render(request, "fpr/idrule/list.html", context(locals()))


Expand Down Expand Up @@ -391,7 +393,9 @@ def idcommand_list(request):
"replaces_id"
)
]
idcommands = fprmodels.IDCommand.objects.exclude(uuid__in=replacing_commands)
idcommands = fprmodels.IDCommand.objects.exclude(
uuid__in=replacing_commands
).prefetch_related("tool")
return render(request, "fpr/idcommand/list.html", context(locals()))


Expand Down Expand Up @@ -493,7 +497,11 @@ def fprule_list(request, usage=None):
else:
opts = {}
# Display disabled rules as long as they aren't replaced by another rule
fprules = fprmodels.FPRule.objects.filter(**opts).exclude(uuid__in=replacing_rules)
fprules = (
fprmodels.FPRule.objects.filter(**opts)
.exclude(uuid__in=replacing_rules)
.prefetch_related("format__format__group", "command")
)
return render(request, "fpr/fprule/list.html", context(locals()))


Expand Down Expand Up @@ -621,8 +629,10 @@ def fpcommand_list(request, usage=None):
"replaces_id"
)
]
fpcommands = fprmodels.FPCommand.objects.filter(**opts).exclude(
uuid__in=replacing_commands
fpcommands = (
fprmodels.FPCommand.objects.filter(**opts)
.exclude(uuid__in=replacing_commands)
.prefetch_related("tool")
)
return render(request, "fpr/fpcommand/list.html", context(locals()))

Expand All @@ -641,7 +651,7 @@ def fpcommand_edit(request, uuid=None):
title = _("Replace command %(name)s") % {"name": fpcommand.description}
else:
fpcommand = None
title = _("Create format version")
title = _("Create format policy command")
if request.method == "POST":
form = fprforms.FPCommandForm(request.POST, instance=fpcommand)
if form.is_valid():
Expand Down
Loading

0 comments on commit 48d3c11

Please sign in to comment.