From 9b74fa38d445585650c742f6055bf610ea043e18 Mon Sep 17 00:00:00 2001 From: Chen Zhe Date: Thu, 11 Oct 2018 22:50:47 +0800 Subject: [PATCH 1/4] fixes for python3 and django1.11 --- select_url_field/fields.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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): From 96eef0570695df80b3cfbca82c508453f959cda3 Mon Sep 17 00:00:00 2001 From: Chen Zhe Date: Wed, 9 Jan 2019 00:31:52 +0800 Subject: [PATCH 2/4] fixes for django 1.11 --- select_url_field/choice_with_other.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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) From 5cd3d2167c5cf498baa5918cbdd22bbf66c2f251 Mon Sep 17 00:00:00 2001 From: Chen Zhe Date: Sat, 8 Jun 2019 00:54:12 +0800 Subject: [PATCH 3/4] bug fix: selecturl field dropdown do not work for new inline before they are saved. --- .../static/admin/choice_with_other.js | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/select_url_field/static/admin/choice_with_other.js b/select_url_field/static/admin/choice_with_other.js index a2bb6c4..4d06525 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 From c6b7d4774bedbee21934803a068e42665d56f0e2 Mon Sep 17 00:00:00 2001 From: Chen Zhe Date: Sat, 31 Aug 2019 12:43:24 +0800 Subject: [PATCH 4/4] tweak js for django2 --- select_url_field/static/admin/choice_with_other.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/select_url_field/static/admin/choice_with_other.js b/select_url_field/static/admin/choice_with_other.js index 4d06525..da2550f 100644 --- a/select_url_field/static/admin/choice_with_other.js +++ b/select_url_field/static/admin/choice_with_other.js @@ -32,4 +32,4 @@ }) }) -})(django.jQuery); \ No newline at end of file +})($ || django.jQuery); \ No newline at end of file