Skip to content

Commit 6abd622

Browse files
committed
add update filter
1 parent a969593 commit 6abd622

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

planet/cli/data.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ def convert(self, value, param, ctx):
142142
return value
143143

144144

145+
class GTComparisonType(ComparisonType):
146+
"""Only support gt or gte comparison"""
147+
valid = ['gt', 'gte']
148+
help = 'COMP can be gt, or gte.'
149+
150+
145151
class DateTimeType(click.ParamType):
146152
name = 'datetime'
147153
help = 'DATETIME can be an RFC 3339 or ISO 8601 string.'
@@ -183,6 +189,22 @@ def convert(self, value, param, ctx):
183189
return data_filter.range_filter(**kwargs)
184190

185191

192+
class UpdateFilter(click.Tuple):
193+
help = ('Filter to items with changes to a specified field value made ' +
194+
'after a specified date.' +
195+
f'{FieldType.help} {GTComparisonType.help} {DateTimeType.help}')
196+
197+
def __init__(self) -> None:
198+
super().__init__([FieldType(), GTComparisonType(), DateTimeType()])
199+
200+
def convert(self, value, param, ctx):
201+
vals = super().convert(value, param, ctx)
202+
203+
field, comp, value = vals
204+
kwargs = {'field_name': field, comp: value}
205+
return data_filter.update_filter(**kwargs)
206+
207+
186208
@data.command()
187209
@click.pass_context
188210
@translate_exceptions
@@ -201,16 +223,28 @@ def convert(self, value, param, ctx):
201223
default=None,
202224
callback=geom_to_filter,
203225
help='Filter to items that overlap a given geometry.')
226+
# @click.option('--number-in',
227+
# type=RangeFilter(),
228+
# multiple=True,
229+
# help=RangeFilter.help)
204230
@click.option('--range',
205231
'nrange',
206232
type=RangeFilter(),
207233
multiple=True,
208234
help=RangeFilter.help)
235+
# @click.option('--string-in',
236+
# type=RangeFilter(),
237+
# multiple=True,
238+
# help=RangeFilter.help)
239+
@click.option('--update',
240+
type=UpdateFilter(),
241+
multiple=True,
242+
help=UpdateFilter.help)
209243
@click.option('--permission',
210244
type=bool,
211245
default=True,
212246
help='Filter to assets with download permissions.')
213-
def filter(ctx, asset, date_range, geom, nrange, permission, pretty):
247+
def filter(ctx, asset, date_range, geom, nrange, update, permission, pretty):
214248
"""Create a structured item search filter.
215249
216250
This command provides basic functionality for specifying a filter by
@@ -223,7 +257,7 @@ def filter(ctx, asset, date_range, geom, nrange, permission, pretty):
223257

224258
# options allowing multiples are broken up so one filter is created for
225259
# each time the option is specified
226-
filter_args = (asset, *date_range, geom, *nrange, permission)
260+
filter_args = (asset, *date_range, geom, *nrange, *update, permission)
227261

228262
filters = [f for f in filter_args if f]
229263

tests/integration/test_data_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ def test_data_filter_range(invoke, assert_and_filters_equal):
205205
assert_and_filters_equal(json.loads(result.output), expected_filt)
206206

207207

208+
@respx.mock
209+
@pytest.mark.asyncio
210+
def test_data_filter_update(invoke, assert_and_filters_equal):
211+
"""Check filter is created correctly and that multiple options results in
212+
multiple filters"""
213+
runner = CliRunner()
214+
215+
result = invoke(["filter"] + '--update field gt 2021-01-01'.split() +
216+
'--update field2 gte 2022-01-01'.split(),
217+
runner=runner)
218+
assert result.exit_code == 0
219+
220+
update_filter1 = {
221+
"type": "UpdateFilter",
222+
"field_name": "field",
223+
"config": {
224+
"gt": "2021-01-01T00:00:00Z"
225+
}
226+
}
227+
update_filter2 = {
228+
"type": "UpdateFilter",
229+
"field_name": "field2",
230+
"config": {
231+
"gte": "2022-01-01T00:00:00Z"
232+
}
233+
}
234+
235+
expected_filt = {
236+
"type": "AndFilter",
237+
"config": [permission_filter, update_filter1, update_filter2]
238+
}
239+
240+
assert_and_filters_equal(json.loads(result.output), expected_filt)
241+
242+
208243
@respx.mock
209244
@pytest.mark.asyncio
210245
@pytest.mark.parametrize("filter", ['{1:1}', '{"foo"}'])

0 commit comments

Comments
 (0)