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/salary calculator #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
50de56b
initial commit
ShiZinDle Jan 19, 2021
c5c5507
Merge branch 'main' of https://github.com/PythonFreeCourse/calendar i…
ShiZinDle Jan 20, 2021
fed1af6
Add salary settings creation page
ShiZinDle Jan 21, 2021
b003735
Add salary view page
ShiZinDle Jan 24, 2021
6c94853
Add edit page, documentation and tests
ShiZinDle Jan 29, 2021
4a856e2
Add tests, fix settings creatioin bug
ShiZinDle Jan 29, 2021
143a936
Add salary link to navbar
ShiZinDle Jan 30, 2021
11e5b56
Linting fix
ShiZinDle Jan 31, 2021
df26f6e
Fix nonexistent settings redirection bug.
ShiZinDle Jan 31, 2021
d7b7038
Fix failing tests, add salary tests fixtures
ShiZinDle Jan 31, 2021
948edea
Fix minor conflicts
ShiZinDle Feb 1, 2021
792d44c
Merge updates from develop
ShiZinDle Feb 1, 2021
2efe0ad
Split overtime function and add tests
ShiZinDle Feb 1, 2021
d0b0499
Fix get_total_synchronous_hours
ShiZinDle Feb 1, 2021
9aabfc5
Split functions, add tests
ShiZinDle Feb 2, 2021
0be68ff
Move 'get_current_user' to 'app.internal.utils'
ShiZinDle Feb 3, 2021
e7384ca
Pull changes from develop
ShiZinDle Feb 3, 2021
4b80fec
Fix routes in tests
ShiZinDle Feb 6, 2021
d4f9d41
Pull changes from develop and resolve conflicts
ShiZinDle Feb 6, 2021
caec66f
Add else clause to 'salary.utils.update_settings'
ShiZinDle Feb 7, 2021
2268441
Pull updates from develop and resolve conflicts
ShiZinDle Feb 7, 2021
5a0b96e
Merge branch 'develop' into feature/salary-calculator
yammesicka Feb 8, 2021
8443939
Pull from develop
ShiZinDle Feb 8, 2021
7d7c614
Pull requirements.txt from develop
ShiZinDle Feb 8, 2021
e87571e
Remove use of sqlalchemy.sql.ekement._and
ShiZinDle Feb 8, 2021
9d6fb45
Pull and resolve conflicts
ShiZinDle Feb 8, 2021
714d863
Merge branch 'develop' into feature/salary-calculator
yammesicka Feb 9, 2021
bfdf981
dev: Solve conflicts
yammesicka Feb 9, 2021
4d2c7f9
Fix test_events imports
ShiZinDle Feb 9, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ dmypy.json
.vscode/
app/.vscode/

app/routers/stam
routes 1.py

# PyCharm
.idea

Expand Down
22 changes: 22 additions & 0 deletions app/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app import config


SQLALCHEMY_DATABASE_URL = os.getenv(
"DATABASE_CONNECTION_STRING", config.DEVELOPMENT_DATABASE_STRING)


def create_env_engine(psql_environment, sqlalchemy_database_url):
if not psql_environment:
return create_engine(
sqlalchemy_database_url, connect_args={"check_same_thread": False})

return create_engine(sqlalchemy_database_url)


