-
Notifications
You must be signed in to change notification settings - Fork 52
feat: Add zodiac signs to the day & month view + refactor json data loader #155
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
Merged
yammesicka
merged 15 commits into
PythonFreeCourse:develop
from
zohary89:feature/zodiac-signs
Feb 5, 2021
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
39e5d00
feat: Add zodiac signs to the day/month view +
zohary89 962739e
update requirements
zohary89 25b35a9
try to fix tests
zohary89 4ccfebc
fix flake error
zohary89 7e31139
cancle change in tests- to see if errors happen
zohary89 507fae3
fix test again- in asyncio_fixture
zohary89 090bb09
Merge branch 'develop' of
zohary89 115f453
fix - changes requested
zohary89 62f1cb7
Merge branch 'develop' of http://github.com/PythonFreeCourse/calendar…
zohary89 b422f2a
Merge branch 'develop' of
zohary89 e719290
fix exception
zohary89 9b49b5a
change png images to svg
zohary89 7820580
Merge branch 'develop' of
zohary89 355bbd5
remove thumbs.db
zohary89 20dc1b3
update requirements
zohary89 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
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,57 @@ | ||
import json | ||
import os | ||
from typing import Dict, List, Callable, Union | ||
|
||
from loguru import logger | ||
from sqlalchemy.orm import Session | ||
|
||
from app.database.database import Base | ||
from app.database.models import Quote, Zodiac | ||
from app.internal import daily_quotes, zodiac | ||
|
||
|
||
def get_data_from_json(path: str) -> List[Dict[str, Union[str, int, None]]]: | ||
"""This function reads all of the data from a specific JSON file. | ||
The json file consists of list of dictionaries""" | ||
try: | ||
with open(path, 'r') as f: | ||
json_content_list = json.load(f) | ||
except (IOError, ValueError): | ||
file_name = os.path.basename(path) | ||
logger.exception( | ||
f"An error occurred during reading of json file: {file_name}") | ||
return [] | ||
zohary89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return json_content_list | ||
|
||
|
||
def is_table_empty(session: Session, table: Base) -> bool: | ||
return session.query(table).count() == 0 | ||
|
||
|
||
def load_data( | ||
session: Session, path: str, | ||
table: Base, object_creator_function: Callable) -> None: | ||
"""This function loads the specific data to the db, | ||
if it wasn't already loaded""" | ||
if not is_table_empty(session, table): | ||
return None | ||
json_objects_list = get_data_from_json(path) | ||
objects = [ | ||
object_creator_function(json_object) | ||
for json_object in json_objects_list] | ||
session.add_all(objects) | ||
session.commit() | ||
|
||
|
||
def load_to_db(session) -> None: | ||
"""This function loads all the data for features | ||
based on pre-defind json data""" | ||
load_data( | ||
session, 'app/resources/zodiac.json', | ||
Zodiac, zodiac.create_zodiac_object) | ||
load_data( | ||
session, 'app/resources/quotes.json', | ||
Quote, daily_quotes.create_quote_object) | ||
"""The quotes JSON file content is copied from the free API: | ||
'https://type.fit/api/quotes'. I saved the content so the API won't be | ||
called every time the app is initialized.""" |
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
from datetime import date | ||
from typing import Dict, Union | ||
|
||
from sqlalchemy.orm import Session | ||
from sqlalchemy import and_, or_ | ||
|
||
from app.database.models import Zodiac | ||
|
||
|
||
def create_zodiac_object(zodiac_fields: Dict[str, Union[str, int]]) -> Zodiac: | ||
"""This function create a zodiac object from given fields dictionary. | ||
It is used for adding the data from the json into the db""" | ||
return Zodiac( | ||
name=zodiac_fields['name'], | ||
start_month=zodiac_fields['start_month'], | ||
start_day_in_month=zodiac_fields['start_day_in_month'], | ||
end_month=zodiac_fields['end_month'], | ||
end_day_in_month=zodiac_fields['end_day_in_month'] | ||
) | ||
|
||
|
||
def get_zodiac_of_day(session: Session, date: date) -> Zodiac: | ||
"""This function return a zodiac object | ||
according to the current day.""" | ||
first_month_of_sign_filter = and_( | ||
Zodiac.start_month == date.month, | ||
Zodiac.start_day_in_month <= date.day) | ||
second_month_of_sign_filter = and_( | ||
Zodiac.end_month == date.month, | ||
Zodiac.end_day_in_month >= date.day) | ||
zodiac_obj = session.query(Zodiac).filter( | ||
or_(first_month_of_sign_filter, second_month_of_sign_filter)).first() | ||
return zodiac_obj | ||
|
||
|
||
# TODO: Call this function from the month view | ||
def get_zodiac_of_month(session: Session, date: date) -> Zodiac: | ||
"""This function return a zodiac object | ||
according to the current month.""" | ||
zodiac_obj = session.query(Zodiac).filter( | ||
Zodiac.end_month == date.month).first() | ||
return zodiac_obj |
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,86 @@ | ||
[ | ||
{ | ||
"name": "Capricorn", | ||
"start_month": 12, | ||
"start_day_in_month": 22, | ||
"end_month": 1, | ||
"end_day_in_month": 19 | ||
}, | ||
{ | ||
"name": "Aquarius", | ||
"start_month": 1, | ||
"start_day_in_month": 20, | ||
"end_month": 2, | ||
"end_day_in_month": 18 | ||
}, | ||
{ | ||
"name": "Pisces", | ||
"start_month": 2, | ||
"start_day_in_month": 19, | ||
"end_month": 3, | ||
"end_day_in_month": 20 | ||
}, | ||
{ | ||
"name": "Aries", | ||
"start_month": 3, | ||
"start_day_in_month": 21, | ||
"end_month": 4, | ||
"end_day_in_month": 19 | ||
}, | ||
{ | ||
"name": "Taurus", | ||
"start_month": 4, | ||
"start_day_in_month": 20, | ||
"end_month": 5, | ||
"end_day_in_month": 20 | ||
}, | ||
{ | ||
"name": "Gemini", | ||
"start_month": 5, | ||
"start_day_in_month": 21, | ||
"end_month": 6, | ||
"end_day_in_month": 20 | ||
}, | ||
{ | ||
"name": "Cancer", | ||
"start_month": 6, | ||
"start_day_in_month": 21, | ||
"end_month": 7, | ||
"end_day_in_month": 22 | ||
}, | ||
{ | ||
"name": "Leo", | ||
"start_month": 7, | ||
"start_day_in_month": 23, | ||
"end_month": 8, | ||
"end_day_in_month": 22 | ||
}, | ||
{ | ||
"name": "Virgo", | ||
"start_month": 8, | ||
"start_day_in_month": 23, | ||
"end_month": 9, | ||
"end_day_in_month": 22 | ||
}, | ||
{ | ||
"name": "Libra", | ||
"start_month": 9, | ||
"start_day_in_month": 23, | ||
"end_month": 10, | ||
"end_day_in_month": 22 | ||
}, | ||
{ | ||
"name": "Scorpio", | ||
"start_month": 10, | ||
"start_day_in_month": 23, | ||
"end_month": 11, | ||
"end_day_in_month": 21 | ||
}, | ||
{ | ||
"name": "Sagittarius", | ||
"start_month": 11, | ||
"start_day_in_month": 22, | ||
"end_month": 12, | ||
"end_day_in_month": 21 | ||
} | ||
] |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.