Skip to content

Feature/HebrewView #280

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 71 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
33723a7
add hebrew dates to calendar-nre feature
elor555 Feb 14, 2021
0d296f3
add test to hebrew dates
elor555 Feb 14, 2021
970534d
edit flake8
elor555 Feb 14, 2021
f0bc588
edit flake8
elor555 Feb 14, 2021
7282e00
fix after pull
elor555 Feb 15, 2021
861587a
edit flake8
elor555 Feb 15, 2021
8426167
edit
elor555 Feb 15, 2021
6b28e84
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 15, 2021
fbdd7f5
edit
elor555 Feb 15, 2021
442a640
edit
elor555 Feb 15, 2021
e4720c8
check
elor555 Feb 16, 2021
fcefcbe
check
elor555 Feb 16, 2021
9e6742e
edit
elor555 Feb 17, 2021
c63471a
edit
elor555 Feb 17, 2021
97a1d8c
edit after flake8
elor555 Feb 17, 2021
201b1d8
edit after flake8
elor555 Feb 17, 2021
739729e
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 17, 2021
d79b1c3
edit after flake8
elor555 Feb 17, 2021
f488161
edit after flake8
elor555 Feb 17, 2021
3d2c150
edit after flake8
elor555 Feb 17, 2021
d873fd6
edit after flake8
elor555 Feb 17, 2021
7593c11
edit after pull
elor555 Feb 19, 2021
3dd122b
edit after pull
elor555 Feb 19, 2021
b06b7be
edit after pull
elor555 Feb 19, 2021
4dd3ac5
edit
elor555 Feb 20, 2021
6c2d65c
edit
elor555 Feb 20, 2021
f6b8607
edit
elor555 Feb 20, 2021
3c70a58
edit
elor555 Feb 20, 2021
6a34a33
edit
elor555 Feb 20, 2021
822ac9d
edit
elor555 Feb 20, 2021
0ae1726
edit
elor555 Feb 20, 2021
9e9ee25
edit
elor555 Feb 20, 2021
0a8b157
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 21, 2021
ac73386
edit
elor555 Feb 21, 2021
426da0a
edit
elor555 Feb 21, 2021
63c437a
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 21, 2021
4ae0595
edit
elor555 Feb 21, 2021
f2323d6
edit
elor555 Feb 22, 2021
9fd7cbc
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 23, 2021
1c52fbd
edit
elor555 Feb 23, 2021
0abd915
edit
elor555 Feb 23, 2021
ad7ea1a
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 23, 2021
78278bc
edit
elor555 Feb 23, 2021
e8c1776
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 23, 2021
d2573b1
edit
elor555 Feb 23, 2021
d6b58f0
edit
elor555 Feb 23, 2021
7af9582
edit
elor555 Feb 23, 2021
a3b0b95
edit
elor555 Feb 23, 2021
be3ce04
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
408de5d
edit
elor555 Feb 24, 2021
848368f
edit
elor555 Feb 24, 2021
3e96b8b
edit
elor555 Feb 24, 2021
34ebe6e
edit
elor555 Feb 24, 2021
4fa9da5
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 24, 2021
a64fcd9
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
elor555 Feb 25, 2021
3b1a6be
Details Message
elor555 Feb 25, 2021
ed58474
Details Message
elor555 Feb 25, 2021
22f0bcc
fix: flake8
elor555 Feb 25, 2021
1a1eb3d
fix: flake8
elor555 Feb 25, 2021
7d6ec9b
fix: bug fix
elor555 Feb 25, 2021
18a0195
fix: bug fix
elor555 Feb 25, 2021
c022096
fix: bug fix
elor555 Feb 25, 2021
44f9d61
fix: fix translation files
elor555 Feb 25, 2021
76726e1
Merge remote-tracking branch 'upstream/develop' into feature/hebrewview
elor555 Feb 27, 2021
a2449d2
fix: bugfix
elor555 Mar 1, 2021
c7d3b7d
fix: bugfix
elor555 Mar 1, 2021
11fc0d6
fix: bugfix
elor555 Mar 1, 2021
377e82d
fix: bugfix
elor555 Mar 1, 2021
9b0c629
fix: styling
elor555 Mar 1, 2021
0a9a4fb
fix: bugfix
elor555 Mar 2, 2021
c48a270
fix: bugfix
elor555 Mar 2, 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
72 changes: 72 additions & 0 deletions app/internal/hebrew_date_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from datetime import datetime

