Skip to content

Commit

Permalink
allowed phone numbers validator
Browse files Browse the repository at this point in the history
  • Loading branch information
KlemenSpruk committed Oct 25, 2023
1 parent 5e24d36 commit a90807e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions django_project_base/notifications/base/phone_number_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from typing import List

from django.conf import settings
from django.utils.module_loading import import_string


class PhoneNumberParser:
@staticmethod
def is_allowed(phone_number: str) -> bool:
if (allowed_function := getattr(settings, "IS_PHONE_NUMBER_ALLOWED_FUNCTION", None)) and allowed_function:
return import_string(allowed_function)(phone_number)
return phone_number and len(phone_number) >= 8

@staticmethod
def valid_phone_numbers(candidates: List[str]) -> List[str]:
valid: List[str] = []
for number in candidates:
if not PhoneNumberParser.is_allowed(number):
continue
if number.startswith("+"):
valid.append(number.lstrip("+"))
continue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.test import TestCase

from django_project_base.notifications.base.phone_number_parser import PhoneNumberParser


class IsPhoneNumberValidTest(TestCase):
def test_is_phone_number_valid(self):
self.assertFalse(PhoneNumberParser.is_allowed("0903028"))
self.assertFalse(PhoneNumberParser.is_allowed("1919"))
self.assertTrue(PhoneNumberParser.is_allowed("03897234"))
self.assertTrue(PhoneNumberParser.is_allowed("0038631697001"))
self.assertTrue(PhoneNumberParser.is_allowed("+38676234567"))
1 change: 1 addition & 0 deletions django_project_base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"name": "DEFAULT_SMS_SENDER_ID_SETTING_NAME",
"default": "",
},
{"name": "IS_PHONE_NUMBER_ALLOWED_FUNCTION", "default": ""},
)

USER_CACHE_KEY = "django-user-{id}"
Expand Down

0 comments on commit a90807e

Please sign in to comment.