Example #1
0
def load_press_releases():
    """
    Transfers all press releases from the old database API
    for all programs into the new database
    creating objects of the PressRelease model
    """
    for post, program_id in NAClient().get_press_releases():
        if post['status'] == "published":
            try:
                post_parent_program = get_program(program_id)
                
                parent_program_press_releases_homepage = get_content_homepage(
                    post_parent_program, 
                    ProgramPressReleasesPage,
                    'Press Releases',
                )

                press_release_slug = slugify(post['title'])

                new_press_release = PressRelease.objects.filter(slug=press_release_slug).first()

                if not new_press_release and press_release_slug:
                    new_press_release = PressRelease(
                        search_description='',
                        seo_title='',
                        depth=5,
                        show_in_menus=False,
                        slug=press_release_slug,
                        title=post['title'],
                        date=get_post_date(post['publish_at']),
                        subheading=post['sub_headline'],
                        body=json.dumps([
                            {
                                'type': 'paragraph',
                                'value': post['content']
                            }
                        ]),
                        attachment=json.dumps(
                            get_attachments(
                                post['attachments'],
                                press_release_slug
                            )
                        ),
                        story_excerpt=get_summary(post['summary']),
                        story_image=download_image(
                            post['cover_image_url'], 
                            press_release_slug + "_image.jpeg"
                        ),
                    )
                    parent_program_press_releases_homepage.add_child(instance=new_press_release)
                    new_press_release.save()
                    get_post_authors(new_press_release, post['authors'])
                    connect_programs_to_post(new_press_release, post['programs'])
                elif new_press_release and press_release_slug and need_to_update_post(post['modified']):
                    new_press_release.search_description = ''
                    new_press_release.seo_title = ''
                    new_press_release.depth = 5
                    new_press_release.date = get_post_date(post['publish_at'])
                    new_press_release.show_in_menus = False
                    new_press_release.slug = press_release_slug
                    new_press_release.title = post['title']
                    new_press_release.body = json.dumps([
                            {
                                'type': 'paragraph',
                                'value': post['content']
                            }
                        ])
                    new_press_release.attachment=json.dumps(
                            get_attachments(
                                post['attachments'],
                                press_release_slug
                            )
                        )
                    new_press_release.story_image = download_image(
                            post['cover_image_url'], 
                            press_release_slug + "_image.jpeg"
                    )
                    new_press_release.subheading=post['sub_headline']
                    new_press_release.save()
                    get_post_authors(new_press_release, post['authors'])
                    connect_programs_to_post(new_press_release, post['programs'])
            except django.db.utils.IntegrityError:
                pass
def load_general_blogs():
    """
    Used the old database API to retrieve articles 
    and then using cleaned CSV data, turns the
    appropriate content items into blog posts
    """
    general_blog_mapping = load_general_blog_mapping()

    for post, program_id in NAClient().get_general_blogs():
        if post['status'] == "published":
            post_id = str(post['id'])
            print(post_id)

            mapped_blog_post = general_blog_mapping.get(post_id, None)

            if mapped_blog_post:
                print(post['id'])
                print("found this id above in the csv - adding blog")

                mapped_programs = mapped_blog_post['program'].split(',')
                program_id = str(program_id)
                print('these are the mapped programs')
                print(mapped_programs)

                if program_id in mapped_programs:
                    print(program_id)
                    print("found program id above in the mapped programs")

                    post_parent = get_program(program_id)
                    parent_blog_homepage = get_content_homepage(
                        post_parent,
                        ProgramBlogPostsPage,
                        'Our Blog',
                    )
                    general_blog_post_slug = post['slug']
                    general_blog_post = BlogPost.objects.filter(
                        slug=general_blog_post_slug).first()
                    if not general_blog_post and general_blog_post_slug:
                        general_blog_post = BlogPost(
                            search_description='',
                            seo_title='',
                            depth=5,
                            show_in_menus=False,
                            slug=general_blog_post_slug,
                            title=post['title'],
                            date=get_post_date(post['publish_at']),
                            subheading=post['sub_headline'],
                            body=json.dumps([{
                                'type': 'paragraph',
                                'value': post['content']
                            }]),
                            story_excerpt=get_summary(post['summary']),
                            story_image=download_image(
                                post['cover_image_url'],
                                general_blog_post_slug + "_image.jpeg"),
                        )
                        parent_blog_homepage.add_child(
                            instance=general_blog_post)
                        general_blog_post.save()
                        get_post_authors(general_blog_post, post['authors'])
                        connect_programs_to_post(general_blog_post,
                                                 post['programs'])
                        print(
                            "----------------------ADDED NEW BLOG POST------")
                        print(post_id)
