From 0adadd4d3f40ee313e392886fab1080e1bee4f92 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 14 May 2018 21:32:22 +0200 Subject: Add new to authors field for m2m images --- ishtar_common/forms_common.py | 2 +- ishtar_common/models.py | 4 +++- ishtar_common/widgets.py | 23 +++++++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 22cd81e7b..061ae49b0 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -1101,7 +1101,7 @@ class BaseImageForm(ManageOldType): name = forms.CharField(label=_(u"Name"), max_length=250, required=False) authors = widgets.Select2MultipleField( model=models.Author, remote=True, label=_(u"Authors"), - required=False, long_widget=True + required=False, long_widget=True, new=True ) description = forms.CharField(label=_(u"Description"), required=False, widget=forms.Textarea) diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 53f4e1c11..ab70faaa0 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2904,10 +2904,12 @@ post_delete.connect(post_save_cache, sender=AuthorType) class Author(FullSearch): + PARENT_SEARCH_VECTORS = ['person'] + SLUG = "author" + person = models.ForeignKey(Person, verbose_name=_(u"Person"), related_name='author') author_type = models.ForeignKey(AuthorType, verbose_name=_(u"Author type")) - PARENT_SEARCH_VECTORS = ['person'] class Meta: verbose_name = _(u"Author") diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index e33669e19..442f12b9a 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -103,11 +103,12 @@ class SelectReadonlyField(forms.ChoiceField): class Select2Multiple(forms.SelectMultiple): def __init__(self, attrs=None, choices=(), remote=None, model=None, - available=None): + new=None, available=None): super(Select2Multiple, self).__init__(attrs, choices) self.remote = remote self.available = available self.model = model + self.new = new @property def media(self): @@ -152,7 +153,7 @@ class Select2Multiple(forms.SelectMultiple): attrs['class'] = klass if 'style' not in attrs: if attrs.get('full-width', None): - attrs['style'] = "width: 100%" + attrs['style'] = "width: calc(100% - 60px)" else: attrs['style'] = "width: 370px" @@ -195,7 +196,18 @@ class Select2Multiple(forms.SelectMultiple): options = "{" options += " containerCssClass: 'full-width'}" self.choices = choices - html = super(Select2Multiple, self).render(name, value, attrs) + + new, html = "", "" + if self.new: + html = u"
" + url_new = 'new-' + self.model.SLUG + url_new = reverse(url_new) + new = u'' \ + u'+
' % url_new + + html += super(Select2Multiple, self).render(name, value, attrs) + html += new html += """