<s> from __future__ import absolute_import , unicode_literals <EOL> from django . conf import settings <EOL> from django . contrib import admin <EOL> from django . contrib . admin . sites import AdminSite <EOL> from django . contrib . contenttypes . generic import ( <EOL> generic_inlineformset_factory , GenericTabularInline ) <EOL> from django . forms . formsets import DEFAULT_MAX_NUM <EOL> from django . forms . models import ModelForm <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from . admin import MediaInline , MediaPermanentInline <EOL> from . models import ( Episode , EpisodeExtra , EpisodeMaxNum , Media , <EOL> EpisodePermanent , Category ) <EOL> @ override_settings ( PASSWORD_HASHERS = ( '<STR_LIT>' , ) ) <EOL> class GenericAdminViewTest ( TestCase ) : <EOL> urls = "<STR_LIT>" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . original_template_debug = settings . TEMPLATE_DEBUG <EOL> settings . TEMPLATE_DEBUG = True <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> e = Episode . objects . create ( name = '<STR_LIT>' ) <EOL> self . episode_pk = e . pk <EOL> m = Media ( content_object = e , url = '<STR_LIT>' ) <EOL> m . save ( ) <EOL> self . mp3_media_pk = m . pk <EOL> m = Media ( content_object = e , url = '<STR_LIT>' ) <EOL> m . save ( ) <EOL> self . png_media_pk = m . pk <EOL> def tearDown ( self ) : <EOL> self . client . logout ( ) <EOL> settings . TEMPLATE_DEBUG = self . original_template_debug <EOL> def testBasicAddGet ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def testBasicEditGet ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' % self . episode_pk ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def testBasicAddPost ( self ) : <EOL> """<STR_LIT>""" <EOL> post_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:1>" , <EOL> "<STR_LIT>" : "<STR_LIT:0>" , <EOL> "<STR_LIT>" : "<STR_LIT:0>" , <EOL> } <EOL> response = self . client . post ( '<STR_LIT>' , post_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def testBasicEditPost ( self ) : <EOL> """<STR_LIT>""" <EOL> post_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:3>" , <EOL> "<STR_LIT>" : "<STR_LIT:2>" , <EOL> "<STR_LIT>" : "<STR_LIT:0>" , <EOL> "<STR_LIT>" : "<STR_LIT>" % self . mp3_media_pk , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" % self . png_media_pk , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> url = '<STR_LIT>' % self . episode_pk <EOL> response = self . client . post ( url , post_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def testGenericInlineFormset ( self ) : <EOL> EpisodeMediaFormSet = generic_inlineformset_factory ( Media , can_delete = False , exclude = [ '<STR_LIT:description>' , '<STR_LIT>' ] , extra = <NUM_LIT:3> ) <EOL> e = Episode . objects . get ( name = '<STR_LIT>' ) <EOL> formset = EpisodeMediaFormSet ( instance = e ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:5> ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:0> ] . as_p ( ) , '<STR_LIT>' % self . mp3_media_pk ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:1> ] . as_p ( ) , '<STR_LIT>' % self . png_media_pk ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:2> ] . as_p ( ) , '<STR_LIT>' ) <EOL> formset = EpisodeMediaFormSet ( instance = e , queryset = Media . objects . order_by ( '<STR_LIT:url>' ) ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:5> ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:0> ] . as_p ( ) , '<STR_LIT>' % self . png_media_pk ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:1> ] . as_p ( ) , '<STR_LIT>' % self . mp3_media_pk ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:2> ] . as_p ( ) , '<STR_LIT>' ) <EOL> formset = EpisodeMediaFormSet ( instance = e , queryset = Media . objects . filter ( url__endswith = "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:4> ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:0> ] . as_p ( ) , '<STR_LIT>' % self . png_media_pk ) <EOL> self . assertHTMLEqual ( formset . forms [ <NUM_LIT:1> ] . as_p ( ) , '<STR_LIT>' ) <EOL> def testGenericInlineFormsetFactory ( self ) : <EOL> inline_formset = generic_inlineformset_factory ( Media , <EOL> exclude = ( '<STR_LIT:url>' , ) ) <EOL> e = Episode . objects . get ( name = '<STR_LIT>' ) <EOL> formset = inline_formset ( instance = e ) <EOL> self . assertTrue ( formset . get_queryset ( ) . ordered ) <EOL> @ override_settings ( PASSWORD_HASHERS = ( '<STR_LIT>' , ) ) <EOL> class GenericInlineAdminParametersTest ( TestCase ) : <EOL> urls = "<STR_LIT>" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> self . client . logout ( ) <EOL> def _create_object ( self , model ) : <EOL> """<STR_LIT>""" <EOL> e = model . objects . create ( name = '<STR_LIT>' ) <EOL> Media . objects . create ( content_object = e , url = '<STR_LIT>' ) <EOL> return e <EOL> def testNoParam ( self ) : <EOL> """<STR_LIT>""" <EOL> e = self . _create_object ( Episode ) <EOL> response = self . client . get ( '<STR_LIT>' % e . pk ) <EOL> formset = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . formset <EOL> self . assertEqual ( formset . total_form_count ( ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( formset . initial_form_count ( ) , <NUM_LIT:1> ) <EOL> def testExtraParam ( self ) : <EOL> """<STR_LIT>""" <EOL> e = self . _create_object ( EpisodeExtra ) <EOL> response = self . client . get ( '<STR_LIT>' % e . pk ) <EOL> formset = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . formset <EOL> self . assertEqual ( formset . total_form_count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( formset . initial_form_count ( ) , <NUM_LIT:1> ) <EOL> def testMaxNumParam ( self ) : <EOL> """<STR_LIT>""" <EOL> e = self . _create_object ( EpisodeMaxNum ) <EOL> inline_form_data = '<STR_LIT>' <EOL> response = self . client . get ( '<STR_LIT>' % e . pk ) <EOL> formset = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . formset <EOL> self . assertEqual ( formset . total_form_count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( formset . initial_form_count ( ) , <NUM_LIT:1> ) <EOL> @ override_settings ( PASSWORD_HASHERS = ( '<STR_LIT>' , ) ) <EOL> class GenericInlineAdminWithUniqueTogetherTest ( TestCase ) : <EOL> urls = "<STR_LIT>" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> self . client . logout ( ) <EOL> def testAdd ( self ) : <EOL> category_id = Category . objects . create ( name = '<STR_LIT>' ) . pk <EOL> post_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:1>" , <EOL> "<STR_LIT>" : "<STR_LIT:0>" , <EOL> "<STR_LIT>" : "<STR_LIT:0>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:%s>" % category_id , <EOL> } <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = self . client . post ( '<STR_LIT>' , post_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> class NoInlineDeletionTest ( TestCase ) : <EOL> urls = "<STR_LIT>" <EOL> def test_no_deletion ( self ) : <EOL> fake_site = object ( ) <EOL> inline = MediaPermanentInline ( EpisodePermanent , fake_site ) <EOL> fake_request = object ( ) <EOL> formset = inline . get_formset ( fake_request ) <EOL> self . assertFalse ( formset . can_delete ) <EOL> class MockRequest ( object ) : <EOL> pass <EOL> class MockSuperUser ( object ) : <EOL> def has_perm ( self , perm ) : <EOL> return True <EOL> request = MockRequest ( ) <EOL> request . user = MockSuperUser ( ) <EOL> class GenericInlineModelAdminTest ( TestCase ) : <EOL> urls = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . site = AdminSite ( ) <EOL> def test_get_formset_kwargs ( self ) : <EOL> media_inline = MediaInline ( Media , AdminSite ( ) ) <EOL> formset = media_inline . get_formset ( request ) <EOL> self . assertEqual ( formset . max_num , DEFAULT_MAX_NUM ) <EOL> self . assertEqual ( formset . can_order , False ) <EOL> formset = media_inline . get_formset ( request , max_num = <NUM_LIT:100> , can_order = True ) <EOL> self . assertEqual ( formset . max_num , <NUM_LIT:100> ) <EOL> self . assertEqual ( formset . can_order , True ) <EOL> def test_custom_form_meta_exclude_with_readonly ( self ) : <EOL> """<STR_LIT>""" <EOL> class MediaForm ( ModelForm ) : <EOL> class Meta : <EOL> model = Media <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> class MediaInline ( GenericTabularInline ) : <EOL> readonly_fields = [ '<STR_LIT:description>' ] <EOL> form = MediaForm <EOL> model = Media <EOL> class EpisodeAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ <EOL> MediaInline <EOL> ] <EOL> ma = EpisodeAdmin ( Episode , self . site ) <EOL> self . assertEqual ( <EOL> list ( list ( ma . get_formsets ( request ) ) [ <NUM_LIT:0> ] ( ) . forms [ <NUM_LIT:0> ] . fields ) , <EOL> [ '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> def test_custom_form_meta_exclude ( self ) : <EOL> """<STR_LIT>""" <EOL> class MediaForm ( ModelForm ) : <EOL> class Meta : <EOL> model = Media <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> class MediaInline ( GenericTabularInline ) : <EOL> exclude = [ '<STR_LIT:description>' ] <EOL> form = MediaForm <EOL> model = Media <EOL> class EpisodeAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ <EOL> MediaInline <EOL> ] <EOL> ma = EpisodeAdmin ( Episode , self . site ) <EOL> self . assertEqual ( <EOL> list ( list ( ma . get_formsets ( request ) ) [ <NUM_LIT:0> ] ( ) . forms [ <NUM_LIT:0> ] . fields ) , <EOL> [ '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> class MediaInline ( GenericTabularInline ) : <EOL> form = MediaForm <EOL> model = Media <EOL> class EpisodeAdmin ( admin . ModelAdmin ) : <EOL> inlines = [ <EOL> MediaInline <EOL> ] <EOL> ma = EpisodeAdmin ( Episode , self . site ) <EOL> self . assertEqual ( <EOL> list ( list ( ma . get_formsets ( request ) ) [ <NUM_LIT:0> ] ( ) . forms [ <NUM_LIT:0> ] . fields ) , <EOL> [ '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> def test_get_fieldsets ( self ) : <EOL> class MediaForm ( ModelForm ) : <EOL> class Meta : <EOL> model = Media <EOL> fields = '<STR_LIT>' <EOL> class MediaInline ( GenericTabularInline ) : <EOL> form = MediaForm <EOL> model = Media <EOL> can_delete = False <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> return [ ( None , { '<STR_LIT>' : [ '<STR_LIT:url>' , '<STR_LIT:description>' ] } ) ] <EOL> ma = MediaInline ( Media , self . site ) <EOL> form = ma . get_formset ( None ) . form <EOL> self . assertEqual ( form . _meta . fields , [ '<STR_LIT:url>' , '<STR_LIT:description>' ] ) </s>
