Merge branch '222-update-import' into 'develop'

Resolve "Add flag during import to replace already present tracks with new version"

Closes #222

See merge request funkwhale/funkwhale!264
This commit is contained in:
Renon 2018-06-22 22:04:06 +00:00
commit 2182227f50
8 changed files with 89 additions and 16 deletions

View file

@ -89,6 +89,7 @@ class ImportJobFactory(factory.django.DjangoModelFactory):
batch = factory.SubFactory(ImportBatchFactory)
source = factory.Faker("url")
mbid = factory.Faker("uuid4")
replace_if_duplicate = factory.Faker("boolean")
class Meta:
model = "music.ImportJob"

View file

@ -0,0 +1,18 @@
# Generated by Django 2.0.6 on 2018-06-22 13:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('music', '0027_auto_20180515_1808'),
]
operations = [
migrations.AddField(
model_name='importjob',
name='replace_if_duplicate',
field=models.BooleanField(default=False),
),
]

View file

@ -567,6 +567,7 @@ class ImportBatch(models.Model):
class ImportJob(models.Model):
uuid = models.UUIDField(unique=True, db_index=True, default=uuid.uuid4)
replace_if_duplicate = models.BooleanField(default=False)
batch = models.ForeignKey(
ImportBatch, related_name="jobs", on_delete=models.CASCADE
)

View file

@ -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
@ -135,8 +136,8 @@ def _do_import(import_job, replace=False, use_acoustid=False):
track_file = None
if replace:
logger.info("[Import Job %s] replacing existing audio file", import_job.pk)
track_file = track.files.first()
logger.info("[Import Job %s] deleting existing audio file", import_job.pk)
track.files.all().delete()
elif track.files.count() > 0:
logger.info(
"[Import Job %s] skipping, we already have a file for this track",
@ -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: