Skip to content

Resolve "[Feature] Add URL formula keyword"

Nigel Gott requested to merge 818-feature-add-url-formula-keyword into develop

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:

  1. 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.
  2. button(some url, some label) this renders a link with a specific label. An empty label will default to rendering the entire link.
  3. get_link_label(a link formula) which lets you extract the label part of a link formula. e.g get_link_label(button('url', 'label test')) = 'label test'
  4. get_link_url(a link formula) which lets you extract the url part of a link formula. e.g get_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.

  1. The model field used on a Django model for formulas is the BaserowExpressionField
  2. 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
  3. We set db_returning=True on BaserowExpressionField which makes Django generate INSERT .... RETURNING field_1, field_2 statements so we don't have to run a second SELECT to see what the was formula calculated to be.
  4. However for link formulas for some reason Django when running row model.objects.create() and then getattr(row, a link formula field) was returning a string instead of a dict (which a JSON field should return)
  5. It turns out that the db_returning feature provided by django never runs field.from_db_value after the INSERT .. RETURNING .. statement is run! This is a bug in Django IMO.
  6. 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 the refresh_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)

Edited by Nigel Gott

Merge request reports