diff --git a/application.py b/application.py index 8e10b6c1..ba51d99d 100644 --- a/application.py +++ b/application.py @@ -2,6 +2,7 @@ import bcrypt import smtplib +import re # from apps import App from flask import json @@ -58,7 +59,7 @@ def login(): if temp is not None and temp['email'] == form.email.data and ( bcrypt.checkpw( form.password.data.encode("utf-8"), - temp['pwd']) or temp['temp'] == form.password.data): + temp['pwd']) or temp['pwd'] == form.password.data): flash('You have been logged in!', 'success') session['email'] = temp['email'] #session['login_type'] = form.type.data @@ -102,7 +103,7 @@ def register(): username = request.form.get('username') email = request.form.get('email') password = request.form.get('password') - mongo.db.user.insert({'name': username, 'email': email, 'pwd': bcrypt.hashpw( + mongo.db.user.insert_one({'name': username, 'email': email, 'pwd': bcrypt.hashpw( password.encode("utf-8"), bcrypt.gensalt())}) flash(f'Account created for {form.username.data}!', 'success') return redirect(url_for('home')) @@ -130,17 +131,23 @@ def calories(): if request.method == 'POST': email = session.get('email') food = request.form.get('food') - cals = food.split(" ") - cals = int(cals[1][1:len(cals[1]) - 1]) + # cals = food.split(" ") + # print('cals is ',cals) + match = re.search(r'\((\d+)\)', food) + if match: + cals=int(match.group(1)) + else: + cals=None + # cals = int(cals[1][1:len(cals[1]) - 1]) burn = request.form.get('burnout') temp = mongo.db.calories.find_one({'email': email}, { 'email', 'calories', 'burnout'}) if temp is not None: - mongo.db.calories.update({'email': email}, {'$set': { + mongo.db.calories.update_one({'email': email}, {'$set': { 'calories': temp['calories'] + cals, 'burnout': temp['burnout'] + int(burn)}}) else: - mongo.db.calories.insert( + mongo.db.calories.insert_one( {'date': now, 'email': email, 'calories': cals, 'burnout': int(burn)}) flash(f'Successfully updated the data', 'success') return redirect(url_for('calories')) @@ -176,7 +183,7 @@ def user_profile(): 'goal': temp['goal'], 'target_weight': temp['target_weight']}}) else: - mongo.db.profile.insert({'email': email, + mongo.db.profile.insert_one({'email': email, 'height': height, 'weight': weight, 'goal': goal, @@ -418,7 +425,7 @@ def yoga(): if form.validate_on_submit(): if request.method == 'POST': enroll = "yoga" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -444,7 +451,7 @@ def swim(): if form.validate_on_submit(): if request.method == 'POST': enroll = "swimming" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -470,7 +477,7 @@ def abbs(): if form.validate_on_submit(): if request.method == 'POST': enroll = "abbs" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -495,7 +502,7 @@ def belly(): if form.validate_on_submit(): if request.method == 'POST': enroll = "belly" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insertOne({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -521,7 +528,7 @@ def core(): if form.validate_on_submit(): if request.method == 'POST': enroll = "core" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -546,7 +553,7 @@ def gym(): if form.validate_on_submit(): if request.method == 'POST': enroll = "gym" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -571,7 +578,7 @@ def walk(): if form.validate_on_submit(): if request.method == 'POST': enroll = "walk" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -596,7 +603,7 @@ def dance(): if form.validate_on_submit(): if request.method == 'POST': enroll = "dance" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') @@ -621,7 +628,7 @@ def hrx(): if form.validate_on_submit(): if request.method == 'POST': enroll = "hrx" - mongo.db.user.insert({'Email': email, 'Status': enroll}) + mongo.db.user.insert_one({'Email': email, 'Status': enroll}) flash( f' You have succesfully enrolled in our {enroll} plan!', 'success') diff --git a/forms.py b/forms.py index 64dec552..4ad38077 100644 --- a/forms.py +++ b/forms.py @@ -7,7 +7,6 @@ from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError from apps import App - class RegistrationForm(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)]) diff --git a/requirements.txt b/requirements.txt index 81ab8254..07bd2950 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,7 @@ bcrypt==4.0.1 blinker==1.4 -bson==0.5.10 certifi==1.0.1 -cffi==1.14.6 +cffi==1.15.1 click==8.0.1 colorama==0.4.4 cryptography==3.4.8 diff --git a/tests/application.py b/tests/application.py index a00d4adf..05d003d7 100644 --- a/tests/application.py +++ b/tests/application.py @@ -8,7 +8,7 @@ from flask_mail import Mail from flask_pymongo import PyMongo -from forms import RegistrationForm, LoginForm, UserProfileForm +from forms import RegistrationForm, UserProfileForm app = Flask(__name__) app.secret_key = 'secret' @@ -39,40 +39,6 @@ def home(): return redirect(url_for('login')) -@app.route("/login", methods=['GET', 'POST']) -def login(): - """" - login() function displays the Login form (login.html) template - route "/login" will redirect to login() function. - LoginForm() called and if the form is submitted then various values are fetched and verified from the database entries - Input: Email, Password, Login Type - Output: Account Authentication and redirecting to Dashboard - """ - if not session.get('email'): - form = LoginForm() - if form.validate_on_submit(): - temp = mongo.db.user.find_one({'email': form.email.data}, { - 'email', 'pwd'}) - if temp is not None and temp['email'] == form.email.data and ( - bcrypt.checkpw( - form.password.data.encode("utf-8"), - temp['pwd']) or temp['temp'] == form.password.data): - flash('You have been logged in!', 'success') - session['email'] = temp['email'] - #session['login_type'] = form.type.data - return redirect(url_for('dashboard')) - else: - flash( - 'Login Unsuccessful. Please check username and password', - 'danger') - else: - return redirect(url_for('home')) - return render_template( - 'login.html', - title='Login', - form=form) - - @app.route("/logout", methods=['GET', 'POST']) def logout(): """ @@ -108,7 +74,7 @@ def register(): return redirect(url_for('home')) return render_template('register.html', title='Register', form=form) -""" + @app.route("/calories", methods=['GET', 'POST']) def calories(): # ############################ @@ -138,14 +104,13 @@ def calories(): mongo.db.calories.update({'email': email}, {'$set': { 'calories': temp['calories'] + cals, 'burnout': temp['burnout'] + int(burn)}}) else: - mongo.db.calories.insert( + mongo.db.calories.insert_one( {'date': now, 'email': email, 'calories': cals, 'burnout': int(burn)}) flash(f'Successfully updated the data', 'success') return redirect(url_for('calories')) else: return redirect(url_for('home')) return render_template('calories.html', form=form, time=now) -""" @app.route("/user_profile", methods=['GET', 'POST']) def user_profile(): diff --git a/tests/login.py b/tests/login.py new file mode 100644 index 00000000..437ea325 --- /dev/null +++ b/tests/login.py @@ -0,0 +1,42 @@ +from forms import LoginForm +from tests.application import app, mongo + + +import bcrypt +from flask import flash, redirect, render_template, session, url_for + + +@app.route("/login", methods=['GET', 'POST']) +def login(): + """" + login() function displays the Login form (login.html) template + route "/login" will redirect to login() function. + LoginForm() called and if the form is submitted then various values are fetched and verified from the database entries + Input: Email, Password, Login Type + Output: Account Authentication and redirecting to Dashboard + """ + if not session.get('email'): + form = LoginForm() + if form.validate_on_submit(): + temp = mongo.db.user.find_one({'email': form.email.data},{ + 'email', 'pwd','temp'}) + # print('temp is ',temp['pwd']) + if temp is not None and temp['email'] == form.email.data and ( + bcrypt.checkpw( + form.password.data.encode("utf-8"), + ('temp' in temp and temp['temp'] == form.password.data) or temp['pwd'] ) ) : + # print(temp['pwd']) + flash('You have been logged in!', 'success') + session['email'] = temp['email'] + #session['login_type'] = form.type.data + return redirect(url_for('dashboard')) + else: + flash( + 'Login Unsuccessful. Please check username and password', + 'danger') + else: + return redirect(url_for('home')) + return render_template( + 'login.html', + title='Login', + form=form) \ No newline at end of file