Skip to content

Commit

Permalink
Merge branch 'main' into utsav_main
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavll0 authored Oct 9, 2023
2 parents 4e28a79 + 59c9006 commit f6f5564
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 57 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
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 3 additions & 38 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 @@ -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():
# ############################
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
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 f6f5564

Please sign in to comment.