Skip to content

Commit ab1d671

Browse files
authored
Feature/weightt (#277)
1 parent ad65015 commit ab1d671

File tree

9 files changed

+159
-4
lines changed

9 files changed

+159
-4
lines changed

app/database/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class User(Base):
4848
privacy = Column(String, default="Private", nullable=False)
4949
is_manager = Column(Boolean, default=False)
5050
language_id = Column(Integer, ForeignKey("languages.id"))
51+
target_weight = Column(Float, nullable=True)
5152

5253
owned_events = relationship(
5354
"Event",

app/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create_tables(engine, psql_environment):
4343
about_us, agenda, calendar, categories, celebrity, credits,
4444
currency, dayview, email, event, export, four_o_four, friendview,
4545
google_connect, invitation, joke, login, logout, profile,
46-
register, search, telegram, user, weekview, whatsapp,
46+
register, search, telegram, user, weekview, weight, whatsapp,
4747
)
4848

4949
json_data_loader.load_to_database(next(get_db()))
@@ -74,12 +74,11 @@ async def swagger_ui_redirect():
7474
credits.router,
7575
currency.router,
7676
dayview.router,
77-
friendview.router,
78-
weekview.router,
7977
email.router,
8078
event.router,
8179
export.router,
8280
four_o_four.router,
81+
friendview.router,
8382
google_connect.router,
8483
invitation.router,
8584
joke.router,
@@ -91,6 +90,8 @@ async def swagger_ui_redirect():
9190
search.router,
9291
telegram.router,
9392
user.router,
93+
weekview.router,
94+
weight.router,
9495
whatsapp.router,
9596
]
9697

app/routers/weight.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from typing import Union
2+
3+
from fastapi import APIRouter, Depends, Request
4+
from sqlalchemy.orm import Session
5+
from starlette.responses import RedirectResponse
6+
7+
from app.database.models import User
8+
from app.dependencies import get_db
9+
from app.dependencies import templates
10+
11+
12+
router = APIRouter(tags=["weight"],)
13+
14+
15+
@router.get("/weight")
16+
async def get_weight(
17+
request: Request,
18+
session: Session = Depends(get_db),
19+
target: Union[float, None] = None,
20+
current_weight: Union[float, None] = None,
21+
):
22+
23+
# TODO Waiting for user registration
24+
user_id = 1
25+
user = session.query(User).filter_by(id=user_id).first()
26+
target = user.target_weight
27+
if current_weight:
28+
return RedirectResponse(url='/')
29+
return templates.TemplateResponse("weight.html", {
30+
"request": request,
31+
"target": target,
32+
"current_weight": current_weight,
33+
}
34+
)
35+
36+
37+
@router.post("/weight")
38+
async def weight(
39+
request: Request,
40+
session: Session = Depends(get_db)):
41+
user_id = 1
42+
user = session.query(User).filter_by(id=user_id).first()
43+
data = await request.form()
44+
target = data['target']
45+
current_weight = data['current_weight']
46+
if target:
47+
user.target_weight = target
48+
session.commit()
49+
else:
50+
target = user.target_weight
51+
if not target:
52+
target = current_weight
53+
way_to_go = float(current_weight) - float(target)
54+
way_to_go = round(way_to_go, 2)
55+
if way_to_go > 0:
56+
way_message = f"Weight to lose: {way_to_go} Kg"
57+
elif way_to_go < 0:
58+
way_to_go = abs(way_to_go)
59+
way_message = f"Weight to add: {way_to_go} Kg"
60+
else:
61+
way_message = f"Great! You have reached your goal: {target} Kg"
62+
63+
return templates.TemplateResponse("weight.html", {
64+
"request": request,
65+
"target": target,
66+
"current_weight": current_weight,
67+
"way_message": way_message
68+
}
69+
)

app/static/weight.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
.weight {
3+
display: block;
4+
margin-top: 1rem;
5+
margin-left: 1rem;
6+
}
7+
8+
9+
.weight-message {
10+
display: block;
11+
margin-top: 1rem;
12+
margin-left: 1rem;
13+
}

