Пример #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
Пример #2
0
def get_event_data(post):
    """
    Takes content from the old database API and
    performs the necessary transformation for 
    the data to fit the new database. Adds the
    correct fields to a dictionary called event_data
    """
    event_mapping = load_events_mapping()

    mapped_event = event_mapping[str(post['id'])]

    event_data = {}

    # Transforms the former event start date
    # and time or provides a default if they do not exist
    if post['start_date']:
        event_data['date'] = post['start_date'].split(" ")[0]
        event_data['start_time'] = post['start_date'].split(" ")[1]
    else:
        event_data['date'] = get_post_date(post['publish_at'])
        event_data['start_time'] = '10:00'

    # Transforms the former event end date
    # and time or provides a default if they do not exist
    if post['end_date']:
        event_data['end_date'] = post['end_date'].split(" ")[0]
        event_data['end_time'] = post['end_date'].split(" ")[1]
    else:
        event_data['end_date'] = '2016-03-01'
        event_data['end_time'] = '1:00'

    # Adds the former event rsvp link to event_data dictionary
    # or provides a default if it does not exist
    if post['rsvp_link']:
        event_data['rsvp_link'] = post['rsvp_link']
    else:
        event_data['rsvp_link'] = 'http://www.newamerica.org'

    # Adds the former event host organization to event_data dictionary
    # or provides a default if it does not exist
    if mapped_event['host_organization']:
        event_data['host_organization'] = mapped_event['host_organization']
    else:
        event_data['host_organization'] = 'New America'

    # Pulls the events former street address from the CSV
    # or provides a default if it does not exist
    if mapped_event['street_address']:
        event_data['street_address'] = mapped_event['street_address']
    else:
        event_data['street_address'] = '740 15th St NW #900'

    # Pulls the events former city from the CSV
    # or provides a default if it does not exist
    if mapped_event['city']:
        event_data['city'] = mapped_event['city']
    else:
        event_data['city'] = 'Washington'

    # Pulls the events former state from the CSV
    # or provides a default if it does not exist
    if mapped_event['state']:
        event_data['state'] = mapped_event['state']
    else:
        event_data['state'] = 'D.C.'

    # Pulls the events former zipcode from the CSV
    # or provides a default if it does not exist
    if mapped_event['zipcode']:
        event_data['zipcode'] = mapped_event['zipcode']
    else:
        event_data['zipcode'] = '20005'

    return event_data
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_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_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()
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'])