Fix duplicate tag error (#65)

This commit is contained in:
Sascha Ißbrücker 2021-01-12 22:42:56 +01:00 committed by GitHub
parent f8fc360d84
commit 70953a52b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 7 deletions

View file

@ -1,3 +1,4 @@
import logging
import operator import operator
from typing import List from typing import List
@ -7,6 +8,8 @@ from django.utils import timezone
from bookmarks.models import Tag from bookmarks.models import Tag
from bookmarks.utils import unique from bookmarks.utils import unique
logger = logging.getLogger(__name__)
def get_or_create_tags(tag_names: List[str], user: User): def get_or_create_tags(tag_names: List[str], user: User):
tags = [get_or_create_tag(tag_name, user) for tag_name in tag_names] tags = [get_or_create_tag(tag_name, user) for tag_name in tag_names]
@ -21,3 +24,14 @@ def get_or_create_tag(name: str, user: User):
tag.date_added = timezone.now() tag.date_added = timezone.now()
tag.save() tag.save()
return tag return tag
except Tag.MultipleObjectsReturned:
# Legacy databases might contain duplicate tags with different capitalization
first_tag = Tag.objects.filter(name__iexact=name, owner=user).first()
message = (
"Found multiple tags for the name '{0}' with different capitalization. "
"Using the first tag with the name '{1}'. "
"Since v.1.2 tags work case-insensitive, which means duplicates of the same name are not allowed anymore. "
"To solve this error remove the duplicate tag in admin."
).format(name, first_tag.name)
logger.error(message)
return first_tag

View file

@ -42,6 +42,13 @@ class TagTestCase(TestCase):
self.assertEqual(len(tags), 1) self.assertEqual(len(tags), 1)
self.assertEqual(first_tag.id, second_tag.id) self.assertEqual(first_tag.id, second_tag.id)
def test_get_or_create_tag_should_handle_legacy_dbs_with_existing_duplicates(self):
Tag.objects.create(name='book', date_added=timezone.now(), owner=self.user)
Tag.objects.create(name='Book', date_added=timezone.now(), owner=self.user)
first_tag = get_or_create_tag('Book', self.user)
self.assertEqual(first_tag.id, first_tag.id)
def test_get_or_create_tags_should_return_tags(self): def test_get_or_create_tags_should_return_tags(self):
books_tag = get_or_create_tag('Book', self.user) books_tag = get_or_create_tag('Book', self.user)
movies_tag = get_or_create_tag('Movie', self.user) movies_tag = get_or_create_tag('Movie', self.user)

View file

@ -12,21 +12,25 @@ DEBUG = True
SASS_PROCESSOR_ENABLED = True SASS_PROCESSOR_ENABLED = True
# Enable debug logging # Enable debug logging
# Logging with SQL statements
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'filters': { 'disable_existing_loggers': False,
'require_debug_true': { 'formatters': {
'()': 'django.utils.log.RequireDebugTrue', 'simple': {
} 'format': '{levelname} {message}',
'style': '{',
},
}, },
'handlers': { 'handlers': {
'console': { 'console': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'formatter': 'simple'
} }
}, },
'root': {
'handlers': ['console'],
'level': 'WARNING',
},
'loggers': { 'loggers': {
'django.db.backends': { 'django.db.backends': {
'level': 'ERROR', # Set to DEBUG to log all SQL calls 'level': 'ERROR', # Set to DEBUG to log all SQL calls