def run(): logger.info('Running script initial_data') admin_password = os.environ.get('WAGTAIL_ADMIN_PW') staging_hostname = os.environ['DJANGO_STAGING_HOSTNAME'] http_port = os.environ.get('DJANGO_HTTP_PORT', '80') # Create admin user if it doesn't exist already. # Update existing one with admin password and active state. User.objects.update_or_create(username='******', defaults={ 'password': make_password(admin_password), 'is_superuser': True, 'is_staff': True, 'is_active': True, }) # Create home page if it doesn't exist already. try: home_page = HomePage.objects.get(slug='cfgov') except HomePage.DoesNotExist: home_page = HomePage(title='CFGov', slug='cfgov', live=True) root_page = Page.objects.get(slug='root') root_page.add_child(instance=home_page) home_page_revision = home_page.save_revision() home_page_revision.publish() # Make sure default site (either the one installed with Wagtail, or one # that has since been manually setup) is running on the correct port and # with the expected home page as its root. default_site = Site.objects.get(is_default_site=True) default_site.hostname = 'localhost' default_site.port = http_port default_site.root_page_id = home_page.id default_site.save() # Setup a staging site if it doesn't exist already. Use the correct # hostname and port, and the same root home page. staging_site, _ = Site.objects.update_or_create(is_default_site=False, defaults={ 'hostname': staging_hostname, 'port': http_port, 'root_page_id': home_page.id, }) # Delete the legacy Wagtail "hello world" page, if it exists. try: hello_world = Page.objects.get(slug='home') except Page.DoesNotExist: pass else: hello_world.delete()
def run(): print('Running script \'scripts.initial_data\' ...') admin_user = None site_root = None events = None admin_user = User.objects.filter(username='******') if not admin_user: admin_user = User(username='******', password=make_password( os.environ.get('WAGTAIL_ADMIN_PW')), is_superuser=True, is_active=True, is_staff=True) admin_user.save() else: admin_user = admin_user[0] # Creates a new site root `CFGov` site_root = HomePage.objects.filter(title='CFGOV') if not site_root: root = Page.objects.first() site_root = HomePage(title='CFGOV', slug='home-page', depth=2, owner=admin_user) site_root.live = True root.add_child(instance=site_root) latest = site_root.save_revision(user=admin_user, submitted_for_moderation=False) latest.save() else: site_root = site_root[0] # Setting new site root if not Site.objects.filter(hostname='content.localhost').exists(): site = Site.objects.first() site.port = 8000 site.root_page_id = site_root.id site.save() content_site = Site(hostname='content.localhost', port=8000, root_page_id=site_root.id) content_site.save() # Clean Up old_site_root = Page.objects.filter(id=2)[0] if old_site_root: old_site_root.delete() # Events Browse Page required for event `import-data` command if not BrowseFilterablePage.objects.filter(title='Events').exists(): events = BrowseFilterablePage(title='Events', slug='events', owner=admin_user) site_root.add_child(instance=events) revision = events.save_revision( user=admin_user, submitted_for_moderation=False, ) revision.publish() # Archived Events Browse Filterable Page if not BrowseFilterablePage.objects.filter(title='Archive').exists(): archived_events = BrowseFilterablePage(title='Archive', slug='archive', owner=admin_user) if not events: events = BrowseFilterablePage.objects.get(title='Events') events.add_child(instance=archived_events) revision = archived_events.save_revision( user=admin_user, submitted_for_moderation=False, ) revision.publish()
def run(): logger.info('Running script initial_data') default_site = Site.objects.get(is_default_site=True) admin_username = os.getenv('DJANGO_ADMIN_USERNAME') admin_password = os.getenv('DJANGO_ADMIN_PASSWORD') http_port = int(os.getenv('DJANGO_HTTP_PORT', default_site.port)) wagtail_sharing_hostname = os.getenv('WAGTAIL_SHARING_HOSTNAME') # If specified in the environment, create or activate superuser. if admin_username and admin_password: logger.info('Configuring superuser, username: {}'.format( admin_username )) User.objects.update_or_create( username=admin_username, defaults={ 'password': make_password(admin_password), 'is_superuser': True, 'is_staff': True, 'is_active': True, } ) # Create home page if it doesn't exist already. try: home_page = HomePage.objects.get(slug='cfgov') except HomePage.DoesNotExist: logger.info('Creating new cfgov home page') # Create the new home page instance. home_page = HomePage( title='CFGov', slug='cfgov', live=True ) # Add the new home page as a child to the Wagtail root page. root_page = Page.objects.get(slug='root') root_page.add_child(instance=home_page) # Delete the legacy Wagtail "hello world" page, if it exists. # This page is created as part of the default Wagtail install. # https://github.com/wagtail/wagtail/blob/master/wagtail/core/migrations/0002_initial_data.py#L29 try: hello_world = Page.objects.get(slug='home', url_path='/home/') except Page.DoesNotExist: pass else: logger.info('Deleting default Wagtail home page') hello_world.delete() # If needed, configure the default Wagtail Site to point to the proper # home page with the desired port. if ( default_site.root_page_id != home_page.id or default_site.port != http_port ): default_site.root_page_id = home_page.id default_site.port = http_port default_site.save() logger.info('Configured default Wagtail Site: {}'.format(default_site)) # Setup a sharing site for the default Wagtail site if a sharing hostname # has been configured in the environment. if wagtail_sharing_hostname: sharing_site, _ = SharingSite.objects.update_or_create( site=default_site, defaults={ 'hostname': wagtail_sharing_hostname, 'port': http_port, } ) logger.info('Configured wagtail-sharing site: {}'.format(sharing_site))