Ticket #3217: invalid_admin_options.diff

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

invalid admin options test

  • tests/modeltests/invalid_admin_options/__init__.py

  • tests/modeltests/invalid_admin_options/models.py

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