Skip to content

Commit

Permalink
Merge pull request #4009 from Faakhir30/add_date_filters
Browse files Browse the repository at this point in the history
Added start and end filters in redirectionSet.
  • Loading branch information
Faakhir30 authored Sep 12, 2024
2 parents 3fe7f24 + 0dc28a0 commit 3c4df5d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
44 changes: 35 additions & 9 deletions Products/CMFPlone/controlpanel/browser/redirects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from csv import writer
import warnings
from DateTime import DateTime
from DateTime.interfaces import DateTimeError
from io import StringIO
Expand Down Expand Up @@ -163,7 +164,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()
Expand All @@ -190,20 +191,35 @@ def __init__(self, query="", created="", manual=""):
else:
manual = ""
if created:
end = created
warnings.warn(
"The 'created' parameter is deprecated. Use 'end' parameter instead.",
DeprecationWarning,
)
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
Expand Down Expand Up @@ -256,6 +272,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")),
Expand All @@ -280,9 +298,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 created or 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 = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)
Expand Down
1 change: 1 addition & 0 deletions news/4009.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Redirection control panel: Added support for start and end filters. @Faakhir30

0 comments on commit 3c4df5d

Please sign in to comment.