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 | #
|
---|
14 | echo c - bookmarks
|
---|
15 | mkdir -p bookmarks > /dev/null 2>&1
|
---|
16 | echo x - bookmarks/__init__.py
|
---|
17 | sed 's/^X//' >bookmarks/__init__.py << '20a1c7151f244b5c6bc388b74c2b6612'
|
---|
18 | 20a1c7151f244b5c6bc388b74c2b6612
|
---|
19 | echo x - bookmarks/admin.py
|
---|
20 | sed 's/^X//' >bookmarks/admin.py << '947cf57c8598ad9386f34efacea96cd9'
|
---|
21 | X# vim: ts=4 sw=4 et tw=78
|
---|
22 | Xfrom django.contrib import admin
|
---|
23 | Xfrom bookmarks.models import Bookmark
|
---|
24 | X
|
---|
25 | Xclass BookmarkAdmin(admin.ModelAdmin) :
|
---|
26 | X def get_fieldsets(self, request, obj=None) :
|
---|
27 | X if obj is None :
|
---|
28 | X return [ (None, { 'fields': ('url',) }) ]
|
---|
29 | X else :
|
---|
30 | X return [ (None, { 'fields': ('url', 'added_by')}) ]
|
---|
31 | X
|
---|
32 | Xclass BookmarkAdminHack(BookmarkAdmin) :
|
---|
33 | X def get_form(self, request, obj=None, **kwargs) :
|
---|
34 | X self.fieldsets = self.get_fieldsets(request, obj)
|
---|
35 | X return super(BookmarkAdminHack, self).get_form(request, obj, **kwargs)
|
---|
36 | 947cf57c8598ad9386f34efacea96cd9
|
---|
37 | echo x - bookmarks/models.py
|
---|
38 | sed 's/^X//' >bookmarks/models.py << '43ade1173a2a323a2b7de45cc78547dd'
|
---|
39 | Xfrom django.db import models
|
---|
40 | Xfrom django.contrib.auth.models import User
|
---|
41 | X
|
---|
42 | Xclass Bookmark(models.Model) :
|
---|
43 | X url = models.URLField(max_length=64)
|
---|
44 | X added_by = models.ForeignKey(User)
|
---|
45 | X
|
---|
46 | 43ade1173a2a323a2b7de45cc78547dd
|
---|
47 | echo x - bookmarks/tests.py
|
---|
48 | sed 's/^X//' >bookmarks/tests.py << '7e892ef10c1a1bfd433c456bb538fb06'
|
---|
49 | X# vim: ts=4 sw=4 et tw=78
|
---|
50 | Xfrom django.test import TestCase
|
---|
51 | Xfrom django.http import HttpRequest
|
---|
52 | Xfrom bookmarks.admin import BookmarkAdmin, BookmarkAdminHack
|
---|
53 | Xfrom bookmarks.models import Bookmark
|
---|
54 | Xfrom django.contrib import admin
|
---|
55 | Xfrom django.contrib.auth.models import User
|
---|
56 | X
|
---|
57 | Xclass SimpleTest(TestCase):
|
---|
58 | X def do_admin_get_fields_test(self, adm) :
|
---|
59 | X request = HttpRequest()
|
---|
60 | X request.user = User(username='admin')
|
---|
61 | X request.POST = { 'url': u'http://example.org' }
|
---|
62 | X request.FILES = {}
|
---|
63 | X request.method = 'POST'
|
---|
64 | X # This is the same logic used as in ModelAdmin.add_view()
|
---|
65 | X ModelForm = adm.get_form(request)
|
---|
66 | X form = ModelForm(request.POST, request.FILES)
|
---|
67 | X # This is form.is_valid() but split out to show errors more clearly
|
---|
68 | X self.assertEqual(form.is_bound, True)
|
---|
69 | X form.full_clean()
|
---|
70 | X self.assertEqual(form.errors, {})
|
---|
71 | X
|
---|
72 | X def test_admin_get_fields(self) :
|
---|
73 | X try :
|
---|
74 | X admin.site.unregister(Bookmark)
|
---|
75 | X except :
|
---|
76 | X pass
|
---|
77 | X admin.site.register(Bookmark, BookmarkAdmin)
|
---|
78 | X adm = BookmarkAdmin(Bookmark, admin.site)
|
---|
79 | X self.do_admin_get_fields_test(adm)
|
---|
80 | X
|
---|
81 | X def test_admin_get_fields_hack(self) :
|
---|
82 | X try :
|
---|
83 | X admin.site.unregister(Bookmark)
|
---|
84 | X except :
|
---|
85 | X pass
|
---|
86 | X admin.site.register(Bookmark, BookmarkAdminHack)
|
---|
87 | X adm = BookmarkAdminHack(Bookmark, admin.site)
|
---|
88 | X self.do_admin_get_fields_test(adm)
|
---|
89 | 7e892ef10c1a1bfd433c456bb538fb06
|
---|
90 | exit
|
---|
91 |
|
---|