From 182cb78e16f8e4f60a8b91dd50cc48a35de4a46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Sat, 7 Sep 2024 15:43:15 +0200 Subject: [PATCH] Fix "TODO" search for Santa targets & ballots --- tests/santa/test_ballots_views.py | 7 +++++++ zentral/contrib/santa/forms.py | 24 ++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/santa/test_ballots_views.py b/tests/santa/test_ballots_views.py index 0eeda39f40..b369b32c7c 100644 --- a/tests/santa/test_ballots_views.py +++ b/tests/santa/test_ballots_views.py @@ -285,6 +285,13 @@ def test_ballots_reset_ballot_included(self): def test_ballots_todo_filter(self): _, realm_user2 = force_realm_user(realm=self.realm) self._login("santa.view_ballot") + TargetState.objects.create( + target=self.file_target, + configuration=self.configuration, + state=TargetState.State.UNTRUSTED, + score=0, + reset_at=datetime.now() - timedelta(days=1) + ) force_ballot(self.file_target, self.realm_user, [(self.configuration, True, 192)]) force_ballot(self.metabundle_target, realm_user2, [(self.configuration, False, 934)]) response = self.client.get(reverse("santa:ballots"), {"todo": "on"}) diff --git a/zentral/contrib/santa/forms.py b/zentral/contrib/santa/forms.py index 574a4ff874..54f94b0ee1 100644 --- a/zentral/contrib/santa/forms.py +++ b/zentral/contrib/santa/forms.py @@ -593,8 +593,8 @@ def search_query( else: bi_where = ce_where = bu_where = mbu_where = "" ti_where = "where c.organizational_unit ~ '[A-Z0-9]{10}'" - ch_where = "where f.cdhash IS NOT NULL" - si_where = "where f.signing_id IS NOT NULL" + ch_where = "where (f.cdhash = '') IS FALSE" + si_where = "where (f.signing_id = '') IS FALSE" wheres = [] havings = [] # target state @@ -629,15 +629,19 @@ def search_query( ) # no votes from user if username or email: - todo_cfg_where = "" + todo_cfg_where = "and (nets.reset_at is null or nets.reset_at < nev.created_at)" if configuration_pk: - todo_cfg_where = "and nev.configuration_id = %(configuration_pk)s" + todo_cfg_where = f"{todo_cfg_where} and nev.configuration_id = %(configuration_pk)s" wheres.append( "not exists (" " select * from santa_vote nev" " join santa_ballot neb on (nev.ballot_id = neb.id)" + " left join santa_targetstate nets on (" + " neb.target_id = nets.target_id" + " and nev.configuration_id = nets.configuration_id" + " )" " left join realms_realmuser neu on (neb.realm_user_id = neu.uuid)" - " where neb.target_id = t.id " + " where neb.target_id = t.id and neb.replaced_by_id is null" " and (neb.user_uid = %(username)s or neu.username = %(username)s" " or neb.user_uid = %(email)s or neu.username = %(email)s)" f" {todo_cfg_where}" @@ -936,15 +940,19 @@ def results(self, current_username, current_email, offset, limit): where_list.append("(ts.reset_at is null or v.created_at is null or v.created_at > ts.reset_at)") todo = self.cleaned_data.get("todo") if todo: - todo_cfg_where = "" + todo_cfg_where = "and (nets.reset_at is null or nets.reset_at < nev.created_at)" if configuration: - todo_cfg_where = "and nev.configuration_id = %(configuration_pk)s" + todo_cfg_where = f"{todo_cfg_where} and nev.configuration_id = %(configuration_pk)s" where_list.append( "not exists (" " select * from santa_vote nev" " join santa_ballot neb on (nev.ballot_id = neb.id)" + " join santa_targetstate nets on (" + " neb.target_id = nets.target_id" + " and nev.configuration_id = nets.configuration_id" + " )" " left join realms_realmuser neu on (neb.realm_user_id = neu.uuid)" - " where neb.target_id = t.id " + " where neb.target_id = t.id and neb.replaced_by_id is null" " and (neb.user_uid = %(current_username)s or neu.username = %(current_username)s" " or neb.user_uid = %(current_email)s or neu.username = %(current_email)s)" f" {todo_cfg_where}"