Add basic duplicate prevention for batchaddmedia.
authorBen Sturmfels <ben@sturm.com.au>
Wed, 18 Sep 2019 05:58:24 +0000 (15:58 +1000)
committerBen Sturmfels <ben@sturm.com.au>
Wed, 18 Sep 2019 05:58:24 +0000 (15:58 +1000)
mediagoblin/gmg_commands/batchaddmedia.py

index dc80f88b31d43ff72c67dc215031d89b4c849d9f..88fa3e5a87b84281c37018e837a246e2a0b2c4e2 100644 (file)
@@ -25,7 +25,7 @@ import requests
 import six
 from six.moves.urllib.parse import urlparse
 
-from mediagoblin.db.models import LocalUser
+from mediagoblin.db.models import LocalUser, MediaEntry
 from mediagoblin.gmg_commands import util as commands_util
 from mediagoblin.submit.lib import (
     submit_media, FileUploadLimit, UserUploadLimit, UserPastUploadLimit)
@@ -86,7 +86,6 @@ def batchaddmedia(args):
 
     all_metadata = open(abs_metadata_filename, 'r')
     media_metadata = csv.DictReader(all_metadata)
-
     for index, file_metadata in enumerate(media_metadata):
         if six.PY2:
             file_metadata = {k.decode('utf-8'): v.decode('utf-8') for k, v in file_metadata.items()}
@@ -101,6 +100,7 @@ def batchaddmedia(args):
 
         ### Pull the important media information for mediagoblin from the
         ### metadata, if it is provided.
+        slug = file_metadata.get('slug')
         title = file_metadata.get('title') or file_metadata.get('dc:title')
         description = (file_metadata.get('description') or
             file_metadata.get('dc:description'))
@@ -119,6 +119,16 @@ Metadata was not uploaded.""".format(
             print(error)
             continue
 
+        if slug and MediaEntry.query.filter_by(actor=user.id, slug=slug).count():
+            # Avoid re-importing media from a previous batch run. Note that this
+            # check isn't quite robust enough, since it requires that a slug is
+            # specified. Probably needs to be based on "location" since this is
+            # the only required field.
+            error = '{}: {}'.format(
+                slug, _('An entry with that slug already exists for this user.'))
+            print(error)
+            continue
+
         url = urlparse(original_location)
         filename = url.path.split()[-1]
 
@@ -155,7 +165,7 @@ FAIL: Local file {filename} could not be accessed.
 {filename} will not be uploaded.""".format(filename=filename)))
                 continue
         try:
-            submit_media(
+            entry = submit_media(
                 mg_app=app,
                 user=user,
                 submitted_file=media_file,
@@ -166,6 +176,11 @@ FAIL: Local file {filename} could not be accessed.
                 license=license,
                 metadata=json_ld_metadata,
                 tags_string="")
+            if slug:
+                # Slug is automatically set by submit_media, so overwrite it
+                # with the desired slug.
+                entry.slug = slug
+                entry.save()
             print(_("""Successfully submitted {filename}!
 Be sure to look at the Media Processing Panel on your website to be sure it
 uploaded successfully.""".format(filename=filename)))