From db085db85b99f0edaea68fe346414fae0e9de6ca Mon Sep 17 00:00:00 2001 From: Faakhir30 Date: Mon, 9 Sep 2024 00:17:11 +0500 Subject: [PATCH] Added start and end filters in redirectionSet. --- .../controlpanel/browser/redirects.py | 42 +++++++++++++++---- .../test_controlpanel_browser_redirection.py | 35 ++++++++++++---- news/4009.feature | 1 + 3 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 news/4009.feature diff --git a/Products/CMFPlone/controlpanel/browser/redirects.py b/Products/CMFPlone/controlpanel/browser/redirects.py index 46b128ca75..729824cacb 100644 --- a/Products/CMFPlone/controlpanel/browser/redirects.py +++ b/Products/CMFPlone/controlpanel/browser/redirects.py @@ -163,7 +163,7 @@ def view_url(self): class RedirectionSet: - def __init__(self, query="", created="", manual=""): + def __init__(self, query="", created="", manual="", start="", end=""): self.storage = getUtility(IRedirectionStorage) portal = getSite() @@ -190,20 +190,34 @@ def __init__(self, query="", created="", manual=""): else: manual = "" if created: + end = created + logger.warning( + "The 'datetime' parameter is deprecated. Use 'end' parameter instead." + ) + if start: + try: + start = DateTime(start) + except DateTimeError: + logger.warning("Failed to parse as DateTime: %s", start) + start = "" + if end: try: - created = DateTime(created) + end = DateTime(end) except DateTimeError: - logger.warning("Failed to parse as DateTime: %s", created) - created = "" - if created or manual != "": + logger.warning("Failed to parse as DateTime: %s", end) + end = "" + if start or end or manual != "": chosen = [] for redirect in self.data: info = self.storage.get_full(redirect) if manual != "": if info[2] != manual: continue - if created and info[1]: - if info[1] >= created: + if start and info[1]: + if info[1] < start: + continue + if end and info[1]: + if info[1] >= end: continue chosen.append(redirect) self.data = chosen @@ -256,6 +270,8 @@ def redirects(self): RedirectionSet( query=self.request.form.get("q", ""), created=self.request.form.get("datetime", ""), + start=self.request.form.get("start", ""), + end=self.request.form.get("end", ""), manual=self.request.form.get("manual", ""), ), int(self.request.form.get("b_size", "15")), @@ -280,9 +296,17 @@ def __call__(self): else: query = self.request.form.get("q", "") created = self.request.form.get("datetime", "") + start = self.request.form.get("start", "") + end = self.request.form.get("end", "") manual = self.request.form.get("manual", "") - if created or manual or (query and query != "/"): - rset = RedirectionSet(query=query, created=created, manual=manual) + if start or end or manual or (query and query != "/"): + rset = RedirectionSet( + query=query, + created=created, + manual=manual, + start=start, + end=end, + ) redirects = list(rset.data) else: redirects = [] diff --git a/Products/CMFPlone/controlpanel/tests/test_controlpanel_browser_redirection.py b/Products/CMFPlone/controlpanel/tests/test_controlpanel_browser_redirection.py index 4099a0f60f..8e8d3057ab 100644 --- a/Products/CMFPlone/controlpanel/tests/test_controlpanel_browser_redirection.py +++ b/Products/CMFPlone/controlpanel/tests/test_controlpanel_browser_redirection.py @@ -336,7 +336,7 @@ def test_redirection_controlpanel_filter_date(self): redirects = RedirectionSet() self.assertEqual(len(redirects), 400) - # created can be anything that can be parsed by DateTime. + # created can be anything that can be parsed by DateTime. (deprecated) # Otherwise it is ignored. self.assertEqual(len(RedirectionSet(created="2019-01-01")), 400) self.assertEqual(len(RedirectionSet(created="1999-01-01")), 0) @@ -346,38 +346,57 @@ def test_redirection_controlpanel_filter_date(self): self.assertEqual(len(RedirectionSet(created="2001-02-01 00:00:00")), 31) self.assertEqual(len(RedirectionSet(created="2001-02-01 00:00:01")), 32) self.assertEqual(len(RedirectionSet(created="badvalue")), 400) + # start is inclusive and can be anything that can be parsed by DateTime. + # Otherwise it is ignored. + self.assertEqual(len(RedirectionSet(start="2019-01-01")), 0) + self.assertEqual(len(RedirectionSet(start="2001-01-02")), 399) + self.assertEqual(len(RedirectionSet(start="2001-02-01 00:00:00")), 369) + self.assertEqual(len(RedirectionSet(start="2001-02-01 00:00:01")), 368) + self.assertEqual(len(RedirectionSet(start="badvalue")), 400) + + # End is exclisive and can be anything that can be parsed by DateTime. + # Otherwise it is ignored. + self.assertEqual(len(RedirectionSet(end="1999-01-01")), 0) + self.assertEqual(len(RedirectionSet(end="2000-01-01")), 0) + self.assertEqual(len(RedirectionSet(end="2001-02-01")), 31) + self.assertEqual(len(RedirectionSet(end="2001-02-01 00:00:00")), 31) + self.assertEqual(len(RedirectionSet(end="2001-02-01 00:00:01")), 32) + self.assertEqual(len(RedirectionSet(end="badvalue")), 400) + + self.assertEqual(len(RedirectionSet(start="2001-01-01", end="2001-01-01")), 0) + self.assertEqual(len(RedirectionSet(start="2001-01-01", end="2001-01-02")), 1) # DateTime('2002-01-01') results in a timezone GMT+0 - self.assertEqual(len(RedirectionSet(created="2002-01-01")), 365) + self.assertEqual(len(RedirectionSet(end="2002-01-01")), 365) # DateTime('2002/01/01') results in a timezone GMT+1 for me, # or a different zone depending on where in the world you are. # So we need to be lenient in the tests. - self.assertGreaterEqual(len(RedirectionSet(created="2002/01/01")), 364) - self.assertLessEqual(len(RedirectionSet(created="2002/01/01")), 366) + self.assertGreaterEqual(len(RedirectionSet(end="2002/01/01")), 364) + self.assertLessEqual(len(RedirectionSet(end="2002/01/01")), 366) request = self.layer["request"].clone() - request.form["datetime"] = "" + request.form["start"] = "" view = getMultiAdapter( (self.layer["portal"], request), name="redirection-controlpanel" ) self.assertEqual(view.redirects().numpages, math.ceil(400 / 15.0)) request = self.layer["request"].clone() - request.form["datetime"] = "2001-01-27" + request.form["end"] = "2001-01-27" view = getMultiAdapter( (self.layer["portal"], request), name="redirection-controlpanel" ) self.assertEqual(view.redirects().numpages, math.ceil(27 / 15.0)) request = self.layer["request"].clone() - request.form["datetime"] = "2002-01-01" + request.form["end"] = "2002-01-01" view = getMultiAdapter( (self.layer["portal"], request), name="redirection-controlpanel" ) self.assertEqual(view.redirects().numpages, math.ceil(365 / 15.0)) request = self.layer["request"].clone() - request.form["datetime"] = "2019-01-01" + request.form["end"] = "2019-01-01" view = getMultiAdapter( (self.layer["portal"], request), name="redirection-controlpanel" ) diff --git a/news/4009.feature b/news/4009.feature new file mode 100644 index 0000000000..ed20049db0 --- /dev/null +++ b/news/4009.feature @@ -0,0 +1 @@ +Added start and end dateTime filters to redirectionSet. @Faakhir30