diff --git a/select_url_field/choice_with_other.py b/select_url_field/choice_with_other.py index ddd3589..590ca81 100644 --- a/select_url_field/choice_with_other.py +++ b/select_url_field/choice_with_other.py @@ -1,5 +1,6 @@ from django import forms from django.conf import settings +from django.utils.safestring import mark_safe OTHER_CHOICE = '__other__' @@ -28,15 +29,16 @@ def decompress(self, value): return [OTHER_CHOICE, value] return ['', ''] - def format_output(self, rendered_widgets): - + def format_output(self, output): """Format the output by substituting the "other" choice into the first widget""" - - return u'
{} {}
'.format( - rendered_widgets[0], - rendered_widgets[1], + return '
{}
'.format( + output ) + def render(self, name, value, attrs=None, renderer=None): + output = super(ChoiceWithOtherWidget, self).render(name, value, attrs=attrs, renderer=renderer) + return mark_safe(self.format_output(output)) + def _media(self): return forms.Media(js=('admin/choice_with_other.js',)) media = property(_media) diff --git a/select_url_field/fields.py b/select_url_field/fields.py index 9abaee9..9d8eedf 100644 --- a/select_url_field/fields.py +++ b/select_url_field/fields.py @@ -6,8 +6,12 @@ from django.core.validators import URLValidator from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +try: + from django.utils.encoding import smart_unicode +except ImportError: + from django.utils.encoding import smart_str as smart_unicode from select_url_field import select_url_field_settings +import collections try: from importlib import import_module @@ -44,7 +48,7 @@ def formfield(self, **kwargs): # When choices given, use them # When not, use global settings if self._has_choices: - if callable(self._url_choices): + if isinstance(self._url_choices, collections.Callable): choices = self._url_choices() else: choices = self._url_choices @@ -83,7 +87,7 @@ def __call__(self, value): try: # OK if it's a valid url self.url_validator(value) - except ValidationError, e: + except ValidationError as e: # Not a valid url, see it's a path if not self.regex.search(smart_unicode(value)): raise e @@ -91,7 +95,7 @@ def __call__(self, value): class SelectURLFormField(forms.CharField): default_error_messages = { - 'invalid': _(u'Enter a valid URL.'), + 'invalid': _('Enter a valid URL.'), } def __init__(self, max_length=None, min_length=None, *args, **kwargs): diff --git a/select_url_field/static/admin/choice_with_other.js b/select_url_field/static/admin/choice_with_other.js index a2bb6c4..da2550f 100644 --- a/select_url_field/static/admin/choice_with_other.js +++ b/select_url_field/static/admin/choice_with_other.js @@ -1,31 +1,35 @@ (function($) { var OTHER_CHOICE = '__other__'; //See $(function(){ - var wrappers = $('.choice_with_other_wrapper'); - wrappers.each(function(){ - var select = $('select', this); - var text_input = $('input', this); - select.change(function(){ - if(select.val() == OTHER_CHOICE){ - ; - }else{ - text_input.val(select.val()); + $('body').delegate('.choice_with_other_wrapper select', 'change', function(e){ + var select = $(this) + var text_input = $(this).parent().find('input') + if(select.val() == OTHER_CHOICE){ + ; + }else{ + text_input.val(select.val()); + } + }); + var text_input = $('input', this); + $('body').delegate('.choice_with_other_wrapper input', 'keyup', function(e){ + var match = false; + var text_input = $(this) + var select = $(this).parent().find('select') + $(select).find('option').each(function(){ + if($(this).attr('value') == text_input.val()){ + match = true; } - }); - text_input.keyup(function(){ - var match = false; - $(select).find('option').each(function(){ - if($(this).attr('value') == text_input.val()){ - match = true; - } - }) - if(!match){ - select.val(OTHER_CHOICE); - }else{ - select.val(text_input.val()); - } - }); - select.change(); + }) + if(!match){ + select.val(OTHER_CHOICE); + }else{ + select.val(text_input.val()); + } + }) + + $('.choice_with_other_wrapper').each(function(){ + $('select', this).change(); }) + }) -})(django.jQuery); +})($ || django.jQuery); \ No newline at end of file