dev: refactor date filters to a single function

This commit is contained in:
pablohashescobar 2023-09-27 14:33:41 +05:30
parent 608ba9d5cb
commit 42d3919459
2 changed files with 37 additions and 205 deletions

View File

@ -24,8 +24,6 @@ from django.core.serializers.json import DjangoJSONEncoder
from django.utils.decorators import method_decorator
from django.views.decorators.gzip import gzip_page
from django.db import IntegrityError
from django.conf import settings
from django.db import IntegrityError
# Third Party imports
from rest_framework.response import Response
@ -58,7 +56,6 @@ from plane.api.serializers import (
IssuePublicSerializer,
)
from plane.api.permissions import (
WorkspaceEntityPermission,
ProjectEntityPermission,
WorkSpaceAdminPermission,
ProjectMemberPermission,
@ -86,7 +83,6 @@ from plane.db.models import (
from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results
from plane.utils.issue_filters import issue_filters
from plane.bgtasks.export_task import issue_export_task
class IssueViewSet(BaseViewSet):

View File

@ -7,7 +7,7 @@ pattern = re.compile(r"\d+_(weeks|months)$")
# Get the 2_weeks, 3_months
def date_filter(filter, duration, subsequent, term, date_filter, offset):
def string_date_filter(filter, duration, subsequent, term, date_filter, offset):
now = timezone.now().date()
if term == "months":
if subsequent == "after":
@ -33,6 +33,32 @@ def date_filter(filter, duration, subsequent, term, date_filter, offset):
filter[f"{date_filter}__lte"] = now - timedelta(days=duration)
def date_filter(filter, date_term, queries):
"""
Handle all date filters
"""
for query in queries:
date_query = query.split(";")
if len(date_query) >= 2:
match = pattern.match(date_query[0])
if match:
if len(date_query) == 3:
digit, term = date_query[0].split("_")
string_date_filter(
filter=filter,
duration=int(digit),
subsequent=date_query[1],
term=term,
date_filter="created_at__date",
offset=date_query[2],
)
else:
if "after" in date_query:
filter[f"{date_term}__gte"] = date_query[0]
else:
filter[f"{date_term}__lte"] = date_query[0]
def filter_state(params, filter, method):
if method == "GET":
states = params.get("state").split(",")
@ -128,48 +154,10 @@ def filter_created_at(params, filter, method):
if method == "GET":
created_ats = params.get("created_at").split(",")
if len(created_ats) and "" not in created_ats:
for query in created_ats:
created_at_query = query.split(";")
if len(created_at_query) >= 2:
match = pattern.match(created_at_query[0])
if match:
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]
date_filter(filter=filter, date_term="created_at__date", queries=created_ats)
else:
if params.get("created_at", None) and len(params.get("created_at")):
for query in params.get("created_at"):
created_at_query = query.split(";")
if len(created_at_query) == 2:
match = pattern.match(created_at_query[0])
if match:
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]
date_filter(filter=filter, date_term="created_at__date", queries=params.get("created_at", []))
return filter
@ -177,48 +165,10 @@ def filter_updated_at(params, filter, method):
if method == "GET":
updated_ats = params.get("updated_at").split(",")
if len(updated_ats) and "" not in updated_ats:
for query in updated_ats:
updated_at_query = query.split(";")
if len(updated_at_query) == 2:
match = pattern.match(updated_at_query[0])
if match:
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]
date_filter(filter=filter, date_term="created_at__date", queries=updated_ats)
else:
if params.get("updated_at", None) and len(params.get("updated_at")):
for query in params.get("updated_at"):
updated_at_query = query.split(";")
if len(updated_at_query) == 2:
match = pattern.match(updated_at_query[0])
if match:
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]
date_filter(filter=filter, date_term="created_at__date", queries=params.get("updated_at", []))
return filter
@ -226,48 +176,10 @@ def filter_start_date(params, filter, method):
if method == "GET":
start_dates = params.get("start_date").split(",")
if len(start_dates) and "" not in start_dates:
for query in start_dates:
start_date_query = query.split(";")
if len(start_date_query) >= 2:
match = pattern.match(start_date_query[0])
if match:
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]
date_filter(filter=filter, date_term="start_date", queries=start_dates)
else:
if params.get("start_date", None) and len(params.get("start_date")):
for query in params.get("start_date"):
start_date_query = query.split(";")
if len(start_date_query) == 2:
match = pattern.match(start_date_query[0])
if match:
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]
date_filter(filter=filter, date_term="start_date", queries=params.get("start_date", None))
return filter
@ -275,49 +187,10 @@ def filter_target_date(params, filter, method):
if method == "GET":
target_dates = params.get("target_date").split(",")
if len(target_dates) and "" not in target_dates:
for query in target_dates:
target_date_query = query.split(";")
if len(target_date_query) == 2:
match = pattern.match(target_date_query[0])
if match:
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]
date_filter(filter=filter, date_term="target_date", queries=target_dates)
else:
if params.get("target_date", None) and len(params.get("target_date")):
for query in params.get("target_date"):
target_date_query = query.split(";")
if len(target_date_query) == 2:
match = pattern.match(target_date_query[0])
if match:
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]
date_filter(filter=filter, date_term="target_date", queries=params.get("target_date", []))
return filter
@ -325,47 +198,10 @@ def filter_completed_at(params, filter, method):
if method == "GET":
completed_ats = params.get("completed_at").split(",")
if len(completed_ats) and "" not in completed_ats:
for query in completed_ats:
completed_at_query = query.split(";")
if len(completed_at_query) == 2:
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:
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]
date_filter(filter=filter, date_term="completed_at__date", queries=completed_ats)
else:
if params.get("completed_at", None) and len(params.get("completed_at")):
for query in params.get("completed_at"):
completed_at_query = query.split(";")
if len(completed_at_query) == 2:
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:
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]
date_filter(filter=filter, date_term="completed_at__date", queries=params.get("completed_at", []))
return filter