diff options
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r-- | ishtar_common/forms.py | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 20c65971e..91e9fb3e9 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -341,6 +341,37 @@ class CustomForm(BSForm): return sorted(customs, key=lambda x: x[1]) +class PkWizardSearch(object): + current_model = None + pk_key = None + + @classmethod + def get_formated_datas(cls, cleaned_datas): + if not cls.current_model or not cls.pk_key: + return [] + items = [] + for data in cleaned_datas: + if not data or cls.pk_key not in data or not data[cls.pk_key]: + continue + pks = data[cls.pk_key] + for pk in unicode(pks).split(u','): + if not pk: + continue + try: + items.append( + unicode(cls.current_model.objects.get(pk=int(pk))) + ) + except (cls.current_model.DoesNotExist, ValueError): + continue + return [ + (u"", + mark_safe( + u"<ul class='compact'><li>" + u"</li><li>".join(items) + + u"</li></ul>" + )) + ] + + class CustomFormSearch(forms.Form): need_user_for_initialization = True @@ -405,6 +436,22 @@ class FormSet(CustomForm, BaseFormSet): form.fields[DELETION_FIELD_NAME].label = '' form.fields[DELETION_FIELD_NAME].widget = self.delete_widget() + def _should_delete_form(self, form): + """ + Returns whether or not the form was marked for deletion. + If no data, set deletion to True + """ + if form.cleaned_data.get(DELETION_FIELD_NAME, False): + return True + if not form.cleaned_data or not [ + __ for __ in form.cleaned_data + if __ != DELETION_FIELD_NAME and + form.cleaned_data[__] is not None and + form.cleaned_data[__] != '']: + form.cleaned_data[DELETION_FIELD_NAME] = True + return True + return False + class FormSetWithDeleteSwitches(FormSet): delete_widget = widgets.DeleteSwitchWidget @@ -714,16 +761,19 @@ class QAForm(CustomForm, ManageOldType): elif hasattr(self.fields[k], "choices"): values = [] for v in kwargs['data'].getlist(k): - values.append( - dict(self.fields[k].choices)[int(v)]) + dct_choices = dict(self.fields[k].choices) + if v in dct_choices: + values.append(unicode(dct_choices[v])) + elif int(v) in dct_choices: + values.append(unicode(dct_choices[int(v)])) self.fields[k].rendered_value = mark_safe( u" ; ".join(values)) if k not in self.REPLACE_FIELDS: self.fields[k].label = unicode(self.fields[k].label) + \ - unicode(u" - append to existing") + unicode(_(u" - append to existing")) else: self.fields[k].label = unicode(self.fields[k].label) + \ - unicode(u" - replace") + unicode(_(u" - replace")) def _set_value(self, item, base_key): value = self.cleaned_data[base_key] |