from hebrew_numbers import int_to_gematria
from pyluach import dates, hebrewcal


EN_TO_HEB_MONTH_NAMES = {
'Tishrei': 'תשרי',
'Cheshvan': 'חשוון',
'Kislev': 'כסלו',
'Teves': 'טבת',
'Shvat': 'שבט',
'Adar': 'אדר',
'Adar Rishon': "אדר(א')",
'Adar Sheni': "אדר(ב')",
'Nissan': 'ניסן',
'Iyar': 'אייר',
'Sivan': 'סיוון',
'Tamuz': 'תמוז',
'Av': 'אב',
'Elul': 'אלול',
}


def get_month_name_by_num(date: datetime) -> str:
"""Returns the Hebrew name date for the specific day.

Args:
date: The requested date.

Returns:
A Hebrew name date.
"""

for month in hebrewcal.Year(date.year).itermonths():
if date.month == month.month:
print(month.name)
return EN_TO_HEB_MONTH_NAMES[month.name]


def from_greogian_to_hebrew_date(date: datetime):
"""Returns the Hebrew date for the specific greogian date.

Args:
date: The requested date.

Returns:
A Hebrew date string.
"""

date_split = str(date).split('-')
new_date_format = [int(x) for x in date_split]
gregorian_date = dates.GregorianDate(*new_date_format)
return gregorian_date.to_heb()


def get_hebrew_date(date: datetime) -> str:
"""Returns the Hebrew date for the specific day.

Args:
date: The requested date.

Returns:
A Hebrew date string.
"""

hebrew_date_list = []
hebrew_date = from_greogian_to_hebrew_date(date)
hebrew_date_list.append(int_to_gematria(hebrew_date.day))
hebrew_date_list.append(get_month_name_by_num(hebrew_date))
hebrew_date_list.append(int_to_gematria(hebrew_date.year % 1000))
return ' '.join(hebrew_date_list)
2 changes: 1 addition & 1 deletion app/internal/json_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from loguru import logger
from sqlalchemy.orm import Session


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

Expand All @@ -14,7 +15,6 @@ def load_to_database(session: Session) -> None:

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'.

Expand Down
2 changes: 1 addition & 1 deletion app/routers/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
DEFAULT_SFX_VOL,
)
from app.dependencies import SOUNDS_PATH, get_db, templates
from app.internal.security.dependancies import current_user
from app.internal.security.dependencies import current_user
from fastapi import APIRouter, Depends, Form, Request
from sqlalchemy.orm.session import Session
from starlette.responses import RedirectResponse
Expand Down
23 changes: 12 additions & 11 deletions app/routers/calendar.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from http import HTTPStatus

from fastapi import APIRouter, Request
from app.dependencies import get_db, templates
from app.routers import calendar_grid
from fastapi import APIRouter, Request, Depends
from fastapi.responses import HTMLResponse
from starlette.responses import Response

from app.dependencies import templates
from app.routers import calendar_grid as cg

router = APIRouter(
prefix="/calendar/month",
tags=["calendar"],
Expand All @@ -16,16 +15,16 @@


@router.get("/")
async def calendar(request: Request) -> Response:
user_local_time = cg.Day.get_user_local_time()
day = cg.create_day(user_local_time)
async def calendar(request: Request, db_session=Depends(get_db)) -> Response:
user_local_time = calendar_grid.Day.get_user_local_time()
day = calendar_grid.create_day(user_local_time)
return templates.TemplateResponse(
"calendar_monthly_view.html",
{
"request": request,
"day": day,
"week_days": cg.Week.DAYS_OF_THE_WEEK,
"weeks_block": cg.get_month_block(day)
"week_days": calendar_grid.Week.DAYS_OF_THE_WEEK,
"weeks_block": calendar_grid.get_month_block(day),
}
)

Expand All @@ -34,8 +33,10 @@ async def calendar(request: Request) -> Response:
async def update_calendar(
request: Request, date: str, days: int
) -> HTMLResponse:
last_day = cg.Day.convert_str_to_date(date)
next_weeks = cg.create_weeks(cg.get_n_days(last_day, days))
last_day = calendar_grid.Day.convert_str_to_date(date)
next_weeks = calendar_grid.create_weeks(
calendar_grid.get_n_days(last_day, days)
)
template = templates.get_template(
'partials/calendar/monthly_view/add_week.html')
content = template.render(weeks_block=next_weeks)
Expand Down
4 changes: 3 additions & 1 deletion app/routers/dayview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from app.database.models import Event, User
from app.dependencies import get_db, templates
from app.internal import zodiac
from app.internal import zodiac, hebrew_date_view
from app.routers.user import get_all_user_events

router = APIRouter()
Expand Down Expand Up @@ -177,6 +177,7 @@ async def dayview(
except ValueError as err:
raise HTTPException(status_code=404, detail=f"{err}")
zodiac_obj = zodiac.get_zodiac_of_day(session, day)
hebrew_obj = hebrew_date_view.get_hebrew_date(day.date())
events_n_attrs = get_events_and_attributes(
day=day,
session=session,
Expand All @@ -198,5 +199,6 @@ async def dayview(
"day": day.day,
"zodiac": zodiac_obj,
"view": view,
"hebrewView": hebrew_obj,
},
)
21 changes: 21 additions & 0 deletions app/static/dayview.css
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,24 @@ body {
width: 1.2rem;
height: 1.2rem;
}

.hebrew-dates {
position: absolute;
right: 1.2rem;
top: 5rem;
padding-right: var(--space_xs);
padding-left: var(--space_xs);
padding-bottom: var(--space_xxs);
border: solid 0.1px var(--primary);
background-color: var(--surface-variant);
box-shadow: 1px 1px 2px #999;
line-height: 1;
height: 3.5rem;
width: 3.5rem;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8rem;
font-weight: bold;
}

7 changes: 6 additions & 1 deletion app/templates/calendar_day_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
{% else %}
<span class="fw-bold text-white date-nums">{{day}} / {{month}}</span>
{% endif %}
</div>
</div>
{% if hebrewView %}
<div class="hebrew-dates">
{{hebrewView}}
</div>
{% endif %}
<div class="all_day_events">
{% for event in all_day_events %}
<p class="text-truncate my-0 {{size}}">{{ event.title }}</p>
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ google-auth-oauthlib==0.4.2
googleapis-common-protos==1.52.0
h11==0.12.0
h2==4.0.0
python-hebrew-numbers==0.2.3
hiredis==1.1.0
hpack==4.0.0
httpcore==0.12.2
Expand Down Expand Up @@ -99,6 +100,7 @@ pycparser==2.20
pydantic==1.7.3
pyflakes==2.2.0
PyJWT==2.0.0
pyluach==1.2.1
pyparsing==2.4.7
pytest==6.2.1
pytest-asyncio==0.12.0
Expand Down
56 changes: 30 additions & 26 deletions tests/test_calendar_grid.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import datetime

import app.routers.calendar_grid as cg
from app.routers import calendar_grid

DATE = datetime.date(1988, 5, 3)
DAY = cg.Day(datetime.date(1988, 5, 3))
WEEKEND = cg.DayWeekend(datetime.date(2021, 1, 23))
DAY = calendar_grid.Day(datetime.date(1988, 5, 3))
WEEKEND = calendar_grid.DayWeekend(datetime.date(2021, 1, 23))
N_DAYS = 3
N_DAYS_BEFORE = datetime.date(1988, 4, 30)
NEXT_N_DAYS = [
cg.Day(datetime.date(1988, 5, 4)),
cg.Day(datetime.date(1988, 5, 5)),
cg.Day(datetime.date(1988, 5, 6))
calendar_grid.Day(datetime.date(1988, 5, 4)),
calendar_grid.Day(datetime.date(1988, 5, 5)),
calendar_grid.Day(datetime.date(1988, 5, 6))
]
DAY_TYPES = [cg.Day, cg.DayWeekend, cg.Today, cg.FirstDayMonth]
WEEK_DAYS = cg.Week.WEEK_DAYS
DAY_TYPES = [calendar_grid.Day, calendar_grid.DayWeekend,
calendar_grid.Today, calendar_grid.FirstDayMonth]
WEEK_DAYS = calendar_grid.Week.WEEK_DAYS


class TestCalendarGrid:
Expand Down Expand Up @@ -42,65 +43,68 @@ def test_create_day():
}

for i, value in enumerate(dates_to_check.values()):
assert isinstance(cg.create_day(value), DAY_TYPES[i])
assert isinstance(calendar_grid.create_day(value), DAY_TYPES[i])

@staticmethod
def test_get_next_date():
next_day_generator = cg.get_next_date(DATE)
next_day_generator = calendar_grid.get_next_date(DATE)
next_day = next(next_day_generator, None)
next_day_check = cg.Day(DATE + datetime.timedelta(days=1))
next_day_check = calendar_grid.Day(DATE + datetime.timedelta(days=1))
assert next_day
assert isinstance(next_day, cg.Day)
assert isinstance(next_day, calendar_grid.Day)
assert next_day.date == next_day_check.date

@staticmethod
def test_get_date_before_n_days():
assert cg.get_date_before_n_days(DATE, N_DAYS) == N_DAYS_BEFORE
assert calendar_grid.get_date_before_n_days(
DATE, N_DAYS) == N_DAYS_BEFORE

@staticmethod
def test_get_first_day_month_block(Calendar):
assert (
cg.get_first_day_month_block(DATE)
== next(Calendar.itermonthdates(DATE.year, DATE.month))
calendar_grid.get_first_day_month_block(DATE)
== next(Calendar.itermonthdates(DATE.year, DATE.month))
)

@staticmethod
def test_get_n_days():
next_n_dates = cg.get_n_days(DATE, N_DAYS)
next_n_dates = calendar_grid.get_n_days(DATE, N_DAYS)
for i in range(N_DAYS):
assert next(next_n_dates).date == NEXT_N_DAYS[i].date

@staticmethod
def test_create_weeks():
week = cg.create_weeks(NEXT_N_DAYS, cg.Week.WEEK_DAYS)
week = calendar_grid.create_weeks(
NEXT_N_DAYS, calendar_grid.Week.WEEK_DAYS)
assert week
assert isinstance(week[0], cg.Week)
assert isinstance(week[0].days[0], cg.Day)
assert isinstance(week[0], calendar_grid.Week)
assert isinstance(week[0].days[0], calendar_grid.Day)
assert len(week) == 1 and len(week[0].days) == 3

@staticmethod
def test_get_month_block(Calendar):
month_weeks = cg.create_weeks(
month_weeks = calendar_grid.create_weeks(
Calendar.itermonthdates(1988, 5), WEEK_DAYS)
get_block = cg.get_month_block(cg.Day(DATE), n=len(month_weeks))
get_block = calendar_grid.get_month_block(
calendar_grid.Day(DATE), n=len(month_weeks))

for i in range(len(month_weeks)):
for j in range(cg.Week.WEEK_DAYS):
for j in range(calendar_grid.Week.WEEK_DAYS):
assert get_block[i].days[j].date == month_weeks[i].days[j]

@staticmethod
def test_get_user_local_time():
time_string = "%b%w%Y"
server_time = cg.Day.get_user_local_time()
server_time = calendar_grid.Day.get_user_local_time()
server_time_check = datetime.datetime.today()
assert server_time
assert server_time.strftime(
time_string) == server_time_check.strftime(time_string)

@staticmethod
def test_is_weekend():
assert not cg.Day.is_weekend(DATE)
assert cg.Day.is_weekend(WEEKEND.date)
assert not calendar_grid.Day.is_weekend(DATE)
assert calendar_grid.Day.is_weekend(WEEKEND.date)

@staticmethod
def test_display_day():
Expand All @@ -116,4 +120,4 @@ def test_display_str():

@staticmethod
def test_create_week_object():
assert cg.Week(NEXT_N_DAYS)
assert calendar_grid.Week(NEXT_N_DAYS)
32 changes: 32 additions & 0 deletions tests/test_hebrew_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from datetime import datetime
from pyluach import dates

from app.internal.hebrew_date_view import (
get_hebrew_date,
from_greogian_to_hebrew_date,
get_month_name_by_num,
)


DAY = datetime.strptime("2021-01-01", "%Y-%m-%d").date()
ADAR = datetime.strptime("2021-02-15", "%Y-%m-%d").date()
ADAR_A = datetime.strptime("2019-02-15", "%Y-%m-%d").date()
ADAR_B = datetime.strptime("2019-03-08", "%Y-%m-%d").date()


def test_get_hebrew_date():
result = get_hebrew_date(DAY)
assert result == "י״ז טבת תשפ״א"


def test_from_greogian_to_hebrew_date_and_find_month_name():
result = from_greogian_to_hebrew_date(ADAR)
assert result == dates.HebrewDate(5781, 12, 3)
assert get_month_name_by_num(result) == 'אדר'


def test_if_leap_year():
result_a = from_greogian_to_hebrew_date(ADAR_A)
result_b = from_greogian_to_hebrew_date(ADAR_B)
assert get_month_name_by_num(result_a) == "אדר(א')"
assert get_month_name_by_num(result_b) == "אדר(ב')"