Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
19 changes: 19 additions & 0 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,22 @@ class Quote(Base):
id = Column(Integer, primary_key=True, index=True)
text = Column(String, nullable=False)
author = Column(String)


class Zodiac(Base):
__tablename__ = "zodiac-signs"

id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
start_month = Column(Integer, nullable=False)
start_day_in_month = Column(Integer, nullable=False)
end_month = Column(Integer, nullable=False)
end_day_in_month = Column(Integer, nullable=False)

def __repr__(self):
return (
f'<Zodiac '
f'{self.name} '
f'{self.start_day_in_month}/{self.start_month}-'
f'{self.end_day_in_month}/{self.end_month}>'
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import date
from typing import Optional
from typing import Dict, Optional

from app.database.models import Quote

Expand All @@ -9,6 +9,15 @@
TOTAL_DAYS = 366


def create_quote_object(quotes_fields: Dict[str, Optional[str]]) -> Quote:
"""This function create a quote object from given fields dictionary.
It is used for adding the data from the json into the db"""
return Quote(
text=quotes_fields['text'],
author=quotes_fields['author']
)


def quote_per_day(
session: Session, date: date = date.today()
) -> Optional[Quote]:
Expand Down
51 changes: 51 additions & 0 deletions app/internal/json_data_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
from typing import Dict, List, Callable, Union

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):
return []
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 is_table_empty(session, table):
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."""
41 changes: 0 additions & 41 deletions app/internal/quotes/load_quotes.py

This file was deleted.

41 changes: 41 additions & 0 deletions app/internal/zodiac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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."""
zodiac_obj = session.query(Zodiac).filter(
or_((and_(Zodiac.start_month == date.month,
Zodiac.start_day_in_month <= date.day)),
(and_
(Zodiac.end_month == date.month,
Zodiac.end_day_in_month >= date.day))
)).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
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from app.database.database import engine, get_db
from app.dependencies import (
MEDIA_PATH, STATIC_PATH, templates)
from app.internal.quotes import load_quotes, daily_quotes
from app.internal import daily_quotes, json_data_loader
from app.routers import (
agenda, dayview, email, event, invitation, profile, search, telegram,
whatsapp
Expand All @@ -33,7 +33,7 @@ def create_tables(engine, psql_environment):
app.mount("/static", StaticFiles(directory=STATIC_PATH), name="static")
app.mount("/media", StaticFiles(directory=MEDIA_PATH), name="media")

load_quotes.load_daily_quotes(next(get_db()))
json_data_loader.load_to_db(next(get_db()))

# Configure logger
logger = LoggerCustomizer.make_logger(config.LOG_PATH,
Expand Down
86 changes: 86 additions & 0 deletions app/resources/zodiac.json
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
}
]
5 changes: 4 additions & 1 deletion app/routers/dayview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.database.database import get_db
from app.database.models import Event, User
from app.dependencies import TEMPLATES_PATH
from app.internal import zodiac


templates = Jinja2Templates(directory=TEMPLATES_PATH)
Expand Down Expand Up @@ -105,9 +106,11 @@ async def dayview(request: Request, date: str, db_session=Depends(get_db)):
and_(Event.end >= day, Event.end < day_end),
and_(Event.start < day_end, day_end < Event.end)))
events_n_attrs = [(event, DivAttributes(event, day)) for event in events]
zodiac_obj = zodiac.get_zodiac_of_day(db_session, day)
return templates.TemplateResponse("dayview.html", {
"request": request,
"events": events_n_attrs,
"month": day.strftime("%B").upper(),
"day": day.day
"day": day.day,
"zodiac": zodiac_obj
})
Binary file added app/static/images/zodiac/Aquarius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Aries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Cancer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Capricorn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Gemini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Leo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Libra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Pisces.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Sagittarius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Scorpio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Taurus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/zodiac/Thumbs.db
Binary file not shown.
Binary file added app/static/images/zodiac/Virgo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/templates/dayview.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<span class="month fw-bold text-white">{{month}}</span>
<span class="day fw-bold text-white">{{day}}</span>
</div>
{% if zodiac %}
<a href="https://www.horoscope.com/zodiac-signs/{{zodiac.name}}" title="Today's zodiac sign: {{zodiac.name}}" target="_blank"><img src="{{ url_for('static', path='/images/zodiac/' + zodiac.name + '.png') }}" alt="zodiac sign" width="15em" height="15em"></a>
{% endif %}
<div class="schedule">
<div class="container times bg-primeary position">
{% for i in range(24)%}
Expand Down
17 changes: 7 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ aiosmtpd==1.2.2
aiosmtplib==1.1.4
apipkg==1.5
arrow==0.17.0
asyncio==3.4.3
async-timeout==3.0.1
asyncio==3.4.3
atomicwrites==1.4.0
atpublic==2.1.2
attrs==20.3.0
blinker==1.4
beautifulsoup4==4.9.3
blinker==1.4
certifi==2020.12.5
Expand All @@ -23,9 +22,8 @@ execnet==1.7.1
Faker==5.6.2
fakeredis==1.4.5
fastapi==0.63.0
flake8==3.8.4
faker==5.6.2
fastapi-mail==0.3.3.1
flake8==3.8.4
frozendict==1.2
h11==0.12.0
h2==4.0.0
Expand All @@ -40,13 +38,13 @@ importlib-metadata==3.3.0
inflect==4.1.0
iniconfig==1.1.1
Jinja2==2.11.2
mccabe==0.6.1
joblib==1.0.0
lazy-object-proxy==1.5.2
loguru==0.5.3
MarkupSafe==1.1.1
mccabe==0.6.1
mypy==0.790
mypy-extensions==0.4.3
MarkupSafe==1.1.1
nltk==3.5
packaging==20.8
Pillow==8.1.0
Expand All @@ -61,8 +59,8 @@ pyparsing==2.4.7
pytest==6.2.1
pytest-asyncio==0.14.0
pytest-cov==2.10.1
pytest-mock==3.5.1
pytest-forked==1.3.0
pytest-mock==3.5.1
pytest-xdist==2.2.0
python-dateutil==2.8.1
python-dotenv==0.15.0
Expand All @@ -72,7 +70,6 @@ PyYAML==5.3.1
redis==3.5.3
regex==2020.11.13
requests==2.25.1
rfc3986==1.4.0
requests-mock==1.8.0
responses==0.12.1
rfc3986==1.4.0
Expand All @@ -81,7 +78,6 @@ smtpdfix==0.2.6
sniffio==1.2.0
sortedcontainers==2.3.0
soupsieve==2.1
sortedcontainers==2.3.0
SQLAlchemy==1.3.22
starlette==0.13.6
text-unidecode==1.3
Expand All @@ -93,6 +89,7 @@ urllib3==1.26.2
uvicorn==0.13.3
watchgod==0.6
websockets==8.1
win32-setctime==1.0.3
word-forms==2.1.0
wsproto==1.0.0
zipp==3.4.0
zipp==3.4.0
Loading