def load_podcasts():
    """
    Transfers all podcasts from the old database API
    for all programs into the new database
    creating objects of the Podcast model
    """
    for post, program_id in NAClient().get_podcasts():
        if post['status'] == "published":
            try:
                post_parent_program = get_program(program_id)
                
                parent_program_podcasts_homepage = get_content_homepage(
                    post_parent_program, 
                    ProgramPodcastsPage,
                    'Podcasts',
                )

                podcast_slug = slugify(post['title'])

                new_podcast = Podcast.objects.filter(slug=podcast_slug).first()

                if not new_podcast and podcast_slug:
                    new_podcast = Podcast(
                        search_description='',
                        seo_title='',
                        depth=5,
                        show_in_menus=False,
                        slug=podcast_slug,
                        title=post['title'],
                        date=get_post_date(post['publish_at']),
                        subheading=post['sub_headline'],
                        body=json.dumps([
                            {
                                'type': 'paragraph',
                                'value': post['content']
                            }
                        ]),
                        soundcloud=json.dumps([
                            {
                                'type': 'soundcloud_embed',
                                'value': post['soundcloud_url']
                            }
                        ]),
                        story_excerpt=get_summary(post['summary']),
                    )
                    parent_program_podcasts_homepage.add_child(
                    	instance=new_podcast
                    )
                    print("new podcast")
                    print(post['id'])
                    new_podcast.save()
                    get_post_authors(new_podcast, post['authors'])
                    connect_programs_to_post(new_podcast, post['programs'])
                elif new_podcast and podcast_slug and need_to_update_post(post['modified']):
                    new_podcast.search_description = ''
                    new_podcast.seo_title = ''
                    new_podcast.depth = 5
                    new_podcast.date = get_post_date(post['publish_at'])
                    new_podcast.show_in_menus = False
                    new_podcast.slug = podcast_slug
                    new_podcast.title = post['title']
                    new_podcast.body = json.dumps([
                            {
                                'type': 'paragraph',
                                'value': post['content']
                            }
                        ])
                    new_podcast.soundcloud=json.dumps([
                            {
                                'type': 'soundcloud_embed',
                                'value': post['soundcloud_url']
                            }
                        ])
                    new_podcast.subheading=post['sub_headline']
                    print("updating podcast")
                    print(post['id'])
                    new_podcast.save()
                    get_post_authors(new_podcast, post['authors'])
                    connect_programs_to_post(new_podcast, post['programs'])
            except django.db.utils.IntegrityError:
                pass
def load_asset_blogs():
    """
    Used the old database API to retrieve Asset Building
    articles and then using cleaned CSV data, turns the
    appropriate content items into blog posts
    """
    asset_blog_mapping = load_asset_blog_mapping()

    for post in NAClient().get_asset_blog_posts():
        if post['status'] == "published":
            post_id = str(post['id'])
            print(post_id)
            mapped_asset_blog_post = asset_blog_mapping.get(post_id, None)
            if mapped_asset_blog_post:
                if mapped_asset_blog_post['initiative']:
                    print("adding asset initiative blog")
                    print(mapped_asset_blog_post['initiative'])
                    post_parent = get_subprogram(
                        'Asset Building', mapped_asset_blog_post['initiative'])
                    parent_blog_homepage = get_content_homepage(
                        post_parent,
                        ProgramBlogPostsPage,
                        mapped_asset_blog_post['blog'],
                    )
                    asset_blog_post_slug = post['slug']
                    new_blog_post = BlogPost.objects.filter(
                        slug=asset_blog_post_slug).first()
                    if not new_blog_post and asset_blog_post_slug:
                        new_blog_post = BlogPost(
                            search_description='',
                            seo_title='',
                            depth=6,
                            show_in_menus=False,
                            slug=asset_blog_post_slug,
                            title=post['title'],
                            date=get_post_date(post['publish_at']),
                            subheading=post['sub_headline'],
                            body=json.dumps([{
                                'type': 'paragraph',
                                'value': post['content']
                            }]),
                            story_excerpt=get_summary(post['summary']),
                            story_image=download_image(
                                post['cover_image_url'],
                                asset_blog_post_slug + "_image.jpeg"),
                        )
                        parent_blog_homepage.add_child(instance=new_blog_post)
                        new_blog_post.save()
                        get_post_authors(new_blog_post, post['authors'])
                else:
                    print("adding asset blog")
                    print(post['id'])
                    post_parent = get_program('15')
                    parent_blog_homepage = get_content_homepage(
                        post_parent,
                        ProgramBlogPostsPage,
                        'Our Blog',
                    )
                    asset_blog_post_slug = post['slug']
                    new_blog_post = BlogPost.objects.filter(
                        slug=asset_blog_post_slug).first()
                    if not new_blog_post and asset_blog_post_slug:
                        new_blog_post = BlogPost(
                            search_description='',
                            seo_title='',
                            depth=5,
                            show_in_menus=False,
                            slug=asset_blog_post_slug,
                            title=post['title'],
                            date=get_post_date(post['publish_at']),
                            subheading=post['sub_headline'],
                            body=json.dumps([{
                                'type': 'paragraph',
                                'value': post['content']
                            }]),
                            story_excerpt=get_summary(post['summary']),
                            story_image=download_image(
                                post['cover_image_url'],
                                asset_blog_post_slug + "_image.jpeg"),
                        )
                        parent_blog_homepage.add_child(instance=new_blog_post)
                        new_blog_post.save()
                        get_post_authors(new_blog_post, post['authors'])