diff options
author | Frederick Muriuki Muriithi | 2024-10-07 12:48:27 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-10-07 12:48:27 -0500 |
commit | 40ae605d358440212a2617d1ec0dddb5f75df5bb (patch) | |
tree | 3ce6bd9a82466aff289746c0f0fa173c4e588221 /uploader | |
parent | dc9799a7f2c5771b770a8d3d825fff6cf5e78d58 (diff) | |
download | gn-uploader-40ae605d358440212a2617d1ec0dddb5f75df5bb.tar.gz |
Extract reusable input validation code.
Diffstat (limited to 'uploader')
-rw-r--r-- | uploader/input_validation.py | 25 | ||||
-rw-r--r-- | uploader/population/views.py | 26 |
2 files changed, 27 insertions, 24 deletions
diff --git a/uploader/input_validation.py b/uploader/input_validation.py index 9abe742..88ffd8c 100644 --- a/uploader/input_validation.py +++ b/uploader/input_validation.py @@ -5,10 +5,12 @@ def is_empty_string(value: str) -> bool: """Check whether as string is empty""" return (isinstance(value, str) and value.strip() == "") + def is_empty_input(value: Any) -> bool: """Check whether user provided an empty value.""" return (value is None or is_empty_string(value)) + def is_integer_input(value: Any) -> bool: """ Check whether user provided a value that can be parsed into an integer. @@ -25,3 +27,26 @@ def is_integer_input(value: Any) -> bool: __is_int__(value, 10) or __is_int__(value, 8) or __is_int__(value, 16)))) + + +def is_valid_representative_name(repr_name: str) -> bool: + """ + Check whether the given representative name is a valid according to our rules. + + Parameters + ---------- + repr_name: a string of characters. + + Checks For + ---------- + * The name MUST start with an alphabet [a-zA-Z] + * The name MUST end with an alphabet [a-zA-Z] or number [0-9] + * The name MUST be composed of alphabets [a-zA-Z], numbers [0-9], + underscores (_) and/or hyphens (-). + + Returns + ------- + Boolean indicating whether or not the name is valid. + """ + pattern = re.compile(r"^[a-zA-Z]+[a-zA-Z0-9_-]*[a-zA-Z0-9]$") + return bool(pattern.match(repr_name)) diff --git a/uploader/population/views.py b/uploader/population/views.py index 3638453..b101d94 100644 --- a/uploader/population/views.py +++ b/uploader/population/views.py @@ -21,6 +21,7 @@ from uploader.datautils import enumerate_sequence from uploader.phenotypes.views import phenotypesbp from uploader.expression_data.views import exprdatabp from uploader.monadic_requests import make_either_error_handler +from uploader.input_validation import is_valid_representative_name from uploader.species.models import (all_species, species_by_id, order_species_by_family) @@ -73,29 +74,6 @@ def list_species_populations(species_id: int): activelink="list-populations") -def valid_population_name(population_name: str) -> bool: - """ - Check whether the given name is a valid population name. - - Parameters - ---------- - population_name: a string of characters. - - Checks For - ---------- - * The name MUST start with an alphabet [a-zA-Z] - * The name MUST end with an alphabet [a-zA-Z] or number [0-9] - * The name MUST be composed of alphabets [a-zA-Z], numbers [0-9], - underscores (_) and/or hyphens (-). - - Returns - ------- - Boolean indicating whether or not the name is valid. - """ - pattern = re.compile(r"^[a-zA-Z]+[a-zA-Z0-9_-]*[a-zA-Z0-9]$") - return bool(pattern.match(population_name)) - - @popbp.route("/<int:species_id>/populations/create", methods=["GET", "POST"]) @require_login def create_population(species_id: int): @@ -139,7 +117,7 @@ def create_population(species_id: int): errors = errors + (("population_name", "You must provide a name for the population!"),) - if not valid_population_name(population_name): + if not is_valid_representative_name(population_name): errors = errors + (( "population_name", "The population name can only contain letters, numbers, " |