From fa47414141165991bbc384eb726f00003dd7a204 Mon Sep 17 00:00:00 2001 From: Ethan Andrews Date: Fri, 15 May 2020 11:44:42 -0400 Subject: [PATCH 1/7] Add try/except block and check for null choice_field_options --- roundabout/userdefinedfields/models.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index b525affac..6de2df9ad 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -81,12 +81,15 @@ def get_field_value(self): pass # Check if field is ChoiceField, set the value to the label if available - if self.field.field_type == 'ChoiceField': - options_list= self.field.choice_field_options['options'] + if self.field.field_type == 'ChoiceField' and self.field.choice_field_options: + try: + options_list= self.field.choice_field_options['options'] - for option in options_list: - if option['value'] == self.field_value and option['label']: - print(option['label']) - return option['label'] + for option in options_list: + if option['value'] == self.field_value and option['label']: + print(option['label']) + return option['label'] + except: + pass return self.field_value From 965f4b5563fb9c6606039e0be1570be3bd331bb8 Mon Sep 17 00:00:00 2001 From: Ethan Andrews Date: Fri, 15 May 2020 11:58:34 -0400 Subject: [PATCH 2/7] Check for empty choice field options in form create --- roundabout/inventory/forms.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/roundabout/inventory/forms.py b/roundabout/inventory/forms.py index 77c49cb00..0e8efe1eb 100644 --- a/roundabout/inventory/forms.py +++ b/roundabout/inventory/forms.py @@ -106,13 +106,14 @@ def __init__(self, *args, **kwargs): options_dict = field.choice_field_options options_list = [('', '- select one -')] - for option in options_dict['options']: - value = option['value'] - label = value - if option['label']: - label = option['label'] - form_option = (value, label) - options_list.append(form_option) + if options_dict: + for option in options_dict['options']: + value = option['value'] + label = value + if option['label']: + label = option['label'] + form_option = (value, label) + options_list.append(form_option) FIELD_CHOICES = (options_list) self.fields['udffield_' + str(field.id)] = forms.ChoiceField(label=str(field.field_name), required=False, From 0425e0da5dcaf9b81b1a6d174eddbe48a21c8b65 Mon Sep 17 00:00:00 2001 From: Sidney Batchelder Date: Fri, 15 May 2020 15:12:49 -0400 Subject: [PATCH 3/7] bugfix: FieldValue.__str__() may no longer return None. Instead returns "None". --- roundabout/userdefinedfields/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index b525affac..8671483aa 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -67,7 +67,7 @@ class Meta: get_latest_by = 'created_at' def __str__(self): - return self.field_value + return str(self.field_value) @property def get_field_value(self): From a43e656ecf1e8b687903f9c2b348bcd726f2f474 Mon Sep 17 00:00:00 2001 From: Sidney Batchelder Date: Fri, 15 May 2020 15:17:07 -0400 Subject: [PATCH 4/7] bugfix: FieldValue.__str__() may no longer return None. Instead returns empty string "" --- roundabout/userdefinedfields/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index 8671483aa..dfac9345e 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -67,7 +67,7 @@ class Meta: get_latest_by = 'created_at' def __str__(self): - return str(self.field_value) + return self.field_value if self.field_value is not None else '' @property def get_field_value(self): From 93aea5614995b4b1c4178fe2bb6300a5fef565f6 Mon Sep 17 00:00:00 2001 From: Ethan Andrews Date: Sun, 17 May 2020 14:11:50 -0400 Subject: [PATCH 5/7] Remove unnecessary conditional --- roundabout/userdefinedfields/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index a103b216b..2e7d9b90d 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -81,7 +81,7 @@ def get_field_value(self): pass # Check if field is ChoiceField, set the value to the label if available - if self.field.field_type == 'ChoiceField' and self.field.choice_field_options: + if self.field.field_type == 'ChoiceField': try: options_list= self.field.choice_field_options['options'] From b6a921cb74d37c99a66c3002a1ed3550a524fd69 Mon Sep 17 00:00:00 2001 From: Ethan Andrews Date: Sun, 17 May 2020 14:23:28 -0400 Subject: [PATCH 6/7] Update field_value to be null=False for __str__ consistency, add migration to handle existing NULLs --- .../migrations/0005_auto_20200517_1814.py | 22 +++++++++++++++++++ .../migrations/0006_auto_20200517_1822.py | 18 +++++++++++++++ roundabout/userdefinedfields/models.py | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 roundabout/userdefinedfields/migrations/0005_auto_20200517_1814.py create mode 100644 roundabout/userdefinedfields/migrations/0006_auto_20200517_1822.py diff --git a/roundabout/userdefinedfields/migrations/0005_auto_20200517_1814.py b/roundabout/userdefinedfields/migrations/0005_auto_20200517_1814.py new file mode 100644 index 000000000..3edf90b2e --- /dev/null +++ b/roundabout/userdefinedfields/migrations/0005_auto_20200517_1814.py @@ -0,0 +1,22 @@ +# Generated by Django 2.2.10 on 2020-05-17 18:14 + +from django.apps import apps +from django.db import migrations + +FieldValue = apps.get_model('userdefinedfields', 'FieldValue') + +def remove_nulls(apps, schema_editor): + for fv in FieldValue.objects.all(): + if fv.field_value is None: + fv.field_value = '' + fv.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('userdefinedfields', '0004_auto_20200421_0056'), + ] + + operations = [ + migrations.RunPython(remove_nulls), + ] diff --git a/roundabout/userdefinedfields/migrations/0006_auto_20200517_1822.py b/roundabout/userdefinedfields/migrations/0006_auto_20200517_1822.py new file mode 100644 index 000000000..e3c8ba3e5 --- /dev/null +++ b/roundabout/userdefinedfields/migrations/0006_auto_20200517_1822.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.10 on 2020-05-17 18:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('userdefinedfields', '0005_auto_20200517_1814'), + ] + + operations = [ + migrations.AlterField( + model_name='fieldvalue', + name='field_value', + field=models.TextField(blank=True), + ), + ] diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index 2e7d9b90d..3d27cbe4d 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -49,7 +49,7 @@ def __str__(self): class FieldValue(models.Model): - field_value = models.TextField(null=True, blank=True) + field_value = models.TextField(null=False, blank=True) field = models.ForeignKey(Field, related_name='fieldvalues', on_delete=models.CASCADE, null=False, blank=False) inventory = models.ForeignKey('inventory.Inventory', related_name='fieldvalues', From f6e0dc5ba6cb1632733cb6af0779c88ea1f1b576 Mon Sep 17 00:00:00 2001 From: Ethan Andrews Date: Sun, 17 May 2020 14:39:45 -0400 Subject: [PATCH 7/7] Remove unnecessary conditional --- roundabout/userdefinedfields/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roundabout/userdefinedfields/models.py b/roundabout/userdefinedfields/models.py index 3d27cbe4d..d2d2bc663 100644 --- a/roundabout/userdefinedfields/models.py +++ b/roundabout/userdefinedfields/models.py @@ -67,7 +67,7 @@ class Meta: get_latest_by = 'created_at' def __str__(self): - return self.field_value if self.field_value is not None else '' + return self.field_value @property def get_field_value(self):