Ticket #18681: django-bug-get_fieldsets.shar

File django-bug-get_fieldsets.shar, 3.2 KB (added by Melvyn Sopacua, 12 years ago)

Shell archive with test case

Line 
1# This is a shell archive. Save it in a file, remove anything before
2# this line, and then unpack it by entering "sh file". Note, it may
3# create directories; files and directories will be owned by you and
4# have default permissions.
5#
6# This archive contains:
7#
8# bookmarks
9# bookmarks/__init__.py
10# bookmarks/admin.py
11# bookmarks/models.py
12# bookmarks/tests.py
13#
14echo c - bookmarks
15mkdir -p bookmarks > /dev/null 2>&1
16echo x - bookmarks/__init__.py
17sed 's/^X//' >bookmarks/__init__.py << '20a1c7151f244b5c6bc388b74c2b6612'
1820a1c7151f244b5c6bc388b74c2b6612
19echo x - bookmarks/admin.py
20sed 's/^X//' >bookmarks/admin.py << '947cf57c8598ad9386f34efacea96cd9'
21X# vim: ts=4 sw=4 et tw=78
22Xfrom django.contrib import admin
23Xfrom bookmarks.models import Bookmark
24X
25Xclass BookmarkAdmin(admin.ModelAdmin) :
26X def get_fieldsets(self, request, obj=None) :
27X if obj is None :
28X return [ (None, { 'fields': ('url',) }) ]
29X else :
30X return [ (None, { 'fields': ('url', 'added_by')}) ]
31X
32Xclass BookmarkAdminHack(BookmarkAdmin) :
33X def get_form(self, request, obj=None, **kwargs) :
34X self.fieldsets = self.get_fieldsets(request, obj)
35X return super(BookmarkAdminHack, self).get_form(request, obj, **kwargs)
36947cf57c8598ad9386f34efacea96cd9
37echo x - bookmarks/models.py
38sed 's/^X//' >bookmarks/models.py << '43ade1173a2a323a2b7de45cc78547dd'
39Xfrom django.db import models
40Xfrom django.contrib.auth.models import User
41X
42Xclass Bookmark(models.Model) :
43X url = models.URLField(max_length=64)
44X added_by = models.ForeignKey(User)
45X
4643ade1173a2a323a2b7de45cc78547dd
47echo x - bookmarks/tests.py
48sed 's/^X//' >bookmarks/tests.py << '7e892ef10c1a1bfd433c456bb538fb06'
49X# vim: ts=4 sw=4 et tw=78
50Xfrom django.test import TestCase
51Xfrom django.http import HttpRequest
52Xfrom bookmarks.admin import BookmarkAdmin, BookmarkAdminHack
53Xfrom bookmarks.models import Bookmark
54Xfrom django.contrib import admin
55Xfrom django.contrib.auth.models import User
56X
57Xclass SimpleTest(TestCase):
58X def do_admin_get_fields_test(self, adm) :
59X request = HttpRequest()
60X request.user = User(username='admin')
61X request.POST = { 'url': u'http://example.org' }
62X request.FILES = {}
63X request.method = 'POST'
64X # This is the same logic used as in ModelAdmin.add_view()
65X ModelForm = adm.get_form(request)
66X form = ModelForm(request.POST, request.FILES)
67X # This is form.is_valid() but split out to show errors more clearly
68X self.assertEqual(form.is_bound, True)
69X form.full_clean()
70X self.assertEqual(form.errors, {})
71X
72X def test_admin_get_fields(self) :
73X try :
74X admin.site.unregister(Bookmark)
75X except :
76X pass
77X admin.site.register(Bookmark, BookmarkAdmin)
78X adm = BookmarkAdmin(Bookmark, admin.site)
79X self.do_admin_get_fields_test(adm)
80X
81X def test_admin_get_fields_hack(self) :
82X try :
83X admin.site.unregister(Bookmark)
84X except :
85X pass
86X admin.site.register(Bookmark, BookmarkAdminHack)
87X adm = BookmarkAdminHack(Bookmark, admin.site)
88X self.do_admin_get_fields_test(adm)
897e892ef10c1a1bfd433c456bb538fb06
90exit
91
Back to Top