diff --git a/vrobbler/apps/scrobbles/utils.py b/vrobbler/apps/scrobbles/utils.py index 2b83bcd..8897ec4 100644 --- a/vrobbler/apps/scrobbles/utils.py +++ b/vrobbler/apps/scrobbles/utils.py @@ -2,12 +2,12 @@ import hashlib import logging import re from datetime import datetime, timedelta, tzinfo +from sqlite3 import IntegrityError import pytz -import requests from django.apps import apps from django.contrib.auth import get_user_model -from django.db import models +from django.db import models, transcation from django.utils import timezone from profiles.models import UserProfile from profiles.utils import now_user_timezone @@ -271,7 +271,6 @@ def get_file_md5_hash(file_path: str) -> str: file_hash.update(chunk) return file_hash.hexdigest() - def deduplicate_tracks(commit=False) -> int: from music.models import Track @@ -288,9 +287,13 @@ def deduplicate_tracks(commit=False) -> int: for other in tracks.exclude(id=first.id): print("moving scrobbles for ", other.id, " to ", first.id) if commit: - other.scrobble_set.update(track=first) - print("deleting ", other.id, " - ", other) - other.delete() + with transaction.atomic(): + other.scrobble_set.update(track=first) + print("deleting ", other.id, " - ", other) + try: + other.delete() + except IntegrityError as e: + print("could not delete ", other.id, f": IntegrityError {e}") return len(dups)