From ff0bdc9c845f65401b8e97649fcea881d07c8f3f Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 18 Mar 2020 19:55:10 +0100 Subject: Autocomplete widget: add modify action to autocomplete field - better layout for previous value --- ishtar_common/widgets.py | 63 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 21 deletions(-) (limited to 'ishtar_common/widgets.py') diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index bf0f90a6c..3410d0824 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -599,7 +599,7 @@ class ModelJQueryAutocompleteField(ModelFieldMixin, forms.CharField): class JQueryAutoComplete(forms.TextInput): def __init__(self, source, associated_model=None, options=None, attrs=None, new=False, url_new='', multiple=False, limit=None, - dynamic_limit=None, tips=""): + dynamic_limit=None, modify=False, tips=""): """ Source can be a list containing the autocomplete values or a string containing the url used for the request. @@ -621,6 +621,7 @@ class JQueryAutoComplete(forms.TextInput): self.new = new self.url_new = url_new + self.modify = modify self.multiple = multiple self.limit = limit or {} self.dynamic_limit = dynamic_limit or [] @@ -665,6 +666,7 @@ class JQueryAutoComplete(forms.TextInput): attrs_hidden = self.build_attrs(attrs, {"name": name}) attrs_select = self.build_attrs(attrs) attrs_select['placeholder'] = _(u"Search...") + values = [] if value: hiddens = [] selects = [] @@ -703,20 +705,32 @@ class JQueryAutoComplete(forms.TextInput): if 'class' not in attrs_select: attrs_select['class'] = 'autocomplete' + has_previous_value = 'value' in attrs_select and attrs_select['value'] attrs_select['class'] += ' form-control' - new = '' - html = u"" - if self.tips or self.new: - html = "
" + new = "" + html = "" + if self.tips or self.new or self.modify: + klass = "input-group" + if has_previous_value: + klass += " has-previous-value" + html = "
".format(klass) + # WARNING: the modal for the form must be in the main template + # "extra_form_modals" list is used for that in form or view + model_name = self.associated_model._meta.object_name.lower() if self.tips: - new = """ + new += """ {} """.format(self.tips) + if self.modify and len(values) == 1: + url_modify = 'modify-' + model_name + url_modify = reverse(url_modify, args=[values[0]]) + new += """ + + + + """.format(url_modify, model_name, model_name) if self.new: - # WARNING: the modal for the form must be in the main template - # "extra_form_modals" list is used for that in form or view - - model_name = self.associated_model._meta.object_name.lower() limits = [] for k in self.limit: limits.append(k + "__" + "-".join( @@ -737,18 +751,25 @@ class JQueryAutoComplete(forms.TextInput): new += "
" old_value = "" - if 'value' in attrs_select and attrs_select['value']: - old_value = u""" - {} {} - -""".format( - _(u"Previous value:"), - attrs_hidden['id'] + u"_previous_label", + if has_previous_value: + old_value = """
+
+ {} +
+ + + + +
""".format( + _("Prev.:"), + attrs_hidden['id'] + "_previous_label", attrs_select['value'], - attrs_hidden['id'] + u"_previous_button", + attrs_hidden['id'] + "_previous_button", + _("Restore previous") ) attrs_hidden_previous = attrs_hidden.copy() attrs_hidden_previous['name'] += u"_previous" -- cgit v1.2.3