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
This commit is contained in:
parent
76dd3550d6
commit
b7dfcdafee
6 changed files with 104 additions and 12 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
23
make_post_sell/lib/phone_numbers.py
Normal file
23
make_post_sell/lib/phone_numbers.py
Normal file
|
|
@ -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
|
||||
27
make_post_sell/lib/uuid_validation.py
Normal file
27
make_post_sell/lib/uuid_validation.py
Normal file
|
|
@ -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")
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue