<s> from __future__ import absolute_import <EOL> from django import forms <EOL> from django . contrib import admin <EOL> from django . contrib . admin . validation import validate , validate_inline <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 InvalidFields ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT>' ] <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> validate ( SongAdmin , Song ) <EOL> def test_custom_modelforms_with_fields_fieldsets ( self ) : <EOL> """<STR_LIT>""" <EOL> validate ( ValidFields , Song ) <EOL> self . assertRaisesMessage ( ImproperlyConfigured , <EOL> "<STR_LIT>" , <EOL> validate , <EOL> InvalidFields , 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> validate , <EOL> ExcludedFields1 , 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> validate , <EOL> ExcludedFields2 , 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> validate , <EOL> ExcludedFieldsAlbumAdmin , 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> validate , <EOL> AlbumAdmin , 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> validate , <EOL> RawIdNonexistingAdmin , 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> validate_inline ( TwoAlbumFKAndAnEInline , None , Album ) <EOL> def test_inline_self_validation ( self ) : <EOL> class TwoAlbumFKAndAnEInline ( admin . TabularInline ) : <EOL> model = TwoAlbumFKAndAnE <EOL> self . assertRaisesMessage ( Exception , <EOL> "<STR_LIT>" , <EOL> validate_inline , <EOL> TwoAlbumFKAndAnEInline , None , Album ) <EOL> def test_inline_with_specified ( self ) : <EOL> class TwoAlbumFKAndAnEInline ( admin . TabularInline ) : <EOL> model = TwoAlbumFKAndAnE <EOL> fk_name = "<STR_LIT>" <EOL> validate_inline ( TwoAlbumFKAndAnEInline , None , Album ) <EOL> def test_readonly ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT:title>" , ) <EOL> validate ( SongAdmin , 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> validate ( SongAdmin , 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> validate ( SongAdmin , Song ) <EOL> def test_readonly_method_on_model ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( "<STR_LIT>" , ) <EOL> validate ( SongAdmin , 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> validate , <EOL> SongAdmin , 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> validate_inline , <EOL> CityInline , None , State ) <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> validate ( SongAdmin , Song ) <EOL> def test_readonly_lambda ( self ) : <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( lambda obj : "<STR_LIT:test>" , ) <EOL> validate ( SongAdmin , 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> validate , <EOL> BookAdmin , 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> validate , <EOL> FieldsetBookAdmin , Book ) <EOL> def test_nested_fields ( self ) : <EOL> class NestedFieldsAdmin ( admin . ModelAdmin ) : <EOL> fields = ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> validate ( NestedFieldsAdmin , 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> validate ( NestedFieldsetAdmin , 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> validate ( BookAdmin , Book ) <EOL> def test_non_model_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> class SongForm ( forms . ModelForm ) : <EOL> extra_data = forms . CharField ( ) <EOL> class Meta : <EOL> model = Song <EOL> class FieldsOnFormOnlyAdmin ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT:title>' , '<STR_LIT>' ] <EOL> validate ( FieldsOnFormOnlyAdmin , 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> class FieldsOnFormOnlyAdmin ( admin . ModelAdmin ) : <EOL> form = SongForm <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:title>' ] <EOL> validate ( FieldsOnFormOnlyAdmin , Song ) </s>
