-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add a daily inspirational quote #128
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
Changes from 8 commits
4d79f12
82134d4
d909adb
bc19f4c
dbd8d85
5adb4f6
e499626
dbea85b
392e5bd
91c832f
81b377b
954f8f4
ef8819b
21bccfb
70edfc4
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,23 @@ | ||
from datetime import date | ||
import random | ||
from typing import Optional | ||
|
||
from app.database.models import Quote | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
TOTAL_DAYS = 366 | ||
|
||
|
||
def quote_per_day( | ||
session: Session, date: date = date.today() | ||
) -> Optional[Quote]: | ||
"""This function provides a daily quote, relevant to the current | ||
day of the year. The quote is randomally selected from a set | ||
of quotes matching to the given day""" | ||
day_num = date.timetuple().tm_yday | ||
quotes = session.query(Quote).filter( | ||
Quote.id % TOTAL_DAYS == day_num).all() | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(quotes) > 0: | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return random.choice(quotes) | ||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import json | ||
from typing import Dict, List | ||
|
||
from app.database.models import Quote | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
|
||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def get_quotes_from_json() -> List[Dict]: | ||
"""This function reads all of the daily quotes from a specific JSON file. | ||
The JSON file content is copied from the free API: | ||
'https://type.fit/api/quotes'. I saved the content so the API won't be | ||
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. Id suggest adding docs in the Google doc string style https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html 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. thanks! :) |
||
called every time the app is initialized.""" | ||
try: | ||
with open('app/resources/quotes.json', 'r') as f: | ||
quotes_list = json.load(f) | ||
except (IOError, ValueError): | ||
return [] | ||
return quotes_list | ||
|
||
|
||
def add_quotes_to_db(session: Session) -> None: | ||
"""This function reads the quotes and inserts them into the db""" | ||
all_quotes = get_quotes_from_json() | ||
quotes_objects = [ | ||
Quote(text=quote['text'], author=quote['author']) | ||
for quote in all_quotes | ||
] | ||
session.add_all(quotes_objects) | ||
session.commit() | ||
|
||
|
||
def is_quotes_table_empty(session: Session) -> bool: | ||
return session.query(Quote).count() == 0 | ||
zohary89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def load_daily_quotes(session: Session) -> None: | ||
"""This function loads the daily quotes to the db, | ||
if they weren't already loaden""" | ||
if is_quotes_table_empty(session): | ||
add_quotes_to_db(session) |
Uh oh!
There was an error while loading. Please reload this page.