Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/rishi2019194/calorieApp int…
Browse files Browse the repository at this point in the history
…o email
  • Loading branch information
ananya173147 committed Nov 25, 2023
2 parents e84917a + d57fc3c commit 4b041bb
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 122 deletions.
59 changes: 41 additions & 18 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from forms import HistoryForm, RegistrationForm, LoginForm, CalorieForm, UserProfileForm, EnrollForm, WorkoutForm
from service import history as history_service
import openai
import os
from utilities import *
import utilities as u
import time

# Set the OpenAI API key
Expand Down Expand Up @@ -268,7 +267,7 @@ def workout():
if form.validate_on_submit():
email = session.get('email')
burnout = form.burnout.data

print(burnout)
mongo.db.workout.insert_one({
'date': form.date.data.strftime('%Y-%m-%d'), # Get the selected date from the form
'email': email,
Expand Down Expand Up @@ -298,45 +297,69 @@ def history():

# Find out the last 7 day's calories burnt by the user
calorie_day_map = {}
entries = get_entries_for_email(mongo.db, email, (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'), datetime.today().strftime('%Y-%m-%d'))
for entry in entries:
entries_cal, entries_workout = u.get_entries_for_email(mongo.db, email, (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'), datetime.today().strftime('%Y-%m-%d'))
# print(entries)
for entry in entries_cal:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])
curr_date = entry['date']
if(curr_date not in calorie_day_map):
calorie_day_map[curr_date] = net_calories
else:
calorie_day_map[curr_date] += net_calories

for entry in entries_workout:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])-2000
net_calories = -int(entry['burnout'])
curr_date = entry['date']
if(curr_date not in calorie_day_map):
calorie_day_map[curr_date] = net_calories
else:
calorie_day_map[curr_date] += net_calories
calorie_day_map = dict(sorted(calorie_day_map.items()))

labels = list(calorie_day_map.keys())
values = list(calorie_day_map.values())
for i in range(len(values)):
values[i] = str(values[i])


# The first day when the user registered or started using the app
user_start_date = mongo.db.user.find({'email': email})[0]['start_date']
user_target_date = mongo.db.user.find({'email': email})[0]['target_date']
target_weight = mongo.db.user.find({'email': email})[0]['target_weight']
current_weight = mongo.db.user.find({'email': email})[0]['weight']
print(current_weight, target_weight, type(user_start_date), datetime.today().strftime('%Y-%m-%d'))

# Find out the actual calories which user needed to burn/gain to achieve goal from the start day
target_calories_to_burn = history_service.total_calories_to_burn(
target_calories_to_burn = u.total_calories_to_burn(
target_weight=int(target_weight), current_weight=int(current_weight))
print(f'########## {target_calories_to_burn}')

# Find out how many calories user has gained or burnt uptill now
calories_till_today = mongo.db.calories.aggregate(
history_service.get_calories_burnt_till_now_pipeline(
email, user_start_date))
query = {
'email': email,
'date': {'$gte': user_start_date, '$lte': datetime.today().strftime('%Y-%m-%d')}
}

entries_till_today_cal = mongo.db.calories.find(query)
entries_till_today_workout = mongo.db.workout.find(query)
current_calories = 0
for calorie in calories_till_today:
current_calories += calorie['SUM']
# current_calories = [x for x in calories_till_today][0]['SUM'] if len(list(calories_till_today)) != 0 else 0
for entry in entries_till_today_cal:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])
current_calories += net_calories

for entry in entries_till_today_workout:
if entry['_id'] == 'Other':
continue
net_calories = -int(entry['burnout'])
current_calories += net_calories

# Find out no of calories user has to burn/gain in future per day
calories_to_burn = history_service.calories_to_burn(
calories_to_burn = u.calories_to_burn(
target_calories_to_burn,
current_calories,
target_date=datetime.strptime(user_target_date, '%Y-%m-%d'),
Expand Down Expand Up @@ -452,8 +475,8 @@ def bmi_calci():
if request.method == 'POST' and 'weight' in request.form:
weight = float(request.form.get('weight'))
height = float(request.form.get('height'))
bmi = calc_bmi(weight, height)
bmi_category = get_bmi_category(bmi)
bmi = u.calc_bmi(weight, height)
bmi_category = u.get_bmi_category(bmi)

return render_template("bmi_cal.html", bmi=bmi, bmi_category=bmi_category)

Expand All @@ -468,7 +491,7 @@ def chatbot():
def get_bot_response():
userText = request.args.get('msg')
return str(
get_response(chat_history, name, chatgpt_output, userText,
u.get_response(chat_history, name, chatgpt_output, userText,
history_file, impersonated_role, explicit_input))


Expand Down
10 changes: 10 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import datetime
import unittest
import os
import sys
# Get the current script's directory
current_dir = os.path.dirname(os.path.abspath(__file__))

# Get the parent directory
parent_dir = os.path.dirname(current_dir)

# Add the parent directory to the Python path
sys.path.append(parent_dir)
from service import history


Expand Down
12 changes: 12 additions & 0 deletions tests/test_history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@

23/11 14:31:50 User: How are you?
23/11 14:31:50 Assistant: Mocked response

24/11 19:38:05 User: How are you?
24/11 19:38:05 Assistant: Mocked response

24/11 19:38:43 User: How are you?
24/11 19:38:43 Assistant: Mocked response

24/11 19:39:11 User: How are you?
24/11 19:39:11 Assistant: Mocked response

24/11 19:39:51 User: How are you?
24/11 19:39:51 Assistant: Mocked response
47 changes: 0 additions & 47 deletions tests/test_module.py

This file was deleted.

19 changes: 0 additions & 19 deletions tests/test_profile.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_register.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_get_entries_for_email(self):
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)

result = get_entries_for_email(mock_db, email, start_date, end_date)
result, [] = get_entries_for_email(mock_db, email, start_date, end_date)

self.assertEqual(result, entries_data)

Expand Down
42 changes: 29 additions & 13 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ def get_response(chat_history, name, chatgpt_output, userText, history_file,
return chat(chat_history, name, chatgpt_output, userText, history_file,
impersonated_role, explicit_input)

def get_entries_for_email(db, email, start_date, end_date):

# Query to find entries for a given email within the date range
query = {
'email': email,
'date': {'$gte': start_date, '$lte': end_date}
}

# Fetch entries from MongoDB
entries = db.calories.find(query)

return list(entries)

def calc_bmi(weight, height):
return round((weight / ((height / 100)**2)), 2)
Expand All @@ -112,4 +100,32 @@ def get_bmi_category(bmi):
elif bmi < 29.9:
return 'Overweight'
else:
return 'Obese'
return 'Obese'

def get_entries_for_email(db, email, start_date, end_date):

# Query to find entries for a given email within the date range
query = {
'email': email,
'date': {'$gte': start_date, '$lte': end_date}
}

# Fetch entries from MongoDB
entries_cal = db.calories.find(query)
entries_workout = db.workout.find(query)

return list(entries_cal), list(entries_workout)

def total_calories_to_burn(target_weight: int, current_weight: int):
return int((target_weight - current_weight) * 7700)


def calories_to_burn(target_calories: int, current_calories: int,
target_date: datetime, start_date: datetime):
actual_current_calories = current_calories - (
(datetime.today() - start_date).days * 2000)

new_target = target_calories - actual_current_calories

days_remaining = (target_date - datetime.today()).days
return int(new_target / days_remaining)

0 comments on commit 4b041bb

Please sign in to comment.