Skip to content

Commit

Permalink
Updated validation on subscribe form
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyu95 committed Aug 7, 2023
1 parent 2ef0d12 commit 8e81869
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/constants/subscribe_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
EMAIL_TAKEN = 1
EMAIL_INVALID = 2
PHONE_TAKEN = 3
PHONE_INVALID = 4
31 changes: 21 additions & 10 deletions app/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
Utility functions used for view functions involving stories
"""
import uuid
import re
from validate_email import validate_email

from flask import current_app, render_template, url_for

from app.constants.event_type import STORY_CREATED, USER_CREATED, NEW_SUBSCRIBER, UNSUBSCRIBED_EMAIL, UNSUBSCRIBED_PHONE
from app.constants.user_type_auth import ANONYMOUS_USER
from app.constants.subscribe_status import VALID, EMAIL_INVALID, EMAIL_TAKEN, PHONE_TAKEN
from app.constants.subscribe_status import VALID, EMAIL_INVALID, EMAIL_TAKEN, PHONE_TAKEN, PHONE_INVALID
from app.db_utils import create_object, update_object
from app.lib.emails_utils import send_email
from app.models import Stories, Users, Events, Subscribers
Expand Down Expand Up @@ -195,12 +196,18 @@ def remove_subscriber(email, phone):

def verify_subscriber(email, phone):
"""
A utility function used to create a Subscriber object.
Clears database field for based on provided input field and value.
:param email: email to be removed from subscriber's table
:param phone: email to be removed from subscriber's table
:return EmailStatus enums
A utility function used to verify email and phone number of a subscriber.
Phone number regex checks against the following formats:
123-456-7890
(123) 456-7890 (jquery mask in place for this format)
123 456 7890
123.456.7890
+91 (123) 456-7890
:param email: email to be verified
:param phone: email to be verified
:return constant from subscribe_status.py
"""
if email:
email_valid = validate_email(email_address=email,
Expand All @@ -209,10 +216,14 @@ def verify_subscriber(email, phone):
if not email_valid:
return EMAIL_INVALID

if Subscribers.query.filter_by(email=email).first():
if Subscribers.query.filter(Subscribers.email.ilike(email)).first():
return EMAIL_TAKEN

if phone and Subscribers.query.filter_by(phone=phone).first():
return PHONE_TAKEN
if phone:
if not re.search("^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$", phone):
return PHONE_INVALID

if Subscribers.query.filter_by(phone=phone).first():
return PHONE_TAKEN

return VALID
27 changes: 18 additions & 9 deletions app/subscribe/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import render_template, flash, request, Markup, redirect, url_for, current_app

from app.constants.subscribe_status import VALID, EMAIL_INVALID, EMAIL_TAKEN, PHONE_TAKEN
from app.constants.subscribe_status import EMAIL_INVALID, EMAIL_TAKEN, PHONE_TAKEN, PHONE_INVALID
from app.lib.utils import create_subscriber, verify_subscriber
from app.subscribe import subscribe
from app.subscribe.forms import SubscribeForm
Expand All @@ -17,7 +17,7 @@ def subscribe():
if form.validate_on_submit():
first_name = form.user_first.data
last_name = form.user_last.data
email = form.user_email.data
email = form.user_email.data.lower()
phone = form.user_phone.data

# Verify recaptcha token
Expand All @@ -33,20 +33,29 @@ def subscribe():

else:
# Check entered email and phone number
verify_email = verify_subscriber(email, phone)
verify = verify_subscriber(email, phone)

if verify_email == EMAIL_TAKEN:
flash(Markup('The email you entered is already subscribed, please try another email.'), category='warning')
if verify == EMAIL_TAKEN:
flash(Markup('The email you entered is already subscribed, please use another email.'),
category='warning')
return render_template('subscribe/subscribe.html', form=form,
RECAPTCHA_PUBLIC_KEY=current_app.config['RECAPTCHA_PUBLIC_KEY'])

elif verify_email == PHONE_TAKEN:
flash(Markup('The phone number you entered is already subscribed, please use another phone number.'), category='warning')
elif verify == PHONE_TAKEN:
flash(
Markup('The phone number you entered is already subscribed, please use another phone number.'),
category='warning')
return render_template('subscribe/subscribe.html', form=form,
RECAPTCHA_PUBLIC_KEY=current_app.config['RECAPTCHA_PUBLIC_KEY'])

elif verify_email == EMAIL_INVALID:
flash(Markup('This email isn\'t valid!'), category='danger')
elif verify == EMAIL_INVALID:
flash(Markup('The email you entered isn\'t valid, please use another email.'), category='danger')
return render_template('subscribe/subscribe.html', form=form,
RECAPTCHA_PUBLIC_KEY=current_app.config['RECAPTCHA_PUBLIC_KEY'])

elif verify == PHONE_INVALID:
flash(Markup('The phone number you entered isn\'t valid, please use another phone number.'),
category='danger')
return render_template('subscribe/subscribe.html', form=form,
RECAPTCHA_PUBLIC_KEY=current_app.config['RECAPTCHA_PUBLIC_KEY'])

Expand Down

0 comments on commit 8e81869

Please sign in to comment.