Skip to content
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

Feature/parasha #260

Open
wants to merge 88 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
25ad676
add parasha feature
elor555 Feb 11, 2021
eed8d7c
update
elor555 Feb 11, 2021
eaadce0
update
elor555 Feb 11, 2021
ecff42d
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 11, 2021
1b1faf2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 11, 2021
5646a56
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 13, 2021
407d692
ines starting
elor555 Feb 13, 2021
3ea2768
dd space
elor555 Feb 13, 2021
c3aa08a
fix farts of flake8
elor555 Feb 13, 2021
819f2b2
edit
elor555 Feb 13, 2021
e5bfc87
edit
elor555 Feb 13, 2021
75805fc
edit5
elor555 Feb 13, 2021
91fff6f
edit6
elor555 Feb 13, 2021
161cb98
edit api
elor555 Feb 13, 2021
91b9fe3
edit by yam
elor555 Feb 14, 2021
cde7aa6
edit by yam2
elor555 Feb 14, 2021
0ab998e
edit flake8
elor555 Feb 14, 2021
e53c37d
edit flake8
elor555 Feb 14, 2021
2f7b60a
edit flake8
elor555 Feb 14, 2021
433539f
edit flake8
elor555 Feb 14, 2021
58988ba
edit pytest
elor555 Feb 14, 2021
871a6d0
edit
elor555 Feb 15, 2021
5cb60fe
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 15, 2021
eca6849
edit flake8
elor555 Feb 15, 2021
032e204
edit flake8
elor555 Feb 15, 2021
6ae3d97
edit after pull
elor555 Feb 16, 2021
05bf66f
edit after flake8
elor555 Feb 16, 2021
9999ac1
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 16, 2021
497a365
edit after pytest
elor555 Feb 16, 2021
18ed51d
edit after pytest
elor555 Feb 16, 2021
8d7a6c1
edit after pytest
elor555 Feb 16, 2021
b072699
edit after pytest
elor555 Feb 16, 2021
60386ce
edit after pytest
elor555 Feb 16, 2021
2044f70
edit after pytest
elor555 Feb 16, 2021
992841a
edit after pytest
elor555 Feb 16, 2021
f588ded
edit after pytest
elor555 Feb 16, 2021
76472c1
edit after pytest
elor555 Feb 16, 2021
1a2ba69
edit after pytest
elor555 Feb 16, 2021
c4c7227
edit after pytest
elor555 Feb 16, 2021
3042a61
check
elor555 Feb 16, 2021
8fbee77
edit after comments
elor555 Feb 16, 2021
2c16624
edit after flake8
elor555 Feb 16, 2021
e08ee63
edit after pytest
elor555 Feb 16, 2021
900d4c5
edit after pytest
elor555 Feb 16, 2021
d734bc1
edit after pytest
elor555 Feb 16, 2021
bee1cbc
edit after pytest
elor555 Feb 16, 2021
f5b8368
edit after pytest
elor555 Feb 16, 2021
48b3c62
edit after flake8
elor555 Feb 17, 2021
1b5d932
edit after flake8
elor555 Feb 17, 2021
495539b
edit after pull
elor555 Feb 17, 2021
bd77d76
edit after pull
elor555 Feb 18, 2021
e1d6e74
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 18, 2021
9754ba2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 18, 2021
b132c7a
edit after pull
elor555 Feb 19, 2021
c4093e1
edit after pull
elor555 Feb 19, 2021
ba8d840
edit after pull
elor555 Feb 19, 2021
35722bc
edit after pull
elor555 Feb 19, 2021
c6c01fb
edit after pull
elor555 Feb 19, 2021
535844e
edit after pull
elor555 Feb 19, 2021
9d0277e
edit
elor555 Feb 20, 2021
d116f25
edit
elor555 Feb 21, 2021
1d50cb1
edit
elor555 Feb 21, 2021
4c16f61
edit
elor555 Feb 21, 2021
15d0e04
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 21, 2021
17340f8
edit
elor555 Feb 21, 2021
4ee3beb
edit
elor555 Feb 21, 2021
66dc75e
edit
elor555 Feb 21, 2021
21b8033
edit
elor555 Feb 21, 2021
66b4457
edit
elor555 Feb 21, 2021
fdc0219
edit
elor555 Feb 21, 2021
0fd6e7e
edit
elor555 Feb 22, 2021
f011437
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 23, 2021
60ce6ce
edit
elor555 Feb 23, 2021
d9b1745
edit
elor555 Feb 23, 2021
ee9faf7
edit
elor555 Feb 23, 2021
59c61e6
edit
elor555 Feb 23, 2021
a3099f4
edit
elor555 Feb 23, 2021
108e152
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
4729f27
Details Message
elor555 Feb 24, 2021
5d27e04
Details Message
elor555 Feb 24, 2021
9f14957
Merge remote-tracking branch 'upstream/develop' into feature/parasha
elor555 Feb 25, 2021
122574f
fix: style improvements
elor555 Feb 25, 2021
1421c19
fix: style improvements
elor555 Feb 25, 2021
52df059
fix: improvments
elor555 Feb 26, 2021
8714224
Details Message
elor555 Feb 26, 2021
09aceb1
fix: improvments
elor555 Feb 26, 2021
baf06ac
Merge remote-tracking branch 'upstream/develop' into feature/parasha
elor555 Mar 1, 2021
6d430ed
fix: styling
elor555 Mar 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions app/database/models.py
Expand Up @@ -191,17 +191,17 @@ class Category(Base):

