Skip to content

Commit

Permalink
Added start and end filters in redirectionSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Faakhir30 committed Sep 8, 2024
1 parent 3fe7f24 commit 84b113c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
35 changes: 23 additions & 12 deletions Products/CMFPlone/controlpanel/browser/redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def view_url(self):


class RedirectionSet:
def __init__(self, query="", created="", manual=""):
def __init__(self, query="", manual="", start="", end=""):
self.storage = getUtility(IRedirectionStorage)

portal = getSite()
Expand All @@ -189,21 +189,30 @@ def __init__(self, query="", created="", manual=""):
manual = False
else:
manual = ""
if created:
if start:
try:
created = DateTime(created)
start = DateTime(start)
except DateTimeError:
logger.warning("Failed to parse as DateTime: %s", created)
created = ""
if created or manual != "":
logger.warning("Failed to parse as DateTime: %s", start)
start = ""
if end:
try:
end = DateTime(end)
except DateTimeError:
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 @@ -255,7 +264,8 @@ def redirects(self):
return Batch(
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 @@ -279,10 +289,11 @@ def __call__(self):
redirects = form.get("redirects", ())
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, 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,48 +336,58 @@ def test_redirection_controlpanel_filter_date(self):

redirects = RedirectionSet()
self.assertEqual(len(redirects), 400)
# created can be anything that can be parsed by DateTime.
# start can be anything that can be parsed by DateTime.
# Otherwise it is ignored.
self.assertEqual(len(RedirectionSet(created="2019-01-01")), 400)
self.assertEqual(len(RedirectionSet(created="1999-01-01")), 0)
self.assertEqual(len(RedirectionSet(created="2001-01-01")), 0)
self.assertEqual(len(RedirectionSet(created="2001-01-02")), 1)
self.assertEqual(len(RedirectionSet(created="2001-02-01")), 31)
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)
self.assertEqual(len(RedirectionSet(start="2019-01-01")), 400)
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)

# Similarly, end 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)

# Start and End are inclusive.
self.assertEqual(len(RedirectionSet(start="2001-01-01", end="2001-01-01")), 1)
self.assertEqual(len(RedirectionSet(start="2001-01-01", end="2001-01-02")), 2)

# 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

0 comments on commit 84b113c

Please sign in to comment.