kopia lustrzana https://dev.funkwhale.audio/funkwhale/funkwhale
				
				
				
			cli import files with replace option
							rodzic
							
								
									bb79d454af
								
							
						
					
					
						commit
						8103ea541f
					
				| 
						 | 
				
			
			@ -567,7 +567,7 @@ class ImportBatch(models.Model):
 | 
			
		|||
 | 
			
		||||
class ImportJob(models.Model):
 | 
			
		||||
    uuid = models.UUIDField(unique=True, db_index=True, default=uuid.uuid4)
 | 
			
		||||
    update_if_duplicate = models.BooleanField(default=False)
 | 
			
		||||
    replace_if_duplicate = models.BooleanField(default=False)
 | 
			
		||||
    batch = models.ForeignKey(
 | 
			
		||||
        ImportBatch, related_name="jobs", on_delete=models.CASCADE
 | 
			
		||||
    )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -80,10 +80,11 @@ def import_track_from_remote(library_track):
 | 
			
		|||
    )[0]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _do_import(import_job, replace=False, use_acoustid=False):
 | 
			
		||||
def _do_import(import_job, use_acoustid=False):
 | 
			
		||||
    logger.info("[Import Job %s] starting job", import_job.pk)
 | 
			
		||||
    from_file = bool(import_job.audio_file)
 | 
			
		||||
    mbid = import_job.mbid
 | 
			
		||||
    replace = import_job.replace_if_duplicate
 | 
			
		||||
    acoustid_track_id = None
 | 
			
		||||
    duration = None
 | 
			
		||||
    track = None
 | 
			
		||||
| 
						 | 
				
			
			@ -163,7 +164,7 @@ def _do_import(import_job, replace=False, use_acoustid=False):
 | 
			
		|||
            # no downloading, we hotlink
 | 
			
		||||
            pass
 | 
			
		||||
    elif not import_job.audio_file and not import_job.source.startswith("file://"):
 | 
			
		||||
        # not an implace import, and we have a source, so let's download it
 | 
			
		||||
        # not an inplace import, and we have a source, so let's download it
 | 
			
		||||
        logger.info("[Import Job %s] downloading audio file from remote", import_job.pk)
 | 
			
		||||
        track_file.download_file()
 | 
			
		||||
    elif not import_job.audio_file and import_job.source.startswith("file://"):
 | 
			
		||||
| 
						 | 
				
			
			@ -243,14 +244,14 @@ def get_cover_from_fs(dir_path):
 | 
			
		|||
@celery.require_instance(
 | 
			
		||||
    models.ImportJob.objects.filter(status__in=["pending", "errored"]), "import_job"
 | 
			
		||||
)
 | 
			
		||||
def import_job_run(self, import_job, replace=False, use_acoustid=False):
 | 
			
		||||
def import_job_run(self, import_job, use_acoustid=False):
 | 
			
		||||
    def mark_errored(exc):
 | 
			
		||||
        logger.error("[Import Job %s] Error during import: %s", import_job.pk, str(exc))
 | 
			
		||||
        import_job.status = "errored"
 | 
			
		||||
        import_job.save(update_fields=["status"])
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        tf = _do_import(import_job, replace, use_acoustid=use_acoustid)
 | 
			
		||||
        tf = _do_import(import_job, use_acoustid=use_acoustid)
 | 
			
		||||
        return tf.pk if tf else None
 | 
			
		||||
    except Exception as exc:
 | 
			
		||||
        if not settings.DEBUG:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,13 +56,14 @@ class Command(BaseCommand):
 | 
			
		|||
            ),
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            "--update",
 | 
			
		||||
            "--replace",
 | 
			
		||||
            action="store_true",
 | 
			
		||||
            dest="update",
 | 
			
		||||
            dest="replace",
 | 
			
		||||
            default=False,
 | 
			
		||||
            help=(
 | 
			
		||||
                "Use this flag to replace duplicates (tracks with same "
 | 
			
		||||
                "musicbrainz mbid) on import with their newest version."
 | 
			
		||||
                "musicbrainz mbid, or same artist, album and title) on import "
 | 
			
		||||
                "with their newest version."
 | 
			
		||||
            ),
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
| 
						 | 
				
			
			@ -122,16 +123,23 @@ class Command(BaseCommand):
 | 
			
		|||
                    "No superuser available, please provide a --username"
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
        filtered = self.filter_matching(matching)
 | 
			
		||||
        if options["replace"]:
 | 
			
		||||
            filtered = {"initial": matching, "skipped": [], "new": matching}
 | 
			
		||||
            message = "- {} files to be replaced"
 | 
			
		||||
            import_paths = matching
 | 
			
		||||
        else:
 | 
			
		||||
            filtered = self.filter_matching(matching)
 | 
			
		||||
            message = "- {} files already found in database"
 | 
			
		||||
            import_paths = filtered["new"]
 | 
			
		||||
 | 
			
		||||
        self.stdout.write("Import summary:")
 | 
			
		||||
        self.stdout.write(
 | 
			
		||||
            "- {} files found matching this pattern: {}".format(
 | 
			
		||||
                len(matching), options["path"]
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        self.stdout.write(
 | 
			
		||||
            "- {} files already found in database".format(len(filtered["skipped"]))
 | 
			
		||||
        )
 | 
			
		||||
        self.stdout.write(message.format(len(filtered["skipped"])))
 | 
			
		||||
 | 
			
		||||
        self.stdout.write("- {} new files".format(len(filtered["new"])))
 | 
			
		||||
 | 
			
		||||
        self.stdout.write(
 | 
			
		||||
| 
						 | 
				
			
			@ -151,12 +159,12 @@ class Command(BaseCommand):
 | 
			
		|||
            if input("".join(message)) != "yes":
 | 
			
		||||
                raise CommandError("Import cancelled.")
 | 
			
		||||
 | 
			
		||||
        batch, errors = self.do_import(filtered["new"], user=user, options=options)
 | 
			
		||||
        batch, errors = self.do_import(import_paths, user=user, options=options)
 | 
			
		||||
        message = "Successfully imported {} tracks"
 | 
			
		||||
        if options["async"]:
 | 
			
		||||
            message = "Successfully launched import for {} tracks"
 | 
			
		||||
 | 
			
		||||
        self.stdout.write(message.format(len(filtered["new"])))
 | 
			
		||||
        self.stdout.write(message.format(len(import_paths)))
 | 
			
		||||
        if len(errors) > 0:
 | 
			
		||||
            self.stderr.write("{} tracks could not be imported:".format(len(errors)))
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue