Skip to content

Commit

Permalink
Merge pull request #6 from utsavll0/vighnesh_first_branch
Browse files Browse the repository at this point in the history
Updated wrong password feature and more than 1 word entry of calories
  • Loading branch information
utsavll0 authored Oct 9, 2023
2 parents fc1ce4c + 4bd7560 commit 59c9006
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 61 deletions.
39 changes: 23 additions & 16 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bcrypt
import smtplib
import re

# from apps import App
from flask import json
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
1 change: 0 additions & 1 deletion forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
2 changes: 1 addition & 1 deletion insert_food_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
45 changes: 5 additions & 40 deletions tests/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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():
"""
Expand Down Expand Up @@ -100,15 +66,15 @@ 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'))
else:
return redirect(url_for('home'))
return render_template('register.html', title='Register', form=form)

"""

@app.route("/calories", methods=['GET', 'POST'])
def calories():
# ############################
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -174,7 +139,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,
Expand Down
42 changes: 42 additions & 0 deletions tests/login.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 59c9006

Please sign in to comment.