mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
chore: udpate date filters to support dynamic options
This commit is contained in:
parent
d47efaa0f0
commit
608ba9d5cb
@ -1,3 +1,38 @@
|
|||||||
|
import re
|
||||||
|
from datetime import timedelta
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
# The date from pattern
|
||||||
|
pattern = re.compile(r"\d+_(weeks|months)$")
|
||||||
|
|
||||||
|
|
||||||
|
# Get the 2_weeks, 3_months
|
||||||
|
def date_filter(filter, duration, subsequent, term, date_filter, offset):
|
||||||
|
now = timezone.now().date()
|
||||||
|
if term == "months":
|
||||||
|
if subsequent == "after":
|
||||||
|
if offset == "fromnow":
|
||||||
|
filter[f"{date_filter}__gte"] = now + timedelta(days=duration * 30)
|
||||||
|
else:
|
||||||
|
filter[f"{date_filter}__gte"] = now - timedelta(days=duration * 30)
|
||||||
|
else:
|
||||||
|
if offset == "fromnow":
|
||||||
|
filter[f"{date_filter}__lte"] = now + timedelta(days=duration * 30)
|
||||||
|
else:
|
||||||
|
filter[f"{date_filter}__lte"] = now - timedelta(days=duration * 30)
|
||||||
|
if term == "weeks":
|
||||||
|
if subsequent == "after":
|
||||||
|
if offset == "fromnow":
|
||||||
|
filter[f"{date_filter}__gte"] = now + timedelta(weeks=duration)
|
||||||
|
else:
|
||||||
|
filter[f"{date_filter}__gte"] = now - timedelta(weeks=duration)
|
||||||
|
else:
|
||||||
|
if offset == "fromnow":
|
||||||
|
filter[f"{date_filter}__lte"] = now + timedelta(days=duration)
|
||||||
|
else:
|
||||||
|
filter[f"{date_filter}__lte"] = now - timedelta(days=duration)
|
||||||
|
|
||||||
|
|
||||||
def filter_state(params, filter, method):
|
def filter_state(params, filter, method):
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
states = params.get("state").split(",")
|
states = params.get("state").split(",")
|
||||||
@ -95,18 +130,46 @@ def filter_created_at(params, filter, method):
|
|||||||
if len(created_ats) and "" not in created_ats:
|
if len(created_ats) and "" not in created_ats:
|
||||||
for query in created_ats:
|
for query in created_ats:
|
||||||
created_at_query = query.split(";")
|
created_at_query = query.split(";")
|
||||||
if len(created_at_query) == 2 and "after" in created_at_query:
|
if len(created_at_query) >= 2:
|
||||||
filter["created_at__date__gte"] = created_at_query[0]
|
match = pattern.match(created_at_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["created_at__date__lte"] = created_at_query[0]
|
if len(created_at_query) == 3:
|
||||||
|
digit, term = created_at_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=created_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="created_at__date",
|
||||||
|
offset=created_at_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in created_at_query:
|
||||||
|
filter["created_at__date__gte"] = created_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["created_at__date__lte"] = created_at_query[0]
|
||||||
else:
|
else:
|
||||||
if params.get("created_at", None) and len(params.get("created_at")):
|
if params.get("created_at", None) and len(params.get("created_at")):
|
||||||
for query in params.get("created_at"):
|
for query in params.get("created_at"):
|
||||||
created_at_query = query.split(";")
|
created_at_query = query.split(";")
|
||||||
if len(created_at_query) == 2 and "after" in created_at_query:
|
if len(created_at_query) == 2:
|
||||||
filter["created_at__date__gte"] = created_at_query[0]
|
match = pattern.match(created_at_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["created_at__date__lte"] = created_at_query[0]
|
if len(created_at_query) == 3:
|
||||||
|
digit, term = created_at_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=created_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="created_at__date",
|
||||||
|
offset=created_at_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in created_at_query:
|
||||||
|
filter["created_at__date__gte"] = created_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["created_at__date__lte"] = created_at_query[0]
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
|
|
||||||
@ -116,18 +179,46 @@ def filter_updated_at(params, filter, method):
|
|||||||
if len(updated_ats) and "" not in updated_ats:
|
if len(updated_ats) and "" not in updated_ats:
|
||||||
for query in updated_ats:
|
for query in updated_ats:
|
||||||
updated_at_query = query.split(";")
|
updated_at_query = query.split(";")
|
||||||
if len(updated_at_query) == 2 and "after" in updated_at_query:
|
if len(updated_at_query) == 2:
|
||||||
filter["updated_at__date__gte"] = updated_at_query[0]
|
match = pattern.match(updated_at_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["updated_at__date__lte"] = updated_at_query[0]
|
if len(updated_at_query) == 3:
|
||||||
|
digit, term = updated_at_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=updated_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="updated_at__date",
|
||||||
|
offset=updated_at_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in updated_at_query:
|
||||||
|
filter["updated_at__date__gte"] = updated_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["updated_at__date__lte"] = updated_at_query[0]
|
||||||
else:
|
else:
|
||||||
if params.get("updated_at", None) and len(params.get("updated_at")):
|
if params.get("updated_at", None) and len(params.get("updated_at")):
|
||||||
for query in params.get("updated_at"):
|
for query in params.get("updated_at"):
|
||||||
updated_at_query = query.split(";")
|
updated_at_query = query.split(";")
|
||||||
if len(updated_at_query) == 2 and "after" in updated_at_query:
|
if len(updated_at_query) == 2:
|
||||||
filter["updated_at__date__gte"] = updated_at_query[0]
|
match = pattern.match(updated_at_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["updated_at__date__lte"] = updated_at_query[0]
|
if len(updated_at_query) == 3:
|
||||||
|
digit, term = updated_at_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=updated_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="updated_at__date",
|
||||||
|
offset=updated_at_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in updated_at_query:
|
||||||
|
filter["updated_at__date__gte"] = updated_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["updated_at__date__lte"] = updated_at_query[0]
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
|
|
||||||
@ -137,18 +228,46 @@ def filter_start_date(params, filter, method):
|
|||||||
if len(start_dates) and "" not in start_dates:
|
if len(start_dates) and "" not in start_dates:
|
||||||
for query in start_dates:
|
for query in start_dates:
|
||||||
start_date_query = query.split(";")
|
start_date_query = query.split(";")
|
||||||
if len(start_date_query) == 2 and "after" in start_date_query:
|
if len(start_date_query) >= 2:
|
||||||
filter["start_date__gte"] = start_date_query[0]
|
match = pattern.match(start_date_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["start_date__lte"] = start_date_query[0]
|
if len(start_date_query) == 3:
|
||||||
|
digit, term = start_date_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=float(digit),
|
||||||
|
subsequent=start_date_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="start_date",
|
||||||
|
offset=start_date_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in start_date_query:
|
||||||
|
filter["start_date__gte"] = start_date_query[0]
|
||||||
|
else:
|
||||||
|
filter["start_date__lte"] = start_date_query[0]
|
||||||
else:
|
else:
|
||||||
if params.get("start_date", None) and len(params.get("start_date")):
|
if params.get("start_date", None) and len(params.get("start_date")):
|
||||||
for query in params.get("start_date"):
|
for query in params.get("start_date"):
|
||||||
start_date_query = query.split(";")
|
start_date_query = query.split(";")
|
||||||
if len(start_date_query) == 2 and "after" in start_date_query:
|
if len(start_date_query) == 2:
|
||||||
filter["start_date__gte"] = start_date_query[0]
|
match = pattern.match(start_date_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["start_date__lte"] = start_date_query[0]
|
if len(start_date_query) == 3:
|
||||||
|
digit, term = start_date_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=float(digit),
|
||||||
|
subsequent=start_date_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="start_date",
|
||||||
|
offset=start_date_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in start_date_query:
|
||||||
|
filter["start_date__gte"] = start_date_query[0]
|
||||||
|
else:
|
||||||
|
filter["start_date__lte"] = start_date_query[0]
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
|
|
||||||
@ -158,18 +277,46 @@ def filter_target_date(params, filter, method):
|
|||||||
if len(target_dates) and "" not in target_dates:
|
if len(target_dates) and "" not in target_dates:
|
||||||
for query in target_dates:
|
for query in target_dates:
|
||||||
target_date_query = query.split(";")
|
target_date_query = query.split(";")
|
||||||
if len(target_date_query) == 2 and "after" in target_date_query:
|
if len(target_date_query) == 2:
|
||||||
filter["target_date__gt"] = target_date_query[0]
|
match = pattern.match(target_date_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["target_date__lt"] = target_date_query[0]
|
if len(target_date_query) == 3:
|
||||||
|
digit, term = target_date_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=target_date_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="target_date",
|
||||||
|
offset=target_date_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in target_date_query:
|
||||||
|
filter["target_date__gt"] = target_date_query[0]
|
||||||
|
else:
|
||||||
|
filter["target_date__lt"] = target_date_query[0]
|
||||||
else:
|
else:
|
||||||
if params.get("target_date", None) and len(params.get("target_date")):
|
if params.get("target_date", None) and len(params.get("target_date")):
|
||||||
for query in params.get("target_date"):
|
for query in params.get("target_date"):
|
||||||
target_date_query = query.split(";")
|
target_date_query = query.split(";")
|
||||||
if len(target_date_query) == 2 and "after" in target_date_query:
|
if len(target_date_query) == 2:
|
||||||
filter["target_date__gt"] = target_date_query[0]
|
match = pattern.match(target_date_query[0])
|
||||||
else:
|
if match:
|
||||||
filter["target_date__lt"] = target_date_query[0]
|
if len(target_date_query) == 3:
|
||||||
|
digit, term = target_date_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=target_date_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="target_date",
|
||||||
|
offset=target_date_query[2],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if "after" in target_date_query:
|
||||||
|
filter["target_date__gt"] = target_date_query[0]
|
||||||
|
else:
|
||||||
|
filter["target_date__lt"] = target_date_query[0]
|
||||||
|
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
@ -180,18 +327,45 @@ def filter_completed_at(params, filter, method):
|
|||||||
if len(completed_ats) and "" not in completed_ats:
|
if len(completed_ats) and "" not in completed_ats:
|
||||||
for query in completed_ats:
|
for query in completed_ats:
|
||||||
completed_at_query = query.split(";")
|
completed_at_query = query.split(";")
|
||||||
if len(completed_at_query) == 2 and "after" in completed_at_query:
|
if len(completed_at_query) == 2:
|
||||||
filter["completed_at__date__gte"] = completed_at_query[0]
|
match = pattern.match(completed_at_query[0])
|
||||||
|
if match:
|
||||||
|
if len(completed_at_query) == 3:
|
||||||
|
digit, term = completed_at_query[0].split("_")
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=completed_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="completed_at__date",
|
||||||
|
offset=completed_at_query[2],
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
filter["completed_at__lte"] = completed_at_query[0]
|
if "after" in completed_at_query:
|
||||||
|
filter["completed_at__date__gte"] = completed_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["completed_at__date__lte"] = completed_at_query[0]
|
||||||
else:
|
else:
|
||||||
if params.get("completed_at", None) and len(params.get("completed_at")):
|
if params.get("completed_at", None) and len(params.get("completed_at")):
|
||||||
for query in params.get("completed_at"):
|
for query in params.get("completed_at"):
|
||||||
completed_at_query = query.split(";")
|
completed_at_query = query.split(";")
|
||||||
if len(completed_at_query) == 2 and "after" in completed_at_query:
|
if len(completed_at_query) == 2:
|
||||||
filter["completed_at__date__gte"] = completed_at_query[0]
|
match = pattern.match(completed_at_query[0])
|
||||||
|
if match:
|
||||||
|
digit, term = match.group(1), match.group(2)
|
||||||
|
date_filter(
|
||||||
|
filter=filter,
|
||||||
|
duration=digit,
|
||||||
|
subsequent=completed_at_query[1],
|
||||||
|
term=term,
|
||||||
|
date_filter="completed_at__date",
|
||||||
|
offset=completed_at_query[2],
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
filter["completed_at__lte"] = completed_at_query[0]
|
if "after" in completed_at_query:
|
||||||
|
filter["completed_at__date__gte"] = completed_at_query[0]
|
||||||
|
else:
|
||||||
|
filter["completed_at__date__lte"] = completed_at_query[0]
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user