Code

Ticket #3217: invalid_admin_options.diff

File invalid_admin_options.diff, 12.4 KB (added by Robert Myers <myer0052@…>, 7 years ago)

invalid admin options test

Line 
1Index: tests/modeltests/invalid_admin_options/__init__.py
2===================================================================
3--- tests/modeltests/invalid_admin_options/__init__.py 
4+++ tests/modeltests/invalid_admin_options/__init__.py 
5Index: tests/modeltests/invalid_admin_options/models.py
6===================================================================
7--- tests/modeltests/invalid_admin_options/models.py   
8+++ tests/modeltests/invalid_admin_options/models.py   
9@@ -0,0 +1,337 @@
10+"""
11+67. Admin options
12+
13+Test invalid and valid admin options to make sure that
14+model validation is working properly.
15+"""
16+
17+from django.db import models
18+model_errors = ""
19+
20+# TODO: Invalid admin options should not cause a metaclass error
21+##This should fail gracefully but is causing a metaclass error
22+#class BadAdminOption(models.Model):
23+#    "Test nonexistent admin option"
24+#    name = models.CharField(maxlength=30)
25+#   
26+#    class Admin:
27+#        nonexistent = 'option'
28+#
29+#model_errors += """invalid_admin_options.badadminoption: "admin" attribute, if given, must be set to a models.AdminOptions() instance.
30+#"""
31+       
32+class ListDisplayBadOne(models.Model):
33+    "Test list_display, list_display must be a list or tuple"
34+    first_name = models.CharField(maxlength=30)
35+
36+    class Admin:
37+        list_display = 'first_name'
38+
39+model_errors += """invalid_admin_options.listdisplaybadone: "admin.list_display", if given, must be set to a list or tuple.
40+"""
41+
42+class ListDisplayBadTwo(models.Model):
43+    "Test list_display, list_display items must be attributes, methods or properties."
44+    first_name = models.CharField(maxlength=30)
45+
46+    class Admin:
47+        list_display = ['first_name','nonexistent']
48+
49+model_errors += """invalid_admin_options.listdisplaybadtwo: "admin.list_display" refers to 'nonexistent', which isn't an attribute, method or property.
50+"""       
51+class ListDisplayBadThree(models.Model):
52+    "Test list_display, list_display items can not be a ManyToManyField."
53+    first_name = models.CharField(maxlength=30)
54+    nick_names = models.ManyToManyField('ListDisplayGood')
55+
56+    class Admin:
57+        list_display = ['first_name','nick_names']
58+       
59+model_errors += """invalid_admin_options.listdisplaybadthree: "admin.list_display" doesn't support ManyToManyFields ('nick_names').
60+"""
61+     
62+class ListDisplayGood(models.Model):
63+    "Test list_display, Admin list_display can be a attribute, method or property."
64+    first_name = models.CharField(maxlength=30)
65+   
66+    def _last_name(self):
67+        return self.first_name
68+    last_name = property(_last_name)
69+   
70+    def full_name(self):
71+        return "%s %s" % (self.first_name, self.last_name)
72+
73+    class Admin:
74+        list_display = ['first_name','last_name','full_name']
75+       
76+class ListDisplayLinksBadOne(models.Model):
77+    "Test list_display_links, item must be included in list_display."
78+    first_name = models.CharField(maxlength=30)
79+    last_name = models.CharField(maxlength=30)
80+   
81+    class Admin:
82+        list_display = ['last_name']
83+        list_display_links = ['first_name']
84+
85+model_errors += """invalid_admin_options.listdisplaylinksbadone: "admin.list_display_links" refers to 'first_name', which is not defined in "admin.list_display".
86+"""
87+
88+class ListDisplayLinksBadTwo(models.Model):
89+    "Test list_display_links, must be a list or tuple."
90+    first_name = models.CharField(maxlength=30)
91+    last_name = models.CharField(maxlength=30)
92+   
93+    class Admin:
94+        list_display = ['first_name','last_name']
95+        list_display_links = 'last_name'   
96+
97+model_errors += """invalid_admin_options.listdisplaylinksbadtwo: "admin.list_display_links", if given, must be set to a list or tuple.
98+"""
99+
100+# TODO: Fix list_display_links validation or remove the check for list_display
101+## This is failing but the validation which should fail is not.
102+#class ListDisplayLinksBadThree(models.Model):
103+#    "Test list_display_links, must define list_display to use list_display_links."
104+#    first_name = models.CharField(maxlength=30)
105+#    last_name = models.CharField(maxlength=30)
106+#   
107+#    class Admin:
108+#        list_display_links = ('first_name',)
109+#
110+#model_errors += """invalid_admin_options.listdisplaylinksbadthree: "admin.list_display" must be defined for "admin.list_display_links" to be used.
111+#"""
112+       
113+class ListDisplayLinksGood(models.Model):
114+    "Test list_display_links, Admin list_display_list can be a attribute, method or property."
115+    first_name = models.CharField(maxlength=30)
116+   
117+    def _last_name(self):
118+        return self.first_name
119+    last_name = property(_last_name)
120+   
121+    def full_name(self):
122+        return "%s %s" % (self.first_name, self.last_name)
123+
124+    class Admin:
125+        list_display = ['first_name','last_name','full_name']
126+        list_display_links = ['first_name','last_name','full_name']
127+
128+class ListFilterBadOne(models.Model):
129+    "Test list_filter, must be a list or tuple."
130+    first_name = models.CharField(maxlength=30)
131+   
132+    class Admin:
133+        list_filter = 'first_name'     
134+
135+model_errors += """invalid_admin_options.listfilterbadone: "admin.list_filter", if given, must be set to a list or tuple.
136+"""
137+
138+class ListFilterBadTwo(models.Model):
139+    "Test list_filter, must be a field not a property or method."
140+    first_name = models.CharField(maxlength=30)
141+   
142+    def _last_name(self):
143+        return self.first_name
144+    last_name = property(_last_name)
145+   
146+    def full_name(self):
147+        return "%s %s" % (self.first_name, self.last_name)
148+
149+    class Admin:
150+        list_filter = ['first_name','last_name','full_name']
151+
152+model_errors += """invalid_admin_options.listfilterbadtwo: "admin.list_filter" refers to 'last_name', which isn't a field.
153+invalid_admin_options.listfilterbadtwo: "admin.list_filter" refers to 'full_name', which isn't a field.
154+"""
155+
156+class DateHierarchyBadOne(models.Model):
157+    "Test date_hierarchy, must be a date or datetime field."
158+    first_name = models.CharField(maxlength=30)
159+    birth_day = models.DateField()
160+   
161+    class Admin:
162+        date_hierarchy = 'first_name'
163+       
164+# TODO: Date Hierarchy needs to check if field is a date/datetime field.
165+#model_errors += """invalid_admin_options.datehierarchybadone: "admin.date_hierarchy" refers to 'first_name', which isn't a date field or datetime field.
166+#"""
167+
168+class DateHierarchyBadTwo(models.Model):
169+    "Test date_hieracrhy, must be a field."
170+    first_name = models.CharField(maxlength=30)
171+    birth_day = models.DateField()
172+   
173+    class Admin:
174+        date_hierarchy = 'nonexistent'         
175+
176+model_errors += """invalid_admin_options.datehierarchybadtwo: "admin.date_hierarchy" refers to 'nonexistent', which isn't a field.
177+"""
178+
179+class DateHierarchyGood(models.Model):
180+    "Test date_hieracrhy, must be a field."
181+    first_name = models.CharField(maxlength=30)
182+    birth_day = models.DateField()
183+   
184+    class Admin:
185+        date_hierarchy = 'birth_day'
186+     
187+class SearchFieldsBadOne(models.Model):
188+    "Test search_fields, must be a list or tuple."
189+    first_name = models.CharField(maxlength=30)
190+   
191+    class Admin:
192+        search_fields = ('nonexistent')         
193+
194+# TODO: Add search_fields validation
195+#model_errors += """invalid_admin_options.seacrhfieldsbadone: "admin.search_fields", if given, must be set to a list or tuple.
196+#"""
197+     
198+class SearchFieldsBadTwo(models.Model):
199+    "Test search_fields, must be a field."
200+    first_name = models.CharField(maxlength=30)
201+
202+    def _last_name(self):
203+        return self.first_name
204+    last_name = property(_last_name)
205+   
206+    class Admin:
207+        search_fields = ['first_name','last_name']         
208+
209+# TODO: Add search_fields validation
210+#model_errors += """invalid_admin_options.seacrhfieldsbadone: "admin.search_fields" refers to 'last_name', which isn't a field.
211+#"""
212+
213+class SearchFieldsGood(models.Model):
214+    "Test search_fields, must be a list or tuple."
215+    first_name = models.CharField(maxlength=30)
216+    last_name = models.CharField(maxlength=30)
217+   
218+    class Admin:
219+        search_fields = ['first_name','last_name']
220+
221+
222+class JsBadOne(models.Model):
223+    "Test js, must be a list or tuple"
224+    name = models.CharField(maxlength=30)
225+   
226+    class Admin:
227+        js = 'test.js'
228+       
229+# TODO: Add a js validator
230+#model_errors += """invalid_admin_options.jsbadone: "admin.js", if given, must be set to a list or tuple.
231+#"""
232+
233+class SaveAsBad(models.Model):
234+    "Test save_as, should be True or False"
235+    name = models.CharField(maxlength=30)
236+   
237+    class Admin:
238+        save_as = 'not True or False'
239+
240+# TODO: Add a save_as validator.       
241+#model_errors += """invalid_admin_options.saveasbad: "admin.save_as", if given, must be set to True or False.
242+#"""
243+
244+class SaveOnTopBad(models.Model):
245+    "Test save_on_top, should be True or False"
246+    name = models.CharField(maxlength=30)
247+   
248+    class Admin:
249+        save_on_top = 'not True or False'
250+
251+# TODO: Add a save_on_top validator.       
252+#model_errors += """invalid_admin_options.saveontopbad: "admin.save_on_top", if given, must be set to True or False.
253+#"""
254+
255+class ListSelectRelatedBad(models.Model):
256+    "Test list_select_related, should be True or False"
257+    name = models.CharField(maxlength=30)
258+   
259+    class Admin:
260+        list_select_related = 'not True or False'
261+
262+# TODO: Add a list_select_related validator.       
263+#model_errors += """invalid_admin_options.listselectrelatebad: "admin.list_select_related", if given, must be set to True or False.
264+#"""
265+
266+class ListPerPageBad(models.Model):
267+    "Test list_per_page, should be a positive integer value."
268+    name = models.CharField(maxlength=30)
269+   
270+    class Admin:
271+        list_per_page = 89.3
272+
273+# TODO: Add a list_per_page validator.       
274+#model_errors += """invalid_admin_options.listperpagebad: "admin.list_per_page", if given, must be a positive integer.
275+#"""
276+
277+class FieldsBadOne(models.Model):
278+    "Test fields, should be a tuple"
279+    first_name = models.CharField(maxlength=30)
280+    last_name = models.CharField(maxlength=30)
281+   
282+    class Admin:
283+        fields = 'not a tuple'
284+
285+# TODO: Add a fields validator.       
286+#model_errors += """invalid_admin_options.fieldsbadone: "admin.fields", if given, must be a tuple.
287+#"""
288+
289+class FieldsBadTwo(models.Model):
290+    """Test fields, 'fields' dict option is required."""
291+    first_name = models.CharField(maxlength=30)
292+    last_name = models.CharField(maxlength=30)
293+   
294+    class Admin:
295+        fields = ('Name', {'description': 'this fieldset needs fields'})
296+       
297+# TODO: Add a fields validator.       
298+#model_errors += """invalid_admin_options.fieldsbadtwo: "admin.fields" each fieldset must include a 'fields' dict.
299+#"""
300+
301+class FieldsBadThree(models.Model):
302+    """Test fields, 'classes' and 'description' are the only allowable extra dict options."""
303+    first_name = models.CharField(maxlength=30)
304+    last_name = models.CharField(maxlength=30)
305+   
306+    class Admin:
307+        fields = ('Name', {'fields': ('first_name','last_name'),'badoption': 'verybadoption'})
308+
309+# TODO: Add a fields validator.       
310+#model_errors += """invalid_admin_options.fieldsbadthree: "admin.fields" fieldset options must be either 'classes' or 'description'.
311+#"""
312+
313+class FieldsGood(models.Model):
314+    "Test fields, working example"
315+    first_name = models.CharField(maxlength=30)
316+    last_name = models.CharField(maxlength=30)
317+    birth_day = models.DateField()
318+   
319+    class Admin:
320+        fields = (
321+                  ('Name', {'fields': ('first_name','last_name'),'classes': 'collapse'}),
322+                  (None, {'fields': ('birth_day',),'description': 'enter your b-day'})
323+                  )
324+                 
325+class OrderingBad(models.Model):
326+    "Test ordering, must be a field."
327+    first_name = models.CharField(maxlength=30)
328+    last_name = models.CharField(maxlength=30)
329+   
330+    class Admin:
331+        ordering = 'nonexistent'
332+
333+# TODO: Add a ordering validator.       
334+#model_errors += """invalid_admin_options.orderingbad: "admin.ordering" refers to 'nonexistent', which isn't a field.
335+#"""
336+
337+## TODO: Add a manager validator, this should fail gracefully.
338+#class ManagerBad(models.Model):
339+#    "Test manager, must be a manager object."
340+#    first_name = models.CharField(maxlength=30)
341+#   
342+#    class Admin:
343+#        manager = 'nonexistent'
344+#       
345+#model_errors += """invalid_admin_options.managerbad: "admin.manager" refers to 'nonexistent', which isn't a Manager().
346+#"""
347\ No newline at end of file