mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-12 16:22:17 -05:00
Localize notification_utils to the API
This changeset pulls in all of the notification_utils code directly into the API and removes it as an external dependency. We are doing this to cut down on operational maintenance of the project and will begin removing parts of it no longer needed for the API. Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
This commit is contained in:
25
notifications_utils/safe_string.py
Normal file
25
notifications_utils/safe_string.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import re
|
||||
import unicodedata
|
||||
|
||||
|
||||
def make_string_safe(string, whitespace):
|
||||
# strips accents, diacritics etc
|
||||
string = "".join(
|
||||
c
|
||||
for c in unicodedata.normalize("NFD", string)
|
||||
if unicodedata.category(c) != "Mn"
|
||||
)
|
||||
string = "".join(
|
||||
word.lower() if word.isalnum() or word == whitespace else ""
|
||||
for word in re.sub(r"\s+", whitespace, string.strip())
|
||||
)
|
||||
string = re.sub(r"\.{2,}", ".", string)
|
||||
return string.strip(".")
|
||||
|
||||
|
||||
def make_string_safe_for_email_local_part(string):
|
||||
return make_string_safe(string, whitespace=".")
|
||||
|
||||
|
||||
def make_string_safe_for_id(string):
|
||||
return make_string_safe(string, whitespace="-")
|
||||
Reference in New Issue
Block a user