<s> from __future__ import unicode_literals <EOL> from django . contrib import admin <EOL> from django . contrib . admin . options import ModelAdmin <EOL> from django . contrib . auth . models import User <EOL> from django . test import RequestFactory , TestCase <EOL> from . models import ( <EOL> Band , DynOrderingBandAdmin , Song , SongInlineDefaultOrdering , <EOL> SongInlineNewOrdering , <EOL> ) <EOL> class MockRequest ( object ) : <EOL> pass <EOL> class MockSuperUser ( object ) : <EOL> def has_perm ( self , perm ) : <EOL> return True <EOL> def has_module_perms ( self , module ) : <EOL> return True <EOL> request = MockRequest ( ) <EOL> request . user = MockSuperUser ( ) <EOL> site = admin . AdminSite ( ) <EOL> class TestAdminOrdering ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . request_factory = RequestFactory ( ) <EOL> Band . objects . bulk_create ( [ <EOL> Band ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:3> ) , <EOL> Band ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:1> ) , <EOL> Band ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:2> ) , <EOL> ] ) <EOL> def test_default_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> ma = ModelAdmin ( Band , site ) <EOL> names = [ b . name for b in ma . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> def test_specified_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> class BandAdmin ( ModelAdmin ) : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> ma = BandAdmin ( Band , site ) <EOL> names = [ b . name for b in ma . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> def test_dynamic_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> super_user = User . objects . create ( username = '<STR_LIT>' , is_superuser = True ) <EOL> other_user = User . objects . create ( username = '<STR_LIT>' ) <EOL> request = self . request_factory . get ( '<STR_LIT:/>' ) <EOL> request . user = super_user <EOL> ma = DynOrderingBandAdmin ( Band , site ) <EOL> names = [ b . name for b in ma . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> request . user = other_user <EOL> names = [ b . name for b in ma . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> class TestInlineModelAdminOrdering ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . band = Band . objects . create ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:3> ) <EOL> Song . objects . bulk_create ( [ <EOL> Song ( band = self . band , name = '<STR_LIT>' , duration = <NUM_LIT> ) , <EOL> Song ( band = self . band , name = '<STR_LIT>' , duration = <NUM_LIT> ) , <EOL> Song ( band = self . band , name = '<STR_LIT>' , duration = <NUM_LIT> ) , <EOL> ] ) <EOL> def test_default_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> inline = SongInlineDefaultOrdering ( self . band , site ) <EOL> names = [ s . name for s in inline . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> def test_specified_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> inline = SongInlineNewOrdering ( self . band , site ) <EOL> names = [ s . name for s in inline . get_queryset ( request ) ] <EOL> self . assertListEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , names ) <EOL> class TestRelatedFieldsAdminOrdering ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . b1 = Band . objects . create ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:1> ) <EOL> self . b2 = Band . objects . create ( name = '<STR_LIT>' , bio = '<STR_LIT>' , rank = <NUM_LIT:5> ) <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> pass <EOL> site . register ( Song , SongAdmin ) <EOL> def tearDown ( self ) : <EOL> site . unregister ( Song ) <EOL> if Band in site . _registry : <EOL> site . unregister ( Band ) <EOL> def check_ordering_of_field_choices ( self , correct_ordering ) : <EOL> fk_field = site . _registry [ Song ] . formfield_for_foreignkey ( Song . band . field , request = None ) <EOL> m2m_field = site . _registry [ Song ] . formfield_for_manytomany ( Song . other_interpreters . field , request = None ) <EOL> self . assertListEqual ( list ( fk_field . queryset ) , correct_ordering ) <EOL> self . assertListEqual ( list ( m2m_field . queryset ) , correct_ordering ) <EOL> def test_no_admin_fallback_to_model_ordering ( self ) : <EOL> self . check_ordering_of_field_choices ( [ self . b2 , self . b1 ] ) <EOL> def test_admin_with_no_ordering_fallback_to_model_ordering ( self ) : <EOL> class NoOrderingBandAdmin ( admin . ModelAdmin ) : <EOL> pass <EOL> site . register ( Band , NoOrderingBandAdmin ) <EOL> self . check_ordering_of_field_choices ( [ self . b2 , self . b1 ] ) <EOL> def test_admin_ordering_beats_model_ordering ( self ) : <EOL> class StaticOrderingBandAdmin ( admin . ModelAdmin ) : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> site . register ( Band , StaticOrderingBandAdmin ) <EOL> self . check_ordering_of_field_choices ( [ self . b1 , self . b2 ] ) <EOL> def test_custom_queryset_still_wins ( self ) : <EOL> """<STR_LIT>""" <EOL> class SongAdmin ( admin . ModelAdmin ) : <EOL> def formfield_for_foreignkey ( self , db_field , request , ** kwargs ) : <EOL> if db_field . name == '<STR_LIT>' : <EOL> kwargs [ "<STR_LIT>" ] = Band . objects . filter ( rank__gt = <NUM_LIT:2> ) <EOL> return super ( SongAdmin , self ) . formfield_for_foreignkey ( db_field , request , ** kwargs ) <EOL> def formfield_for_manytomany ( self , db_field , request , ** kwargs ) : <EOL> if db_field . name == '<STR_LIT>' : <EOL> kwargs [ "<STR_LIT>" ] = Band . objects . filter ( rank__gt = <NUM_LIT:2> ) <EOL> return super ( SongAdmin , self ) . formfield_for_foreignkey ( db_field , request , ** kwargs ) <EOL> class StaticOrderingBandAdmin ( admin . ModelAdmin ) : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> site . unregister ( Song ) <EOL> site . register ( Song , SongAdmin ) <EOL> site . register ( Band , StaticOrderingBandAdmin ) <EOL> self . check_ordering_of_field_choices ( [ self . b2 ] ) </s>
