From e02e912c5df93f3726e1c76ee000cac1132ecf4a Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 10 Mar 2020 19:08:28 +0100 Subject: [PATCH] fix 500 error when including letters in dect number field, update description to clarify that the field is for numbers only. While here, update some other help_texts in the DectRegistration model, and add so numbers can be used in the letters field. Also allow using 0 and 1 in phone numbers, even though they can not be expressed as letters, only as 0 and 1. Fixes #481 and #480 --- src/phonebook/dectutils.py | 23 +++++------- .../migrations/0002_auto_20200310_1902.py | 37 +++++++++++++++++++ src/phonebook/models.py | 12 ++++-- 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 src/phonebook/migrations/0002_auto_20200310_1902.py diff --git a/src/phonebook/dectutils.py b/src/phonebook/dectutils.py index 7be4eff4..957fb330 100644 --- a/src/phonebook/dectutils.py +++ b/src/phonebook/dectutils.py @@ -9,14 +9,16 @@ class DectUtils: """ DECT_MATRIX = { - "2": ["A", "B", "C"], - "3": ["D", "E", "F"], - "4": ["G", "H", "I"], - "5": ["J", "K", "L"], - "6": ["M", "N", "O"], - "7": ["P", "Q", "R", "S"], - "8": ["T", "U", "V"], - "9": ["W", "X", "Y", "Z"], + "0": ["0"], + "1": ["1"], + "2": ["2", "A", "B", "C"], + "3": ["3", "D", "E", "F"], + "4": ["4", "G", "H", "I"], + "5": ["5", "J", "K", "L"], + "6": ["6", "M", "N", "O"], + "7": ["7", "P", "Q", "R", "S"], + "8": ["8", "T", "U", "V"], + "9": ["9", "W", "X", "Y", "Z"], } def __init__(self): @@ -32,11 +34,6 @@ class DectUtils: """ Generator to recursively get all combinations of letters for this number """ - if "0" in numbers or "1" in numbers: - logger.error( - "Numbers with 0 or 1 in them are not expressible as letters, bail out" - ) - return False # loop over the possible letters for the first digit for letter in self.DECT_MATRIX[numbers[0]]: # if we have more digits.. diff --git a/src/phonebook/migrations/0002_auto_20200310_1902.py b/src/phonebook/migrations/0002_auto_20200310_1902.py new file mode 100644 index 00000000..90e39808 --- /dev/null +++ b/src/phonebook/migrations/0002_auto_20200310_1902.py @@ -0,0 +1,37 @@ +# Generated by Django 3.0.3 on 2020-03-10 18:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("phonebook", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="dectregistration", + name="description", + field=models.TextField( + blank=True, + help_text="Description of this registration, like a name/handle, or a location or service name.", + ), + ), + migrations.AlterField( + model_name="dectregistration", + name="letters", + field=models.CharField( + blank=True, + help_text="The letters or numbers chosen to represent this DECT number in the phonebook. Optional.", + max_length=9, + ), + ), + migrations.AlterField( + model_name="dectregistration", + name="number", + field=models.CharField( + help_text="The DECT phonenumber, four digits or more.", max_length=9 + ), + ), + ] diff --git a/src/phonebook/models.py b/src/phonebook/models.py index 79f78911..3e4147cf 100644 --- a/src/phonebook/models.py +++ b/src/phonebook/models.py @@ -29,18 +29,18 @@ class DectRegistration(CampRelatedModel): ) number = models.CharField( - max_length=9, help_text="The DECT number, numeric or as letters", + max_length=9, help_text="The DECT phonenumber, four digits or more.", ) letters = models.CharField( max_length=9, blank=True, - help_text="The letters chosen to represent this DECT number in the phonebook. Optional.", + help_text="The letters or numbers chosen to represent this DECT number in the phonebook. Optional.", ) description = models.TextField( blank=True, - help_text="Description of this registration, like a name or a location or a service.", + help_text="Description of this registration, like a name/handle, or a location or service name.", ) activation_code = models.CharField( @@ -65,6 +65,12 @@ class DectRegistration(CampRelatedModel): This code really belongs in model.clean(), but that gets called before form_valid() which is where we set the Camp object for the model instance. """ + # First of all, check that number is numeric + try: + int(self.number) + except ValueError: + raise ValidationError("Phonenumber must be numeric!") + # check for conflicts with the same number if ( DectRegistration.objects.filter(camp=self.camp, number=self.number)