summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2011-01-20 00:33:32 +0000
committerCarl Meyer <carl@oddbird.net>2011-01-20 00:33:32 +0000
commit93a4d46184bba069fc6a9aa5517802b2488032ac (patch)
tree48f820d5a0fbd42164b140ed2c9f9927abdafffe /tests
parente01cb0740496f12323260205687111a54a91e10f (diff)
Fixed #14672 - Added admin handling for on_delete=PROTECT. Thanks to jtiai for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/admin_views/models.py12
-rw-r--r--tests/regressiontests/admin_views/tests.py42
2 files changed, 49 insertions, 5 deletions
diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
index 0a27964ea3..e64f713b7e 100644
--- a/tests/regressiontests/admin_views/models.py
+++ b/tests/regressiontests/admin_views/models.py
@@ -626,6 +626,16 @@ class WorkHourAdmin(admin.ModelAdmin):
list_display = ('datum', 'employee')
list_filter = ('employee',)
+class Question(models.Model):
+ question = models.CharField(max_length=20)
+
+class Answer(models.Model):
+ question = models.ForeignKey(Question, on_delete=models.PROTECT)
+ answer = models.CharField(max_length=20)
+
+ def __unicode__(self):
+ return self.answer
+
admin.site.register(Article, ArticleAdmin)
admin.site.register(CustomArticle, CustomArticleAdmin)
admin.site.register(Section, save_as=True, inlines=[ArticleInline])
@@ -674,3 +684,5 @@ admin.site.register(ChapterXtra1, ChapterXtra1Admin)
admin.site.register(Pizza, PizzaAdmin)
admin.site.register(Topping)
admin.site.register(Album, AlbumAdmin)
+admin.site.register(Question)
+admin.site.register(Answer)
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
index 9b881291de..ecc2316760 100644
--- a/tests/regressiontests/admin_views/tests.py
+++ b/tests/regressiontests/admin_views/tests.py
@@ -29,11 +29,12 @@ from django.utils.translation import activate, deactivate
from django.utils import unittest
# local test models
-from models import Article, BarAccount, CustomArticle, EmptyModel, \
- FooAccount, Gallery, ModelWithStringPrimaryKey, \
- Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
- Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
- Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee
+from models import (Article, BarAccount, CustomArticle, EmptyModel,
+ FooAccount, Gallery, ModelWithStringPrimaryKey,
+ Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
+ Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
+ Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
+ Question, Answer)
class AdminViewBasicTest(TestCase):
@@ -884,6 +885,15 @@ class AdminViewDeletedObjectsTest(TestCase):
self.assertContains(response, "your account doesn't have permission to delete the following types of objects")
self.assertContains(response, "<li>plot details</li>")
+ def test_protected(self):
+ q = Question.objects.create(question="Why?")
+ a1 = Answer.objects.create(question=q, answer="Because.")
+ a2 = Answer.objects.create(question=q, answer="Yes.")
+
+ response = self.client.get("/test_admin/admin/admin_views/question/%s/delete/" % quote(q.pk))
+ self.assertContains(response, "would require deleting the following protected related objects")
+ self.assertContains(response, '<li>Answer: <a href="/test_admin/admin/admin_views/answer/%s/">Because.</a></li>' % a1.pk)
+ self.assertContains(response, '<li>Answer: <a href="/test_admin/admin/admin_views/answer/%s/">Yes.</a></li>' % a2.pk)
def test_not_registered(self):
should_contain = """<li>Secret hideout: underground bunker"""
@@ -1627,6 +1637,28 @@ class AdminActionsTest(TestCase):
response = self.client.post('/test_admin/admin/admin_views/subscriber/', delete_confirmation_data)
self.assertEqual(Subscriber.objects.count(), 0)
+ def test_model_admin_default_delete_action_protected(self):
+ """
+ Tests the default delete action defined as a ModelAdmin method in the
+ case where some related objects are protected from deletion.
+ """
+ q1 = Question.objects.create(question="Why?")
+ a1 = Answer.objects.create(question=q1, answer="Because.")
+ a2 = Answer.objects.create(question=q1, answer="Yes.")
+ q2 = Question.objects.create(question="Wherefore?")
+
+ action_data = {
+ ACTION_CHECKBOX_NAME: [q1.pk, q2.pk],
+ 'action' : 'delete_selected',
+ 'index': 0,
+ }
+
+ response = self.client.post("/test_admin/admin/admin_views/question/", action_data)
+
+ self.assertContains(response, "would require deleting the following protected related objects")
+ self.assertContains(response, '<li>Answer: <a href="/test_admin/admin/admin_views/answer/%s/">Because.</a></li>' % a1.pk)
+ self.assertContains(response, '<li>Answer: <a href="/test_admin/admin/admin_views/answer/%s/">Yes.</a></li>' % a2.pk)
+
def test_custom_function_mail_action(self):
"Tests a custom action defined in a function"
action_data = {