def date_filter_tokens_to_q_obj(tokens: list, field: str) -> Q:
"""Convert a date tokens parsed using `tokenize_date_filter_value` into a
Q object that can be used to filter a queryset.
Args:
- tokens: A list of tokens parsed using `tokenize_date_filter_value`.
- field: This should be the name of a DateTimeField.
"""
match tokens:
case [operator, year, month, day]:
if operator == ">=":
return Q(**{f"{field}__date__gte": dt.date(year, month, day)})
elif operator == "<=":
return Q(**{f"{field}__date__lte": dt.date(year, month, day)})
elif operator == ">":
return Q(**{f"{field}__date__gt": dt.date(year, month, day)})
elif operator == "<":
return Q(**{f"{field}__date__lt": dt.date(year, month, day)})
elif operator == "=":
return Q(**{f"{field}__date": dt.date(year, month, day)})
else:
return Q(**{f"{field}__date": dt.date(year, month, day)})
case [operator, year, month]:
if operator == ">=":
return Q(**{f"{field}__year__gte": year, f"{field}__month__gte": month})
elif operator == "<=":
return Q(**{f"{field}__year__lte": year, f"{field}__month__lte": month})
elif operator == ">":
return Q(**{f"{field}__year__gt": year, f"{field}__month__gt": month})
elif operator == "<":
return Q(**{f"{field}__year__lt": year, f"{field}__month__lt": month})
elif operator == "=":
return Q(**{f"{field}__year": year, f"{field}__month": month})
else:
return Q(**{f"{field}__year": year, f"{field}__month": month})
case [operator, year]:
if operator == ">=":
return Q(**{f"{field}__year__gte": year})
elif operator == "<=":
return Q(**{f"{field}__year__lte": year})
elif operator == ">":
return Q(**{f"{field}__year__gt": year})
elif operator == "<":
return Q(**{f"{field}__year__lt": year})
elif operator == "=":
return Q(**{f"{field}__year": year})
else:
return Q(**{f"{field}__year": year})
return Q(None)