<s> from __future__ import absolute_import <EOL> from django import forms <EOL> from django . contrib import admin <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test import TestCase <EOL> from . models import Song , Book , Album , TwoAlbumFKAndAnE , State , City <EOL> class SongForm ( forms . ModelForm ) : <EOL> pass <EOL> class ValidFields ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT:title>' ] <EOL> class ValidFormFieldsets ( admin . ModelAdmin ) : <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> class ExtraFieldForm ( SongForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:50> ) <EOL> return ExtraFieldForm <EOL> fieldsets = ( <EOL> ( None , { <EOL> '<STR_LIT>' : ( '<STR_LIT:name>' , ) , <EOL> } ) , <EOL> ) <EOL> class ValidationTestCase ( TestCase ) : <EOL> def test_readonly_and_editable ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = [ "<STR_LIT>" ] <EOL> fieldsets = [ <EOL> ( None , { <EOL> "<STR_LIT>" : [ "<STR_LIT:title>" , "<STR_LIT>" ] , <EOL> } ) , <EOL> ] <EOL> SongAdmin . validate ( Song ) <EOL> def test_custom_modelforms_with_fields_fieldsets ( self ) : <EOL> """<STR_LIT>""" <EOL> ValidFields . validate ( Song ) <EOL> def test_custom_get_form_with_fieldsets ( self ) : <EOL> """<STR_LIT>""" <EOL> ValidFormFieldsets . validate ( Song ) <EOL> def test_exclude_values ( self ) : <EOL> """<STR_LIT>""" <EOL> class ExcludedFields1 ( admin . ModelAdmin ) : <EOL> exclude = ( '<STR_LIT:foo>' ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> ExcludedFields1 . validate , <EOL> Book ) <EOL> def test_exclude_duplicate_values ( self ) : <EOL> class ExcludedFields2 ( admin . ModelAdmin ) : <EOL> exclude = ( '<STR_LIT:name>' , '<STR_LIT:name>' ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> ExcludedFields2 . validate , <EOL> Book ) <EOL> def test_exclude_in_inline ( self ) : <EOL> class ExcludedFieldsInline ( admin . TabularInline ) : <EOL> model = Song <EOL> exclude = ( '<STR_LIT:foo>' ) <EOL> class ExcludedFieldsAlbumAdmin ( admin . ModelAdmin ) : <EOL> model = Album <EOL> inlines = [ ExcludedFieldsInline ] <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> ExcludedFieldsAlbumAdmin . validate , <EOL> Album ) <EOL> def test_exclude_inline_model_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> class SongInline ( admin . StackedInline ) : <EOL> model = Song <EOL> exclude = [ '<STR_LIT>' ] <EOL> class AlbumAdmin ( admin . ModelAdmin ) : <EOL> model = Album <EOL> inlines = [ SongInline ] <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> AlbumAdmin . validate , <EOL> Album ) <EOL> def test_app_label_in_admin_validation ( self ) : <EOL> """<STR_LIT>""" <EOL> class RawIdNonexistingAdmin ( admin . ModelAdmin ) : <EOL> raw_id_fields = ( '<STR_LIT>' , ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> RawIdNonexistingAdmin . validate , <EOL> Album ) <EOL> def test_fk_exclusion ( self ) : <EOL> """<STR_LIT>""" <EOL> class TwoAlbumFKAndAnEInline ( admin . TabularInline ) : <EOL> model = TwoAlbumFKAndAnE <EOL> exclude = ( "<STR_LIT:e>" , ) <EOL> fk_name = "<STR_LIT>" <EOL> class MyAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ TwoAlbumFKAndAnEInline ] <EOL> MyAdmin . validate ( Album ) <EOL> def test_inline_self_validation ( self ) : <EOL> class TwoAlbumFKAndAnEInline ( admin . TabularInline ) : <EOL> model = TwoAlbumFKAndAnE <EOL> class MyAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ TwoAlbumFKAndAnEInline ] <EOL> self . assertRaisesMessage ( Exception , <EOL> "<STR_LIT>" , <EOL> MyAdmin . validate , Album ) <EOL> def test_inline_with_specified ( self ) : <EOL> class TwoAlbumFKAndAnEInline ( admin . TabularInline ) : <EOL> model = TwoAlbumFKAndAnE <EOL> fk_name = "<STR_LIT>" <EOL> class MyAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ TwoAlbumFKAndAnEInline ] <EOL> MyAdmin . validate ( Album ) <EOL> def test_readonly ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT:title>" , ) <EOL> SongAdmin . validate ( Song ) <EOL> def test_readonly_on_method ( self ) : <EOL> def my_function ( obj ) : <EOL> pass <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( my_function , ) <EOL> SongAdmin . validate ( Song ) <EOL> def test_readonly_on_modeladmin ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT>" , ) <EOL> def readonly_method_on_modeladmin ( self , obj ) : <EOL> pass <EOL> SongAdmin . validate ( Song ) <EOL> def test_readonly_method_on_model ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT>" , ) <EOL> SongAdmin . validate ( Song ) <EOL> def test_nonexistant_field ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT:title>" , "<STR_LIT>" ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> SongAdmin . validate , <EOL> Song ) <EOL> def test_nonexistant_field_on_inline ( self ) : <EOL> class CityInline ( admin . TabularInline ) : <EOL> model = City <EOL> readonly_fields = [ '<STR_LIT>' ] <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> CityInline . validate , <EOL> City ) <EOL> def test_extra ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> def awesome_song ( self , instance ) : <EOL> if instance . title == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> SongAdmin . validate ( Song ) <EOL> def test_readonly_lambda ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( lambda obj : "<STR_LIT:test>" , ) <EOL> SongAdmin . validate ( Song ) <EOL> def test_graceful_m2m_fail ( self ) : <EOL> """<STR_LIT>""" <EOL> class BookAdmin ( admin . ModelAdmin ) : <EOL> fields = [ '<STR_LIT>' ] <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> BookAdmin . validate , <EOL> Book ) <EOL> def test_cannot_include_through ( self ) : <EOL> class FieldsetBookAdmin ( admin . ModelAdmin ) : <EOL> fieldsets = ( <EOL> ( '<STR_LIT>' , { '<STR_LIT>' : ( '<STR_LIT:name>' , ) } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT>' : ( '<STR_LIT>' , ) } ) , <EOL> ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> FieldsetBookAdmin . validate , <EOL> Book ) <EOL> def test_nested_fields ( self ) : <EOL> class NestedFieldsAdmin ( admin . ModelAdmin ) : <EOL> fields = ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> NestedFieldsAdmin . validate ( Book ) <EOL> def test_nested_fieldsets ( self ) : <EOL> class NestedFieldsetAdmin ( admin . ModelAdmin ) : <EOL> fieldsets = ( <EOL> ( '<STR_LIT>' , { '<STR_LIT>' : ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT>' ) ) } ) , <EOL> ) <EOL> NestedFieldsetAdmin . validate ( Book ) <EOL> def test_explicit_through_override ( self ) : <EOL> """<STR_LIT>""" <EOL> class AuthorsInline ( admin . TabularInline ) : <EOL> model = Book . authors . through <EOL> class BookAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ AuthorsInline ] <EOL> BookAdmin . validate ( Book ) <EOL> def test_non_model_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> class SongForm ( forms . ModelForm ) : <EOL> extra_data = forms . CharField ( ) <EOL> class FieldsOnFormOnlyAdmin ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT:title>' , '<STR_LIT>' ] <EOL> FieldsOnFormOnlyAdmin . validate ( Song ) <EOL> def test_non_model_first_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class SongForm ( forms . ModelForm ) : <EOL> extra_data = forms . CharField ( ) <EOL> class Meta : <EOL> model = Song <EOL> fields = '<STR_LIT>' <EOL> class FieldsOnFormOnlyAdmin ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:title>' ] <EOL> FieldsOnFormOnlyAdmin . validate ( Song ) </s>