engine = create_env_engine(config.PSQL_ENVIRONMENT, SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
33 changes: 0 additions & 33 deletions app/database/database.py

This file was deleted.

128 changes: 108 additions & 20 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@
from datetime import datetime
from typing import Dict, Any


from app.config import PSQL_ENVIRONMENT
from app.database.database import Base
from sqlalchemy import (DDL, Boolean, Column, DateTime, ForeignKey, Index,
Integer, String, event, UniqueConstraint, JSON)
from sqlalchemy import (
Boolean, Column, DateTime, DDL, 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 declarative_base
from sqlalchemy.orm import relationship, Session
from sqlalchemy.sql.schema import CheckConstraint

from app.config import PSQL_ENVIRONMENT
from app.dependencies import logger
import app.routers.salary.config as SalaryConfig


class UserEvent(Base):
__tablename__ = "user_event"

id = Column(Integer, primary_key=True, index=True)
user_id = Column('user_id', Integer, ForeignKey('users.id'))
event_id = Column('event_id', Integer, ForeignKey('events.id'))

events = relationship("Event", back_populates="participants")
participants = relationship("User", back_populates="events")

def __repr__(self):
return f'<UserEvent ({self.participants}, {self.events})>'
Base = declarative_base()


class User(Base):
Expand All @@ -43,7 +34,16 @@ class User(Base):
telegram_id = Column(String, unique=True)
is_active = Column(Boolean, default=False)
language_id = Column(Integer, ForeignKey("languages.id"))
events = relationship("UserEvent", back_populates="participants")

owned_events = relationship(
"Event", cascade="all, delete", back_populates="owner",
)
events = relationship(
"UserEvent", cascade="all, delete", back_populates="participants",
)
salary_settings = relationship(
"SalarySettings", cascade="all, delete", back_populates="user",
)

def __repr__(self):
return f'<User {self.id}>'
Expand All @@ -65,8 +65,10 @@ class Event(Base):
color = Column(String, nullable=True)
category_id = Column(Integer, ForeignKey("categories.id"))

owner = relationship("User")
participants = relationship("UserEvent", back_populates="events")
owner = relationship("User", back_populates="owned_events")
participants = relationship(
"UserEvent", cascade="all, delete", back_populates="events",
)

# PostgreSQL
if PSQL_ENVIRONMENT:
Expand All @@ -81,6 +83,20 @@ def __repr__(self):
return f'<Event {self.id}>'


class UserEvent(Base):
__tablename__ = "user_event"

id = Column(Integer, primary_key=True, index=True)
user_id = Column('user_id', Integer, ForeignKey('users.id'))
event_id = Column('event_id', Integer, ForeignKey('events.id'))

events = relationship("Event", back_populates="participants")
participants = relationship("User", back_populates="events")

def __repr__(self):
return f'<UserEvent ({self.participants}, {self.events})>'


class Language(Base):
__tablename__ = "languages"

Expand Down Expand Up @@ -161,6 +177,78 @@ def __repr__(self):
)


class SalarySettings(Base):
# Code revision required after categories feature is added
# Code revision required after holiday times feature is added
# Code revision required after Shabbat times feature is added
__tablename__ = "salary_settings"

user_id = Column(
Integer, ForeignKey("users.id"), primary_key=True,
)
# category_id = Column(
yammesicka marked this conversation as resolved.
Show resolved Hide resolved
# Integer, ForeignKey("categories.id"), primary_key=True,
# )
category_id = Column(
Integer, primary_key=True,
)
wage = Column(
Float, nullable=False, default=SalaryConfig.MINIMUM_WAGE,
)
off_day = Column(
Integer, CheckConstraint("0<=off_day<=6"), nullable=False,
default=SalaryConfig.SATURDAY,
)
# holiday_category_id = Column(
# Integer, ForeignKey("holiday_categories.id"), nullable=False,
# default=SalaryConfig.ISRAELI_JEWISH,
# )
holiday_category_id = Column(
Integer, nullable=False,
default=SalaryConfig.ISRAELI_JEWISH,
)
regular_hour_basis = Column(
Float, nullable=False, default=SalaryConfig.REGULAR_HOUR_BASIS,
)
night_hour_basis = Column(
Float, nullable=False, default=SalaryConfig.NIGHT_HOUR_BASIS,
)
night_start = Column(
Time, nullable=False, default=SalaryConfig.NIGHT_START,
)
night_end = Column(
Time, nullable=False, default=SalaryConfig.NIGHT_END,
)
night_min_len = Column(
Time, nullable=False, default=SalaryConfig.NIGHT_MIN_LEN,
)
first_overtime_amount = Column(
Float, nullable=False, default=SalaryConfig.FIRST_OVERTIME_AMOUNT,
)
first_overtime_pay = Column(
Float, nullable=False, default=SalaryConfig.FIRST_OVERTIME_PAY,
)
second_overtime_pay = Column(
Float, nullable=False, default=SalaryConfig.SECOND_OVERTIME_PAY,
)
week_working_hours = Column(
Float, nullable=False, default=SalaryConfig.WEEK_WORKING_HOURS,
)
daily_transport = Column(
Float, CheckConstraint(
f"daily_transport<={SalaryConfig.MAXIMUM_TRANSPORT}"),
nullable=False, default=SalaryConfig.STANDARD_TRANSPORT,
)

user = relationship("User", back_populates="salary_settings")
# category = relationship("Category", back_populates="salary_settings")
# holiday_category =relationship("HolidayCategory",
# back_populates="salary_settings")

def __repr__(self):
return f'<SalarySettings ({self.user_id}, {self.category_id})>'


class WikipediaEvents(Base):
__tablename__ = "wikipedia_events"

Expand Down
Empty file removed app/database/schemas.py
Empty file.
9 changes: 9 additions & 0 deletions app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.templating import Jinja2Templates

from app import config
from app.database import SessionLocal
from app.internal.logger_customizer import LoggerCustomizer

APP_PATH = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -23,6 +24,14 @@
config.LOG_FORMAT)


