Ticket #22887: proposal-22887.patch

File proposal-22887.patch, 7.9 KB (added by Vlada Macek, 10 years ago)
  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 0335c48..2b909ab 100644
    a b class Model(six.with_metaclass(ModelBase)):  
    962962        return errors
    963963
    964964    def date_error_message(self, lookup_type, field_name, unique_for):
    965965        opts = self._meta
    966966        field = opts.get_field(field_name)
     967        code = 'unique_for_' + lookup_type
    967968        return ValidationError(
    968             message=field.error_messages['unique_for_date'],
    969             code='unique_for_date',
     969            message=field.error_messages[code],
     970            code=code,
    970971            params={
    971972                'model': self,
    972973                'model_name': six.text_type(capfirst(opts.verbose_name)),
    973974                'lookup_type': lookup_type,
    974975                'field': field_name,
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 56c25f0..2d35f68 100644
    a b class Field(RegisterLookupMixin):  
    104104        'invalid_choice': _('Value %(value)r is not a valid choice.'),
    105105        'null': _('This field cannot be null.'),
    106106        'blank': _('This field cannot be blank.'),
    107107        'unique': _('%(model_name)s with this %(field_label)s '
    108108                    'already exists.'),
    109         # Translators: The 'lookup_type' is one of 'date', 'year' or 'month'.
    110         # Eg: "Title must be unique for pub_date year"
    111         'unique_for_date': _("%(field_label)s must be unique for "
    112                              "%(date_field_label)s %(lookup_type)s."),
     109        'unique_for_date': _("%(field_label)s must be unique for the date "
     110                             "of the field %(date_field_label)s."),
     111        'unique_for_year': _("%(field_label)s must be unique for the year "
     112                            "of the field %(date_field_label)s."),
     113        'unique_for_month': _("%(field_label)s must be unique for the month "
     114                              "of the field %(date_field_label)s."),
    113115    }
    114116    class_lookups = default_lookups.copy()
    115117
    116118    # Generic field type description, usually overridden by subclasses
    117119    def _description(self):
  • tests/model_forms/tests.py

    diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
    index 783a031..21f7e75 100644
    a b class UniqueTest(TestCase):  
    758758        p = Post.objects.create(title="Django 1.0 is released",
    759759            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
    760760        form = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
    761761        self.assertFalse(form.is_valid())
    762762        self.assertEqual(len(form.errors), 1)
    763         self.assertEqual(form.errors['title'], ['Title must be unique for Posted date.'])
     763        self.assertEqual(form.errors['title'], ['Title must be unique for the date of the field Posted.'])
    764764        form = PostForm({'title': "Work on Django 1.1 begins", 'posted': '2008-09-03'})
    765765        self.assertTrue(form.is_valid())
    766766        form = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-04'})
    767767        self.assertTrue(form.is_valid())
    768768        form = PostForm({'slug': "Django 1.0", 'posted': '2008-01-01'})
    769769        self.assertFalse(form.is_valid())
    770770        self.assertEqual(len(form.errors), 1)
    771         self.assertEqual(form.errors['slug'], ['Slug must be unique for Posted year.'])
     771        self.assertEqual(form.errors['slug'], ['Slug must be unique for the year of the field Posted.'])
    772772        form = PostForm({'subtitle': "Finally", 'posted': '2008-09-30'})
    773773        self.assertFalse(form.is_valid())
    774         self.assertEqual(form.errors['subtitle'], ['Subtitle must be unique for Posted month.'])
     774        self.assertEqual(form.errors['subtitle'], ['Subtitle must be unique for the month of the field Posted.'])
    775775        form = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
    776776            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
    777777        self.assertTrue(form.is_valid())
    778778        form = PostForm({'title': "Django 1.0 is released"})
    779779        self.assertFalse(form.is_valid())
    class UniqueTest(TestCase):  
    808808        p = Post.objects.create(title="Django 1.0 is released",
    809809            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
    810810        form = DerivedPostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
    811811        self.assertFalse(form.is_valid())
    812812        self.assertEqual(len(form.errors), 1)
    813         self.assertEqual(form.errors['title'], ['Title must be unique for Posted date.'])
     813        self.assertEqual(form.errors['title'], ['Title must be unique for the date of the field Posted.'])
    814814        form = DerivedPostForm({'title': "Work on Django 1.1 begins", 'posted': '2008-09-03'})
    815815        self.assertTrue(form.is_valid())
    816816        form = DerivedPostForm({'title': "Django 1.0 is released", 'posted': '2008-09-04'})
    817817        self.assertTrue(form.is_valid())
    818818        form = DerivedPostForm({'slug': "Django 1.0", 'posted': '2008-01-01'})
    819819        self.assertFalse(form.is_valid())
    820820        self.assertEqual(len(form.errors), 1)
    821         self.assertEqual(form.errors['slug'], ['Slug must be unique for Posted year.'])
     821        self.assertEqual(form.errors['slug'], ['Slug must be unique for the year of the field Posted.'])
    822822        form = DerivedPostForm({'subtitle': "Finally", 'posted': '2008-09-30'})
    823823        self.assertFalse(form.is_valid())
    824         self.assertEqual(form.errors['subtitle'], ['Subtitle must be unique for Posted month.'])
     824        self.assertEqual(form.errors['subtitle'], ['Subtitle must be unique for the month of the field Posted.'])
    825825        form = DerivedPostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
    826826            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
    827827        self.assertTrue(form.is_valid())
    828828
    829829    def test_unique_for_date_with_nullable_date(self):
  • tests/validation/test_unique.py

    diff --git a/tests/validation/test_unique.py b/tests/validation/test_unique.py
    index 76c7ec1..5e60981 100644
    a b class PerformUniqueChecksTest(TestCase):  
    116116            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
    117117
    118118        p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
    119119        with self.assertRaises(ValidationError) as cm:
    120120            p.full_clean()
    121         self.assertEqual(cm.exception.message_dict, {'title': ['Title must be unique for Posted date.']})
     121        self.assertEqual(cm.exception.message_dict, {'title': ['Title must be unique for the date of the field Posted.']})
    122122
    123123        # Should work without errors
    124124        p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
    125125        p.full_clean()
    126126
    class PerformUniqueChecksTest(TestCase):  
    129129        p.full_clean()
    130130
    131131        p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
    132132        with self.assertRaises(ValidationError) as cm:
    133133            p.full_clean()
    134         self.assertEqual(cm.exception.message_dict, {'slug': ['Slug must be unique for Posted year.']})
     134        self.assertEqual(cm.exception.message_dict, {'slug': ['Slug must be unique for the year of the field Posted.']})
    135135
    136136        p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
    137137        with self.assertRaises(ValidationError) as cm:
    138138            p.full_clean()
    139         self.assertEqual(cm.exception.message_dict, {'subtitle': ['Subtitle must be unique for Posted month.']})
     139        self.assertEqual(cm.exception.message_dict, {'subtitle': ['Subtitle must be unique for the month of the field Posted.']})
    140140
    141141        p = Post(title="Django 1.0 is released")
    142142        with self.assertRaises(ValidationError) as cm:
    143143            p.full_clean()
    144144        self.assertEqual(cm.exception.message_dict, {'posted': ['This field cannot be null.']})
Back to Top