app/templates/base.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<li class="nav-item"></li>
6262
<a class="nav-link" href="{{ url_for('category_color_insert') }}">Create Categories</a>
6363
</li>
64+
<li class="nav-item">
65+
<a class="nav-link" href="{{ url_for('weight') }}">Weight</a>
66+
</li>
6467
<li class="nav-item">
6568
<a class="nav-link" href="{{ url_for('about') }}">{{ gettext("About Us") }}</a>
6669
</li>

app/templates/weight.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "base.html" %}
2+
{% block content %}
3+
<form method="POST" action="/weight/">
4+
<div class="mb-3 weight target_weight"><br>
5+
<label for="target">Target</label><br>
6+
<input class="target_weight" type="text" id="target" name="target" value={{ target }}><br>
7+
<label for="current_weight"> Current Weight</label><br>
8+
<input class="target_weight" type="text" id="current_weight" name="current_weight" value={{ current_weight }}>
9+
<input class="target_weight" type="submit" value="calculate">
10+
</div>
11+
</form>
12+
<div>
13+
<div class="weight-message">
14+
{% if way_message %}
15+
<a> {{ way_message }} </a>
16+
{% endif %}
17+
</div>
18+
</div>
19+
<link href="{{ url_for('static', path='/weight.css') }}" rel="stylesheet">
20+
{% endblock %}

tests/client_fixture.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from app.database.models import Base, User
99
from app.routers import (
1010
agenda, categories, event, friendview, google_connect,
11-
invitation, profile
11+
invitation, profile, weight,
1212
)
1313
from app.routers.salary import routes as salary
1414
from tests import security_testing_routes
@@ -54,6 +54,11 @@ def friendview_test_client() -> Generator[TestClient, None, None]:
5454
yield from create_test_client(friendview.get_db)
5555

5656

57+
@pytest.fixture(scope="session")
58+
def weight_test_client() -> Generator[TestClient, None, None]:
59+
yield from create_test_client(weight.get_db)
60+
61+
5762
@pytest.fixture(scope="session")
5863
def event_test_client() -> Generator[TestClient, None, None]:
5964
yield from create_test_client(event.get_db)

tests/test_weight.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TestWeight:
2+
WEIGHT = "/weight"
3+
WAY_TO_GO = b'15.5'
4+
"""Target weight set to 60"""
5+
6+
@staticmethod
7+
def test_weight_ok(weight_test_client, user, session):
8+
new_weight = {'target': user.target_weight, 'current_weight': 70}
9+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
10+
assert resp.ok
11+
12+
@staticmethod
13+
def test_need_to_lose_weight(weight_test_client, user):
14+
new_weight = {'target': user.target_weight, 'current_weight': 80}
15+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
16+
assert b"Weight to lose" in resp.content
17+
18+
@staticmethod
19+
def test_need_to_gain_weight(weight_test_client, user):
20+
new_weight = {'target': user.target_weight, 'current_weight': 50}
21+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
22+
assert b"Weight to add" in resp.content
23+
24+
@staticmethod
25+
def test_reached_the_goal(weight_test_client, user):
26+
new_weight = {'target': user.target_weight, 'current_weight': 60}
27+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
28+
assert b"reached your goal" in resp.content
29+
30+
@staticmethod
31+
def test_way_to_go(weight_test_client, user):
32+
new_weight = {'target': user.target_weight, 'current_weight': 75.5}
33+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
34+
assert TestWeight.WAY_TO_GO in resp.content
35+
36+
@staticmethod
37+
def test_no_target_entered(weight_test_client, user):
38+
"""In this case, the target set to current weight"""
39+
40+
new_weight = {'target': '', 'current_weight': 60}
41+
resp = weight_test_client.post(TestWeight.WEIGHT, data=new_weight)
42+
assert b"reached your goal" in resp.content

tests/user_fixture.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def user(session: Session) -> Generator[User, None, None]:
1616
password="test_password",
1717
email="test.email@gmail.com",
1818
language_id=1,
19+
target_weight=60,
1920
)
2021
yield mock_user
2122
delete_instance(session, mock_user)

0 commit comments

Comments
 (0)