def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()


@lru_cache()
def get_settings():
return config.Settings()
Empty file removed app/internal/admin.py
Empty file.
6 changes: 3 additions & 3 deletions app/internal/import_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, Generator, List, Tuple, Union

from icalendar import Calendar
from sqlalchemy.orm.session import Session

from app.config import (
EVENT_CONTENT_LIMIT,
Expand All @@ -16,7 +17,6 @@
MAX_FILE_SIZE_MB,
VALID_FILE_EXTENSION
)
from app.database.database import SessionLocal
from app.routers.event import create_event
from loguru import logger

Expand Down Expand Up @@ -168,7 +168,7 @@ def import_ics_file(ics_file: str) -> List[Dict[str, Union[str, Any]]]:

def save_events_to_database(events: List[Dict[str, Union[str, Any]]],
user_id: int,
session: SessionLocal) -> None:
session: Session) -> None:
"""insert the events into Event table"""
for event in events:
title = event["Head"]
Expand All @@ -184,7 +184,7 @@ def save_events_to_database(events: List[Dict[str, Union[str, Any]]],
owner_id=owner_id)


def user_click_import(file: str, user_id: int, session: SessionLocal) -> bool:
def user_click_import(file: str, user_id: int, session: Session) -> bool:
"""
when user choose a file and click import, we are checking the file
and if everything is ok we will insert the data to DB
Expand Down
3 changes: 1 addition & 2 deletions app/internal/json_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from loguru import logger
from sqlalchemy.orm import Session

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


Expand Down
2 changes: 1 addition & 1 deletion app/internal/on_this_day_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from sqlalchemy.orm import Session
from sqlalchemy.orm.exc import NoResultFound

from app.database.database import get_db
from app.database.models import WikipediaEvents
from app.dependencies import get_db


def insert_on_this_day_data(
Expand Down
4 changes: 2 additions & 2 deletions app/internal/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import List

from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.session import Session

from app.database.database import SessionLocal
from app.database.models import Event


Expand All @@ -16,7 +16,7 @@ def get_stripped_keywords(keywords: str) -> str:


def get_results_by_keywords(
session: SessionLocal,
session: Session,
yammesicka marked this conversation as resolved.
Show resolved Hide resolved
keywords: str,
owner_id: int
) -> List[Event]:
Expand Down
9 changes: 4 additions & 5 deletions app/internal/translation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Optional

from iso639 import languages
from loguru import logger
from sqlalchemy.orm.session import Session
from textblob import TextBlob, download_corpora
from textblob.exceptions import NotTranslated

from app.database.database import SessionLocal
from loguru import logger

from app.routers.user import get_users

download_corpora.download_all()
Expand Down Expand Up @@ -46,7 +45,7 @@ def _detect_text_language(text: str) -> str:
return str(TextBlob(text).detect_language())


def _get_user_language(user_id: int, session: SessionLocal) -> str:
def _get_user_language(user_id: int, session: Session) -> str:
"""
Gets a user-id and returns the language he speaks
Uses the DB"""
Expand All @@ -62,7 +61,7 @@ def _get_user_language(user_id: int, session: SessionLocal) -> str:


def translate_text_for_user(text: str,
session: SessionLocal,
session: Session,
user_id: int) -> str:
"""
Gets a text and a user-id and returns the text,
Expand Down