@staticmethod
def create(
db_session: Session,
session: Session,
name: str,
color: str,
user_id: int,
) -> Category:
try:
category = Category(name=name, color=color, user_id=user_id)
db_session.add(category)
db_session.flush()
db_session.commit()
db_session.refresh(category)
session.add(category)
session.flush()
session.commit()
session.refresh(category)
except (SQLAlchemyError, IntegrityError) as e:
logger.error(f"Failed to create category: {e}")
raise e
Expand Down Expand Up @@ -533,6 +533,15 @@ def __repr__(self):
)


class Parasha(Base):
__tablename__ = "parashot"

id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
hebrew_name = Column(String, nullable=False)
link = Column(String, nullable=False)


class SharedListItem(Base):
__tablename__ = "shared_list_item"

Expand Down Expand Up @@ -600,8 +609,8 @@ class Task(Base):


# insert language data


# Credit to adrihanu https://stackoverflow.com/users/9127249/adrihanu
# https://stackoverflow.com/questions/17461251
def insert_data(target, session: Session, **kw):
"""insert language data
Credit to adrihanu https://stackoverflow.com/users/9127249/adrihanu
Expand Down
40 changes: 28 additions & 12 deletions app/internal/json_data_loader.py
Expand Up @@ -5,46 +5,64 @@
from loguru import logger
from sqlalchemy.orm import Session

from app.database.models import Base, InternationalDays, Joke, Quote, Zodiac
from app.internal import daily_quotes, international_days, jokes, zodiac

from app.database.models import (
elor555 marked this conversation as resolved.
Show resolved Hide resolved
Base, InternationalDays, Joke, Quote, Parasha, Zodiac,
)
from app.config import RESOURCES_DIR
from app.internal import (
daily_quotes, international_days, jokes, weekly_parasha, zodiac,
)


elor555 marked this conversation as resolved.
Show resolved Hide resolved
def load_to_database(session: Session) -> None:
"""Loads data from JSON data files into the database.

elor555 marked this conversation as resolved.
Show resolved Hide resolved
On startup, data from the JSON files should be added to the database and
not be accessed from a network call for each request as it is costly.

