diff --git a/shiftregister/app/dynamic_preferences_registry.py b/shiftregister/app/dynamic_preferences_registry.py new file mode 100644 index 0000000..af4912e --- /dev/null +++ b/shiftregister/app/dynamic_preferences_registry.py @@ -0,0 +1,17 @@ +from dynamic_preferences.registries import global_preferences_registry +from dynamic_preferences.types import MultipleChoicePreference +from dynamic_preferences.types import Section +import phonenumbers + +registration = Section("registation") + + +@global_preferences_registry.register +class AllowedPhoneCountries(MultipleChoicePreference): + section = registration + name = "allowed_countries" + choices = [ + (f"{phonenumbers.country_code_for_region(cc)}", cc) + for cc in phonenumbers.SUPPORTED_REGIONS + ] + default = [49, 41, 43] diff --git a/shiftregister/app/forms.py b/shiftregister/app/forms.py index be3e18a..ff3e3c4 100644 --- a/shiftregister/app/forms.py +++ b/shiftregister/app/forms.py @@ -1,6 +1,11 @@ from django import forms from .models import Helper from phonenumber_field.formfields import PhoneNumberField +from phonenumber_field.validators import validate_international_phonenumber +from django.core.exceptions import ValidationError +from dynamic_preferences.registries import global_preferences_registry + +global_preferences = global_preferences_registry.manager() def text_input(type=None): @@ -11,6 +16,17 @@ def text_input(type=None): return forms.TextInput(attrs=attrs) +def validate_allowed_countries(value): + if ( + not f"{value.country_code}" + in global_preferences["registation__allowed_countries"] + ): + raise ValidationError( + "Vorwahl nicht Erlaubt, bitte wende dich an den Infodesk oder schicke uns eine Mail" + ) + return value + + class RegisterForm(forms.Form): name = forms.CharField( max_length=Helper.name.field.max_length, label="Name", widget=text_input() @@ -21,6 +37,10 @@ class RegisterForm(forms.Form): 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.", widget=text_input("tel"), + validators=[ + validate_international_phonenumber, + validate_allowed_countries, + ], ) okf = forms.BooleanField(label="Ich bin damit einverstanden, SMS zu erhalten.")