Skip to content

Commit

Permalink
Merge pull request #657 from tcely/patch-2
Browse files Browse the repository at this point in the history
Fix CommaSepChoiceField
  • Loading branch information
meeb authored Jan 29, 2025
2 parents e0bcd33 + 24ac2e1 commit 30dbb10
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions tubesync/sync/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,28 @@ def get_context(self, name: str, value: Any, attrs) -> Dict[str, Any]:
class CommaSepChoiceField(models.Field):
"Implements comma-separated storage of lists"

def __init__(self, separator=",", possible_choices=(("","")), all_choice="", all_label="All", allow_all=False, *args, **kwargs):
self.separator = separator
# If 'text' isn't correct add the vendor override here.
_DB_TYPES = {}

def __init__(self, *args, separator=",", possible_choices=(("","")), all_choice="", all_label="All", allow_all=False, **kwargs):
super().__init__(*args, **kwargs)
self.separator = str(separator)
self.possible_choices = possible_choices
self.selected_choices = []
self.allow_all = allow_all
self.all_label = all_label
self.all_choice = all_choice
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if self.separator != ",":
if ',' != self.separator:
kwargs['separator'] = self.separator
kwargs['possible_choices'] = self.possible_choices
return name, path, args, kwargs

def db_type(self, connection):
return 'text'
value = self._DB_TYPES.get(connection.vendor, None)
return value if value is not None else 'text'

def get_my_choices(self):
choiceArray = []
Expand All @@ -91,21 +95,13 @@ def formfield(self, **kwargs):
'label': '',
'required': False}
defaults.update(kwargs)
#del defaults.required
return super().formfield(**defaults)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
# Only include kwarg if it's not the default
if self.separator != ",":
kwargs['separator'] = self.separator
return name, path, args, kwargs

def from_db_value(self, value, expr, conn):
if value is None:
if 0 == len(value) or value is None:
self.selected_choices = []
else:
self.selected_choices = value.split(",")
self.selected_choices = value.split(self.separator)

return self

Expand All @@ -116,7 +112,7 @@ def get_prep_value(self, value):
return ""

if self.all_choice not in value:
return ",".join(value)
return self.separator.join(value)
else:
return self.all_choice

Expand Down

0 comments on commit 30dbb10

Please sign in to comment.