On startup, data from the JSON files should be added to the
database and not be accessed from a network call for each
request as it is costly.
The quotes JSON file content is copied from the free API:
'https://type.fit/api/quotes'.
The parashot and hebrew_view JSON files content is copied
from the free API:
'https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&min=on&
mod=on&nx=on&year=now&month=x&ss=on&mf=on&c=on&geo=geoname
&geonameid=293397&m=50&s=on&d=on&D=on'.
elor555 marked this conversation as resolved.
Show resolved Hide resolved

Args:
session: The database connection.
"""
_insert_into_database(
session,
'app/resources/zodiac.json',
RESOURCES_DIR / "zodiac.json",
Zodiac,
zodiac.get_zodiac,
)

_insert_into_database(
session,
'app/resources/quotes.json',
RESOURCES_DIR / "quotes.json",
Quote,
daily_quotes.get_quote,
)

_insert_into_database(
session,
'app/resources/international_days.json',
RESOURCES_DIR / "parashot.json",
Parasha,
weekly_parasha.create_parasha_object,
)

_insert_into_database(
session,
RESOURCES_DIR / "international_days.json",
InternationalDays,
international_days.get_international_day,
)

_insert_into_database(
session,
'app/resources/jokes.json',
RESOURCES_DIR / "jokes.json",
Joke,
jokes.get_joke,
)
Expand All @@ -57,13 +75,11 @@ def _insert_into_database(
model_creator: Callable
) -> bool:
"""Inserts the extracted JSON data into the database.

Args:
session: The database connection.
path: The file path.
table: A model entity table.
model_creator: A model creation function.

elor555 marked this conversation as resolved.
Show resolved Hide resolved
Returns:
True if the save was successful, otherwise returns False.
"""
Expand Down Expand Up @@ -104,7 +120,7 @@ def _get_data_from_json(path: str) -> List[Dict[str, Any]]:
A list of dictionary objects.
"""
try:
with open(path, 'r') as json_file:
with open(path, 'r', encoding='utf-8') as json_file:
json_content = json.load(json_file)
elor555 marked this conversation as resolved.
Show resolved Hide resolved
except (IOError, ValueError):
file_name = os.path.basename(path)
Expand Down
51 changes: 51 additions & 0 deletions app/internal/weekly_parasha.py
@@ -0,0 +1,51 @@
from datetime import datetime
from typing import Dict, Optional

from pyluach import dates, parshios
elor555 marked this conversation as resolved.
Show resolved Hide resolved
from sqlalchemy.orm import Session

from app.database.models import Parasha
elor555 marked this conversation as resolved.
Show resolved Hide resolved


def create_parasha_object(parashot_fields: Dict[str, str]) -> Parasha:
"""This function create a parasha object from given fields dictionary.
It is used for adding the data from the json into the db"""
return Parasha(
name=parashot_fields["name"],
hebrew_name=parashot_fields["hebrew"],
link=parashot_fields["link"],
)


def get_parasha_only_to_saturday(date: datetime) -> Optional[str]:
"""Returns the parasha name if the date is Saturday.

Args:
date: The requested date.

Returns:
If the date is Saturday, return the parasha name,
else return None.
"""
date_split = str(date).split("-")
new_date_format = [int(x) for x in date_split]
gregorian_date = dates.GregorianDate(*new_date_format)
if gregorian_date == gregorian_date.shabbos():
return parshios.getparsha_string(gregorian_date)


def get_parasha_object(session: Session, date: datetime) -> Optional[Parasha]:
"""Returns the parasha object for the specific day.

Args:
session: The database connection.
date: The requested date.

Returns:
A HebrewView object.
IF the specific day in not Saturday, it return None.
"""
parasha_name = get_parasha_only_to_saturday(date)
if parasha_name is None:
return None
return session.query(Parasha).filter_by(name=parasha_name).first()
1 change: 1 addition & 0 deletions app/main.py
Expand Up @@ -92,6 +92,7 @@ def create_tables(engine, psql_environment):
whatsapp,
)


json_data_loader.load_to_database(next(get_db()))


Expand Down