From b7dfcdafee2276b3f2526d94e8df7f5fac0ac78d Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Tue, 5 Jul 2022 14:31:08 -0400 Subject: [PATCH] Migrate another handful of code from miscutils modified: make_post_sell/__init__.py new file: make_post_sell/lib/phone_numbers.py new file: make_post_sell/lib/uuid_validation.py modified: make_post_sell/request_methods.py modified: make_post_sell/views/product.py modified: make_post_sell/views/shop.py --- make_post_sell/__init__.py | 59 +++++++++++++++++++++++---- make_post_sell/lib/phone_numbers.py | 23 +++++++++++ make_post_sell/lib/uuid_validation.py | 27 ++++++++++++ make_post_sell/request_methods.py | 2 +- make_post_sell/views/product.py | 2 +- make_post_sell/views/shop.py | 3 +- 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 make_post_sell/lib/phone_numbers.py create mode 100644 make_post_sell/lib/uuid_validation.py diff --git a/make_post_sell/__init__.py b/make_post_sell/__init__.py index 1375482..14873fe 100644 --- a/make_post_sell/__init__.py +++ b/make_post_sell/__init__.py @@ -4,14 +4,57 @@ from pyramid.session import SignedCookieSessionFactory from miscutils import get_children_settings +from pyramid.session import JSONSerializer -def session_serializer(serializer_name): - if serializer_name == "json": - from pyramid.session import JSONSerializer - return JSONSerializer() - from pyramid.session import PickleSerializer - return PickleSerializer() +def get_int_or_bool_or_none_or_str(value): + """ + Given a string value pulled from a configuration file, + this function attempts to return the value with the proper type. + """ + try: + return int(value) + except ValueError: + if value.lower() in {"yes", "y", "true", "y"}: + return True + elif value.lower() in {"no", "n", "false", "f"}: + return False + elif value.lower() == "none": + return None + return str(value) + + +def get_children_settings(settings, parent_key): + """ + Accept a settings dict and parent key, return dict of children + + For example: + + auth_tkt.hashalg = md5 + + Results to: + + {'auth_tkt.hashalg': 'md5'} + + This function returns the following: + + >>> get_children_settings({'auth_tkt.hashalg': 'md5'}, 'auth_tkt') + {'hashalg': 'md5'} + + """ + # needed to support expanding ENV vars from ini. + from os.path import expandvars + + # the +1 is the . between parent and child settings. + parent_len = len(parent_key) + 1 + children = {} + for key, value in settings.items(): + if parent_key in key: + # expandvars replaces template with ENV vars. + children[key[parent_len:]] = get_int_or_bool_or_none_or_str( + expandvars(value) + ) + return children def main(global_config, **settings): @@ -21,9 +64,7 @@ def main(global_config, **settings): session_settings = get_children_settings(settings, "session") # Setup session serializer. - session_settings["serializer"] = session_serializer( - session_settings.get("serializer") - ) + session_settings["serializer"] = JSONSerializer() session_factory = SignedCookieSessionFactory(**session_settings) diff --git a/make_post_sell/lib/phone_numbers.py b/make_post_sell/lib/phone_numbers.py new file mode 100644 index 0000000..4b453ea --- /dev/null +++ b/make_post_sell/lib/phone_numbers.py @@ -0,0 +1,23 @@ +def is_phone_number_valid(phone_number): + """Test if user phone_number meets our criteria for validity.""" + # Choices here are subject to change. + # allow dashes. + phone_number = phone_number.replace("-", "") + # allow period. + phone_number = phone_number.replace(".", "") + # allow spaces. + phone_number = phone_number.replace(" ", "") + # allow left parethesis. + phone_number = phone_number.replace("(", "") + # allow right parethesis. + phone_number = phone_number.replace(")", "") + + # must be left with only digits. + if not phone_number.isdigit(): + return False + + # must be at least 10 digits. + if len(phone_number) < 10: + return False + + return True diff --git a/make_post_sell/lib/uuid_validation.py b/make_post_sell/lib/uuid_validation.py new file mode 100644 index 0000000..a269e52 --- /dev/null +++ b/make_post_sell/lib/uuid_validation.py @@ -0,0 +1,27 @@ +# Reference: +# https://gist.github.com/kgriffs/c20084db6686fee2b363fdc1a8998792#file-uuid_regex-py-L7-L17 + + +import re + +# RFC 4122 states that the characters should be output as lowercase, but +# that input is case-insensitive. When validating input strings, +# include re.I or re.IGNORECASE per below: + + +def _create_pattern(version="[1-5]"): + return re.compile( + ( + "[a-f0-9]{8}-" + + "[a-f0-9]{4}-" + + version + + "[a-f0-9]{3}-" + + "[89ab][a-f0-9]{3}-" + + "[a-f0-9]{12}" + ), + re.IGNORECASE, + ) + + +UUID_ALL_PATTERN = _create_pattern() +UUID_V4_PATTERN = _create_pattern("4") diff --git a/make_post_sell/request_methods.py b/make_post_sell/request_methods.py index 844f733..3109bad 100644 --- a/make_post_sell/request_methods.py +++ b/make_post_sell/request_methods.py @@ -5,7 +5,7 @@ from .models.shop import get_shop_by_id from .models.shop import get_shop_by_domain_name from .models.product import get_product_by_id -from miscutils import get_children_settings +from . import get_children_settings def includeme(config): diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index 7106659..49ebade 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -312,7 +312,7 @@ def product_bundle_edit(request): from ..models.product import get_products_by_ids - from miscutils.uuid_validation import UUID_ALL_PATTERN + from ..lib.uuid_validation import UUID_ALL_PATTERN product = request.product diff --git a/make_post_sell/views/shop.py b/make_post_sell/views/shop.py index 75ad53a..e679b8a 100644 --- a/make_post_sell/views/shop.py +++ b/make_post_sell/views/shop.py @@ -32,9 +32,10 @@ from ..models.shop_search_request import ( from ..lib.mail import send_invite_email +from ..lib.phone_numbers import is_phone_number_valid + from pyramid.httpexceptions import HTTPFound -from miscutils.phone_numbers import is_phone_number_valid def checkbox_to_bool(checkbox):