Created
August 30, 2019 03:05
-
-
Save rohanpm/88931ee34b3c0c4cefed0bd933766af2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From bfb8b1c967586346d1d67433b3631bc473eb04c3 Mon Sep 17 00:00:00 2001 | |
| From: Rohan McGovern <rmcgover@redhat.com> | |
| Date: Fri, 30 Aug 2019 11:03:26 +0800 | |
| Subject: [PATCH] Let callers pass datetime objects when searching on datetime | |
| fields | |
| --- | |
| pubtools/pulplib/_impl/client/search.py | 24 +++++++++++++++--------- | |
| 1 file changed, 15 insertions(+), 9 deletions(-) | |
| diff --git a/pubtools/pulplib/_impl/client/search.py b/pubtools/pulplib/_impl/client/search.py | |
| index 35fa508..cdd0690 100644 | |
| --- a/pubtools/pulplib/_impl/client/search.py | |
| +++ b/pubtools/pulplib/_impl/client/search.py | |
| @@ -1,3 +1,6 @@ | |
| +import datetime | |
| +import six | |
| + | |
| from pubtools.pulplib._impl.criteria import ( | |
| AndCriteria, | |
| OrCriteria, | |
| @@ -12,7 +15,6 @@ from pubtools.pulplib._impl.criteria import ( | |
| from pubtools.pulplib._impl import compat_attr as attr | |
| from pubtools.pulplib._impl.model.attr import PULP2_FIELD, PY_PULP2_CONVERTER | |
| -from pubtools.pulplib._impl.util import _is_iso_date_format | |
| def all_subclasses(klass): | |
| @@ -23,6 +25,15 @@ def all_subclasses(klass): | |
| return out | |
| +def to_mongo_json(value): | |
| + # Return a value converted to the format expected for a mongo JSON | |
| + # expression. Only a handful of special types need explicit conversions. | |
| + if isinstance(value, datetime.datetime): | |
| + return {"$date": value.strftime("%Y-%m-%dT%H:%M:%SZ")} | |
| + | |
| + return value | |
| + | |
| + | |
| def map_field_for_type(field_name, matcher, type_hint): | |
| if not type_hint: | |
| return (field_name, matcher) | |
| @@ -81,20 +92,15 @@ def field_match(to_match): | |
| return {"$regex": to_match._pattern} | |
| if isinstance(to_match, EqMatcher): | |
| - return {"$eq": to_match._value} | |
| + return {"$eq": to_mongo_json(to_match._value)} | |
| if isinstance(to_match, InMatcher): | |
| - return {"$in": to_match._values} | |
| + return {"$in": [to_mongo_json(x) for x in to_match._values]} | |
| if isinstance(to_match, ExistsMatcher): | |
| return {"$exists": True} | |
| if isinstance(to_match, LessThanMatcher): | |
| - value = to_match._value | |
| - # only value of the format YYYY-mm-ddTHH:MM:SSZ is | |
| - # treated as date | |
| - if _is_iso_date_format(value): | |
| - return {"$lt": {"$date": value}} | |
| - return {"$lt": value} | |
| + return {"$lt": to_mongo_json(to_match._value)} | |
| raise TypeError("Not a matcher: %s" % repr(to_match)) | |
| -- | |
| 2.21.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment