Merge pull request #687 from tcely/patch-4

Do not store duplicate categories
This commit is contained in:
meeb 2025-02-04 13:17:46 +11:00 committed by GitHub
commit 9ece0c26e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 9 deletions

View File

@ -1,5 +1,4 @@
from collections import namedtuple
from functools import lru_cache
from typing import Any, Dict
from django import forms
from django.db import connection, models
@ -143,16 +142,10 @@ class CommaSepChoiceField(models.CharField):
# **kwargs,
# })
@lru_cache(maxsize=10)
def from_db_value(self, value, expression, connection):
'''
Create a data structure to be used in Python code.
This is called quite often with the same input,
because the database value doesn't change often.
So, it's being cached to prevent excessive logging.
'''
self.log.debug(f'fdbv:1: {type(value)} {repr(value)}')
if isinstance(value, str) and len(value) > 0:
value = value.split(self.separator)
if not isinstance(value, list):
@ -180,7 +173,8 @@ class CommaSepChoiceField(models.CharField):
return ''
if data.all_choice in value:
return data.all_choice
return data.separator.join(value)
ordered_unique = list(dict.fromkeys(value))
return data.separator.join(ordered_unique)
# extra functions not used by any parent classes
def get_all_choices(self):

View File

@ -172,7 +172,7 @@ class FrontEndTestCase(TestCase):
response = c.get('/source-add')
self.assertEqual(response.status_code, 200)
# Create a new source
data_categories = ('sponsor', 'preview',)
data_categories = ('sponsor', 'preview', 'preview', 'sponsor',)
expected_categories = ['sponsor', 'preview']
data = {
'source_type': 'c',
@ -223,6 +223,7 @@ class FrontEndTestCase(TestCase):
self.assertEqual(response.status_code, 200)
# save and refresh the Source
source.refresh_from_db()
source.sponsorblock_categories.selected_choices.append('sponsor')
source.save()
source.refresh_from_db()
# Check that the SponsorBlock categories remain saved