-
-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
case search "date_add" function #31388
Changes from 9 commits
86a5a1c
07167ad
f0b26f8
b70885b
730155d
08adcfd
4729425
02a81f0
9fb8d04
72f84b4
e32c951
3c448f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import datetime | ||
|
||
import pytz | ||
from django.utils.dateparse import parse_date | ||
from django.utils.translation import gettext as _ | ||
|
||
import pytz | ||
from dateutil.relativedelta import relativedelta | ||
from eulxml.xpath.ast import serialize | ||
|
||
from dimagi.utils.parsing import ISO_DATE_FORMAT | ||
|
||
from corehq.apps.case_search.exceptions import XPathFunctionException | ||
from corehq.apps.domain.models import Domain | ||
from dimagi.utils.parsing import ISO_DATE_FORMAT | ||
|
||
|
||
def date(node, context): | ||
|
@@ -24,27 +26,30 @@ def date(node, context): | |
|
||
arg = unwrap_value(arg, context) | ||
|
||
if isinstance(arg, int): | ||
return (datetime.date(1970, 1, 1) + datetime.timedelta(days=arg)).strftime("%Y-%m-%d") | ||
parsed_date = _value_to_date(node, arg) | ||
return parsed_date.strftime(ISO_DATE_FORMAT) | ||
|
||
if isinstance(arg, str): | ||
|
||
def _value_to_date(node, value): | ||
if isinstance(value, int): | ||
parsed_date = datetime.date(1970, 1, 1) + datetime.timedelta(days=value) | ||
elif isinstance(value, str): | ||
try: | ||
parsed_date = parse_date(arg) | ||
parsed_date = parse_date(value) | ||
except ValueError: | ||
raise XPathFunctionException(_("{} is not a valid date").format(arg), serialize(node)) | ||
|
||
if parsed_date is None: | ||
raise XPathFunctionException( | ||
_("The \"date\" function only accepts strings of the format \"YYYY-mm-dd\""), | ||
serialize(node) | ||
) | ||
raise XPathFunctionException(_("{} is not a valid date").format(value), serialize(node)) | ||
elif isinstance(value, datetime.date): | ||
parsed_date = value | ||
else: | ||
parsed_date = None | ||
|
||
return arg | ||
if parsed_date is None: | ||
raise XPathFunctionException( | ||
_("Invalid date value. Dates must be an integer or a string of the format \"YYYY-mm-dd\""), | ||
serialize(node) | ||
) | ||
|
||
raise XPathFunctionException( | ||
"The \"date\" function only accepts integers or strings of the format \"YYYY-mm-dd\"", | ||
serialize(node) | ||
) | ||
return parsed_date | ||
|
||
|
||
def today(node, context): | ||
|
@@ -58,3 +63,50 @@ def today(node, context): | |
domain_obj = Domain.get_by_name(context.domain) | ||
timezone = domain_obj.get_default_timezone() if domain_obj else pytz.UTC | ||
return datetime.datetime.now(timezone).strftime(ISO_DATE_FORMAT) | ||
|
||
|
||
def date_add(node, context): | ||
from corehq.apps.case_search.dsl_utils import unwrap_value | ||
|
||
assert node.name == 'date_add' | ||
if len(node.args) != 3: | ||
raise XPathFunctionException( | ||
_("The \"date_add\" function expects three arguments"), | ||
serialize(node) | ||
) | ||
|
||
date_arg = unwrap_value(node.args[0], context) | ||
date_value = _value_to_date(node, date_arg) | ||
|
||
interval_type = unwrap_value(node.args[1], context) | ||
interval_types = ("days", "weeks", "months", "years") | ||
if interval_type not in interval_types: | ||
raise XPathFunctionException( | ||
_("The \"date_add\" function expects the interval argument to be one of {types}").format( | ||
types=interval_types | ||
), | ||
serialize(node) | ||
) | ||
|
||
quantity = unwrap_value(node.args[2], context) | ||
if isinstance(quantity, str): | ||
try: | ||
quantity = float(quantity) | ||
except (ValueError, TypeError): | ||
raise XPathFunctionException( | ||
_("The \"date_add\" function expects the interval quantity to be a numeric value"), | ||
serialize(node) | ||
) | ||
|
||
if not isinstance(quantity, (int, float)): | ||
raise XPathFunctionException( | ||
_("The \"date_add\" function expects the interval quantity to be a numeric value"), | ||
serialize(node) | ||
) | ||
|
||
try: | ||
result = date_value + relativedelta(**{interval_type: quantity}) | ||
except ValueError as e: | ||
raise XPathFunctionException(str(e), serialize(node)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think this will show a python style error to the user which might be hard to understand. Is there another way we could show this error to the user? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've handled the value error but kept the try block for unexpected exceptions: 3c448f5 |
||
|
||
return result.strftime(ISO_DATE_FORMAT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe add which arguments it needs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.