From 39630a1a4ae2073b89ea9618f4759f5a172fcc19 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 20 Dec 2023 13:47:27 +1100 Subject: [PATCH 1/5] Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions --- data_interrogator/db.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/data_interrogator/db.py b/data_interrogator/db.py index 06533fe..774ce6d 100755 --- a/data_interrogator/db.py +++ b/data_interrogator/db.py @@ -33,8 +33,14 @@ def as_postgresql(self, compiler, connection): return super().as_sql(compiler, connection) -def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField()): - expression = Coalesce( +class ComplexLookup(Func): + """Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions""" + function = '' + template = "%(expressions)s" + arity = 1 + + def __init__(self, lookup_field, condition, lookup_value, output_field=TextField(), **extra): + self.__expression = expression = Coalesce( GroupConcat( Cast( Case( @@ -46,8 +52,8 @@ def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField( ) ), Value("") - ) - return expression + ) + super().__init__(expression, **extra) class SumIf(Sum): @@ -64,7 +70,7 @@ def __init__(self, field, condition=None, output_field=DecimalField(), **lookups case = Case( When( condition, then=field - ), + ), default=0, output_field=output_field ) From 48d0e35bb2cd6fe14fcc4fe497166c48bf89ffeb Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 20 Dec 2023 13:48:01 +1100 Subject: [PATCH 2/5] Update version for pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b17e354..f3c43cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-data-interrogator" -version = "0.3.2" +version = "0.3.3" description = "A suite of interactive table builder utilities that create reports using efficient SQL queries" authors = ["Aristotle Metadata Enterprises"] license = "BSD-3-Clause" From ecbe190f8d7b05889ebb43501b271c3085256a11 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 20 Dec 2023 13:47:27 +1100 Subject: [PATCH 3/5] Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions --- data_interrogator/db.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/data_interrogator/db.py b/data_interrogator/db.py index 06533fe..774ce6d 100755 --- a/data_interrogator/db.py +++ b/data_interrogator/db.py @@ -33,8 +33,14 @@ def as_postgresql(self, compiler, connection): return super().as_sql(compiler, connection) -def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField()): - expression = Coalesce( +class ComplexLookup(Func): + """Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions""" + function = '' + template = "%(expressions)s" + arity = 1 + + def __init__(self, lookup_field, condition, lookup_value, output_field=TextField(), **extra): + self.__expression = expression = Coalesce( GroupConcat( Cast( Case( @@ -46,8 +52,8 @@ def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField( ) ), Value("") - ) - return expression + ) + super().__init__(expression, **extra) class SumIf(Sum): @@ -64,7 +70,7 @@ def __init__(self, field, condition=None, output_field=DecimalField(), **lookups case = Case( When( condition, then=field - ), + ), default=0, output_field=output_field ) From 8124fc7715609d9635de59e3714c28d7f09534d6 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 20 Dec 2023 13:48:01 +1100 Subject: [PATCH 4/5] Update version for pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b17e354..f3c43cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-data-interrogator" -version = "0.3.2" +version = "0.3.3" description = "A suite of interactive table builder utilities that create reports using efficient SQL queries" authors = ["Aristotle Metadata Enterprises"] license = "BSD-3-Clause" From 0329a26b9305d7e4910a58cddb12747e19b1eb50 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 20 Dec 2023 13:55:16 +1100 Subject: [PATCH 5/5] Simplify __init__ function --- data_interrogator/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_interrogator/db.py b/data_interrogator/db.py index 774ce6d..2605a0d 100755 --- a/data_interrogator/db.py +++ b/data_interrogator/db.py @@ -40,7 +40,7 @@ class ComplexLookup(Func): arity = 1 def __init__(self, lookup_field, condition, lookup_value, output_field=TextField(), **extra): - self.__expression = expression = Coalesce( + expression = Coalesce( GroupConcat( Cast( Case(