From 94bc993377bfab0ef869b8393fdba8e87dc6b0fb Mon Sep 17 00:00:00 2001 From: "AppleGrew (applegrew)" Date: Mon, 3 Sep 2012 09:39:50 +0530 Subject: [PATCH] Further bug fixes. --- django_select2/fields.py | 3 ++- django_select2/static/js/heavy_data.js | 7 +++++-- django_select2/widgets.py | 4 ++-- testapp/testmain/forms.py | 24 +++++++++++++++++++----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/django_select2/fields.py b/django_select2/fields.py index 36f420f..898872d 100644 --- a/django_select2/fields.py +++ b/django_select2/fields.py @@ -489,7 +489,8 @@ class HeavyChoiceField(ChoiceMixin, forms.Field): try: value = self.coerce_value(value) self.validate_value(value) - except: + except Exception, e: + logger.exception("Exception while trying to get label for value") return None return self.get_val_txt(value) diff --git a/django_select2/static/js/heavy_data.js b/django_select2/static/js/heavy_data.js index a8c2420..09ab76b 100644 --- a/django_select2/static/js/heavy_data.js +++ b/django_select2/static/js/heavy_data.js @@ -79,8 +79,11 @@ if (!window['django_select2']) { // Cookies are used to persist selection's text. This is needed // when the form springs back if there is any validation failure. $(res[0]).each(function (idx) { - django_select2.setCookie(id + '_heavy_val:' + idx, this); - django_select2.setCookie(id + '_heavy_txt:' + idx, res[1][idx]); + var txt = res[1][idx]; + if (typeof(txt) !== 'undefined') { + django_select2.setCookie(id + '_heavy_val:' + idx, this); + django_select2.setCookie(id + '_heavy_txt:' + idx, txt); + } }); } else { django_select2.delCookie(id + '_heavy_val:', true); diff --git a/django_select2/widgets.py b/django_select2/widgets.py index 7f0d520..6a8d0ac 100644 --- a/django_select2/widgets.py +++ b/django_select2/widgets.py @@ -364,8 +364,8 @@ class HeavySelect2Mixin(Select2Mixin): selected_choices = [v for v in selected_choices if v != val] txts.append(txt) if hasattr(self.field, '_get_val_txt') and selected_choices: - for v in selected_choices: - txt = self.field._get_val_txt(v) + for val in selected_choices: + txt = self.field._get_val_txt(val) if txt is not None: txts.append(txt) if txts: diff --git a/testapp/testmain/forms.py b/testapp/testmain/forms.py index 8024305..3d90645 100644 --- a/testapp/testmain/forms.py +++ b/testapp/testmain/forms.py @@ -40,11 +40,26 @@ class SelfChoices(AutoSelect2Field): return (NO_ERR_RESP, False, res) class SelfMultiChoices(AutoSelect2MultipleField): + big_data = { + 1: "First", 2: "Second", 3: "Third", + } + + def validate_value(self, value): + if value in [v for v in self.big_data]: + return True + else: + return False + + def coerce_value(self, value): + return int(value) + + def get_val_txt(self, value): + return self.big_data.get(value, None) + def get_results(self, request, term, page, context): - res = [] - for i in range(1, 6): + res = [(v, self.big_data[v]) for v in self.big_data] + for i in range(len(res), 6): res.append((i, term * i,)) - self.choices = res return (NO_ERR_RESP, False, res) @@ -67,6 +82,5 @@ class InitialValueForm(forms.Form): heavySelect2Choice = AutoSelect2Field(initial=2, choices=((1, "First"), (2, "Second"), (3, "Third"), )) heavySelect2MultipleChoice = AutoSelect2MultipleField(initial=[1,3], choices=((1, "First"), (2, "Second"), (3, "Third"), )) self_choices = SelfChoices(label='Self copy choices', initial=2, choices=((1, "First"), (2, "Second"), (3, "Third"), )) - self_multi_choices = SelfMultiChoices(label='Self copy multi-choices', - initial=[2,3], choices=((1, "First"), (2, "Second"), (3, "Third"), )) + self_multi_choices = SelfMultiChoices(label='Self copy multi-choices', initial=[2,3])