<s> from django . contrib import admin <EOL> from django . contrib . admin . options import IncorrectLookupParameters <EOL> from django . contrib . admin . views . main import ChangeList <EOL> from django . core . paginator import Paginator <EOL> from django . template import Context , Template <EOL> from django . test import TransactionTestCase <EOL> from models import ( Child , Parent , Genre , Band , Musician , Group , Quartet , <EOL> Membership , ChordsMusician , ChordsBand , Invitation ) <EOL> class ChangeListTests ( TransactionTestCase ) : <EOL> def test_select_related_preserved ( self ) : <EOL> """<STR_LIT>""" <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> cl = ChangeList ( MockRequest ( ) , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> self . assertEqual ( cl . query_set . query . select_related , { '<STR_LIT>' : { '<STR_LIT:name>' : { } } } ) <EOL> def test_result_list_empty_changelist_value ( self ) : <EOL> """<STR_LIT>""" <EOL> new_child = Child . objects . create ( name = '<STR_LIT:name>' , parent = None ) <EOL> request = MockRequest ( ) <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> cl . formset = None <EOL> template = Template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT>' : cl } ) <EOL> table_output = template . render ( context ) <EOL> row_html = '<STR_LIT>' % ( new_child . id , new_child . id ) <EOL> self . assertFalse ( table_output . find ( row_html ) == - <NUM_LIT:1> , <EOL> '<STR_LIT>' % table_output ) <EOL> def test_result_list_html ( self ) : <EOL> """<STR_LIT>""" <EOL> new_parent = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> new_child = Child . objects . create ( name = '<STR_LIT:name>' , parent = new_parent ) <EOL> request = MockRequest ( ) <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> cl . formset = None <EOL> template = Template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT>' : cl } ) <EOL> table_output = template . render ( context ) <EOL> row_html = '<STR_LIT>' % ( new_child . id , new_child . id ) <EOL> self . assertFalse ( table_output . find ( row_html ) == - <NUM_LIT:1> , <EOL> '<STR_LIT>' % table_output ) <EOL> def test_result_list_editable_html ( self ) : <EOL> """<STR_LIT>""" <EOL> new_parent = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> new_child = Child . objects . create ( name = '<STR_LIT:name>' , parent = new_parent ) <EOL> request = MockRequest ( ) <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> m . list_display = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> m . list_display_links = [ '<STR_LIT:id>' ] <EOL> m . list_editable = [ '<STR_LIT:name>' ] <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> FormSet = m . get_changelist_formset ( request ) <EOL> cl . formset = FormSet ( queryset = cl . result_list ) <EOL> template = Template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT>' : cl } ) <EOL> table_output = template . render ( context ) <EOL> hiddenfields_div = '<STR_LIT>' % new_child . id <EOL> self . assertFalse ( table_output . find ( hiddenfields_div ) == - <NUM_LIT:1> , <EOL> '<STR_LIT>' % table_output ) <EOL> editable_name_field = '<STR_LIT>' <EOL> self . assertFalse ( '<STR_LIT>' % editable_name_field == - <NUM_LIT:1> , <EOL> '<STR_LIT>' % table_output ) <EOL> def test_result_list_editable ( self ) : <EOL> """<STR_LIT>""" <EOL> new_parent = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:200> ) : <EOL> new_child = Child . objects . create ( name = '<STR_LIT>' % i , parent = new_parent ) <EOL> request = MockRequest ( ) <EOL> request . GET [ '<STR_LIT:p>' ] = - <NUM_LIT:1> <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> m . list_display = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> m . list_display_links = [ '<STR_LIT:id>' ] <EOL> m . list_editable = [ '<STR_LIT:name>' ] <EOL> self . assertRaises ( IncorrectLookupParameters , lambda : ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) ) <EOL> def test_custom_paginator ( self ) : <EOL> new_parent = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:200> ) : <EOL> new_child = Child . objects . create ( name = '<STR_LIT>' % i , parent = new_parent ) <EOL> request = MockRequest ( ) <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> m . list_display = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> m . list_display_links = [ '<STR_LIT:id>' ] <EOL> m . list_editable = [ '<STR_LIT:name>' ] <EOL> m . paginator = CustomPaginator <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> cl . get_results ( request ) <EOL> self . assertIsInstance ( cl . paginator , CustomPaginator ) <EOL> def test_distinct_for_m2m_in_list_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> blues = Genre . objects . create ( name = '<STR_LIT>' ) <EOL> band = Band . objects . create ( name = '<STR_LIT>' , nr_of_members = <NUM_LIT:11> ) <EOL> band . genres . add ( blues ) <EOL> band . genres . add ( blues ) <EOL> m = BandAdmin ( Band , admin . site ) <EOL> cl = ChangeList ( MockFilteredRequestA ( blues . pk ) , Band , m . list_display , <EOL> m . list_display_links , m . list_filter , m . date_hierarchy , <EOL> m . search_fields , m . list_select_related , m . list_per_page , <EOL> m . list_editable , m ) <EOL> cl . get_results ( MockFilteredRequestA ( blues . pk ) ) <EOL> self . assertEqual ( cl . result_count , <NUM_LIT:1> ) <EOL> def test_distinct_for_through_m2m_in_list_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> lead = Musician . objects . create ( name = '<STR_LIT>' ) <EOL> band = Group . objects . create ( name = '<STR_LIT>' ) <EOL> Membership . objects . create ( group = band , music = lead , role = '<STR_LIT>' ) <EOL> Membership . objects . create ( group = band , music = lead , role = '<STR_LIT>' ) <EOL> m = GroupAdmin ( Group , admin . site ) <EOL> cl = ChangeList ( MockFilteredRequestB ( lead . pk ) , Group , m . list_display , <EOL> m . list_display_links , m . list_filter , m . date_hierarchy , <EOL> m . search_fields , m . list_select_related , m . list_per_page , <EOL> m . list_editable , m ) <EOL> cl . get_results ( MockFilteredRequestB ( lead . pk ) ) <EOL> self . assertEqual ( cl . result_count , <NUM_LIT:1> ) <EOL> def test_distinct_for_inherited_m2m_in_list_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> lead = Musician . objects . create ( name = '<STR_LIT>' ) <EOL> four = Quartet . objects . create ( name = '<STR_LIT>' ) <EOL> Membership . objects . create ( group = four , music = lead , role = '<STR_LIT>' ) <EOL> Membership . objects . create ( group = four , music = lead , role = '<STR_LIT>' ) <EOL> m = QuartetAdmin ( Quartet , admin . site ) <EOL> cl = ChangeList ( MockFilteredRequestB ( lead . pk ) , Quartet , m . list_display , <EOL> m . list_display_links , m . list_filter , m . date_hierarchy , <EOL> m . search_fields , m . list_select_related , m . list_per_page , <EOL> m . list_editable , m ) <EOL> cl . get_results ( MockFilteredRequestB ( lead . pk ) ) <EOL> self . assertEqual ( cl . result_count , <NUM_LIT:1> ) <EOL> def test_distinct_for_m2m_to_inherited_in_list_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> lead = ChordsMusician . objects . create ( name = '<STR_LIT>' ) <EOL> three = ChordsBand . objects . create ( name = '<STR_LIT>' ) <EOL> Invitation . objects . create ( band = three , player = lead , instrument = '<STR_LIT>' ) <EOL> Invitation . objects . create ( band = three , player = lead , instrument = '<STR_LIT>' ) <EOL> m = ChordsBandAdmin ( ChordsBand , admin . site ) <EOL> cl = ChangeList ( MockFilteredRequestB ( lead . pk ) , ChordsBand , m . list_display , <EOL> m . list_display_links , m . list_filter , m . date_hierarchy , <EOL> m . search_fields , m . list_select_related , m . list_per_page , <EOL> m . list_editable , m ) <EOL> cl . get_results ( MockFilteredRequestB ( lead . pk ) ) <EOL> self . assertEqual ( cl . result_count , <NUM_LIT:1> ) <EOL> def test_pagination ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> Child . objects . create ( name = '<STR_LIT>' % i , parent = parent ) <EOL> Child . objects . create ( name = '<STR_LIT>' % i , parent = parent ) <EOL> request = MockRequest ( ) <EOL> m = ChildAdmin ( Child , admin . site ) <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> self . assertEqual ( cl . query_set . count ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( cl . paginator . count , <NUM_LIT> ) <EOL> self . assertEqual ( cl . paginator . page_range , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> m = FilteredChildAdmin ( Child , admin . site ) <EOL> cl = ChangeList ( request , Child , m . list_display , m . list_display_links , <EOL> m . list_filter , m . date_hierarchy , m . search_fields , <EOL> m . list_select_related , m . list_per_page , m . list_editable , m ) <EOL> self . assertEqual ( cl . query_set . count ( ) , <NUM_LIT:30> ) <EOL> self . assertEqual ( cl . paginator . count , <NUM_LIT:30> ) <EOL> self . assertEqual ( cl . paginator . page_range , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> class ChildAdmin ( admin . ModelAdmin ) : <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> list_per_page = <NUM_LIT:10> <EOL> def queryset ( self , request ) : <EOL> return super ( ChildAdmin , self ) . queryset ( request ) . select_related ( "<STR_LIT>" ) <EOL> class FilteredChildAdmin ( admin . ModelAdmin ) : <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> list_per_page = <NUM_LIT:10> <EOL> def queryset ( self , request ) : <EOL> return super ( FilteredChildAdmin , self ) . queryset ( request ) . filter ( <EOL> name__contains = '<STR_LIT>' ) <EOL> class MockRequest ( object ) : <EOL> GET = { } <EOL> class CustomPaginator ( Paginator ) : <EOL> def __init__ ( self , queryset , page_size , orphans = <NUM_LIT:0> , allow_empty_first_page = True ) : <EOL> super ( CustomPaginator , self ) . __init__ ( queryset , <NUM_LIT:5> , orphans = <NUM_LIT:2> , <EOL> allow_empty_first_page = allow_empty_first_page ) <EOL> class BandAdmin ( admin . ModelAdmin ) : <EOL> list_filter = [ '<STR_LIT>' ] <EOL> class GroupAdmin ( admin . ModelAdmin ) : <EOL> list_filter = [ '<STR_LIT>' ] <EOL> class QuartetAdmin ( admin . ModelAdmin ) : <EOL> list_filter = [ '<STR_LIT>' ] <EOL> class ChordsBandAdmin ( admin . ModelAdmin ) : <EOL> list_filter = [ '<STR_LIT>' ] <EOL> class MockFilteredRequestA ( object ) : <EOL> def __init__ ( self , pk ) : <EOL> self . GET = { '<STR_LIT>' : pk } <EOL> class MockFilteredRequestB ( object ) : <EOL> def __init__ ( self , pk ) : <EOL> self . GET = { '<STR_LIT>' : pk } </s>
