-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Add non-restrictive palette choices option #73
Add non-restrictive palette choices option #73
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, could you rename the option to samples
as discussed in #68 ?
Then I will test your PR as soon as possible, thanks in advance!
118a0a2
to
32d64be
Compare
Done :) thanks for the quick response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of scope for this PR... but as plus, when using choices the color-widget spectrum should be hidden, allowing the user to choose only from the choices, but this is hard to do even by dirty-patching the widget via css.
If you can try to do it it would be very appreciated.
colorfield/fields.py
Outdated
if self.choices: | ||
choices = self.get_choices(include_blank=False) | ||
palette = [choice[0] for choice in choices] | ||
if self.samples: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The choices options has been removed, this is not backward-compatible at all.
We should keep both options: choices
(restrictive) and samples
(not restrictive).
colorfield/fields.py
Outdated
kwargs['widget'] = ColorWidget(attrs={ | ||
'default': self.get_default(), | ||
'format': self.format, | ||
'palette': palette, | ||
}) | ||
return super(ColorField, self).formfield(**kwargs) | ||
|
||
def deconstruct(self): | ||
name, path, args, kwargs = super().deconstruct() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please pass the class and self to the super call for backward-compatibility.
32d64be
to
9382e24
Compare
colorfield/fields.py
Outdated
@@ -53,12 +55,23 @@ def __init__(self, *args, **kwargs): | |||
|
|||
def formfield(self, **kwargs): | |||
palette = [] | |||
if self.choices and self.samples: | |||
raise ImproperlyConfigured("You can only set one of 'choices' and 'samples' for ColorFields.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe is it more correct to raise this exception in the init method?
9382e24
to
4f9ff26
Compare
addresses Issue #68
This PR proposes a new field option which works similarly to django's
choices
kwarg, the difference being that the field's values are not restricted to the given choices. This way, one can use both the color palette and the color picker. Until now, withchoices
defined, the color picker is effectively useless, since we cannot choose values outside of the palette.The supplied screenshot shows this logic in action in a sample django project. It illustrates the ability to save a color value outside the predefined choices. I realise screenshots are not necessarily always accurate - there is always a possibility that I doctored/edited it - so I have also added a test that cleans a model instance with
palette_choices
set and a supplied value outside those choices. Feel free to run the code yourself as well and test it all out.I have replaced the
choices
option with the new option here. Please let me know if we should keep the originalchoices
as well. Perhaps adding some logic to make the two options mutually exclusive would be a good idea.