-
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
base: develop
Are you sure you want to change the base?
Feature/timer #244
Changes from 23 commits
779de2d
6370ef2
7f38da9
b587f6c
53bca85
079e6f5
8bb9b1e
c91fbf0
66eed5d
7d74135
38a47e2
92c6f00
d82d950
0c98206
5531a43
d3ea415
bc58d93
c75f2ba
fa6d739
b56f3da
af7c971
a11cb5a
cc29fc1
91e85bb
2823cae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from datetime import datetime | ||
from typing import Dict, List, 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 = get_only_future_events( | ||
sort_by_date(get_all_user_events(session, user_id)) | ||
) | ||
if events: | ||
return events[0] | ||
|
||
|
||
def get_only_future_events(events: List[Event]) -> List[Event]: | ||
future_events = [] | ||
for event in events: | ||
if event.start >= datetime.now(): | ||
future_events.append(event) | ||
return future_events | ||
|
||
|
||
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} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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) |
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); | ||
fandomario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Output the result to base.html in an element with id="eventtimer" | ||
document.getElementById("eventtimer").innerText = "Upcoming event in: " + days + "d " + hours + "h " | ||
+ minutes + "m " + seconds + "s "; | ||
// Countdown had finished | ||
if (distance < 0) { | ||
clearInterval(timerInterval); | ||
document.getElementById("eventtimer").innerText = "Your Event Starts NOW:)"; | ||
} | ||
}, 1000); | ||
} ); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", countdownTimer); |
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(next_week_event, session): | ||
next_event = get_next_user_event( | ||
session=session, | ||
user_id=next_week_event.owner_id, | ||
) | ||
assert next_event == next_week_event | ||
|
||
|
||
def test_time_left(next_week_event, session): | ||
time_left = get_next_user_event_start_time( | ||
session=session, | ||
user_id=next_week_event.owner_id, | ||
) | ||
assert isinstance(time_left["timer"], str) |
Uh oh!
There was an error while loading. Please reload this page.