Skip to content

Commit

Permalink
Merge pull request #12 from utsavll0/neha-1
Browse files Browse the repository at this point in the history
Neha 1: user registration added profile info and changed the flow
  • Loading branch information
Viggy12126 authored Oct 15, 2023
2 parents 2a7f373 + 4521a86 commit e804e85
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 107 deletions.
95 changes: 50 additions & 45 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def register():
register() function displays the Registration portal (register.html) template
route "/register" will redirect to register() function.
RegistrationForm() called and if the form is submitted then various values are fetched and updated into database
Input: Username, Email, Password, Confirm Password
Output: Value update in database and redirected to home login page
Input: Username, Email, Password, Confirm Password, cuurent height, current weight, target weight, target date
Output: Value update in database and redirected to dashboard
"""
if not session.get('email'):
form = RegistrationForm()
Expand All @@ -103,14 +103,60 @@ def register():
username = request.form.get('username')
email = request.form.get('email')
password = request.form.get('password')
weight = request.form.get('weight')
height = request.form.get('height')
target_weight = request.form.get('target_weight')
target_date = request.form.get('target_date')
now = datetime.now()
now = now.strftime('%Y-%m-%d')
mongo.db.user.insert_one({'name': username, 'email': email, 'pwd': bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt())})
password.encode("utf-8"), bcrypt.gensalt()), 'weight': weight, 'height': height, 'target_weight': target_weight,'start_date' : now, 'target_date':target_date})
flash(f'Account created for {form.username.data}!', 'success')
return redirect(url_for('home'))
session['email'] = email
return redirect(url_for('dashboard'))
else:
return redirect(url_for('home'))
return render_template('register.html', title='Register', form=form)

@app.route("/user_profile", methods=['GET', 'POST'])
def user_profile():
"""
user_profile() function displays the UserProfileForm (user_profile.html) template
route "/user_profile" will redirect to user_profile() function.
user_profile() called and if the form is submitted then various values are fetched and updated into the database entries
Input: Email, height, weight, goal, Target weight
Output: Value update in database and redirected to home login page
"""
if session.get('email'):
form = UserProfileForm()
if form.validate_on_submit():
if request.method == 'POST':
email = session.get('email')
weight = request.form.get('weight')
height = request.form.get('height')
goal = request.form.get('goal')
target_weight = request.form.get('target_weight')
temp = mongo.db.profile.find_one({'email': email}, {
'height', 'weight', 'goal', 'target_weight'})
if temp is not None:
mongo.db.profile.update_one({'email': email},
{'$set': {'weight': temp['weight'],
'height': temp['height'],
'goal': temp['goal'],
'target_weight': temp['target_weight']}})
else:
mongo.db.profile.insert_one({'email': email,
'height': height,
'weight': weight,
'goal': goal,
'target_weight': target_weight})

flash(f'User Profile Updated', 'success')
return render_template('display_profile.html', status=True, form=form)
else:
return redirect(url_for('login'))
return render_template('user_profile.html', status=True, form=form)


@app.route("/calories", methods=['GET', 'POST'])
def calories():
Expand Down Expand Up @@ -170,47 +216,6 @@ def workout():
return render_template('workout.html', form=form, time=now)


# print(intake)
@app.route("/user_profile", methods=['GET', 'POST'])
def user_profile():
"""
user_profile() function displays the UserProfileForm (user_profile.html) template
route "/user_profile" will redirect to user_profile() function.
user_profile() called and if the form is submitted then various values are fetched and updated into the database entries
Input: Email, height, weight, goal, Target weight
Output: Value update in database and redirected to home login page
"""
if session.get('email'):
form = UserProfileForm()
if form.validate_on_submit():
if request.method == 'POST':
email = session.get('email')
weight = request.form.get('weight')
height = request.form.get('height')
goal = request.form.get('goal')
target_weight = request.form.get('target_weight')
temp = mongo.db.profile.find_one({'email': email}, {
'height', 'weight', 'goal', 'target_weight'})
if temp is not None:
mongo.db.profile.update_one({'email': email},
{'$set': {'weight': temp['weight'],
'height': temp['height'],
'goal': temp['goal'],
'target_weight': temp['target_weight']}})
else:
mongo.db.profile.insert_one({'email': email,
'height': height,
'weight': weight,
'goal': goal,
'target_weight': target_weight})

flash(f'User Profile Updated', 'success')
return render_template('display_profile.html', status=True, form=form)
else:
return redirect(url_for('login'))
return render_template('user_profile.html', status=True, form=form)


@app.route("/history", methods=['GET'])
def history():
# ############################
Expand Down
17 changes: 17 additions & 0 deletions forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 All @@ -16,6 +17,19 @@ class RegistrationForm(FlaskForm):
confirm_password = PasswordField(
'Confirm Password', validators=[
DataRequired(), EqualTo('password')])
weight = StringField(
'Weight', validators=[
DataRequired(), Length(
min=2, max=20), ])
height = StringField(
'Height', validators=[
DataRequired(), Length(
min=2, max=20)])
target_weight = StringField(
'Target Weight', validators=[
DataRequired(), Length(
min=2, max=20)])
target_date = DateField(DataRequired())
submit = SubmitField('Sign Up')

def validate_email(self, email):
Expand All @@ -34,6 +48,7 @@ class LoginForm(FlaskForm):
remember = BooleanField('Remember Me')
submit = SubmitField('Login')


class WorkoutForm(FlaskForm):
app = App()
mongo = app.mongo
Expand All @@ -55,6 +70,7 @@ class WorkoutForm(FlaskForm):
burnout = StringField('Burn Out', validators=[DataRequired()])
submit = SubmitField('Save')


class CalorieForm(FlaskForm):
app = App()
mongo = app.mongo
Expand Down Expand Up @@ -108,6 +124,7 @@ class EnrollForm(FlaskForm):
mongo = app.mongo
submit = SubmitField('Enroll')


class ResetPasswordForm(FlaskForm):
password = PasswordField('Password', validators=[DataRequired()])
confirm_password = PasswordField(
Expand Down
167 changes: 105 additions & 62 deletions templates/register.html
Original file line number Diff line number Diff line change
@@ -1,72 +1,115 @@
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}

{% if form.username.errors %}
{{ form.username(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.username.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.username(class="form-control form-control-lg") }}
{% endif %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
{% if form.username.errors %}
{{ form.username(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.username.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{% if form.email.errors %}
{{ form.email(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.email(class="form-control form-control-lg") }}
{% endif %}
{% else %}
{{ form.username(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{% if form.email.errors %}
{{ form.email(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
<div class="form-group">
{{ form.password.label(class="form-control-label") }}
{% if form.password.errors %}
{{ form.password(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.password.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.password(class="form-control form-control-lg") }}
{% endif %}
{% else %}
{{ form.email(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.password.label(class="form-control-label") }}
{% if form.password.errors %}
{{ form.password(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.password.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
<div class="form-group">
{{ form.confirm_password.label(class="form-control-label") }}
{% if form.confirm_password.errors %}
{{ form.confirm_password(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.confirm_password.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.confirm_password(class="form-control form-control-lg") }}
{% endif %}
{% else %}
{{ form.password(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.confirm_password.label(class="form-control-label") }}
{% if form.confirm_password.errors %}
{{ form.confirm_password(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.confirm_password.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
</fieldset>
{% else %}
{{ form.confirm_password(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.height.label(class="form-control-label") }} (cms)
{% if form.height.errors %}
{{ form.height(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.height.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.height(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.weight.label(class="form-control-label") }} (kgs)
{% if form.weight.errors %}
{{ form.weight(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.weight.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.weight(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.target_weight.label(class="form-control-label") }} (kgs)
{% if form.target_weight.errors %}
{{ form.goal(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.target_weight.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.target_weight(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
<label class="form-control-label">Target Date</label>
<input type="date" name="target_date" required>
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
Already Have An Account? <a class="ml-2" href="{{ url_for('login') }}">Sign In</a>
</small>
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
Already Have An Account? <a class="ml-2" href="{{ url_for('login') }}">Sign In</a>
</small>
</div>
{% endblock content %}

0 comments on commit e804e85

Please sign in to comment.