Package: wagtail-image-import
wagtail-image-importAdds image import from Google Drive, including identifying potential duplicates, to the Wagtail Admin.InstallationAdd toINSTALLED_APPSin your settings file abovewagtail.adminSetupWagtail Image Import relies on Google APIs, which you will first need to enable for your project:Navigate to theGoogle API Library. Select a project for your Wagtail site, or create a new one.Find and enable theGoogle DocsandGoogle DriveAPIs.Find and enable theGoogle PickerAPI, and copy its API key to the settingWAGTAIL_IMAGE_IMPORT_GOOGLE_PICKER_API_KEY.Open theCredentialspage in the API Console.SelectCreate credentials, thenOAuth client IDIf you haven't already configured the consent screen, you will need to configure this now.UnderScopes for Google APIs, clickAdd scope.Add../auth/documents.readonlyand../auth/drive.readonlyscopes.Note: adding these sensitive scopes means that you will need to submit your project for verification by Google to avoid user caps and warning pages during use.Add your domain toAuthorised domains.ForApplication type, chooseWeb application.UnderAuthorised JavaScript origins, add your domain.On the Credentials page, next to your Client ID, click the download item to download a JSON file of your client
secret.Copy the text from this file, and use it to setWAGTAILIMAGEIMPORT_GOOGLE_OAUTH_CLIENT_SECRET.Duplicate Comparison SetupWagtail Image Import will attempt to identify the most likely duplicate of an image, in case it has been previously imported. By default, it will do this by comparing the unique Drive ID of each import, as well as the titles. If this is all you need, you can skip this section, or just use it to tweak the default experience.If you are using acustom image model, you can also add thewagtail_image_import.models.DuplicateFindingMixinto your custom model, which exposes the EXIF datetime and md5 hash for even better duplicate identification. An example of adding this to a very basic custom image model is shown below:fromwagtail.images.modelsimportImage,AbstractImage,AbstractRenditionfromwagtail_image_import.modelsimportDuplicateFindingMixinclassCustomImage(DuplicateFindingMixin,AbstractImage):admin_form_fields=Image.admin_form_fieldsclassCustomRendition(AbstractRendition):image=models.ForeignKey(CustomImage,on_delete=models.CASCADE,related_name="renditions")classMeta:unique_together=(("image","filter_spec","focal_point_key"),)If you choose to add the mixin and have existing image data, you will need to callsave()on all existing instances to fill in the new fields. This can be done in the Django shell:fromwagtail.imagesimportget_image_modelforimageinget_image_model().objects.all():image.save()In order to adjust the duplicate finding process, you can use theWAGTAILIMAGEIMPORT_FIELD_MAPPINGandWAGTAILIMAGEIMPORT_FIELD_WEIGHTINGsettings.WAGTAILIMAGEIMPORT_FIELD_MAPPINGmaps Google Drive field names to the database field names. By default, it is:{"id":"driveidmapping__drive_id","name":"title",}If you add the mixin, use:{"id":"driveidmapping__drive_id","name":"title","imageMediaMetadata__time":"exif_datetime","md5Checksum":"md5_hash"}To map the new fields. (If you have additional custom fields, the full list of Drive fields is:'id', 'name', 'thumbnailLink', 'fileExtension' 'md5Checksum', 'size', 'imageMediaMetadata__width', 'imageMediaMetadata__height', 'imageMediaMetadata__rotation', 'imageMediaMetadata__time, though not all may be present on every image)To adjust the field weighting - their relative importance in finding the most likely duplicate - you can setWAGTAILIMAGEIMPORT_FIELD_WEIGHTING, which maps database fields to weightings. The default is:{"driveidmapping__drive_id":10,"md5Checksum":5,"title":2}All fields not listed are given a weighting of 1.Other SettingsWAGTAILIMAGEIMPORT_SET_DRIVE_PARENT_FUNCTION:
If you would like to choose the starting folder for the Google picker, and prevent navigation outside it (note that due to the limitations of the picker options, this does not limit search results to children of this folder), this setting can be set to the string path to a function. This function must take the request object, and return a string Drive ID for the parent folder.UsageFor superusers or users with the 'import' permission (which can be added to groups in the Wagtail admin groups section), the image index's "Add image" button will now be a dropdown, with an additional "Import from Drive" option. Choose this to be taken to the import screen.You may select folders (all of whose direct image children will be imported - currently, children of subfolders are ignored) or images.Once selected, Wagtail Image Import will find potential duplicates and - if duplicates are found - take you to the review screen, where you can choose whether to replace existing images, keep both, or cancel the upload for the new image.Once confirmed, the upload will begin. As images finish importing, you will be able to edit their metadata.