Ticket #15611: django-ticket-15611.diff
File django-ticket-15611.diff, 8.8 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/helpers.py
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 0055e83..1a7fde7 100644
a b from django.contrib.admin.util import (flatten_fieldsets, lookup_field, 4 4 display_for_field, label_for_field, help_text_for_field) 5 5 from django.contrib.contenttypes.models import ContentType 6 6 from django.core.exceptions import ObjectDoesNotExist 7 from django.db import models 7 8 from django.db.models.fields.related import ManyToManyRel 8 9 from django.forms.util import flatatt 9 10 from django.template.defaultfilters import capfirst … … class AdminForm(object): 36 37 37 38 def __iter__(self): 38 39 for name, options in self.fieldsets: 39 yieldFieldset(self.form, name,40 fieldset = Fieldset(self.form, name, 40 41 readonly_fields=self.readonly_fields, 41 42 model_admin=self.model_admin, 42 43 **options 43 44 ) 45 if not fieldset.hidden: 46 yield fieldset 44 47 45 48 def first_field(self): 46 49 try: … … class Fieldset(object): 73 76 self.model_admin = model_admin 74 77 self.readonly_fields = readonly_fields 75 78 76 def _media(self): 79 @property 80 def media(self): 77 81 if 'collapse' in self.classes: 78 82 js = ['js/jquery.min.js', 'js/jquery.init.js', 'js/collapse.min.js'] 79 83 return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js]) 80 84 return forms.Media() 81 media = property(_media) 85 86 @property 87 def hidden(self): 88 # The fieldset is visible only if there is at least one visible line in it 89 for line in self: 90 return False 91 return True 82 92 83 93 def __iter__(self): 84 94 for field in self.fields: 85 yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin) 95 line = Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin) 96 if not line.hidden: 97 yield line 86 98 87 99 class Fieldline(object): 88 100 def __init__(self, form, field, readonly_fields=None, model_admin=None): … … class Fieldline(object): 96 108 readonly_fields = () 97 109 self.readonly_fields = readonly_fields 98 110 111 @property 112 def hidden(self): 113 # The line is visible only if there is at least one visible field in it 114 for field in self: 115 return False 116 return True 117 99 118 def __iter__(self): 100 119 for i, field in enumerate(self.fields): 101 120 if field in self.readonly_fields: 102 yieldAdminReadonlyField(self.form, field, is_first=(i == 0),121 field_obj = AdminReadonlyField(self.form, field, is_first=(i == 0), 103 122 model_admin=self.model_admin) 104 123 else: 105 yield AdminField(self.form, field, is_first=(i == 0)) 124 field_obj = AdminField(self.form, field, is_first=(i == 0)) 125 if not field_obj.hidden: 126 yield field_obj 106 127 107 128 def errors(self): 108 129 return mark_safe(u'\n'.join([self.form[f].errors.as_ul() for f in self.fields if f not in self.readonly_fields]).strip('\n')) … … class AdminField(object): 112 133 self.field = form[field] # A django.forms.BoundField instance 113 134 self.is_first = is_first # Whether this field is first on the line 114 135 self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput) 136 self.hidden = False 115 137 116 138 def label_tag(self): 117 139 classes = [] … … class AdminReadonlyField(object): 138 160 # For convenience, store other field-related data here too. 139 161 if callable(field): 140 162 class_name = field.__name__ != '<lambda>' and field.__name__ or '' 163 self.hidden = False 141 164 else: 142 165 class_name = field 166 if form.instance.pk is None: 167 # Creation form, field is hidden unless it has a default or isn't a real field 168 try: 169 f = form.instance._meta.get_field(field) 170 except models.FieldDoesNotExist: 171 self.hidden = False 172 else: 173 self.hidden = f.default is models.fields.NOT_PROVIDED 174 else: 175 self.hidden = False 143 176 self.field = { 144 177 'name': class_name, 145 178 'label': label, -
tests/regressiontests/admin_views/models.py
diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index b65f8a4..911c806 100644
a b class OtherStoryAdmin(admin.ModelAdmin): 758 758 list_display_links = ('title', 'id') # 'id' in list_display_links 759 759 list_editable = ('content', ) 760 760 761 class ReadonlyFieldsetTestModel(models.Model): 762 field1 = models.CharField(max_length=100) 763 field2 = models.CharField(max_length=100) 764 field3 = models.CharField(max_length=100) 765 field4 = models.CharField(max_length=100) 766 field5 = models.CharField(max_length=100) 767 field6 = models.CharField(max_length=100) 768 field7 = models.CharField(max_length=100, default=lambda:'foo') 769 field8 = models.CharField(max_length=100, default='foo') 770 771 class ReadonlyFieldsetTestAdmin(admin.ModelAdmin): 772 fieldsets = ( 773 (None, { 774 'fields': ('field1', 'field2') 775 }), 776 ('Set1', { 777 'fields': ('field3', 'field4') 778 }), 779 ('Set2', { 780 'fields': ('field5', 'field6') 781 }), 782 ('Set3', { 783 'fields': ('field7', 'field8') 784 }), 785 ) 786 readonly_fields = ('field4', 'field5', 'field6', 'field7', 'field8') 787 761 788 admin.site.register(Article, ArticleAdmin) 762 789 admin.site.register(CustomArticle, CustomArticleAdmin) 763 790 admin.site.register(Section, save_as=True, inlines=[ArticleInline]) … … admin.site.register(Paper, PaperAdmin) 800 827 admin.site.register(CoverLetter, CoverLetterAdmin) 801 828 admin.site.register(Story, StoryAdmin) 802 829 admin.site.register(OtherStory, OtherStoryAdmin) 830 admin.site.register(ReadonlyFieldsetTestModel, ReadonlyFieldsetTestAdmin) 803 831 804 832 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. 805 833 # 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 57bd87f..372ba33 100644
a b from models import (Article, BarAccount, CustomArticle, EmptyModel, 36 36 Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, 37 37 Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee, 38 38 Question, Answer, Inquisition, Actor, FoodDelivery, 39 RowLevelChangePermissionModel, Paper, CoverLetter, Story, OtherStory) 39 RowLevelChangePermissionModel, Paper, CoverLetter, Story, OtherStory, 40 ReadonlyFieldsetTestModel) 40 41 41 42 42 43 class AdminViewBasicTest(TestCase): … … class ReadonlyTest(TestCase): 2619 2620 response = self.client.get('/test_admin/admin/admin_views/post/%d/' % p.pk) 2620 2621 self.assertContains(response, "%d amount of cool" % p.pk) 2621 2622 2623 response = self.client.get('/test_admin/admin/admin_views/readonlyfieldsettestmodel/add/') 2624 self.assertContains(response, 'Field1:</label>') 2625 self.assertContains(response, 'Field2:</label>') 2626 self.assertContains(response, 'Set1') 2627 self.assertContains(response, 'Field3:</label>') 2628 self.assertNotContains(response, 'Field4:</label>') 2629 self.assertNotContains(response, 'Set2') 2630 self.assertNotContains(response, 'Field5:</label>') 2631 self.assertNotContains(response, 'Field6:</label>') 2632 self.assertContains(response, 'Set3') 2633 self.assertContains(response, 'Field7:</label>') 2634 self.assertContains(response, 'Field8:</label>') 2635 2636 obj = ReadonlyFieldsetTestModel.objects.create(field1='a', field2='b', field3='c', 2637 field4='d', field5='e', field6='f') 2638 response = self.client.get('/test_admin/admin/admin_views/readonlyfieldsettestmodel/%d/' % obj.pk) 2639 self.assertContains(response, 'Field1:</label>') 2640 self.assertContains(response, 'Field2:</label>') 2641 self.assertContains(response, 'Set1') 2642 self.assertContains(response, 'Field3:</label>') 2643 self.assertContains(response, 'Field4:</label>') 2644 self.assertContains(response, 'Set2') 2645 self.assertContains(response, 'Field5:</label>') 2646 self.assertContains(response, 'Field6:</label>') 2647 self.assertContains(response, 'Set3') 2648 self.assertContains(response, 'Field7:</label>') 2649 self.assertContains(response, 'Field8:</label>') 2650 2622 2651 def test_readonly_post(self): 2623 2652 data = { 2624 2653 "title": "Django Got Readonly Fields",