Further bug fixes.

This commit is contained in:
AppleGrew (applegrew) 2012-09-03 09:39:50 +05:30
parent 3c3fe0b130
commit 94bc993377
4 changed files with 28 additions and 10 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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:

View file

@ -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])