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 all 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 @@ -18,6 +18,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
26 changes: 26 additions & 0 deletions app/internal/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from datetime import datetime
from typing import Dict, List, Optional

from sqlalchemy.orm import Session
from app.database.models import Event, UserEvent


def get_next_user_event(session: Session, user_id: int) -> List[Event]:
next_user_event = (
session.query(Event).join(UserEvent)
.filter(UserEvent.user_id == user_id)
.filter(Event.start >= datetime.now())
.order_by(Event.start)
)
return next_user_event[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}
2 changes: 2 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def create_tables(engine, psql_environment):
register,
search,
telegram,
timer,
user,
weekview,
weight,
Expand Down Expand Up @@ -121,6 +122,7 @@ async def swagger_ui_redirect():
salary.router,
search.router,
telegram.router,
timer.router,
user.router,
weekview.router,
weight.router,
Expand Down
19 changes: 19 additions & 0 deletions app/routers/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Dict, Optional

from fastapi import APIRouter, Depends, Request
from app.internal.security.dependancies import current_user, schema
from app.internal.timer import get_next_user_event_start_time
from app.dependencies import get_db


router = APIRouter()


@router.get("/timer")
def timer(
request: Request,
session=Depends(get_db),
user: schema.CurrentUser = Depends(current_user)
) -> Dict[str, Optional[str]]:

return get_next_user_event_start_time(session, user.user_id)
29 changes: 29 additions & 0 deletions app/static/timer.js
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 => {

const 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));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
// 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);
1 change: 1 addition & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<script src="{{ url_for('static', path='/horoscope.js') }}"></script>
<script type="text/javascript" src="{{ url_for( 'static', path='/audio_settings.js' ) }}"></script>
<script src="{{ url_for('static', path='/joke.js') }}"></script>
<script src="{{ url_for('static', path='/timer.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<audio id="my-audio" muted="true"></audio>
<audio id="sfx" muted="true"></audio>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Export my calendar
</a>

<p id="eventtimer"></p>
<div class="collapse" id="export-calendar">
<form method="GET" action="{{ url_for('export') }}">
<label for="start_date">{{ gettext("From") }}</label><br>
Expand Down
1 change: 1 addition & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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(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)