Issue
Im learning about Flask and Angular and i tried to make a web app, i want to implement a register watching some examples an other things, so im doing one but i want it to do like a filter, the typical filter of the email, a filter that validates that you have a domain and not only pipo123, or validates that there are not numbers in the names.
This is my code:
@app.route('/user/register', methods=['POST'])
def register():
r_first_name=request.get_json()['first_name']
r_last_name=request.get_json()['last_name']
r_email=request.get_json()['email']
r_password=bcrypt.generate_password_hash(request.get_json()['password']).decode('utf-8')
result=""
if user_database_service.if_user_exists(r_email):
result="user already exists"
return jsonify({'result': result})
new_user=LocalUser(first_name=r_first_name, last_name=r_last_name, email=r_email, password=r_password, created=datetime.utcnow())
added=user_database_service.add_user(new_user)
if added is True:
result="user successfully added"
else:
result="unable to add"
return jsonify({'result': result})
and this is the user_database_service
from sqlalchemy import create_engine, false
from sqlalchemy import exc
from sqlalchemy.orm import sessionmaker
from user_database_setup import Base, LocalUser
engine=create_engine('sqlite:///users.db')
Base.metadata.bind=engine
DBSession=sessionmaker(bind=engine)
def get_user(r_email):
session=DBSession()
q=session.query(LocalUser).filter(r_email == LocalUser.email).first()
session.close()
if q:
resulted_user=LocalUser(id=q.id, first_name=q.first_name, last_name=q.last_name, email=q.email, password=q.password, created=q.created)
return resulted_user
else:
return None
def if_user_exists(r_email):
session=DBSession()
q=session.query(LocalUser).filter(r_email == LocalUser.email).first()
session.close()
if q is None:
return False
else:
return True
def add_user(new_user):
try:
session=DBSession()
session.add(new_user)
session.flush()
session.commit()
session.close()
return True
except exc.SQLAlchemyError as e:
return False
def get_user(r_email):
session=DBSession()
q=session.query(LocalUser).filter(r_email == LocalUser.email).first()
session.close()
if q:
result=LocalUser(id=q.id, first_name=q.first_name, last_name=q.last_name, email=q.email, password=q.password, created=q.created)
return result
else:
return None
I apreciate all your tips! Thanks!
Solution
You can check if an email adress is valid using regular expressions regex
. A regex
is a string that specifies some rules that you want to check.
A quick intro to regex
[a-z]
is for checking for a character between a and z[0-9]
is for checking for digits from 0 to 9 then you can check for strings that have a letter followed by a hashtag then by a number with thisregex
:[a-z]#[0-9]
. Some strings that satisfy thisregex
area#4
,s#1
.
This way you can make a regex
for emails, and checking if a username does not start with a number
Python library re
is quite easy to use for these purposes
Here are some useful sites
https://www.w3schools.com/jsref/jsref_obj_regexp.asp
https://www.w3schools.com/python/python_regex.asp
Hope that helps!
Answered By - crayola skies
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.