Skip to content

Commit 392d7c5

Browse files
committed
have (single) listed as a select choice
1 parent e878fd6 commit 392d7c5

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/olympia/devhub/forms.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ class RollbackVersionForm(forms.Form):
16031603
coerce=int,
16041604
widget=forms.RadioSelect(),
16051605
)
1606+
listed_version = forms.TypedChoiceField(choices=(), coerce=int, required=False)
16061607
unlisted_version = forms.ModelChoiceField(
16071608
queryset=Version.objects.none(), empty_label=gettext('Choose version')
16081609
)
@@ -1611,13 +1612,19 @@ class RollbackVersionForm(forms.Form):
16111612
def __init__(self, *args, **kwargs):
16121613
self.addon = kwargs.pop('addon')
16131614
super().__init__(*args, **kwargs)
1615+
listed = self.fields['listed_version']
16141616
unlisted = self.fields['unlisted_version']
16151617
channel = self.fields['channel']
16161618

16171619
listed_queryset = self.addon.rollbackable_versions_qs(amo.CHANNEL_LISTED)
16181620
self.listed_count = listed_queryset.count()
16191621
# We currently only allow rolling back to a single listed version.
1620-
self.listed_version = listed_queryset.first() if self.listed_count else None
1622+
if self.listed_count:
1623+
listed_version_obj = listed_queryset.first()
1624+
listed.choices = ((listed_version_obj.id, listed_version_obj),)
1625+
else:
1626+
listed.choices = ((None, 'No appropriate version available'),)
1627+
16211628
unlisted.queryset = self.addon.rollbackable_versions_qs(amo.CHANNEL_UNLISTED)
16221629
self.unlisted_count = unlisted.queryset.count()
16231630

@@ -1652,10 +1659,15 @@ def clean_new_version_string(self):
16521659
raise forms.ValidationError(error)
16531660
return new_version_string
16541661

1662+
def clean_listed_version(self):
1663+
version_pk, version_obj = self.fields['listed_version'].choices[0]
1664+
self.cleaned_data['listed_version'] = version_obj if version_pk else None
1665+
return self.cleaned_data['listed_version']
1666+
16551667
def clean(self):
16561668
data = super().clean()
16571669
data['version'] = (
1658-
self.listed_version
1670+
data.get('listed_version')
16591671
if data.get('channel') == amo.CHANNEL_LISTED
16601672
else data.get('unlisted_version')
16611673
if data.get('channel') == amo.CHANNEL_UNLISTED

src/olympia/devhub/templates/devhub/includes/modal_rollback_version.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ <h3>{{ _('Roll back to a previous version') }}</h3>
2020
{% endif %}
2121
{% if has_listed_versions %}
2222
<tr id="listed-version-row">
23-
<td><label>{{ _('Version') }}</label></td>
23+
<td><label for="id_listed_version">{{ _('Version') }}</label></td>
2424
<td>
25-
<span id="listed_version_for_rollback">{{ rollback_form.listed_version or _('No appropriate version available') }}</span>
25+
{{ rollback_form.listed_version }}
26+
{{ rollback_form.listed_version.errors }}
2627
</td>
2728
</tr>
2829
{% endif %}

src/olympia/devhub/tests/test_forms.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,9 @@ def test_no_listed_version_choices(self):
14681468
v2.version,
14691469
v1.version,
14701470
]
1471-
assert form.listed_version is None
1471+
assert form.fields['listed_version'].choices == [
1472+
(None, 'No appropriate version available')
1473+
]
14721474

