Ticket #13126: 13126_changelist_non_field_errors.2.diff

File 13126_changelist_non_field_errors.2.diff, 6.3 KB (added by julien, 4 years ago)

Slightly more robust tests

  • django/contrib/admin/templates/admin/change_list_results.html

    diff --git a/django/contrib/admin/templates/admin/change_list_results.html b/django/contrib/admin/templates/admin/change_list_results.html
    index 3e38978..29cf265 100644
    a b  
    1616</thead>
    1717<tbody>
    1818{% for result in results %}
    19 <tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr>
     19{% if result.form.non_field_errors %}
     20    <tr><td colspan="{{ result.row|length }}">{{ result.form.non_field_errors }}</td></tr>
     21{% endif %}
     22<tr class="{% cycle 'row1' 'row2' %}">{% for item in result.row %}{{ item }}{% endfor %}</tr>
    2023{% endfor %}
    2124</tbody>
    2225</table>
  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index 806be24..0e8d0b5 100644
    a b def items_for_result(cl, result, form): 
    198198def results(cl):
    199199    if cl.formset:
    200200        for res, form in zip(cl.result_list, cl.formset.forms):
    201             yield list(items_for_result(cl, res, form))
     201            yield {'row': list(items_for_result(cl, res, form)), 'form': form}
    202202    else:
    203203        for res in cl.result_list:
    204             yield list(items_for_result(cl, res, None))
     204            yield {'row': list(items_for_result(cl, res, None)), 'form': None}
    205205
    206206def result_hidden_fields(cl):
    207207    if cl.formset:
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 60319ea..c6dd9c1 100644
    a b class Answer(models.Model): 
    669669class Reservation(models.Model):
    670670    start_date = models.DateTimeField()
    671671    price = models.IntegerField()
     672
     673   
     674DRIVER_CHOICES = (
     675    (u'bill', 'Bill G'),
     676    (u'steve', 'Steve J'),
     677)
     678
     679RESTAURANT_CHOICES = (
     680    (u'indian', u'A Taste of India'),
     681    (u'thai', u'Thai Pography'),
     682    (u'pizza', u'Pizza Mama'),
     683)
     684
     685class FoodDelivery(models.Model):
     686    reference = models.CharField(max_length=100)
     687    driver = models.CharField(max_length=100, choices=DRIVER_CHOICES, blank=True)
     688    restaurant = models.CharField(max_length=100, choices=RESTAURANT_CHOICES, blank=True)
     689   
     690    class Meta:
     691        unique_together = (("driver", "restaurant"),)
    672692   
    673693admin.site.register(Article, ArticleAdmin)
    674694admin.site.register(CustomArticle, CustomArticleAdmin)
    admin.site.register(CyclicOne) 
    706726admin.site.register(CyclicTwo)
    707727admin.site.register(WorkHour, WorkHourAdmin)
    708728admin.site.register(Reservation)
     729admin.site.register(FoodDelivery, list_display=('reference', 'driver', 'restaurant'), list_editable = ('driver', 'restaurant'))
    709730
    710731# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    711732# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index acbbbfc..a30a319 100644
    a b from models import (Article, BarAccount, CustomArticle, EmptyModel, 
    3535    Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    38     Question, Answer, Inquisition, Actor)
     38    Question, Answer, Inquisition, Actor, FoodDelivery)
    3939
    4040
    4141class AdminViewBasicTest(TestCase):
    class AdminViewListEditable(TestCase): 
    14201420
    14211421        self.assertEqual(Person.objects.get(name="John Mauchly").alive, False)
    14221422
     1423    def test_non_field_errors(self):
     1424        ''' Ensure that non field errors are displayed for each of the
     1425            forms in the changelist's formset. Refs #13126.
     1426        '''
     1427        FoodDelivery.objects.create(reference='123', driver='bill', restaurant='thai')
     1428        FoodDelivery.objects.create(reference='456', driver='bill', restaurant='india')
     1429        FoodDelivery.objects.create(reference='789', driver='bill', restaurant='pizza')
     1430       
     1431        data = {
     1432            "form-TOTAL_FORMS": "3",
     1433            "form-INITIAL_FORMS": "3",
     1434            "form-MAX_NUM_FORMS": "0",
     1435
     1436            "form-0-id": "1",
     1437            "form-0-reference": "123",
     1438            "form-0-driver": "bill",
     1439            "form-0-restaurant": "thai",
     1440
     1441            # Same data as above: Forbidden because of unique_together!
     1442            "form-1-id": "2",
     1443            "form-1-reference": "456",
     1444            "form-1-driver": "bill",
     1445            "form-1-restaurant": "thai",
     1446
     1447            "form-2-id": "3",
     1448            "form-2-reference": "789",
     1449            "form-2-driver": "bill",
     1450            "form-2-restaurant": "pizza",
     1451           
     1452            "_save": "Save",
     1453        }
     1454        response = self.client.post('/test_admin/admin/admin_views/fooddelivery/', data)
     1455        self.assertContains(response, '<tr><td colspan="4"><ul class="errorlist"><li>Food delivery with this Driver and Restaurant already exists.</li></ul></td></tr>', 1)
     1456       
     1457        data = {
     1458            "form-TOTAL_FORMS": "3",
     1459            "form-INITIAL_FORMS": "3",
     1460            "form-MAX_NUM_FORMS": "0",
     1461
     1462            "form-0-id": "1",
     1463            "form-0-reference": "123",
     1464            "form-0-driver": "bill",
     1465            "form-0-restaurant": "thai",
     1466
     1467            # Same data as above: Forbidden because of unique_together!
     1468            "form-1-id": "2",
     1469            "form-1-reference": "456",
     1470            "form-1-driver": "bill",
     1471            "form-1-restaurant": "thai",
     1472
     1473            # Same data also.
     1474            "form-2-id": "3",
     1475            "form-2-reference": "789",
     1476            "form-2-driver": "bill",
     1477            "form-2-restaurant": "thai",
     1478           
     1479            "_save": "Save",
     1480        }
     1481        response = self.client.post('/test_admin/admin/admin_views/fooddelivery/', data)
     1482        self.assertContains(response, '<tr><td colspan="4"><ul class="errorlist"><li>Food delivery with this Driver and Restaurant already exists.</li></ul></td></tr>', 2)
     1483       
    14231484    def test_non_form_errors(self):
    14241485        # test if non-form errors are handled; ticket #12716
    14251486        data = {
Back to Top