-
Notifications
You must be signed in to change notification settings - Fork 52
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
fandomario
wants to merge
25
commits into
PythonFreeCourse:develop
Choose a base branch
from
fandomario:feature/timer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/timer #244
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
779de2d
feat: add i18n support (#115)
Gonzom 6370ef2
Revert "feat: add i18n support (#115)" (#161)
yammesicka 7f38da9
Update our production site. (#209)
yammesicka b587f6c
fix: tests
fandomario 53bca85
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario 079e6f5
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario 8bb9b1e
feat: countdown timer to next event on profile page
fandomario c91fbf0
fix: flake8
fandomario 66eed5d
fix: flake8 2
fandomario 7d74135
fix: flake8 3
fandomario 38a47e2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario 92c6f00
fix: fix bugs in routers\event.py and in config,py
fandomario d82d950
fix: fix variables names
fandomario 0c98206
fix: according to the CR
fandomario 5531a43
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario d3ea415
fix: fix tests
fandomario bc58d93
fix: according to CR
fandomario c75f2ba
Merge branch 'develop' into feature/timer
fandomario fa6d739
fix: according to CR
fandomario b56f3da
Merge branch 'feature/timer' of https://github.com/fandomario/calenda…
fandomario af7c971
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario a11cb5a
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario cc29fc1
fix: fix a bug after QA :)
fandomario 91e85bb
fix: improoving query, adding current_user
fandomario 2823cae
fix: fix conflicts
fandomario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Dict, Optional | ||
|
||
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) -> Optional[Event]: | ||
events = list(sort_by_date(get_all_user_events(session, user_id))) | ||
if events: | ||
return events[0] | ||
|
||
|
||
def get_next_user_event_start_time( | ||
session: Session, | ||
user_id: int | ||
) -> Dict[str, Optional[str]]: | ||
next_event = get_next_user_event(session, user_id) | ||
timer_to_next_event = None | ||
if next_event is not None: | ||
timer_to_next_event = next_event.start.strftime("%Y-%m-%d %H:%M") | ||
return {"timer": timer_to_next_event} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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.dependencies 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() | ||
return get_next_user_event_start_time(session, user.id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//credit for the countdowntimer: https://gist.github.com/Mak-Pro/0e1194d0f8696489a5c8bac72c8fa300 | ||
function countdownTimer() { | ||
fetch('/timer') | ||
.then(response => response.json()) | ||
.then(data => { | ||
|
||
let countDownDate = new Date(data.timer).getTime(); | ||
|
||
// Update the countdown every 1 second | ||
const timerInterval = setInterval(function() { | ||
const now = new Date().getTime(); | ||
const distance = countDownDate - now; | ||
const days = Math.floor(distance / (1000 * 60 * 60 * 24)); | ||
let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | ||
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | ||
let seconds = Math.floor((distance % (1000 * 60)) / 1000); | ||
// Output the result to base.html in an element with id="eventtimer" | ||
document.getElementById("eventtimer").innerHTML = "Upcoming event in: " + days + "d " + hours + "h " | ||
fandomario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ minutes + "m " + seconds + "s "; | ||
// Countdown had finished | ||
if (distance < 0) { | ||
clearInterval(timerInterval); | ||
document.getElementById("eventtimer").innerHTML = "Your Event Starts NOW:)"; | ||
} | ||
fandomario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, 1000); | ||
} ); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", countdownTimer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
fandomario marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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