Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User input fixes #120

Merged
merged 1 commit into from
Jan 15, 2021
Merged

User input fixes #120

merged 1 commit into from
Jan 15, 2021

Conversation

AlisterD
Copy link
Contributor

Overview

User ID verification with the following criteria:

  1. Maximum length of 25 letters or numbers
  2. Minimum length of one letter or number
  3. No special characters
  4. No spaces

Added User ID Verification for the following

#176210324

https://www.pivotaltracker.com/story/show/175563946

Contributions

  • Created function user_id_validator, imported regex to help with validation, and called function in start_experiment

Tested as much as I could with combinations oh spaces, special charts, numbers,

  • ran make bci-gui for every case

@AlisterD AlisterD requested review from lawhead and tab-cmd January 13, 2021 01:14
@tab-cmd tab-cmd changed the base branch from main to 1.4.3 January 13, 2021 15:47
@@ -22,3 +22,6 @@ clean:
find . -name "*.py[co]" -o -name __pycache__ -exec rm -rf {} +
find . -path "*/*.pyo" -delete
find . -path "*/*.pyc" -delete

bci-gui:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!

@@ -369,7 +418,8 @@ def start_experiment(self) -> None:
Using the inputs gathers, check for validity using the check_input method, then launch the experiment using a
command to bci_main.py and subprocess.
"""
if self.check_input():

if self.check_input() and self.user_id_validator():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should go in the check_input method, which already extracts the self.user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that make sense! thank you for the feedback!

"""

# Update based on current inputs
self.user = self.user_input.currentText()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned below this value should already be set when you call this in check_input(). In the current flow, we would extract user from the input and assign self.user twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted from function

@@ -327,6 +328,54 @@ def check_input(self) -> bool:
return False
return True

def user_id_validator(self) -> bool:
"""Check User ID Requirements. User ID must follow the following Requirements:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great documentation!


# Check the length of minimum one character up to 25 characters
try:
if len(self.user) == 0 or len(self.user) > 25:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider assigning a length_user variable since you call it twice, and you could then append it in the message of the alert using f strings,

f'User ID must be of at least one or up to 25. Your current ID is {length_user} characters.'

message_type=AlertMessageType.INFO,
okay_to_exit=True)
return False
# Check for white space
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are off.. try running make lint and see if it will catch this formatting stuff for you. If not, you'll need to go in and fix!

def user_id_validator(self) -> bool:
"""Check User ID Requirements. User ID must follow the following Requirements:

1. Maximum length of 25 letters or numbers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add these as class-level variables at the top. For instance,

self.max_length = 25 in the init

and then use that value instead of the hardcoded 25. Update the documentation to reflect that we set it above. This will make changing it easier in the future. I'd do this for all cases. self.min_length, self.special_character_regex

message_type=AlertMessageType.CRIT,
okay_to_exit=True)
return False
return True
Copy link
Collaborator

@lawhead lawhead Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of repeated code in this method that follows the same pattern: the user_id is checked against some condition and if it evaluates to True an alert is displayed with an error message specific to that condition. In addition to the increased code, this is also difficult to fully test and will only provide the user with 1 error message at a time. If the user_id has multiple problems this can be a frustrating experience for the end user.

One way to avoid repeating yourself is to abstract out the validations as separate functions (each of which can be independently tested), and store the validation functions along with the error message. For example:

# These functions could be in a separate module
def invalid_length(min=1, max=25):
    return lambda string: len(string) < min or len(string) > max

def contains_whitespaces(string):
    return re.match(r"^(?=.*[\s])", string)

def has_special_characters(string):
    disallowed_chars = re.compile('[^0-9a-zA-Z]+')
    return bool(disallowed_chars.search(string))

# This would go in the class definition
user_id_validations = [
  (invalid_length(min=self.min_length, max=self.max_length), f'must contain between {self.min_length} and {self.max_length} alphanumeric characters'),
  (contains_whitespaces, 'cannot contain white spaces'),
  (has_special_characters, 'cannot contain special characters') 
]

Then in the user_id_validator method you can iterate through these validations, check them against the user_id and collect the error messages for any that return True. If any problems are detected they can all be displayed to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so elegant!

Copy link
Contributor Author

@AlisterD AlisterD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great~

@AlisterD AlisterD merged commit 5d55058 into 1.4.3 Jan 15, 2021
@tab-cmd tab-cmd mentioned this pull request Jan 20, 2021
4 tasks
@tab-cmd tab-cmd deleted the user_input_fixes branch April 16, 2021 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants