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/shabbatime #333

Open
wants to merge 44 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6952f62
new featue-shabbat time
elor555 Feb 23, 2021
c745b37
edit
elor555 Feb 23, 2021
58d8fba
edit
elor555 Feb 24, 2021
4d2e3d3
edit
elor555 Feb 24, 2021
f4451ca
edit
elor555 Feb 24, 2021
03f8200
edit
elor555 Feb 24, 2021
09f3447
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
ad29c3b
edit
elor555 Feb 24, 2021
e82ad81
edit
elor555 Feb 24, 2021
5805579
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
e2612a9
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
55e82fa
Details Message
elor555 Feb 25, 2021
c422398
Details Message
elor555 Feb 25, 2021
a027151
Details Message
elor555 Feb 25, 2021
d07f516
Details Message
elor555 Feb 25, 2021
e49b624
Merge remote-tracking branch 'upstream/develop' into feature/shabbatime
elor555 Feb 25, 2021
d1da1f3
fix: flake8
elor555 Feb 25, 2021
00450c3
fix: many code improvements
elor555 Feb 25, 2021
1d21cd2
fix: many code improvements
elor555 Feb 25, 2021
10e5b04
fix: style improvements
elor555 Feb 25, 2021
5ef54f1
Merge remote-tracking branch 'upstream/develop' into feature/shabbatime
elor555 Feb 26, 2021
48cae11
fix: improvments
elor555 Feb 26, 2021
2e8be84
fix: improvments
elor555 Feb 26, 2021
7a4c32c
fix: improvments
elor555 Feb 26, 2021
e519138
fix: BUGFIX
elor555 Feb 26, 2021
6c580dd
fix: BUGFIX
elor555 Feb 26, 2021
ea4e41b
fix: STYLE
elor555 Feb 26, 2021
b0bb745
fix: bugfix
elor555 Feb 27, 2021
8fea1f5
fix: bugfix
elor555 Feb 27, 2021
bc9f14f
Merge remote-tracking branch 'upstream/develop' into feature/shabbatime
elor555 Mar 1, 2021
50c42d2
fix: improvment
elor555 Mar 1, 2021
1c743c3
fix: bugfix
elor555 Mar 2, 2021
935a9de
fix: bugfix
elor555 Mar 2, 2021
4c6a434
fix: bugfix
elor555 Mar 2, 2021
c8cf12f
fix: bugfix
elor555 Mar 3, 2021
bf56e9b
fix : adding information
elor555 Mar 3, 2021
f89ea45
fix : bugfix
elor555 Mar 3, 2021
ba2f35c
fix : bugfix
elor555 Mar 3, 2021
421e70b
fix : bugfix
elor555 Mar 10, 2021
8d3e106
fix : bugfix
elor555 Mar 10, 2021
9e22801
fix : bugfix
elor555 Mar 10, 2021
7313c85
fix : bugfix
elor555 Mar 10, 2021
855a871
fix : bugfix
elor555 Mar 11, 2021
b7c8067
fix : bugfix
elor555 Mar 11, 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
32 changes: 9 additions & 23 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@
from datetime import datetime
from typing import Any, Dict

from sqlalchemy import (
DDL,
JSON,
Boolean,
Column,
Date,
DateTime,
Enum,
Float,
ForeignKey,
Index,
Integer,
String,
Time,
UniqueConstraint,
event,
)
from sqlalchemy import (Boolean, Column, Date, DateTime, DDL, Enum, event,
Float, ForeignKey, Index, Integer, JSON, String, Time,
UniqueConstraint)
from sqlalchemy.dialects.postgresql import TSVECTOR
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.ext.declarative.api import DeclarativeMeta, declarative_base
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative.api import declarative_base, DeclarativeMeta
from sqlalchemy.orm import relationship, Session
from sqlalchemy.sql.schema import CheckConstraint

import app.routers.salary.config as SalaryConfig
Expand Down Expand Up @@ -191,10 +177,10 @@ class Category(Base):

@staticmethod
def create(
db_session: Session,
name: str,
color: str,
user_id: int,
db_session: Session,
name: str,
color: str,
user_id: int,
) -> Category:
try:
category = Category(name=name, color=color, user_id=user_id)
Expand Down
17 changes: 11 additions & 6 deletions app/internal/json_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
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.config import RESOURCES_DIR
from app.database.models import (
Base, InternationalDays, Joke, Quote, Zodiac
)
from app.internal import (
daily_quotes, international_days, jokes, zodiac
)


def load_to_database(session: Session) -> None:
Expand All @@ -23,28 +28,28 @@ def load_to_database(session: Session) -> None:
"""
_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 / "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 Down
86 changes: 86 additions & 0 deletions app/internal/shabbat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import json
from datetime import date, datetime
from typing import Any, Dict, List, Optional, Union

import httpx

from app.config import RESOURCES_DIR

SHABBAT_API = 'https://www.hebcal.com/shabbat?cfg=json&geonameid='


def return_zip_code_of_user_location(location_by_ip) -> str:
"""Returns zip code from locations JSON file that match user location.

Args:
location_by_ip: location by ip that create with "geocoder" module.

Returns:
A zip code string for the user location.
"""
path = RESOURCES_DIR / "locations.json"
with open(path, 'r', encoding="utf8") as json_file:
locations = json.load(json_file)
for location in locations:
if (location["city"] == location_by_ip.city
and location["country"] == location_by_ip.country):
return location["zip_number"]


def return_shabbat_times(
shabat_items: Dict[Any, List[Dict[str, str]]]
) -> Dict[str, Union[date, Any]]:
"""Returns the shabbat time which match to ip(of the user) location.
Used the content of this is free API:
'https://www.hebcal.com/shabbat?cfg=json&geonameid=295277'.

Args:
shabat_items: dictionary of all the details about shabbat according to
specific ip location.

Returns:
Shabbat start end ending time and user location by ip.
"""
for item in shabat_items:
if "Candle lighting" in item["title"]:
shabbat_entry = item["date"]
if "Havdalah" in item["title"]:
shabbat_exit = item["date"]

shabbat_entry_date = shabbat_entry.split("T")[0]
shabbat_entry_hour = shabbat_entry.split("T")[1]
shabbat_exit_date = shabbat_exit.split("T")[0]
shabbat_exit_hour = shabbat_exit.split("T")[1]
shabbat_limit = {
"start_hour": shabbat_entry_hour[:5],
"start_date": datetime.strptime(shabbat_entry_date, "%Y-%m-%d").date(),
"end_hour": shabbat_exit_hour[:5],
"end_date": datetime.strptime(shabbat_exit_date, "%Y-%m-%d").date(),
}
return shabbat_limit


async def get_shabbat_if_date_friday(calendar_date: date,
location_by_ip,
) -> Optional[Dict[str, date]]:
"""Returns shabbat start end ending time if specific date is friday,
else None.
The function used in the free API:
'https://www.hebcal.com/shabbat?cfg=json&geonameid=295277'.

Args:
calendar_date: date.
location_by_ip: location by ip that create with "geocoder" module.

Returns:
Shabbat start and ending time if specific date
is Saturday and user location by ip, else None
"""
async with httpx.AsyncClient() as client:
zip_code = return_zip_code_of_user_location(location_by_ip)
shabbat_api = SHABBAT_API + zip_code
response = await client.get(shabbat_api)
shabat_items = response.json()['items']
shabbat_obj = return_shabbat_times(shabat_items)
if calendar_date == shabbat_obj["start_date"]:
return shabbat_obj
Loading