-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements in ExtendedChoices enum creation + bootstrap widget
- Loading branch information
1 parent
f4d87da
commit 5658270
Showing
7 changed files
with
244 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="fr-fieldset__element"> | ||
<div class="fr-radio-group fr-radio-rich"> | ||
<input value="1" type="radio" id="radio-rich-1" name="radio-rich"> | ||
<label class="fr-label" for="radio-rich-1"> | ||
Libellé bouton radio | ||
</label> | ||
<div class="fr-radio-rich__pictogram"> | ||
<svg aria-hidden="true" class="fr-artwork" viewBox="0 0 80 80" width="80px" height="80px"> | ||
<use class="fr-artwork-decorative" href="../../../dist/artwork/pictograms/buildings/city-hall.svg#artwork-decorative"></use> | ||
<use class="fr-artwork-minor" href="../../../dist/artwork/pictograms/buildings/city-hall.svg#artwork-minor"></use> | ||
<use class="fr-artwork-major" href="../../../dist/artwork/pictograms/buildings/city-hall.svg#artwork-major"></use> | ||
</svg> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% with id=widget.attrs.id %}<fieldset{% if id %} id="{{ id }}"{% endif %} class="fr-fieldset{% if widget.attrs.class %} {{ widget.attrs.class }}{% endif %}"> | ||
{% for group, options, index in widget.optgroups %} | ||
{% if group %}<div><legend class="fr-fieldset__legend{% if widget.attrs.legend_classes %} {{ widget.attrs.legend_classes }}{% endif %}">{{ group }}</legend>{% endif %} | ||
{% for option in options %} | ||
<div>{% include option.template_name with widget=option %}</div> | ||
{% endfor %} | ||
{% if group %}</div>{% endif %} | ||
{% endfor %} | ||
</fieldset>{% endwith %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.test import SimpleTestCase | ||
from dsfr.utils import generate_random_id, generate_summary_items | ||
|
||
|
||
class ExtendedChoicesTestCase(SimpleTestCase): | ||
def test_create_dict_enum(self): | ||
... | ||
|
||
def test_absent_value_key_raises_error(self): | ||
... | ||
|
||
def test_absent_label_computes_default(self): | ||
... | ||
|
||
def test_absent_additionnal_key_calls_dynamic_attribute_value_method(self): | ||
... | ||
|
||
def test_absent_additionnal_key_no_dynamic_attribute_value_method(self): | ||
... | ||
|
||
def test_additional_attributes(self): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,53 @@ | ||
from typing import Type | ||
|
||
from django.forms.widgets import ChoiceWidget | ||
from django.forms.widgets import RadioSelect, ChoiceWidget, CheckboxSelectMultiple | ||
|
||
from dsfr.enums import RichRadioButtonChoices | ||
|
||
|
||
class RichRadioButtonWidget(ChoiceWidget): | ||
def __init__( | ||
self, rich_choices: Type[RichRadioButtonChoices], attrs=None | ||
): | ||
super().__init__(attrs, rich_choices.choices) | ||
class _RichChoiceWidget(ChoiceWidget): | ||
def __init__(self, rich_choices: Type[RichRadioButtonChoices], attrs=None): | ||
super().__init__(attrs) | ||
self.rich_choices = rich_choices | ||
|
||
@property | ||
def choices(self): | ||
return self.rich_choices.choices | ||
|
||
@choices.setter | ||
def choices(self, value): | ||
""" | ||
Superseded by self.rich_choices; | ||
kept for compatibility with ChoiceWidget.__init__ | ||
""" | ||
... | ||
|
||
def __deepcopy__(self, memo): | ||
obj = super().__deepcopy__(memo) | ||
obj.rich_choices = self.rich_choices | ||
return obj | ||
|
||
def create_option( | ||
self, name, value, label, selected, index, subindex=None, attrs=None | ||
): | ||
opt = super().create_option( | ||
name, value, label, selected, index, subindex, attrs | ||
) | ||
|
||
opt.update( | ||
{ | ||
k: getattr(self.rich_choices(value), k) | ||
for k in self.rich_choices.additional_attributes | ||
} | ||
) | ||
|
||
return opt | ||
|
||
|
||
class RichRadioSelect(_RichChoiceWidget, RadioSelect): | ||
template_name = "dsfr/widgets/rich_radio.html" | ||
option_template_name = "dsfr/widgets/rich_radio_option.html" | ||
|
||
|
||
class RichCheckboxSelect(_RichChoiceWidget, CheckboxSelectMultiple): | ||
... |
Oops, something went wrong.