Skip to content

Commit

Permalink
Correctly render label and input fields.
Browse files Browse the repository at this point in the history
Somehow they got lost along the way when I refactored the IconTypeSelect
widget.

Fixes #8900
  • Loading branch information
EnTeQuAk committed Jul 19, 2018
1 parent f071b94 commit 82c7d1c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
25 changes: 0 additions & 25 deletions src/olympia/addons/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ def test_tags_long(self):
class TestIconForm(TestCase):
fixtures = ['base/addon_3615']

# TODO: AddonFormMedia save() method could do with cleaning up
# so this isn't necessary
def setUp(self):
super(TestIconForm, self).setUp()
self.temp_dir = tempfile.mkdtemp(dir=settings.TMP_PATH)
Expand All @@ -283,29 +281,6 @@ def get_icon_paths(self):
path = os.path.join(self.addon.get_icon_dir(), str(self.addon.id))
return ['%s-%s.png' % (path, size) for size in amo.ADDON_ICON_SIZES]

@patch('olympia.addons.models.Addon.get_icon_dir')
def testIconUpload(self, get_icon_dir):
# TODO(gkoberger): clarify this please.
# We no longer use AddonFormMedia to upload icons, so
# skipping until I can ask andym what the point of this
# test is. Additionally, it's called "TestIconRemoval",
# but it doesn't seem to remove icons.
return
get_icon_dir.return_value = self.temp_dir

for path in self.get_icon_paths():
assert not os.path.exists(path)

img = get_image_path('non-animated.png')
data = {'icon_upload': img, 'icon_type': 'text/png'}
self.request.FILES = {'icon_upload': open(img)}
form = forms.AddonFormMedia(data=data, request=self.request,
instance=self.addon)
assert form.is_valid()
form.save(self.addon)
for path in self.get_icon_paths():
assert os.path.exists(path)

@patch('olympia.amo.models.ModelBase.update')
def test_icon_modified(self, update_mock):
name = 'transparent.png'
Expand Down
40 changes: 28 additions & 12 deletions src/olympia/addons/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,49 @@

from django import forms
from django.utils.encoding import force_text
from django.utils.html import conditional_escape, format_html_join
from django.utils.html import conditional_escape, format_html
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext

from olympia.addons.models import Category


class IconTypeSelect(forms.RadioSelect):
base_html = (
'<li>'
'<a href="#" class="{active}">'
'<img src="{static}img/addon-icons/{icon_name}-32.png" alt="">'
'</a>'
'<label for="{label_id}">{original_widget}</label>'
'</li>'
)

def render(self, name, value, attrs=None, renderer=None):
args = []
output = []

for option in self.options(name, value, attrs):
for option in self.subwidgets(name, value, attrs):
option_value = option['value']

if option_value.split('/')[0] == 'icon' or option_value == '':
icon_name = option['label']
args.append((
'active' if option_value == value else '',
settings.STATIC_URL,
icon_name))

tmpl = (
'<li><a href="#" class="{}">'
'<img src="{}img/addon-icons/{}-32.png" alt="">'
'</a></li>')
option['widget'] = self.create_option(
name=name, value=option['value'], label=option['label'],
selected=option_value == value,
index=option['index'],
attrs=option['attrs'])

output.append(format_html(
self.base_html,
active='active' if option_value == value else '',
static=settings.STATIC_URL,
icon_name=icon_name,
label_id=option['widget']['attrs']['id'],
original_widget=self._render(
self.option_template_name, option)
))

return format_html_join('', tmpl, args)
return mark_safe(u'\n'.join(output))


class CategoriesSelectMultiple(forms.CheckboxSelectMultiple):
Expand Down
20 changes: 20 additions & 0 deletions src/olympia/devhub/tests/test_views_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,26 @@ def test_edit_media_defaulticon(self):
for k in data:
assert unicode(getattr(addon, k)) == data[k]

def test_edit_media_shows_proper_labels(self):
"""Regression test for
https://github.com/mozilla/addons-server/issues/8900"""
doc = pq(self.client.get(self.media_edit_url).content)

labels = doc('#icons_default li label')

assert labels.length == 18

# First one is the default icon
assert labels[0].get('for') == 'id_icon_type_2_2'
assert labels[0].find('input').get('name') == 'icon_type'
assert labels[0].find('input').get('value') == ''

# Second one is a regualr icon
assert labels[1].get('for') == 'id_icon_type_3_3'
assert labels[1].find('input').get('name') == 'icon_type'
assert labels[1].find('input').get('value') == 'icon/alerts'

def test_edit_media_preuploadedicon(self):
data = {'icon_type': 'icon/appearance'}
data_formset = self.formset_media(**data)
Expand Down

0 comments on commit 82c7d1c

Please sign in to comment.