Ticket #19066: 0001-Allow-overriding-help_text-default-message-on-ManyTo.patch

File 0001-Allow-overriding-help_text-default-message-on-ManyTo.patch, 5.1 KB (added by rjalves, 3 years ago)
  • django/db/models/fields/related.py

    From a5aa707a2ac522611d6f7191833fa9149ab25de0 Mon Sep 17 00:00:00 2001
    From: Renato Alves <rjalves@igc.gulbenkian.pt>
    Date: Wed, 3 Oct 2012 13:44:59 +0100
    Subject: [PATCH] Allow overriding help_text default message on ManyToMany
     fields
    
    ---
     django/db/models/fields/related.py     |    5 ++-
     tests/modeltests/model_forms/models.py |   12 ++++++++
     tests/modeltests/model_forms/tests.py  |   47 ++++++++++++++++++++++++++++++-
     3 files changed, 60 insertions(+), 4 deletions(-)
    
    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 157640c..b4060df 100644
    a b class ManyToManyField(RelatedField, Field): 
    11621162
    11631163        Field.__init__(self, **kwargs)
    11641164
    1165         msg = _('Hold down "Control", or "Command" on a Mac, to select more than one.')
    1166         self.help_text = string_concat(self.help_text, ' ', msg)
     1165        # Only display "Hold down..." message if an empty string is passed.
     1166        if not str(self.help_text):
     1167            self.help_text = _('Hold down "Control", or "Command" on a Mac, to select more than one.')
    11671168
    11681169    def get_choices_default(self):
    11691170        return Field.get_choices(self, include_blank=False)
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index 90b019b..134cab7 100644
    a b class FlexibleDatePost(models.Model): 
    272272    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
    273273    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
    274274    posted = models.DateField(blank=True, null=True)
     275
     276class M2MDefaultHelpText(models.Model):
     277    ref = models.ManyToManyField("self", blank=True)
     278
     279class M2MNoHelpText(models.Model):
     280    ref = models.ManyToManyField("self", blank=True, help_text=None)
     281
     282class M2MCustomHelpText(models.Model):
     283    ref = models.ManyToManyField("self", blank=True, help_text="Help")
     284
     285class M2MEmptyHelpText(models.Model):
     286    ref = models.ManyToManyField("self", blank=True, help_text="")
  • tests/modeltests/model_forms/tests.py

    diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
    index 038ce32..8ce299b 100644
    a b from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book, 
    1717    Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook,
    1818    DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
    1919    ImprovedArticleWithParentLink, Inventory, PhoneNumber, Post, Price,
    20     Product, TextFile, Writer, WriterProfile, test_images)
     20    Product, TextFile, Writer, WriterProfile, test_images, M2MDefaultHelpText,
     21    M2MNoHelpText, M2MCustomHelpText, M2MEmptyHelpText)
    2122
    2223if test_images:
    2324    from .models import ImageFile, OptionalImageFile
    class PriceFormWithoutQuantity(forms.ModelForm): 
    176177        model = Price
    177178        exclude = ('quantity',)
    178179
    179 
    180180class ModelFormBaseTest(TestCase):
    181181    def test_base_form(self):
    182182        self.assertEqual(list(BaseCategoryForm.base_fields),
    class ModelFormBaseTest(TestCase): 
    330330        self.assertEqual(list(OrderFields2.base_fields),
    331331                         ['slug', 'name'])
    332332
     333    def test_default_M2M_helptext(self):
     334        class DefaultHelpText(forms.ModelForm):
     335            class Meta:
     336                model = M2MDefaultHelpText
     337
     338        self.assertHTMLEqual(
     339            str(DefaultHelpText()),
     340            """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext">
     341            Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>"""
     342            )
     343
     344    def test_override_M2M_helptext(self):
     345        class NoHelpText(forms.ModelForm):
     346            class Meta:
     347                model = M2MNoHelpText
     348
     349        self.assertHTMLEqual(
     350            str(NoHelpText()),
     351            """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /></td></tr>"""
     352            )
     353
     354
     355    def test_custom_M2M_helptext(self):
     356        class CustomHelpText(forms.ModelForm):
     357            class Meta:
     358                model = M2MCustomHelpText
     359
     360        self.assertHTMLEqual(
     361            str(CustomHelpText()),
     362            """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext">Help</span></td></tr>"""
     363            )
     364
     365    def test_empty_M2M_helptext(self):
     366        class EmptyHelpText(forms.ModelForm):
     367            class Meta:
     368                model = M2MEmptyHelpText
     369
     370        self.assertHTMLEqual(
     371            str(EmptyHelpText()),
     372            """<tr><th><label for="id_ref">Ref:</label></th><td><select id="id_ref" multiple="multiple" name="ref" /><br /><span class="helptext">
     373            Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>"""
     374            )
     375
    333376
    334377class TestWidgetForm(forms.ModelForm):
    335378    class Meta:
Back to Top