Skip to content

Feature/timer #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
779de2d
feat: add i18n support (#115)
Gonzom Jan 30, 2021
6370ef2
Revert "feat: add i18n support (#115)" (#161)
yammesicka Jan 30, 2021
7f38da9
Update our production site. (#209)
yammesicka Feb 5, 2021
b587f6c
fix: tests
fandomario Feb 5, 2021
53bca85
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 5, 2021
079e6f5
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 8, 2021
8bb9b1e
feat: countdown timer to next event on profile page
fandomario Feb 9, 2021
c91fbf0
fix: flake8
fandomario Feb 9, 2021
66eed5d
fix: flake8 2
fandomario Feb 9, 2021
7d74135
fix: flake8 3
fandomario Feb 9, 2021
38a47e2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 9, 2021
92c6f00
fix: fix bugs in routers\event.py and in config,py
fandomario Feb 9, 2021
d82d950
fix: fix variables names
fandomario Feb 17, 2021
0c98206
fix: according to the CR
fandomario Feb 17, 2021
5531a43
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 17, 2021
d3ea415
fix: fix tests
fandomario Feb 17, 2021
bc58d93
fix: according to CR
fandomario Feb 18, 2021
c75f2ba
Merge branch 'develop' into feature/timer
fandomario Feb 18, 2021
fa6d739
fix: according to CR
fandomario Feb 20, 2021
b56f3da
Merge branch 'feature/timer' of https://github.com/fandomario/calenda…
fandomario Feb 20, 2021
af7c971
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 20, 2021
a11cb5a
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 20, 2021
cc29fc1
fix: fix a bug after QA :)
fandomario Feb 20, 2021
91e85bb
fix: improoving query, adding current_user
fandomario Feb 22, 2021
2823cae
fix: fix conflicts
fandomario Feb 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Settings(BaseSettings):
# GENERAL
DOMAIN = 'Our-Domain'


# DATABASE
DEVELOPMENT_DATABASE_STRING = "sqlite:///./dev.db"
# Set the following True if working on PSQL environment or set False otherwise
Expand Down
21 changes: 21 additions & 0 deletions app/internal/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from app.routers.event import sort_by_date
from app.routers.user import get_all_user_events
from sqlalchemy.orm import Session
from app.database.models import Event


def get_next_user_event(session: Session, user_id: int) -> Event:
events_as_list = list(sort_by_date(get_all_user_events(session, user_id)))
next_event = None
if len(events_as_list) > 0:
next_event = events_as_list[0]
return next_event


def get_next_user_event_start_time(session: Session, user_id: int):
next_event = get_next_user_event(session, user_id)
if next_event is None:
timer_to_next_event = None
else:
timer_to_next_event = next_event.start.strftime("%Y-%m-%d %H:%M")
return {"timer": timer_to_next_event}
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create_tables(engine, psql_environment):

from app.routers import ( # noqa: E402
agenda, calendar, categories, currency, dayview, email,
event, invitation, profile, search, telegram, whatsapp
event, invitation, profile, search, telegram, timer, whatsapp
)

json_data_loader.load_to_db(next(get_db()))
Expand All @@ -50,6 +50,7 @@ def create_tables(engine, psql_environment):
profile.router,
search.router,
telegram.router,
timer.router,
whatsapp.router,
]

Expand Down
2 changes: 1 addition & 1 deletion app/routers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def delete_event(event_id: int,
url="/calendar", status_code=status.HTTP_200_OK)


def is_date_before(start_time: datetime, end_time: datetime) -> bool:
def is_date_before(start_time: dt, end_time: dt) -> bool:
"""Check if the start_date is smaller then the end_time"""
try:
return start_time < end_time
Expand Down
14 changes: 14 additions & 0 deletions app/routers/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

from fastapi import APIRouter, Depends, Request
from app.internal.timer import get_next_user_event_start_time
from app.database.models import User
from app.database.database import get_db


router = APIRouter()


@router.get("/timer")
def timer(request: Request, session=Depends(get_db)):
user = session.query(User).filter_by(id=1).first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This route queries the event of the user. It can make the system go really slow.

  1. Prefer to use Depends(current_user) from Kobi's login system to get the user details without querying the database.
  2. [Optional]: To make this commit merge-ready, add last_added_event to the localStorage. If it exists, take it from the localStorage and Voilà! You don't have to access the database (you don't even have to send requests to the server). If it doesn't, query and add it to the localStorage. Note that you need to update the localStorage if the user add a new event.

Copy link
Author

@fandomario fandomario Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Done :)
  2. The countdown timer is counting till the current user's next event and not necessarily to the last added event.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand it. The note means to veck if the new event becomes the last event. If it does, it should replace the next event in the localStorage

return get_next_user_event_start_time(session, user.id)
25 changes: 25 additions & 0 deletions app/static/timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//credit for the countdowntimer: https://gist.github.com/Mak-Pro/0e1194d0f8696489a5c8bac72c8fa300
fetch('/timer')
.then(response => response.json())
.then(data => {

var countDownDate = new Date(data.timer).getTime();

// Update the countdown every 1 second
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result to base.html in an element with id="eventtimer"
document.getElementById("eventtimer").innerHTML = "Next Event will be in: " + days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// Countdown had finished
if (distance < 0) {
clearInterval(x);
document.getElementById("eventtimer").innerHTML = "Your Event Starts NOW:)";
}
}, 1000);
} );
4 changes: 4 additions & 0 deletions app/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ <h6 class="card-title text-center mb-1">{{ user.full_name }}</h6>
<a class="text-decoration-none text-secondary" href="#">Your feature</a>
</li>
</ul>
<div>
<p id="eventtimer"></p>
<script src="{{ url_for('static', path='/timer.js') }}"></script>
</div>
</div>
</div>
<!-- End Features card -->
Expand Down
1 change: 1 addition & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_profile_page(profile_test_client):
assert b'FakeName' in data
assert b'Happy new user!' in data
assert b'On This Day' in data
assert b'Event' in data


def test_update_user_fullname(profile_test_client):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from app.internal.timer import get_next_user_event
from app.internal.timer import get_next_user_event_start_time


def test_get_last_event_success(event, session):
next_event = get_next_user_event(
session=session,
user_id=event.owner_id,
)
assert next_event == event


def test_time_left(event, session):
time_left = get_next_user_event_start_time(
session=session,
user_id=event.owner_id,
)
assert type(time_left["timer"]) is str