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 18 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
23 changes: 23 additions & 0 deletions app/internal/timer.py
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}
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_tables(engine, psql_environment):
about_us, agenda, calendar, categories, celebrity, credits,
currency, dayview, email, event, export, four_o_four, friendview,
google_connect, invitation, login, logout, profile,
register, search, telegram, user, weekview, whatsapp,
register, search, telegram, timer, user, weekview, whatsapp,
)

json_data_loader.load_to_database(next(get_db()))
Expand Down Expand Up @@ -89,6 +89,7 @@ async def swagger_ui_redirect():
salary.router,
search.router,
telegram.router,
timer.router,
user.router,
whatsapp.router,
]
Expand Down
13 changes: 13 additions & 0 deletions app/routers/timer.py
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()
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)
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 => {

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 "
+ minutes + "m " + seconds + "s ";
// Countdown had finished
if (distance < 0) {
clearInterval(timerInterval);
document.getElementById("eventtimer").innerHTML = "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 @@ -75,6 +75,7 @@
<!-- This bootstrap version is needed here because of the toggler bug in the beta version-->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/js/bootstrap.bundle.min.js" integrity="sha384-BOsAfwzjNJHrJ8cZidOg56tcQWfp6y72vEJ8xQ9w6Quywb24iOsW913URv1IS4GD" crossorigin="anonymous"></script>
<script src="{{ url_for('static', path='/horoscope.js') }}"></script>
<script src="{{ url_for('static', path='/timer.js') }}"></script>
</body>

</html>
41 changes: 35 additions & 6 deletions app/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,44 @@ <h6 class="card-title text-center mb-1">{{ user.full_name }}</h6>
<!-- End Features card -->

</div>

<!-- Center -->
<div class="col-5">
<div class="container" id="daily_horoscope">
<!-- Center -->
<div class="col-5">
<div class="container" id="daily_horoscope">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

<!-- Features card -->
<div class="card card-profile">
<div class="card-body">
<p class="card-title">
Explore more features
</p>
<ul class="list-group">
{% if not user.telegram_id %}
<li class="list-group-item list-group-item-action no-border">
<a class="text-decoration-none text-success" href="https://t.me/pylander_bot" target="_blank">
<strong>Try PyLander bot</strong>
</a>
</li>
{% endif %}
<li class="list-group-item list-group-item-action no-border">
<a class="text-decoration-none text-secondary" href="#">Export my calendar</a>
</li>
<li class="list-group-item list-group-item-action no-border">
<a class="text-decoration-none text-secondary" href="#">Your feature</a>
</li>
</ul>
<div>
<p id="eventtimer"></p>
</div>
</div>
</div>
<!-- End Features card -->
</div>
<!-- Center -->
<div class="col-5">

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(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