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_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
Example #3
0
def load_events():
    """
    Goes through the events for each program and creates 
    or updates events as necessary using the data from the 
    event_data dictionary 
    """
    for post, program_id in NAClient().get_events():
        if post['status'] == "published":
            try:
                post_parent_program = get_program(program_id)

                parent_program_events_homepage = get_content_homepage(
                    post_parent_program,
                    ProgramEventsPage,
                    'Events',
                )

                event_slug = slugify(post['title'])

                new_event = Event.objects.filter(slug=event_slug).first()

                event_data = get_event_data(post)

                if not new_event and event_slug:
                    new_event = Event(
                        search_description='',
                        seo_title='',
                        depth=5,
                        show_in_menus=False,
                        slug=event_slug,
                        title=post['title'],
                        subheading=post['sub_headline'],
                        date=event_data['date'],
                        end_date=event_data['end_date'],
                        start_time=event_data['start_time'],
                        end_time=event_data['end_time'],
                        host_organization=event_data['host_organization'],
                        street_address=event_data['street_address'],
                        city=event_data['city'],
                        state=event_data['state'],
                        zipcode=event_data['zipcode'],
                        rsvp_link=event_data['rsvp_link'],
                        body=json.dumps([{
                            'type': 'paragraph',
                            'value': post['content']
                        }]),
                        soundcloud_url=post['soundcloud_url'],
                        story_image=download_image(post['cover_image_url'],
                                                   event_slug + "_image.jpeg"),
                        story_excerpt=get_summary(post['summary']),
                    )
                    parent_program_events_homepage.add_child(
                        instance=new_event)
                    new_event.save()
                    connect_programs_to_post(new_event, post['programs'])
                elif new_event and event_slug and need_to_update_post(
                        post['modified']):
                    new_event.search_description = ''
                    new_event.seo_title = ''
                    new_event.depth = 5
                    new_event.date = event_data['date']
                    new_event.end_date = event_data['end_date']
                    new_event.start_time = event_data['start_time']
                    new_event.end_time = event_data['end_time']
                    new_event.host_organization = event_data[
                        'host_organization']
                    new_event.street_address = event_data['street_address']
                    new_event.city = event_data['city']
                    new_event.state = event_data['state']
                    new_event.zipcode = event_data['zipcode']
                    new_event.rsvp_link = event_data['rsvp_link']
                    new_event.show_in_menus = False
                    new_event.slug = event_slug
                    new_event.title = post['title']
                    new_event.subheading = post['sub_headline']
                    new_event.body = json.dumps([{
                        'type': 'paragraph',
                        'value': post['content']
                    }])
                    new_event.story_image = download_image(
                        post['cover_image_url'], event_slug + "_image.jpeg")
                    new_event.story_excerpt = get_summary(post['summary'])
                    new_event.soundcloud_url = post['soundcloud_url']
                    new_event.save()
                    connect_programs_to_post(new_event, post['programs'])
            except django.db.utils.IntegrityError:
                pass
def load_weekly_articles():
    """
    Transfers all New America Weekly articles from the old database API
    into the new database creating objects of the WeeklyArticle model.
    Also creates Weekly Editions if they do not exist.
    """
    weekly_mapping = load_weekly_mapping()

    weekly = Weekly.objects.first()

    for post in NAClient().get_weekly_articles():
        # Only moves over published content
        if post['status'] == "published":
            mapped_weekly_article = weekly_mapping[str(post['id'])]
            # Ensures content in the CSV with an edition number only is transferred
            if mapped_weekly_article['edition_number']:
                # Checking if Weekly Edition from the CSV exists,
                # if it does not, creates that edition
                try:
                    weekly_edition = WeeklyEdition.objects.get(
                        title=mapped_weekly_article['edition_number'])
                except ObjectDoesNotExist:
                    weekly_edition = WeeklyEdition(
                        title=mapped_weekly_article['edition_number'],
                        slug=slugify(mapped_weekly_article['edition_number']),
                        depth=4)
                    weekly.add_child(instance=weekly_edition)
                    weekly_edition.save()

                weekly_article_slug = slugify(post['title'])

                new_weekly_article = WeeklyArticle.objects.filter(
                    slug=weekly_article_slug).first()
                # Checks that a new weekly article with this slug does not
                # exist already in the database and creates a new object
                if not new_weekly_article and weekly_article_slug:
                    new_weekly_article = WeeklyArticle(
                        search_description='',
                        seo_title='',
                        depth=5,
                        show_in_menus=False,
                        slug=weekly_article_slug,
                        title=post['title'],
                        date=get_post_date(post['publish_at']),
                        body=json.dumps([{
                            'type': 'paragraph',
                            'value': post['content']
                        }]),
                        story_excerpt=get_summary(post['summary']),
                        story_image=download_image(
                            post['cover_image_url'],
                            post['title'] + "_image.jpeg"))
                    weekly_edition.add_child(instance=new_weekly_article)
                    new_weekly_article.save()
                    print(weekly_edition)
                    print(new_weekly_article)
                    # If the article does exist and has
                    # been modified within the specified
                    # last number of days, it updates the fields
                elif new_weekly_article and weekly_article_slug and need_to_update_post(
                        post['modified']):
                    new_weekly_article.search_description = ''
                    new_weekly_article.seo_title = ''
                    new_weekly_article.depth = 5
                    new_weekly_article.show_in_menus = False
                    new_weekly_article.slug = weekly_article_slug
                    new_weekly_article.title = post['title']
                    new_weekly_article.story_excerpt = get_summary(
                        post['summary'])
                    new_weekly_article.date = get_post_date(post['publish_at'])
                    new_weekly_article.body = json.dumps([{
                        'type':
                        'paragraph',
                        'value':
                        post['content']
                    }])
                    new_weekly_article.story_image = download_image(
                        post['cover_image_url'], post['title'] + "_image.jpeg")
                    new_weekly_article.save()