sciapp / sampledb

Sample and Measurement Metadata Database
https://scientific-it-systems.iffgit.fz-juelich.de/SampleDB/
MIT License
21 stars 11 forks source link

Dynamic template selection #18

Closed MayerBjoern closed 3 years ago

MayerBjoern commented 3 years ago

This "under the hood" patch allows the style scheme attribute to be applied to all datatypes. Now the view or form template is no longer selected through a hard-coded if-else jinja list, but through a newly defined jinja function. In the defined folder it looks for a template named

[schema.style]_[schema.type].html

If there is no style defined in the scheme or there exists no template file for the given style, then the default

[schema.type].html

is taken.

The already existing array styles "list" and "table" are now implemented this way. The array template inherits its own scheme style attribute to all its array items. This way the items automatically also get displayed in the table_ templates for example. This has the nice upside, that the table_any and list_any files are now obsolete. Also the base array definitions for the different styles ("list", "table", "horizontal_table") are now split in different files, slightly increasing readability ;)

The only downside is that the choices exception now moved into the text templates (similar to the already existing if clauses for multilines or markdowns) (which does not make a big difference for the view templates at all). But i could seperate those again, if need be.

Why bother? The future usage of this is, that you can apply a different display style to anything you want without having to modify multiple hard-coded lists. You only have to create the corresponding view or form template. We will use this for our chemical compound datatype object (coming soon) which we want to display in multiple different ways, depending on context. The differente styles there would be for example compound name, sum formula, 2D Rendering of the molecule. We also need a whole new array style for the stoichiometry of chemical reactions.

FlorianRhiem commented 3 years ago

Thank you for cleaning up this if-elif-mess! :)

I've made a few minor improvements in this commit. Something that I'm not entirely happy with is that the style of the current object and the style of it's parent are conflated. Like you say, future styles may be introduced to something other than an array, and let's say you have a list of chemical compounds. If those compounds have a style of their own, it is overridden by the list style of the array. It might be cleaner to have a style for the child object and a parent_style automatically inserted by the parent template (as it's done right now for lists and tables), or something like that.

What are your thoughts on that?

MayerBjoern commented 3 years ago

You are right, the simplest solution i could come up with can be found in the recent commits. On top of that i fixed the form_data comparison for multilanguage selects, which didn't work properly since the choice option value in that case becomes a dictionary, which in form_data is only represented as a string.

The inherited style is now called parent_style, which is indeed way better for the future. Additionally the dataobject can now have an additional personal style, which if present takes precedence. Highest priority gets the combination of both. Obviously this has the downside, that if you really need an individual style variant to be different depending on table or non-table context, you would have to create two templates:

style_type.html table_style_type.html

FlorianRhiem commented 3 years ago

Merged, thank you for the pull request 🎉