Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion airtabledb/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ def maybe_guess_field(field_name: str, values: List[Any]) -> FieldInfo:
# See:
# https://support.airtable.com/hc/en-us/articles/360051564873-Record-ID
# https://support.airtable.com/hc/en-us/articles/203255215-Formula-field-reference#record_functions
self.columns = dict(columns, id=String(), createdTime=ISODateTime())
self.columns = dict(
columns, id=String(filters=[Equal], exact=True), createdTime=ISODateTime()
)

@staticmethod
def supports(uri: str, fast: bool = True, **kwargs: Any) -> Optional[bool]:
Expand Down
59 changes: 42 additions & 17 deletions airtabledb/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
BLANK = "BLANK()"
TRUE = "TRUE()"
FALSE = "FALSE()"
RECORD_ID = "RECORD_ID()"

ID_FIELD = "id"


def AND_BETTER(*args):
Expand Down Expand Up @@ -45,29 +48,51 @@ def STR_CAST(left: Any) -> str:


def get_formula(field_name: str, filter: Filter) -> str:
if isinstance(filter, IsNull):
if field_name == ID_FIELD:
if not isinstance(filter, Equal):
raise NotImplementedError(field_name, filter)

return base_formulas.EQUAL(
RECORD_ID,
base_formulas.to_airtable_value(filter.value),
)
elif isinstance(filter, IsNull):
# https://community.airtable.com/t/blank-zero-problem/5662/13
return base_formulas.IF(STR_CAST(base_formulas.FIELD(field_name)), FALSE, TRUE)
elif isinstance(filter, IsNotNull):
# https://community.airtable.com/t/blank-zero-problem/5662/13
return base_formulas.IF(STR_CAST(base_formulas.FIELD(field_name)), TRUE, FALSE)
elif isinstance(filter, Range):
parts = []
if filter.start is not None:
start_airtable_value = base_formulas.to_airtable_value(filter.start)
if filter.include_start:
parts.append(GE(base_formulas.FIELD(field_name), start_airtable_value))
else:
parts.append(GT(base_formulas.FIELD(field_name), start_airtable_value))

if filter.end is not None:
end_airtable_value = base_formulas.to_airtable_value(filter.end)
if filter.include_end:
parts.append(LE(base_formulas.FIELD(field_name), end_airtable_value))
else:
parts.append(LT(base_formulas.FIELD(field_name), end_airtable_value))

return AND_BETTER(*parts)
if filter.start == filter.end and filter.include_start and filter.include_end:
return base_formulas.EQUAL(
base_formulas.FIELD(field_name),
base_formulas.to_airtable_value(filter.start),
)
else:
parts = []
if filter.start is not None:
start_airtable_value = base_formulas.to_airtable_value(filter.start)
if filter.include_start:
parts.append(
GE(base_formulas.FIELD(field_name), start_airtable_value)
)
else:
parts.append(
GT(base_formulas.FIELD(field_name), start_airtable_value)
)

if filter.end is not None:
end_airtable_value = base_formulas.to_airtable_value(filter.end)
if filter.include_end:
parts.append(
LE(base_formulas.FIELD(field_name), end_airtable_value)
)
else:
parts.append(
LT(base_formulas.FIELD(field_name), end_airtable_value)
)

return AND_BETTER(*parts)
elif isinstance(filter, Equal):
return base_formulas.EQUAL(
base_formulas.FIELD(field_name),
Expand Down
11 changes: 11 additions & 0 deletions tests/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_get_formula_range():
== "AND({the field} > 0,{the field} <= 33)"
)

# Remap range to eq if start == end
assert (
get_formula(
"the field",
filters.Range(start=13, end=13, include_start=True, include_end=True),
)
== "{the field}=13"
)


def test_get_airtable_formula():
assert (
Expand All @@ -61,6 +70,8 @@ def test_get_formula_eq():

assert get_formula("the field", filters.Equal("foo bar")) == "{the field}='foo bar'"

assert get_formula("id", filters.Equal("reacasdf")) == "RECORD_ID()='reacasdf'"


def test_get_formula_neq():
assert get_formula("the field", filters.NotEqual(33)) == "{the field}!=33"