From 8200d7d4044549a210b795b63db8685fab32fa2b Mon Sep 17 00:00:00 2001 From: Viggy12126 Date: Mon, 9 Oct 2023 11:24:37 -0400 Subject: [PATCH] updated enter calories section --- application.py | 37 ++++++++++++++++++++++--------------- forms.py | 1 - insert_food_data.py | 2 +- requirements.txt | 5 ++--- tests/application.py | 42 ++++-------------------------------------- tests/login.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 tests/login.py diff --git a/application.py b/application.py index 8e10b6c1..95ee0970 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 @@ -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/insert_food_data.py b/insert_food_data.py index a9a2603b..10f501bf 100644 --- a/insert_food_data.py +++ b/insert_food_data.py @@ -10,4 +10,4 @@ for i in range(1, len(l)): temp = l[i].split(",") - mongo.db.food.insert({'food': temp[0], 'calories': temp[1]}) + mongo.db.food.insert_one({'food': temp[0], 'calories': temp[1]}) diff --git a/requirements.txt b/requirements.txt index c849055b..2de37966 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,7 @@ bcrypt==3.2.0 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 @@ -19,7 +18,7 @@ Jinja2==3.0.1 jwt==1.2.0 MarkupSafe==2.0.1 pycparser==2.20 -pymongo==3.11.3 +pymongo==4.5.0 python-dateutil==2.8.2 six==1.16.0 tabulate==0.8.9 diff --git a/tests/application.py b/tests/application.py index 2d26380a..2cf0934a 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(): """ @@ -100,7 +66,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')) @@ -138,7 +104,7 @@ 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')) @@ -174,7 +140,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, 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