def test_create_complex_page_with_m2m(self): simple_page_1 = SimplePage( title='Test Page', int_field=42, ) simple_page_2 = SimplePage( title='Another Test Page', int_field=27, ) home = Page.objects.get(slug='home') home.add_child(instance=simple_page_1) home.add_child(instance=simple_page_2) csv_data = StringIO( 'id,parent,title,m2m\r\n' f',2,Page with M2M,"{simple_page_1.pk},{simple_page_2.pk}"\r\n') successes, errors = import_pages(csv_data, M2MPage) self.assertEqual(successes, ['Created page Page with M2M with id 5']) self.assertEqual(errors, []) page = M2MPage.objects.latest('id') self.assertEqual(page.get_parent().id, home.pk) self.assertEqual(page.title, 'Page with M2M') self.assertIsNone(page.fk) self.assertQuerysetEqual( page.m2m.order_by('id'), ['<SimplePage: Test Page>', '<SimplePage: Another Test Page>']) # page is in draft because live was not specified self.assertIs(page.live, False)
def test_export_only_published(self): page1 = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page', live=True ) page2 = SimplePage( bool_field=True, char_field='almendras', int_field=27, rich_text_field='', title='Another test page', slug='custom-slug', seo_title='SEO title', search_description='SEO description', live=False ) home = Page.objects.get(pk=2) home.add_child(instance=page1) home.add_child(instance=page2) row_iter = export_pages(home, fieldnames=['id'], only_published=True) self.assertIteratorEquals( row_iter, [ 'id\r\n', '2\r\n', '3\r\n', ] )
def test_export_foreign_key_and_m2m(self): simple_page_1 = SimplePage( title='Simple page 1', int_field=27 ) simple_page_2 = SimplePage( title='Simple page 2', int_field=42 ) home = Page.objects.get(slug='home') home.add_child(instance=simple_page_1) home.add_child(instance=simple_page_2) m2m_page = M2MPage( title='Test page', fk=simple_page_1 ) home.add_child(instance=m2m_page) m2m_page.m2m.add(simple_page_1, simple_page_2) ct = ContentType.objects.get_for_model(M2MPage) row_iter = export_pages(home, content_type=ct, fieldnames=['id', 'content_type', 'fk', 'm2m']) self.assertIteratorEquals( row_iter, [ 'id,content_type,fk,m2m\r\n', f'5,tests.m2mpage,{simple_page_1.pk},"{simple_page_1.pk},{simple_page_2.pk}"\r\n', ] )
class TestFrontendViews(TestCase): fixtures = ['test.json'] def setUp(self): self.admin_user = User.objects.create_superuser( username='******', email='*****@*****.**', password='******') self.homepage = Page.objects.get(url_path='/home/').specific self.page = SimplePage(title="Simple page original", slug="simple-page") self.homepage.add_child(instance=self.page) self.page.title = "Simple page submitted" submitted_revision = self.page.save_revision() self.review = Review.objects.create(page_revision=submitted_revision, submitter=self.admin_user) self.reviewer = Reviewer.objects.create( review=self.review, user=User.objects.get(username='******')) self.page.title = "Simple page with draft edit" self.page.save_revision() def test_view_token_must_match(self): response = self.client.get('/review/view/%d/xxxxx/' % self.reviewer.id) self.assertEqual(response.status_code, 403) def test_view(self): response = self.client.get( '/review/view/%d/%s/' % (self.reviewer.id, self.reviewer.view_token)) self.assertEqual(response.status_code, 200) self.assertContains(response, "<h1>Simple page submitted</h1>") self.assertContains(response, "var app = new annotator.App();") self.assertContains(response, "app.include(annotatorExt.viewerModeUi);") def test_response_token_must_match(self): response = self.client.get('/review/respond/%d/xxxxx/' % self.reviewer.id) self.assertEqual(response.status_code, 403) def test_respond_view(self): response = self.client.get( '/review/respond/%d/%s/' % (self.reviewer.id, self.reviewer.response_token)) self.assertEqual(response.status_code, 200) self.assertContains(response, "<h1>Simple page submitted</h1>") self.assertContains(response, "var app = new annotator.App();") self.assertContains(response, "app.include(annotator.ui.main,") def test_live_page_has_no_annotator_js(self): response = self.client.get('/simple-page/') self.assertEqual(response.status_code, 200) self.assertContains(response, "<h1>Simple page original</h1>") self.assertNotContains(response, "var app = new annotator.App();")
def setUp(self): self.admin_user = User.objects.create_superuser( username='******', email='*****@*****.**', password='******' ) self.homepage = Page.objects.get(url_path='/home/').specific self.page = SimplePage(title="Simple page original", slug="simple-page") self.homepage.add_child(instance=self.page) self.page.title = "Simple page submitted" submitted_revision = self.page.save_revision() self.review = Review.objects.create(page_revision=submitted_revision, submitter=self.admin_user) self.reviewer = Reviewer.objects.create(review=self.review, user=User.objects.get(username='******')) self.page.title = "Simple page with draft edit" self.page.save_revision()
def test_import_post(self): # create a page to update it simple_page = SimplePage(title='Existing Page', int_field=79) home = Page.objects.get(slug='home') home.add_child(instance=simple_page) csv_data = ( 'id,content_type,parent,title,int_field\r\n' ',tests.simplepage,2,New Page,42\r\n' '3,tests.simplepage,2,Updated Existing Page,27\r\n' ',tests.simplepage,,Orphan,\r\n' ',tests.m2mpage,2,Wrong Type,\r\n' ) csv_file = SimpleUploadedFile("test_import_post.csv", csv_data.encode('utf-8'), content_type="text/csv") data = { 'file': csv_file, 'page_type': ContentType.objects.get_for_model(SimplePage).pk, } response = self.client.post('/admin/csv/import-from-file/', data) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Created page New Page') self.assertContains(response, 'Updated page Updated Existing Page') self.assertContains(response, 'Errors processing row number 3: <li>parent: Need a parent when creating a new page</li>\n<li>int_field: This field is required.</li>') self.assertContains(response, 'Errors processing row number 4: <li>int_field: This field is required.</li>\n<li>content_type: Expected tests.simplepage, was tests.m2mpage</li>') # successes were committed even if there were errors self.assertQuerysetEqual( SimplePage.objects.all(), ['<SimplePage: Updated Existing Page>', '<SimplePage: New Page>'] )
def test_update_simple_page(self): page = SimplePage( title='Test Page', slug='test-page', seo_title='Not good SEO', search_description='This won\'t change', bool_field=True, char_field='Blag', int_field=42, rich_text_field='<p>Read Understand by Ted Chiang</p>') home = Page.objects.get(slug='home') home.add_child(instance=page) csv_data = StringIO( 'id,title,seo_title,int_field,rich_text_field\r\n' f'3,A New Title,Now THIS is SEO,27,<p>Anything by Ted Chiang really</p>\r\n' ) successes, errors = import_pages(csv_data, SimplePage) self.assertEqual(successes, ['Updated page A New Title with id 3'], f'Errors: {errors}') self.assertEqual(errors, []) page = SimplePage.objects.get(pk=3) # listed fields have been updated self.assertEqual(page.title, 'A New Title') self.assertEqual(page.seo_title, 'Now THIS is SEO') self.assertEqual(page.int_field, 27) self.assertEqual(page.rich_text_field, '<p>Anything by Ted Chiang really</p>') # unlisted fields are not changed self.assertEqual(page.slug, 'test-page') self.assertEqual(page.search_description, 'This won\'t change') self.assertIs(page.bool_field, True) self.assertEqual(page.char_field, 'Blag')
def test_export_no_content_type_exports_basic_fields(self): page1 = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page', live=True ) home = Page.objects.get(pk=2) home.add_child(instance=page1) page2 = M2MPage( title='M2M page', fk=page1, live=True ) home.add_child(instance=page2) row_iter = export_pages(home) self.assertIteratorEquals( row_iter, [ 'id,content_type,parent,title,slug,full_url,live,draft_title,expire_at,expired,first_published_at,go_live_at,has_unpublished_changes,last_published_at,latest_revision_created_at,live_revision,locked,owner,search_description,seo_title,show_in_menus\r\n', '2,wagtailcore.page,1,Welcome to your new Wagtail site!,home,http://localhost/,True,Welcome to your new Wagtail site!,,False,,,False,,,,False,,,,False\r\n', '3,tests.simplepage,2,Test page,test-page,http://localhost/test-page/,True,Test page,,False,,,False,,,,False,,,,False\r\n', '4,tests.m2mpage,2,M2M page,m2m-page,http://localhost/m2m-page/,True,M2M page,,False,,,False,,,,False,,,,False\r\n', ] )
def test_update_simple_page_unpublish(self): page = SimplePage(title='Test Page', slug='test-page', int_field=42, live=False) home = Page.objects.get(slug='home') home.add_child(instance=page) rev = page.save_revision() rev.publish() csv_data = StringIO('id,live\r\n' f'3,False\r\n') successes, errors = import_pages(csv_data, SimplePage) self.assertEqual(successes, ['Updated page Test Page with id 3'], f'Errors: {errors}') self.assertEqual(errors, []) page = SimplePage.objects.get(pk=page.pk) # page has been unpublished self.assertIs(page.live, False)
def test_export_with_content_type_exports_all_fields(self): page1 = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page', live=True, first_published_at=pytz.datetime.datetime(2019, 1, 1, 1, 1, 1, tzinfo=pytz.UTC) ) page2 = SimplePage( bool_field=True, char_field='almendras', int_field=27, rich_text_field='', title='Another test page', slug='custom-slug', seo_title='SEO title', search_description='SEO description', live=True, first_published_at=pytz.datetime.datetime(2019, 2, 2, 2, 2, 2, tzinfo=pytz.UTC) ) home = Page.objects.get(pk=2) home.add_child(instance=page1) home.add_child(instance=page2) ct = ContentType.objects.get_for_model(SimplePage) row_iter = export_pages(home, content_type=ct) self.assertIteratorEquals( row_iter, [ 'id,content_type,parent,title,slug,full_url,live,bool_field,char_field,draft_title,expire_at,expired,first_published_at,go_live_at,has_unpublished_changes,int_field,last_published_at,latest_revision_created_at,live_revision,locked,owner,rich_text_field,search_description,seo_title,show_in_menus\r\n', '3,tests.simplepage,2,Test page,test-page,http://localhost/test-page/,True,False,char,Test page,,False,2019-01-01 01:01:01,,False,42,,,,False,,<p>Rich text</p>,,,False\r\n', '4,tests.simplepage,2,Another test page,custom-slug,http://localhost/custom-slug/,True,True,almendras,Another test page,,False,2019-02-02 02:02:02,,False,27,,,,False,,,SEO description,SEO title,False\r\n', ] )
def test_create_complex_page_with_foreign_key(self): simple_page = SimplePage( title='Test Page', int_field=42, ) home = Page.objects.get(slug='home') home.add_child(instance=simple_page) csv_data = StringIO('id,parent,title,fk\r\n' ',2,Page with FK,3\r\n') successes, errors = import_pages(csv_data, M2MPage) self.assertEqual(successes, ['Created page Page with FK with id 4']) self.assertEqual(errors, []) page = M2MPage.objects.latest('id') self.assertEqual(page.get_parent().id, home.pk) self.assertEqual(page.title, 'Page with FK') self.assertEqual(page.fk, simple_page) self.assertQuerysetEqual(page.m2m.all(), []) # page is in draft because live was not specified self.assertIs(page.live, False)
def test_post_with_page_type_only_specific_fields(self): page = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page' ) home = Page.objects.get(pk=2) home.add_child(instance=page) data = { 'fields': ['id', 'content_type', 'title', 'int_field'], 'page_type': page.content_type_id, 'root_page': page.pk, } response = self.client.post('/admin/csv/export-to-file/', data) self.assertEqual(response.status_code, 200) self.assertTrue(response.streaming) full_response = io.BytesIO(b''.join(response.streaming_content)) self.assertEqual(full_response.getvalue(), b'id,content_type,title,int_field\r\n3,tests.simplepage,Test page,42\r\n')
def test_cleaned_data_specific_fields(self): from wagtailcsvimport.forms import ExportForm page = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page' ) root = Page.objects.get(pk=1) root.add_child(instance=page) data = { 'fields': ['id', 'title', 'int_field'], 'root_page': page.pk, } form = ExportForm(data, page_model=SimplePage) form.is_valid() self.assertEqual(form.errors, {}) self.assertEqual(form.cleaned_data['root_page'], page) self.assertEqual(form.cleaned_data['only_published'], False) self.assertEqual(form.cleaned_data['fields'], ['id', 'title', 'int_field'])
def test_post_with_page_type_includes_extra_fields(self): page = SimplePage( bool_field=False, char_field='char', int_field=42, rich_text_field='<p>Rich text</p>', title='Test page' ) home = Page.objects.get(pk=2) home.add_child(instance=page) data = { 'fields': ['id', 'content_type', 'parent', 'title', 'slug', 'full_url', 'seo_title', 'search_description', 'live', 'bool_field', 'char_field', 'int_field', 'rich_text_field'], 'page_type': page.content_type_id, 'root_page': home.pk, } response = self.client.post('/admin/csv/export-to-file/', data) self.assertEqual(response.status_code, 200) self.assertTrue(response.streaming) full_response = io.BytesIO(b''.join(response.streaming_content)) self.assertEqual(full_response.getvalue(), b'id,content_type,parent,title,slug,full_url,seo_title,search_description,live,bool_field,char_field,int_field,rich_text_field\r\n3,tests.simplepage,2,Test page,test-page,http://wagtailcsvimport.test/home/test-page/,,,True,False,char,42,<p>Rich text</p>\r\n')