Resolve "[Feature] Add URL formula keyword"
Whats in this MR
This MR was created by by @liob and picked up and dusted off by me a few months later. It contains:
4 new formula functions:
-
link(some url)
which renders a clickable link in the formulas cell. If the value doesn't pass a very basic and leniant regex to check it is a link then it will instead just be rendered as non clickable text. -
button(some url, some label
) this renders a link with a specific label. An empty label will default to rendering the entire link. -
get_link_label(a link formula)
which lets you extract the label part of a link formula. e.gget_link_label(button('url', 'label test')) = 'label test'
-
get_link_url(a link formula)
which lets you extract the url part of a link formula. e.gget_link_url(button('url', 'label test')) = 'url'
Other tag along changes\
JSONField + db_returning django bug
For future extensibility and simplicity sake under the hood the link formula type is a JSONField. This means we can easily add new link attributes in the future (perhaps extra styling options on a per link basis, perhaps javascript hooks etcetc) .
As a result I encountered a bug in Django.
- The model field used on a Django model for formulas is the BaserowExpressionField
- This field pretends to be other types of model fields depending on the type of the formula. For example for a link formula type it pretends to be a JSONField
- We set
db_returning=True
onBaserowExpressionField
which makes Django generateINSERT .... RETURNING field_1, field_2
statements so we don't have to run a secondSELECT
to see what the was formula calculated to be. - However for link formulas for some reason Django when running
row model.objects.create()
and thengetattr(row, a link formula field)
was returning a string instead of a dict (which a JSON field should return) - It turns out that the
db_returning
feature provided by django never runsfield.from_db_value
after theINSERT .. RETURNING ..
statement is run! This is a bug in Django IMO. - For now I have fixed this bug by detecting any
db_returning
fields which are also JSONFIelds (and hence need to have from_db_value called after) and forcing these to go through therefresh_from_db
step after the insert so the model row instance is correctly setup.
Fixed Row Edit Modal header bug
The Row Edit modal calls field_type.toHumanReadibleString(field, value)
when calculating the row edit modals header. For formulas this was incorrectly implemented for the LookupFieldType, ArrayFormulaType so you were shown strings literally like [Object object], [Object object]
in the header. Instead so it works nicely for the new link field type I fixed this for the link field type and the array field type.
Merge Request Checklist
-
changelog.md has been updated if required -
New/updated Premium features are separated correctly in the premium folder -
The latest Chrome and Firefox have been used to test any new frontend features -
Documentation has been updated -
Quality Standards are met -
Performance: tables are still fast with 100k+ rows, 100+ field tables -
The redoc API pages have been updated for any REST API changes -
Our custom API docs are updated for changes to endpoints accessed via api tokens -
The UI/UX has been updated following UI Style Guide
Closes #818 (closed)