Skip to content

Commit cabf111

Browse files
jrmipicklepete
andauthored
Add element visibility condition (baserow#4188)
Co-authored-by: peter_baserow <peter@baserow.io>
1 parent 2a6d92e commit cabf111

File tree

34 files changed

+883
-420
lines changed

34 files changed

+883
-420
lines changed

backend/src/baserow/contrib/builder/api/domains/serializers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from baserow.contrib.builder.pages.handler import PageHandler
2727
from baserow.contrib.builder.pages.models import Page
2828
from baserow.core.app_auth_providers.registries import app_auth_provider_type_registry
29+
from baserow.core.formula.serializers import FormulaSerializerField
2930
from baserow.core.services.registries import service_type_registry
3031
from baserow.core.user_sources.models import UserSource
3132
from baserow.core.user_sources.registries import user_source_type_registry
@@ -100,6 +101,10 @@ class PublicElementSerializer(serializers.ModelSerializer):
100101
def get_type(self, instance):
101102
return element_type_registry.get_by_model(instance.specific_class).type
102103

104+
visibility_condition = FormulaSerializerField(
105+
help_text=Element._meta.get_field("visibility_condition").help_text,
106+
)
107+
103108
style_background_file = UserFileField(
104109
allow_null=True,
105110
help_text="The background image file",
@@ -118,6 +123,7 @@ class Meta:
118123
"place_in_container",
119124
"css_classes",
120125
"visibility",
126+
"visibility_condition",
121127
"styles",
122128
"style_border_top_color",
123129
"style_border_top_size",

backend/src/baserow/contrib/builder/api/elements/serializers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class ElementSerializer(serializers.ModelSerializer):
5050
def get_type(self, instance):
5151
return element_type_registry.get_by_model(instance.specific_class).type
5252

53+
visibility_condition = FormulaSerializerField(
54+
help_text=Element._meta.get_field("visibility_condition").help_text,
55+
)
56+
5357
style_background_file = UserFileField(
5458
allow_null=True,
5559
help_text="The background image file",
@@ -67,6 +71,7 @@ class Meta:
6771
"place_in_container",
6872
"css_classes",
6973
"visibility",
74+
"visibility_condition",
7075
"styles",
7176
"style_border_top_color",
7277
"style_border_top_size",
@@ -132,6 +137,10 @@ class CreateElementSerializer(serializers.ModelSerializer):
132137
validators=[image_file_validation],
133138
)
134139

140+
visibility_condition = FormulaSerializerField(
141+
help_text=Element._meta.get_field("visibility_condition").help_text,
142+
)
143+
135144
class Meta:
136145
model = Element
137146
fields = (
@@ -142,6 +151,7 @@ class Meta:
142151
"place_in_container",
143152
"css_classes",
144153
"visibility",
154+
"visibility_condition",
145155
"styles",
146156
"style_border_top_color",
147157
"style_border_top_size",
@@ -181,11 +191,16 @@ class UpdateElementSerializer(serializers.ModelSerializer):
181191
validators=[image_file_validation],
182192
)
183193

194+
visibility_condition = FormulaSerializerField(
195+
help_text=Element._meta.get_field("visibility_condition").help_text,
196+
)
197+
184198
class Meta:
185199
model = Element
186200
fields = (
187201
"css_classes",
188202
"visibility",
203+
"visibility_condition",
189204
"styles",
190205
"style_border_top_color",
191206
"style_border_top_size",

backend/src/baserow/contrib/builder/elements/handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ElementHandler:
4949
"parent_element_id",
5050
"place_in_container",
5151
"visibility",
52+
"visibility_condition",
5253
"css_classes",
5354
"styles",
5455
"style_border_top_color",
@@ -82,6 +83,7 @@ class ElementHandler:
8283
"place_in_container",
8384
"css_classes",
8485
"visibility",
86+
"visibility_condition",
8587
"styles",
8688
"style_border_top_color",
8789
"style_border_top_size",

backend/src/baserow/contrib/builder/elements/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ class ROLE_TYPES(models.TextChoices):
160160
db_index=True,
161161
)
162162

163+
visibility_condition = FormulaField(
164+
help_text="Change element visibility depending on a formula value"
165+
)
166+
163167
styles = models.JSONField(
164168
default=dict,
165169
help_text="The theme overrides for this element",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 5.0.14 on 2025-11-09 10:09
2+
3+
from django.db import migrations
4+
5+
import baserow.core.formula.field
6+
7+
8+
class Migration(migrations.Migration):
9+
dependencies = [
10+
("builder", "0065_aiagentworkflowaction"),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name="element",
16+
name="visibility_condition",
17+
field=baserow.core.formula.field.FormulaField(
18+
blank=True,
19+
default="",
20+
help_text="Change element visibility depending on a formula value",
21+
null=True,
22+
),
23+
),
24+
]

backend/src/baserow/contrib/builder/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import List, Optional, TypedDict
22

33
from baserow.contrib.builder.pages.types import PagePathParams, PageQueryParams
4+
from baserow.core.formula import BaserowFormulaObject
45
from baserow.core.integrations.types import IntegrationDictSubClass
56
from baserow.core.services.types import ServiceDictSubClass
67
from baserow.core.user_sources.types import UserSourceDictSubClass
@@ -15,6 +16,7 @@ class ElementDict(TypedDict):
1516
place_in_container: str
1617
css_classes: str
1718
visibility: str
19+
visibility_condition: BaserowFormulaObject
1820
role_type: str
1921
roles: list
2022
styles: dict

backend/src/baserow/core/formula/field.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def _transform_db_value_to_dict(
5757

5858
# If the column type is "text", then we haven't yet migrated the schema.
5959
if self.db_type(connection) == "text":
60+
if value is None:
61+
return BaserowFormulaObject.create("")
62+
6063
if isinstance(value, int):
6164
# A small hack for our backend tests: if we
6265
# receive an integer, we convert it to a string.

backend/tests/baserow/contrib/builder/api/domains/test_domain_public_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ def test_get_elements_of_public_builder(api_client, data_fixture):
408408
"place_in_container": None,
409409
"css_classes": "",
410410
"visibility": "all",
411+
"visibility_condition": {"formula": "", "mode": "simple", "version": "0.1"},
411412
"styles": {},
412413
"style_border_top_color": "border",
413414
"style_border_top_size": 0,

backend/tests/baserow/contrib/builder/test_builder_application_type.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from baserow.core.action.registries import action_type_registry
3636
from baserow.core.actions import CreateApplicationActionType
3737
from baserow.core.db import specific_iterator
38+
from baserow.core.formula import BaserowFormulaObject
39+
from baserow.core.formula.field import BASEROW_FORMULA_VERSION_INITIAL
40+
from baserow.core.formula.types import BASEROW_FORMULA_MODE_SIMPLE
3841
from baserow.core.registries import ImportExportConfig, application_type_registry
3942
from baserow.core.storage import ExportZipFile
4043
from baserow.core.trash.handler import TrashHandler
@@ -248,6 +251,11 @@ def test_builder_application_export(data_fixture):
248251
"parent_element_id": None,
249252
"place_in_container": None,
250253
"visibility": "all",
254+
"visibility_condition": BaserowFormulaObject(
255+
formula="",
256+
mode=BASEROW_FORMULA_MODE_SIMPLE,
257+
version=BASEROW_FORMULA_VERSION_INITIAL,
258+
),
251259
"css_classes": "",
252260
"styles": {},
253261
"style_border_top_color": "border",
@@ -296,6 +304,11 @@ def test_builder_application_export(data_fixture):
296304
"place_in_container": None,
297305
"css_classes": "",
298306
"visibility": "all",
307+
"visibility_condition": BaserowFormulaObject(
308+
formula="",
309+
mode=BASEROW_FORMULA_MODE_SIMPLE,
310+
version=BASEROW_FORMULA_VERSION_INITIAL,
311+
),
299312
"styles": {},
300313
"style_border_top_color": "border",
301314
"style_border_top_size": 0,
@@ -432,6 +445,11 @@ def test_builder_application_export(data_fixture):
432445
"place_in_container": None,
433446
"css_classes": "",
434447
"visibility": "all",
448+
"visibility_condition": BaserowFormulaObject(
449+
formula="",
450+
mode=BASEROW_FORMULA_MODE_SIMPLE,
451+
version=BASEROW_FORMULA_VERSION_INITIAL,
452+
),
435453
"styles": {},
436454
"style_border_top_color": "border",
437455
"style_border_top_size": 0,
@@ -470,6 +488,11 @@ def test_builder_application_export(data_fixture):
470488
"place_in_container": None,
471489
"css_classes": "",
472490
"visibility": "all",
491+
"visibility_condition": BaserowFormulaObject(
492+
formula="",
493+
mode=BASEROW_FORMULA_MODE_SIMPLE,
494+
version=BASEROW_FORMULA_VERSION_INITIAL,
495+
),
473496
"styles": {},
474497
"style_border_top_color": "border",
475498
"style_border_top_size": 0,
@@ -507,6 +530,11 @@ def test_builder_application_export(data_fixture):
507530
"place_in_container": None,
508531
"css_classes": "",
509532
"visibility": "all",
533+
"visibility_condition": BaserowFormulaObject(
534+
formula="",
535+
mode=BASEROW_FORMULA_MODE_SIMPLE,
536+
version=BASEROW_FORMULA_VERSION_INITIAL,
537+
),
510538
"styles": {},
511539
"style_border_top_color": "border",
512540
"style_border_top_size": 0,
@@ -546,6 +574,11 @@ def test_builder_application_export(data_fixture):
546574
"place_in_container": "0",
547575
"css_classes": "",
548576
"visibility": "all",
577+
"visibility_condition": BaserowFormulaObject(
578+
formula="",
579+
mode=BASEROW_FORMULA_MODE_SIMPLE,
580+
version=BASEROW_FORMULA_VERSION_INITIAL,
581+
),
549582
"styles": {},
550583
"style_border_top_color": "border",
551584
"style_border_top_size": 0,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "feature",
3+
"message": "Element can be visually hidden on complex conditions",
4+
"domain": "builder",
5+
"issue_number": 2566,
6+
"bullet_points": [],
7+
"created_at": "2025-11-09"
8+
}

0 commit comments

Comments
 (0)