示例#1
0
    def test_create_media_folder(self, requests_get):
        with self.assertRaises(GoogleDriveError):
            create_media_folder_with_images('test_parent_id',
                                            ['http://in.gogo/image.jpg'],
                                            'error')

        # Make sure that if the correct image is provided,
        # it creates a folder.
        media_images = create_media_folder_with_images(
            'test_parent_id', ['http://inp.gogo/image.jpg'], 'test')
        self.assertEqual(media_images['folder']['id'], 1)
        self.assertEqual(media_images['image_counter']['uploaded_images'], 1)

        # Make sure that if the incorrect image is provided,
        # still returns a new folder.
        media_images = create_media_folder_with_images(
            'test_parent_id', ['http://nocontenttype.com/test.jp', None],
            'test')
        self.assertEqual(media_images['folder']['id'], 1)
        self.assertEqual(media_images['image_counter']['not_uploaded_images'],
                         2)

        with self.assertRaises(GoogleDriveError):
            folder = create_folder_with_permissions('test_parent_id', 'error')

        # In order to test permission failure we did something convoluted.
        # create_folder_with_permissions calls service.insert_folder which in
        # turn will create a new folder named 'error'.
        # When we try to add permission to folder 'error' that fails in
        # our helper function and raises an error.
        folder = create_folder_with_permissions('test_parent_id',
                                                'permission_fail')
        self.assertEqual(folder['id'], 'error')
示例#2
0
def create_documents(project_data):
    """Create documents and folders needed for the journalism workflow.

    The following will be created:
    * an 'Article Draft' document where a reporter can draft text.
    * a 'Raw Photos' folder where a photographer can upload images.

    Documents are created in the project root folder.
    """
    task_data = {}
    folder_id = project_data['project_folder_id']

    # Create an Article Draft document.
    article_draft_template = project_data['article_draft_template']
    task_data['articleURL'] = create_document_from_template(
        article_draft_template,
        'Article Draft',
        parent_ids=[folder_id],
        permissions=[write_with_link_permission],
    )['alternateLink']

    # Create a Raw Photos folder.
    task_data['raw_photo_folder'] = create_folder_with_permissions(
        folder_id,
        'Raw Photos',
        permissions=[write_with_link_permission],
    )['id']

    return task_data
示例#3
0
def create_documents(project_data, prerequisites):
    """Create documents and folders needed for the journalism workflow.

    The following will be created:
    * an 'Article Draft' document where a reporter can draft text.
    * a 'Raw Photos' folder where a photographer can upload images.

    Documents are created in the project root folder.
    """
    task_data = {}
    folder_id = project_data['project_folder_id']

    # Create an Article Draft document.
    article_draft_template = project_data['article_draft_template']
    task_data['articleURL'] = create_document_from_template(
        article_draft_template,
        'Article Draft',
        parent_ids=[folder_id],
        permissions=[write_with_link_permission],
    )['alternateLink']

    # Create a Raw Photos folder.
    task_data['raw_photo_folder'] = create_folder_with_permissions(
        folder_id,
        'Raw Photos',
        permissions=[write_with_link_permission],
    )['id']

    return task_data
示例#4
0
def autoadjust_photos(project_data, prerequisites):
    """Resize all images in a google drive directory."""
    task_data = {}
    parent_folder_id = project_data['project_folder_id']

    # Create a directory to output the photos
    output_folder = create_folder_with_permissions(
        parent_folder_id,
        'Processed Photos',
        permissions=[write_with_link_permission],
    )
    task_data['processed_photo_folder'] = output_folder['id']

    # List the existing photos
    raw_photo_folder_id = (
        prerequisites.get('document_creation').get('raw_photo_folder'))
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    photos_metadata = service.list_folder(raw_photo_folder_id)

    # Iterate over the input photos and process them.
    task_data['photos_for_caption'] = []
    for photo_metadata in photos_metadata:
        photo, title, mimetype = download_file(photo_metadata)
        adjusted_photo_tmpfile = adjust_photo(photo)
        upload = upload_file(task_data['processed_photo_folder'],
                             adjusted_photo_tmpfile.name, title, 'image',
                             mimetype)
        os.unlink(adjusted_photo_tmpfile.name)
        embed_link = upload['webContentLink'].replace('&export=download', '')
        task_data['photos_for_caption'].append(embed_link)
    return task_data
示例#5
0
    def test_create_media_folder(self, requests_get):
        with self.assertRaises(GoogleDriveError):
            create_media_folder_with_images('test_parent_id',
                                            ['http://in.gogo/image.jpg'],
                                            'error')

        # Make sure that if the correct image is provided,
        # it creates a folder.
        media_images = create_media_folder_with_images(
            'test_parent_id',
            ['http://inp.gogo/image.jpg'],
            'test')
        self.assertEquals(media_images['folder']['id'], 1)
        self.assertEquals(
            media_images['image_counter']['uploaded_images'], 1)

        # Make sure that if the incorrect image is provided,
        # still returns a new folder.
        media_images = create_media_folder_with_images(
            'test_parent_id',
            ['http://nocontenttype.com/test.jp', None],
            'test')
        self.assertEquals(media_images['folder']['id'], 1)
        self.assertEquals(
            media_images['image_counter']['not_uploaded_images'], 2)

        with self.assertRaises(GoogleDriveError):
            folder = create_folder_with_permissions(
                'test_parent_id', 'error')

        # In order to test permission failure we did something convoluted.
        # create_folder_with_permissions calls service.insert_folder which in
        # turn will create a new folder named 'error'.
        # When we try to add permission to folder 'error' that fails in
        # our helper function and raises an error.
        folder = create_folder_with_permissions('test_parent_id',
                                                'permission_fail')
        self.assertEquals(folder['id'], 'error')
示例#6
0
def autoadjust_photos(project_data, prerequisites):
    """Resize all images in a google drive directory."""
    task_data = {}
    parent_folder_id = project_data['project_folder_id']

    # Create a directory to output the photos
    output_folder = create_folder_with_permissions(
        parent_folder_id,
        'Processed Photos',
        permissions=[write_with_link_permission],
    )
    task_data['processed_photo_folder'] = output_folder['id']

    # List the existing photos
    raw_photo_folder_id = (prerequisites
                           .get('photography')
                           .get('prerequisites')
                           .get('article_planning')
                           .get('prerequisites')
                           .get('document_creation')
                           .get('task')
                           .get('data')
                           .get('raw_photo_folder'))
    service = Service(settings.GOOGLE_P12_PATH,
                      settings.GOOGLE_SERVICE_EMAIL)
    photos_metadata = service.list_folder(raw_photo_folder_id)

    # Iterate over the input photos and process them.
    task_data['photos_for_caption'] = []
    for photo_metadata in photos_metadata:
        photo, title, mimetype = download_file(photo_metadata)
        adjusted_photo_tmpfile = adjust_photo(photo)
        upload = upload_file(
            task_data['processed_photo_folder'],
            adjusted_photo_tmpfile.name,
            title,
            'image',
            mimetype
        )
        os.unlink(adjusted_photo_tmpfile.name)
        embed_link = upload['webContentLink'].replace('&export=download', '')
        task_data['photos_for_caption'].append(embed_link)
    return task_data