|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +'''😌 EDRN Site Content: remove old explorer, create new one.''' |
| 4 | + |
| 5 | +from django.conf import settings |
| 6 | +from django.core.management.base import BaseCommand |
| 7 | +from edrnsite.content.models import FlexPage |
| 8 | +from edrnsite.policy.management.commands.utils import set_site |
| 9 | +from edrnsite.streams.views import update_data_element_explorer_trees |
| 10 | +from wagtail.models import Page |
| 11 | +from wagtail.rich_text import RichText |
| 12 | + |
| 13 | + |
| 14 | +_help_html = '''<p>To use the data models:</p> |
| 15 | +<ul> |
| 16 | +<li>Click/tap on the triangle icon to the left of each section to expand and view its contents.</li> |
| 17 | +<li>Click/tap on the text within a section to access detailed information about the model, including its specific attributes and details.</li> |
| 18 | +</ul>''' |
| 19 | + |
| 20 | +_faq_html = '''<p>For questions about the data models, <a href="mailto:[email protected]">email |
| 21 | +the Informatics Center</a>.''' |
| 22 | + |
| 23 | + |
| 24 | +class Command(BaseCommand): |
| 25 | + help = 'Replace old CDE viewer page with new block-based explorer flex page' |
| 26 | + |
| 27 | + def _update_cde_explorer(self, home_page): |
| 28 | + # There should be just one current CDEExplorerPage |
| 29 | + count = Page.objects.filter(slug='edrn-data-model').count() |
| 30 | + if count > 1: |
| 31 | + raise ValueError("There's more than one edrn-data-model! Not sure what to do") |
| 32 | + elif count == 0: |
| 33 | + self.stdout.write('No edrn-data-model found, so using existing "cde" page as the parent') |
| 34 | + parent = FlexPage.objects.filter(slug='cde').first() |
| 35 | + assert parent is not None |
| 36 | + else: |
| 37 | + self.stdout.write('Found the one edrn-data-model, deleting it') |
| 38 | + page = Page.objects.filter(slug='edrn-data-model').first() |
| 39 | + parent = page.get_parent() |
| 40 | + page.delete() |
| 41 | + parent.refresh_from_db() |
| 42 | + |
| 43 | + self.stdout.write('Creating new EDRN Data Model page') |
| 44 | + page = FlexPage(title='EDRN Data Model', slug='edrn-data-model', show_in_menus=False) |
| 45 | + parent.add_child(instance=page) |
| 46 | + |
| 47 | + page.body.append(('rich_text', RichText(_help_html))) |
| 48 | + page.body.append(('data_explorer', { |
| 49 | + 'title': 'Biomarker Data Models', |
| 50 | + 'block_id': 'bio', 'spreadsheet_id': '1Kjkvi-bF5GNpAzvq4Kq6r4g1WpceIyP0Srs2OHJJtGs', |
| 51 | + })) |
| 52 | + page.body.append(('data_explorer', { |
| 53 | + 'title': 'Cancer Biomarker Data Commons (LabCAS) Data Model', |
| 54 | + 'block_id': 'lab', 'spreadsheet_id': '1btbwoROmVbZlzSLBn3DQ_6rakZ48j24p-NoOkI3OCFg' |
| 55 | + })) |
| 56 | + page.body.append(('rich_text', RichText(_faq_html))) |
| 57 | + page.save() |
| 58 | + update_data_element_explorer_trees() |
| 59 | + |
| 60 | + def handle(self, *args, **options): |
| 61 | + self.stdout.write('Updating CDE explorer') |
| 62 | + |
| 63 | + old = getattr(settings, 'WAGTAILREDIRECTS_AUTO_CREATE', True) |
| 64 | + try: |
| 65 | + settings.WAGTAILREDIRECTS_AUTO_CREATE = False |
| 66 | + settings.WAGTAILSEARCH_BACKENDS['default']['AUTO_UPDATE'] = False |
| 67 | + site, home_page = set_site() |
| 68 | + self._update_cde_explorer(home_page) |
| 69 | + |
| 70 | + finally: |
| 71 | + settings.WAGTAILREDIRECTS_AUTO_CREATE = old |
| 72 | + settings.WAGTAILSEARCH_BACKENDS['default']['AUTO_UPDATE'] = True |
| 73 | + self.stdout.write("Job's done!") |
0 commit comments