Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added value_template_string property to BaseInput #961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

monim67
Copy link

@monim67 monim67 commented Jan 5, 2020

Fixes:

What has been changed:

  • Added a new property value_template_string to BaseInput class.

How it fixes the issue:

The additional property value_template_string preserves the original value passed to sub-classes
of BaseInput from being overwritten in the render method.

@codecov-io
Copy link

codecov-io commented Jan 5, 2020

Codecov Report

Merging #961 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #961   +/-   ##
=======================================
  Coverage   95.62%   95.62%           
=======================================
  Files          24       24           
  Lines        2768     2768           
  Branches      244      244           
=======================================
  Hits         2647     2647           
  Misses         79       79           
  Partials       42       42
Impacted Files Coverage Δ
crispy_forms/layout.py 96.23% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d355c9...f396c20. Read the comment docs.

@smithdc1 smithdc1 added this to the Next Release milestone Jan 19, 2020
@smithdc1
Copy link
Member

Hi @monim67 - thanks for looking into this!

Do you think we should have a small regression test?

@smithdc1 smithdc1 removed this from the 1.9.0 milestone Jan 19, 2020
@monim67
Copy link
Author

monim67 commented Jan 20, 2020

@smithdc1 how can we do that? So far the tests run successfully so and I have checked the input renders properly after this change. Do you have anything on mind?

To prevent the loss of original value passed to BaseInput sub-classes
due to overwrite in render method, the value is stored into
value_template_string property.
@monim67 monim67 force-pushed the fix-button-translation-issue branch from 9f60ec0 to f396c20 Compare January 20, 2020 15:13
@smithdc1
Copy link
Member

I had a look at your heroku demo app which I think you've updated for the change as the buttons now change language.

In this version it doesn't look like it's working as intended. Whilst the labels change language ok, the button behaviour is a bit random.

ezgif-1-9b562fe18507

@monim67
Copy link
Author

monim67 commented Jan 21, 2020

The heroku app is intended to demonstrate the bug, it does not include the fix. And the button labels don't change language. You might be seeing a cached version there.

@carltongibson
Copy link
Collaborator

carltongibson commented Jan 21, 2020

Traditionally we've said that if you need translation then you must instantiate your layout objects in a per-request context, such as a form's __init__. You can wrap the shared layout in a helper/factory function to keep it DRY.

Making the values properly translatable would be a reasonable addition I think. But it would be a new feature, and I think we should make sure we've thought it through fully before just sneaking it in. (Including tests, and documenting any new attributes.)

c.f. #776 here, which looked at the same issue. (Not sure if the approach was sound or not as yet.)

An alternate approach I was pondering whilst thinking about this, but haven't thought through, is finding a way of not needing to overwrite value each time through render... (From the second pass it's a noop — can we lazily evaluate it somehow? — Not sure as yet.)

@monim67
Copy link
Author

monim67 commented Jan 22, 2020

@carltongibson while looking into it the lazy object also came to my mind, but if I assign a lazy object to value in the constructor then it will require the context object from render function to evaluate, so it's not going to work unless we find a way to pass the context.

The simple solution I thought was separating two value variables instead of overwriting, the one passed to the constructor value_template and the one passed to html template rendered_value. I was in a confusion about which one to rename. I chose to rename the value_template considering the affect was less.

We should really think deeper before we decide how to provide the feature. Haul me if I can be of any help 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants