def migrate_streamfield_forward(page_or_revision, streamfield_name, new_type): """ Migrate a StreamField belonging to the page or revision """ old_stream_data = get_stream_data(page_or_revision, streamfield_name) new_stream_data = [] migrated = False block_conversions = { 'image_text_25_75_group': '25-75', 'image_text_50_50_group': '50-50', } for block in old_stream_data: block_type = block['type'] if block_type in block_conversions: new_block = { 'type': new_type, 'value': image_text_group_to_info_unit_group( block['value'], block_conversions[block_type] ) } new_stream_data.append(new_block) migrated = True else: new_stream_data.append(block) if migrated: set_stream_data( page_or_revision, streamfield_name, new_stream_data )
def migrate_streamfield_forward(page_or_revision, streamfield_name): """ Migrate a StreamField belonging to the page or revision """ old_stream_data = get_stream_data(page_or_revision, streamfield_name) new_stream_data = [] migrated = False block_conversions = { 'half_width_link_blob_group': '50-50', 'third_width_link_blob_group': '33-33-33', } for block in old_stream_data: block_type = block['type'] if block_type in block_conversions: new_block = { 'type': 'info_unit_group', 'value': link_blob_group_to_info_unit_group( block['value'], block_conversions[block_type]) } new_stream_data.append(new_block) migrated = True else: new_stream_data.append(block) if migrated: set_stream_data(page_or_revision, streamfield_name, new_stream_data)
def test_get_answer_preview(self): """Check that get_answer_preview returns truncated text, no tags.""" page = self.page1 stream_data = [{ 'type': 'video_player', 'id': '402b933b', 'value': { 'video_url': 'https://www.youtube.com/embed/wcQ1a_Gg8tI' } }, { 'type': 'text', 'id': '402b933c', 'value': { 'content': ('<p><span>' 'This is more than forty words: ' 'word word word word word word word word word word ' 'word word word word word word word word word word ' 'word word word word word word word word word word ' 'word word word word word word too-many.' '</span></p>') } }] set_stream_data(page, 'answer_content', stream_data) self.assertTrue(get_answer_preview(page).endswith('word word ...'))
def test_set_stream_data_page(self): """ Test that set_stream_data correctly sets stream data for a given page and saves the page. """ new_stream_data = [{'type': 'text', 'value': 'new text'}] set_stream_data(self.page, 'body', new_stream_data) self.assertEqual(self.page.body.stream_data[0]['value'], 'new text')
def migrate_streamfield_forward(page_or_revision, streamfield_name): """ Migrate a StreamField belonging to the page or revision """ old_stream_data = get_stream_data(page_or_revision, streamfield_name) new_stream_data = [] migrated = False block_conversions = { 'half_width_link_blob_group': '50-50', 'third_width_link_blob_group': '33-33-33', } for block in old_stream_data: block_type = block['type'] if block_type in block_conversions: new_block = { 'type': 'info_unit_group', 'value': link_blob_group_to_info_unit_group( block['value'], block_conversions[block_type] ) } new_stream_data.append(new_block) migrated = True else: new_stream_data.append(block) if migrated: set_stream_data( page_or_revision, streamfield_name, new_stream_data )
def migrate_streamfield_forward(page_or_revision, streamfield_name, new_type): """ Migrate a StreamField belonging to the page or revision """ old_stream_data = get_stream_data(page_or_revision, streamfield_name) new_stream_data = [] migrated = False block_conversions = { 'image_text_25_75_group': '25-75', 'image_text_50_50_group': '50-50', } for block in old_stream_data: block_type = block['type'] if block_type in block_conversions: new_block = { 'type': new_type, 'value': image_text_group_to_info_unit_group( block['value'], block_conversions[block_type]) } new_stream_data.append(new_block) migrated = True else: new_stream_data.append(block) if migrated: set_stream_data(page_or_revision, streamfield_name, new_stream_data)
def test_answer_content_preview_word(self): """answer_content_preview returns truncated text by word count And without HTML tags. """ page = self.page1 stream_data = [ { "type": "video_player", "id": "402b933b", "value": { "video_url": "https://www.youtube.com/embed/wcQ1a_Gg8tI" }, }, { "type": "text", "id": "402b933c", "value": { "content": ( "<p><span>" "This is more than forty words: " "word word word word word word word word word word " "word word word word word word word word word word " "word word word word word word word word word word " "word word word word word word too-many." "</span></p>" ) }, }, ] set_stream_data(page, "answer_content", stream_data) self.assertTrue( page.answer_content_preview().endswith("word word ...") )
def update_sidefoot(): draft_pages = [] for page in DocumentDetailPage.objects.all(): url = page.get_url() if not page.live: continue if 'policy-compliance/enforcement/actions' not in url: continue if page.has_unpublished_changes: draft_pages.append(url) continue stream_data = get_stream_data(page, 'sidefoot') for field in stream_data: if field['type'] == 'related_metadata': field_content = field['value']['content'] for block in field_content: # Switch File number to Docker number if block['value'].get('heading', '') == 'File number': block['value']['heading'] = 'Docket number' break set_stream_data(page.specific, 'sidefoot', stream_data) if len(draft_pages) > 0: print('Skipped the following draft pages:', ' '.join(draft_pages)) else: print('No draft pages found, all valid enforcement pages updated')
def test_returns_form_block(self): page = BrowsePage(title='test', slug='test') revision = save_new_page(page) registration_form_block = atomic.conference_registration_form set_stream_data(page, 'content', [registration_form_block]) self.assertEqual(get_registration_form_from_page(revision.page_id), registration_form_block)
def test_set_stream_data_page_without_committing(self): """ Test that set_stream_data correctly sets stream data for a given page and saves the page. """ self.page.save = mock.Mock() new_stream_data = [{'type': 'text', 'value': 'new text'}] set_stream_data(self.page, 'body', new_stream_data, commit=False) self.assertEqual(self.page.save.mock_calls, [])
def check_page_content(self, page_cls, field): page = page_cls(slug='slug', title='title') publish_page(child=page) set_stream_data(page, field, [atomic.email_signup]) publish_changes(child=page) response = self.client.get('/slug/') self.assertContains(response, 'Email Sign Up')
def test_set_stream_data_revision(self): """ Test that set_stream_data correctly sets stream data for a given revision and saves the page. """ new_stream_data = [{'type': 'text', 'value': 'new text'}] set_stream_data(self.revision, 'body', new_stream_data) self.assertEqual( self.revision.as_page_object().body.stream_data[0]['value'], 'new text')
def test_returns_form_block(self): page = BrowsePage(title='test', slug='test') revision = save_new_page(page) registration_form_block = atomic.conference_registration_form set_stream_data(page, 'content', [registration_form_block]) self.assertEqual( get_registration_form_from_page(revision.page_id), registration_form_block )
def setUp(self): self.root = Page.objects.get(slug='cfgov') self.page = StreamPage(title="Test Page", slug="testpage") save_new_page(self.page, self.root) set_stream_data(self.page, 'body', [ {'type': 'text', 'value': 'some text'} ]) self.revision = self.page.save_revision() self.page.save()
def make_page_with_form(code='TEST_CODE', capacity=999): page = BrowsePage(title='test', slug='test') revision = save_new_page(page) block = atomic.conference_registration_form.copy() block['value'].update({ 'code': code, 'capacity': capacity, }) set_stream_data(page, 'content', [block]) return revision.page_id
def test_page_renders_block_safely(self): """ Test to make sure that a page with a jobs list block renders it in a safe way, meaning as raw HTML vs. as a quoted string. """ page = SublandingPage(title='title', slug='slug') save_new_page(page) set_stream_data(page, 'sidebar_breakout', [job_listing_list]) self.assertPageIncludesHtml(page, ( '><aside class="m-jobs-list" data-qa-hook="openings-section">' '.*' '</aside><' ))
def test_page_renders_block_safely(self): """ Test to make sure that a page with a jobs list block renders it in a safe way, meaning as raw HTML vs. as a quoted string. """ page = SublandingPage(title='title', slug='slug') save_new_page(page) set_stream_data(page, 'sidebar_breakout', [job_listing_list]) self.assertPageIncludesHtml( page, ('><aside class="m-jobs-list" data-qa-hook="openings-section">' '.*' '</aside><'))
def test_page_renders_block_safely(self): """ Test to make sure that a page with a jobs list block renders it in a safe way, meaning as raw HTML vs. as a quoted string. """ page = SublandingPage(title='title', slug='slug') save_new_page(page) set_stream_data(page, 'sidebar_breakout', [job_listing_list]) request = RequestFactory().get('/') rendered_html = force_str(page.serve(request).render().content) self.assertInHTML( ('<aside class="m-jobs-list" data-qa-hook="openings-section">' '<h3 class="short-desc">There are no current openings at this ' 'time.</h3></aside>'), rendered_html)
def migrate_page(page, field_name, mapper): old_stream_data = get_stream_data(page, field_name) new_stream_data = [] migrated = False for field in old_stream_data: if 'hero' == field['type']: field['value'] = mapper(field['value']) migrated = True new_stream_data.append(field) if migrated: print('migrated page {}'.format(page.slug)) set_stream_data(page, field_name, new_stream_data)
def test_page_renders_block_safely(self): """ Test to make sure that a page with a jobs list block renders it in a safe way, meaning as raw HTML vs. as a quoted string. """ page = SublandingPage(title='title', slug='slug') save_new_page(page) set_stream_data(page, 'sidebar_breakout', [job_listing_list]) request = RequestFactory().get('/') request.user = Mock() rendered_html = force_text(page.serve(request).render().content) self.assertInHTML( ('<aside class="m-jobs-list" data-qa-hook="openings-section">' '<h3 class="short-desc">There are no current openings at this ' 'time.</h3></aside>'), rendered_html )
def test_get_meta_description(self): page = self.page1 # Defaults to empty string self.assertEqual( page.get_meta_description(), "" ) # Second fallback is truncated answer_content text block stream_data = [ { "type": "video_player", "id": "402b933b", "value": { "video_url": "https://www.youtube.com/embed/wcQ1a_Gg8tI" }, }, { "type": "text", "id": "402b933c", "value": { "content": ( "<p><span>" "This is more than forty words: " "word word word word word word word word word word " "word word word word word word word word word word " "word word word word word word word word word word " "word word word word word word too-many." "</span></p>" ) }, }, ] set_stream_data(page, "answer_content", stream_data) self.assertTrue(page.get_meta_description().endswith("word word ...")) # First fallback is the short_answer page.short_answer = "Test short answer" self.assertEqual(page.get_meta_description(), page.short_answer) # First choice is the search_description page.search_description = "Test search description" self.assertEqual(page.get_meta_description(), page.search_description)
def handle(self, *args, **options): """Read markets from file into update dicts.""" with open(self.expand_path(options['snapshot_file'])) as json_data: data = json.load(json_data) markets = data['markets'] snapshots = self.get_data_snapshots() for market in markets: key = market['market_key'] snapshot_stream_data = self.find_data_snapshot(key, snapshots) if not snapshot_stream_data: # Market may not have been added to Wagtail yet # noqa logger.warning('Market key {} not found'.format(key)) continue # Update snapshot fields with the provided values snapshot = snapshot_stream_data[0]['value'] snapshot['last_updated_projected_data'] = market['data_month'] snapshot['num_originations'] = market['num_originations'] snapshot['value_originations'] = market['value_originations'] snapshot['year_over_year_change'] = market['year_over_year_change'] # Update inquiry index info if it exists for this market if "inquiry_yoy_change" in market: snapshot['inquiry_month'] = market['inquiry_month'] snapshot['inquiry_year_over_year_change'] = \ market['inquiry_yoy_change'] else: snapshot['inquiry_month'] = "" snapshot['inquiry_year_over_year_change'] = "" # Update tightness index info if it exists for this market if "tightness_yoy_change" in market: snapshot['tightness_month'] = market['tightness_month'] snapshot['tightness_year_over_year_change'] = \ market['tightness_yoy_change'] else: snapshot['tightness_month'] = "" snapshot['tightness_year_over_year_change'] = "" # Publish changes to the browse page the data snapshot lives on page = BrowsePage.objects.get(pk=snapshot['page']) del snapshot['page'] set_stream_data(page, 'content', snapshot_stream_data) publish_changes(page)
def update_categories(): draft_pages = [] for page in DocumentDetailPage.objects.all(): url = page.get_url() if not page.live: continue if 'policy-compliance/enforcement/actions' not in url: continue if page.has_unpublished_changes: draft_pages.append(url) continue stream_data = get_stream_data(page, 'sidefoot') # Remove inline Category, use the page category instead for field in stream_data: if field['type'] == 'related_metadata': field_content = field['value']['content'] new_content = [{ 'type': 'categories', 'value': { 'heading': 'Category', 'show_categories': True } }] for block in field_content: if block['value'].get('heading', '') != 'Category': new_content.append(block) field['value']['content'] = new_content break set_stream_data(page.specific, 'sidefoot', stream_data) # Update page categories according to defined map update_page_category(page) if len(draft_pages) > 0: print('Skipped the following draft pages:', ' '.join(draft_pages)) else: print('No draft pages found') print('Inline categories removed and page categories updated.')
def test_loan_quiz_template(self): """ We are using get_template to set a 'situation_id' for development. We hope to drop this value, or deliver it another way, when quiz structure is decided. """ page = self.loan_quiz_page stream_data = [{ 'type': 'guided_quiz', 'id': '12345', 'value': { 'question': '?', 'answer': 'huh?'}}] set_stream_data(page, 'content', stream_data) self.assertEqual( page.get_template(HttpRequest()), 'paying-for-college/choose-a-student-loan.html') self.assertEqual( self.loan_quiz_page.content[0].value['situation_id'], '12345')
def test_answer_content_preview_char(self): """answer_content_preview returns truncated text by character count And without HTML tags. """ page = self.page1 stream_data = [ { "type": "video_player", "id": "402b933b", "value": { "video_url": "https://www.youtube.com/embed/wcQ1a_Gg8tI" }, }, { "type": "text", "id": "402b933c", "value": { "content": ( "<p><span>" "This a word with more than 255 characters: " "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "char char char char char char char char char char " "char char char char char char char char char char " "char char char char char char char char char char " "char char char char char char too-many." "</span></p>" ) }, }, ] set_stream_data(page, "answer_content", stream_data) self.assertTrue(page.answer_content_preview().endswith(" ..."))
def setUp(self): self.site_root = Site.objects.get(is_default_site=True).root_page self.policy_compliance_page = Page( title='Policy & Compliance', slug='policy-compliance' ) save_new_page(self.policy_compliance_page, root=self.site_root) self.enforcement_page = Page(title='Enforcement', slug='enforcement') save_new_page(self.enforcement_page, root=self.policy_compliance_page) self.actions_page = Page(title='Actions', slug='actions') save_new_page(self.actions_page, root=self.enforcement_page) self.test_all_data_page = DocumentDetailPage( title="Great Test Page", live=True, preview_description='This is a great test page.' ) save_new_page(self.test_all_data_page, root=self.actions_page) set_stream_data( self.test_all_data_page, 'sidefoot', [ { 'type': 'related_metadata', 'value': { 'content': [ { 'type': 'text', 'value': { 'heading': 'Status', 'blob': '<p>Inactive or resolved</p>' }, }, { 'type': 'text', 'value': { 'heading': 'File number', 'blob': '<p>2012-CFPB-0001</p>' }, }, { 'type': 'date', 'value': { 'heading': 'Date filed', 'date': datetime.date(2012, 7, 18) }, } ], }, }, ] ) set_stream_data( self.test_all_data_page, 'content', [ { 'type': 'full_width_text', 'value': [ { 'type': 'content', 'value': 'CONTENT' } ] }, ] ) self.test_no_data_page = DocumentDetailPage( title="Terrible Test Page", live=False, preview_description='This is a terrible test page.' ) save_new_page(self.test_no_data_page, root=self.actions_page) self.test_wrong_page = DocumentDetailPage( title="Wrong Test Page", live=True, preview_description='This is the wrong test page.' ) save_new_page(self.test_wrong_page, root=self.enforcement_page)