Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Script checks search #831

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions tests/munki/test_script_check_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,40 @@ def test_script_checks_no_create_link(self):
self.assertNotContains(response, reverse("munki:create_script_check"))

def test_script_checks_with_create_link(self):
sc = force_script_check()
sc_one = force_script_check()
sc_two = force_script_check()
self._login("munki.view_scriptcheck", "munki.add_scriptcheck")
response = self.client.get(reverse("munki:script_checks"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "munki/scriptcheck_list.html")
self.assertContains(response, sc.compliance_check.name)
self.assertContains(response, reverse("munki:create_script_check"))
self.assertContains(response, sc_one.compliance_check.name)
self.assertContains(response, sc_two.compliance_check.name)
self.assertContains(response, "Script checks (2)")

def test_script_check_search(self):
sc_one = force_script_check()
sc_two = force_script_check(type=ScriptCheck.Type.ZSH_BOOL)
self._login("munki.view_scriptcheck", "munki.add_scriptcheck")

response = self.client.get(reverse("munki:script_checks"), {"name": "test"})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "munki/scriptcheck_list.html")
self.assertContains(response, "Script checks (0)")

response = self.client.get(reverse("munki:script_checks"),
{"name": sc_one.compliance_check.name})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "munki/scriptcheck_list.html")
self.assertContains(response, sc_one.compliance_check.name)
self.assertContains(response, "Script check (1)")

response = self.client.get(reverse("munki:script_checks"),
{"type": ScriptCheck.Type.ZSH_BOOL})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "munki/scriptcheck_list.html")
self.assertContains(response, sc_two.compliance_check.name)
self.assertContains(response, "Script check (1)")

# create

Expand Down
28 changes: 28 additions & 0 deletions zentral/contrib/munki/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ def __init__(self, *args, **kwargs):
self.fields["configuration"].widget = forms.HiddenInput()


class ScriptCheckSearchForm(forms.Form):
template_name = "django/forms/search.html"

name = forms.CharField(
label='Name',
required=False,
widget=forms.TextInput(
attrs={"autofocus": True,
"size": 32,
}
)
)
type = forms.ChoiceField(
choices=[('', '...')] + ScriptCheck.Type.choices,
required=False,
)

def get_queryset(self):
qs = ScriptCheck.objects.all()
name = self.cleaned_data.get("name")
if name:
qs = qs.filter(compliance_check__name__icontains=name)
type = self.cleaned_data.get("type")
if type:
qs = qs.filter(type=type)
return qs


class ScriptCheckForm(forms.ModelForm):
class Meta:
model = ScriptCheck
Expand Down
20 changes: 13 additions & 7 deletions zentral/contrib/munki/templates/munki/scriptcheck_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ <h3 class="m-0">Script check{{ paginator.count|pluralize }} ({{ paginator.count
<div class="ms-auto">
{% if perms.munki.add_scriptcheck %}
{% url 'munki:create_script_check' as url %}
{% button 'CREATE' url "Create new Configuration" %}
{% button 'CREATE' url "Create new Script check" %}
{% endif %}
</div>
</div>

<div class="d-flex flex-wrap align-items-center mb-1">
<form method="GET" class="search-form d-flex flex-row flex-wrap w-75">

{{ form }}

<button type="submit" class="btn btn-link text-dark text-decoration-none mb-2">
<i class="bi bi-search"></i>
</button>
</div>

{% if object_list %}

{% pagination next_url previous_url %}
Expand Down Expand Up @@ -52,12 +62,8 @@ <h3 class="m-0">Script check{{ paginator.count|pluralize }} ({{ paginator.count
{% pagination next_url previous_url %}

{% else %}
{% if perms.munki.add_scriptcheck %}
{% url 'munki:create_script_check' as link %}
{% no_entities 'Script checks' link %}
{% else %}
{% no_entities 'Script checks' %}
{% endif %}
{% url 'munki:script_checks' as empty_results_url %}
{% empty_results empty_results_url %}
{% endif %}

{% endblock %}
18 changes: 16 additions & 2 deletions zentral/contrib/munki/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from zentral.utils.text import encode_args
from zentral.utils.views import DeleteViewWithAudit, UserPaginationListView
from .compliance_checks import MunkiScriptCheck
from .forms import CreateInstallProbeForm, ConfigurationForm, EnrollmentForm, ScriptCheckForm, UpdateInstallProbeForm
from .forms import (CreateInstallProbeForm, ConfigurationForm, EnrollmentForm, ScriptCheckForm,
ScriptCheckSearchForm, UpdateInstallProbeForm)
from .models import Configuration, Enrollment, PrincipalUserDetectionSource, ScriptCheck
from .terraform import iter_resources

Expand Down Expand Up @@ -204,7 +205,20 @@ def post(self, request, *args, **kwargs):

class ScriptCheckListView(PermissionRequiredMixin, UserPaginationListView):
permission_required = "munki.view_scriptcheck"
model = ScriptCheck
template_name = "munki/scriptcheck_list.html"

def dispatch(self, request, *args, **kwargs):
self.form = ScriptCheckSearchForm(self.request.GET)
self.form.is_valid()
return super().dispatch(request, *args, **kwargs)

def get_queryset(self):
return self.form.get_queryset()

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["form"] = self.form
return ctx


class CreateScriptCheckView(PermissionRequiredMixin, TemplateView):
Expand Down