From 145f9c8187e02ca9b97fabc24eee9f63a292ce42 Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Wed, 27 Apr 2022 15:07:43 +0200 Subject: [PATCH] initial phone number validation --- requirements.txt | 3 +++ shiftregister/app/forms.py | 4 ++-- .../app/migrations/0007_alter_helper_phone.py | 19 +++++++++++++++++++ shiftregister/app/models.py | 5 ++--- shiftregister/app/templates/register.html | 3 +++ shiftregister/settings.py | 3 +++ 6 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 shiftregister/app/migrations/0007_alter_helper_phone.py diff --git a/requirements.txt b/requirements.txt index fdfd4d7..1d75eb7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +## The following requirements were added by pip freeze: amqp==5.1.1 asgiref==3.5.0 async-timeout==4.0.2 @@ -11,11 +12,13 @@ click-plugins==1.1.1 click-repl==0.2.0 Deprecated==1.2.13 Django==4.0.4 +django-phonenumber-field==6.1.0 icalendar==4.0.9 idna==3.3 kombu==5.2.4 librabbitmq==2.0.0 packaging==21.3 +phonenumbers==8.12.47 prompt-toolkit==3.0.29 psycopg2-binary==2.9.3 pyparsing==3.0.8 diff --git a/shiftregister/app/forms.py b/shiftregister/app/forms.py index baaabe7..5134d6c 100644 --- a/shiftregister/app/forms.py +++ b/shiftregister/app/forms.py @@ -1,6 +1,6 @@ from django import forms from .models import Helper - +from phonenumber_field.formfields import PhoneNumberField def text_input(type=None): attrs = {"class": "input"} @@ -15,7 +15,7 @@ class RegisterForm(forms.Form): max_length=Helper.name.field.max_length, label="Name", widget=text_input() ) # actually verify phone number, lol - phone = forms.CharField( + phone = PhoneNumberField( max_length=Helper.phone.field.max_length, label="Handynummer für Benachrichtigungen", help_text="Wir nutzen deine Handynummer, um dir Benachrichtigungen zu deinen Schichten zu schicken. Wir löschen alle Daten 7 Tage nach dem Festival.", diff --git a/shiftregister/app/migrations/0007_alter_helper_phone.py b/shiftregister/app/migrations/0007_alter_helper_phone.py new file mode 100644 index 0000000..260fe3c --- /dev/null +++ b/shiftregister/app/migrations/0007_alter_helper_phone.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.4 on 2022-04-27 12:41 + +from django.db import migrations +import phonenumber_field.modelfields + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0006_shift_deleted'), + ] + + operations = [ + migrations.AlterField( + model_name='helper', + name='phone', + field=phonenumber_field.modelfields.PhoneNumberField(editable=False, max_length=128, primary_key=True, region=None, serialize=False), + ), + ] diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index 89cf961..b479bc9 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -4,7 +4,7 @@ from django.shortcuts import reverse from datetime import timedelta from django.utils import timezone from django.db.models import F, Count, Q, ExpressionWrapper - +from phonenumber_field.modelfields import PhoneNumberField class Room(models.Model): name = models.CharField(max_length=200, primary_key=True) @@ -32,8 +32,7 @@ class Shift(models.Model): class Helper(models.Model): - # todo: add propper phone number validation, maybe even country code? - phone = models.CharField(max_length=200, primary_key=True, editable=False) + phone = PhoneNumberField(primary_key=True, editable=False) name = models.CharField(max_length=200) # change this to a generic state variable to allow for number blocking/account deactivation? number_validated = models.BooleanField(default=False) diff --git a/shiftregister/app/templates/register.html b/shiftregister/app/templates/register.html index 435125c..441ecaf 100644 --- a/shiftregister/app/templates/register.html +++ b/shiftregister/app/templates/register.html @@ -16,6 +16,9 @@ {{ field }} {% endif %} +{%for error in field.errors %} +

{{ error }} +{% endfor %} {% if field.help_text %}

{{ field.help_text }}

{% endif %} diff --git a/shiftregister/settings.py b/shiftregister/settings.py index 64d4475..84773c4 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -46,6 +46,7 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "phonenumber_field", ] MIDDLEWARE = [ @@ -162,3 +163,5 @@ if getenv("SENTRY_DSN"): auto_session_tracking=False, traces_sample_rate=0, ) + +PHONENUMBER_DEFAULT_REGION = "DE" \ No newline at end of file