14731475
def test_no_unlisted_version_choices(self):
14741476
"""No listed version choices should default the channel to unlisted, and make
@@ -1479,7 +1481,7 @@ def test_no_unlisted_version_choices(self):
14791481
form = forms.RollbackVersionForm(addon=addon)
14801482
assert form.fields['channel'].initial == amo.CHANNEL_LISTED
14811483
assert form.fields['unlisted_version'].required is False
1482-
assert form.listed_version == v2
1484+
assert form.fields['listed_version'].choices == [(v2.id, v2)]
14831485

14841486
def test_listed_and_unlisted_version_choices(self):
14851487
"""Both listed and unlisted version choices should be available."""
@@ -1492,7 +1494,7 @@ def test_listed_and_unlisted_version_choices(self):
14921494
form = forms.RollbackVersionForm(addon=addon)
14931495
assert form.fields['channel'].initial is amo.CHANNEL_UNLISTED
14941496
assert form.fields['unlisted_version'].required is True
1495-
assert form.listed_version == lv2
1497+
assert form.fields['listed_version'].choices == [(lv2.id, lv2)]
14961498
assert [str(lab) for _, lab in form.fields['unlisted_version'].choices] == [
14971499
'Choose version',
14981500
uv2.version,
@@ -1585,17 +1587,27 @@ def test_clean_adds_version(self):
15851587
}
15861588
# test we're selecting the listed channel
15871589
form = forms.RollbackVersionForm(data, addon=addon)
1588-
assert form.listed_version == lv1
1590+
assert form.fields['listed_version'].choices == [(lv1.id, lv1)]
15891591
assert form.is_valid(), form.errors
1590-
# the listed version is added as the chosen version
1591-
assert form.clean() == {**data, 'unlisted_version': uv1, 'version': lv1}
1592+
# the listed version is added, and as the chosen version
1593+
assert form.clean() == {
1594+
**data,
1595+
'unlisted_version': uv1,
1596+
'listed_version': lv1,
1597+
'version': lv1,
1598+
}
15921599

15931600
# repeat with unlisted
15941601
data['channel'] = amo.CHANNEL_UNLISTED
15951602
form = forms.RollbackVersionForm(data, addon=addon)
15961603
assert form.is_valid(), form.errors
15971604
# the selected unlisted version is added as the chosen version
1598-
assert form.clean() == {**data, 'unlisted_version': uv1, 'version': uv1}
1605+
assert form.clean() == {
1606+
**data,
1607+
'unlisted_version': uv1,
1608+
'listed_version': lv1,
1609+
'version': uv1,
1610+
}
15991611

16001612
# and when we select listed but there is no listed version availble: error
16011613
data['channel'] = amo.CHANNEL_LISTED

src/olympia/devhub/tests/test_views_versions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,8 @@ def test_version_rollback_form_listed_only(self):
842842
# we hide the channel selector, because there are no other channels
843843
assert modal('input[name="channel"]').attr('type') == 'hidden'
844844
assert modal('input[name="channel"]').attr('value') == str(amo.CHANNEL_LISTED)
845-
assert modal('#listed_version_for_rollback').text() == first_version.version
845+
assert modal('select option').length == 1
846+
assert modal('select option')[0].text == first_version.version
846847
# and the select for unlisted versions is not present
847848
assert 'Choose version' not in modal.html()
848849

@@ -861,7 +862,7 @@ def test_version_rollback_form_unlisted_only(self):
861862
channel_input = modal('input[name="channel"]')
862863
assert channel_input.attr('type') == 'hidden'
863864
assert channel_input.attr('value') == str(amo.CHANNEL_UNLISTED)
864-
assert modal('#listed_version_for_rollback').length == 0
865+
assert modal('#id_listed_version').length == 0
865866
assert 'Choose version' in modal.html()
866867
assert modal('select option').length == 2
867868
assert modal('select option')[0].text == 'Choose version'
@@ -891,12 +892,13 @@ def test_version_rollback_form_listed_but_not_appropriate(self):
891892
# and disable it
892893
assert modal(f'{channel_selector}[disabled]').length == 2
893894
assert modal(channel_selector).length == 2
895+
assert modal('#id_listed_version option').length == 1
894896
assert (
895-
modal('#listed_version_for_rollback').text()
897+
modal('#id_listed_version option').text()
896898
== 'No appropriate version available'
897899
)
898900
assert 'Choose version' in modal.html()
899-
assert modal('select option').length == 2
901+
assert modal('#id_unlisted_version option').length == 2
900902

901903
@override_switch('version-rollback', active=True)
902904
def test_version_rollback_form_both_channels(self):
@@ -914,7 +916,8 @@ def test_version_rollback_form_both_channels(self):
914916
# this time we show the channel selector, because there are both channels
915917
channel_selector = 'input[name="channel"]'
916918
assert modal(channel_selector).attr('type') == 'radio'
917-
assert not modal(f'{channel_selector}[value="{amo.CHANNEL_UNLISTED}"]').attr(
919+
# and preselect unlisted because it's the most recent channel for a version
920+
assert modal(f'{channel_selector}[value="{amo.CHANNEL_UNLISTED}"]').attr(
918921
'checked'
919922
)
920923
assert not modal(f'{channel_selector}[value="{amo.CHANNEL_LISTED}"]').attr(
@@ -923,8 +926,10 @@ def test_version_rollback_form_both_channels(self):
923926
# and they're enabled
924927
assert modal(f'{channel_selector}[disabled]').length == 0
925928
assert modal(channel_selector).length == 2
926-
assert modal('#listed_version_for_rollback').text() == listed_version.version
929+
assert modal('#id_listed_version option').length == 1
930+
assert modal('#id_listed_version option').text() == listed_version.version
927931
assert 'Choose version' in modal.html()
932+
assert modal('#id_unlisted_version option').length == 2
928933

929934
@override_switch('version-rollback', active=True)
930935
def test_version_rollback_submit(self):

0 commit comments

Comments
 (0)