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/out of office #311

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
07ad396
Add new table out of office
YairEn Feb 3, 2021
3603fc6
Change requirements
YairEn Feb 10, 2021
4c0ef83
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
YairEn Feb 13, 2021
ab738c9
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
YairEn Feb 13, 2021
c67115b
Merge branch 'develop' into feature/out-of-office
YairEn Feb 13, 2021
6758cfb
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
YairEn Feb 14, 2021
d547f24
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
YairEn Feb 15, 2021
b33002f
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
YairEn Feb 16, 2021
185d37f
Merge branch 'develop' into feature/out-of-office
YairEn Feb 16, 2021
7a03352
Add out of office file
YairEn Feb 16, 2021
9ff90a7
Add out of office.py file
YairEn Feb 16, 2021
c4a41e3
Check Profile.html
YairEn Feb 16, 2021
cea872c
Fix Pylint
YairEn Feb 16, 2021
dde0739
Fix Pylint
YairEn Feb 16, 2021
d52f52f
Fix Js Out of office
YairEn Feb 16, 2021
75f5b9b
Add js script to html
YairEn Feb 16, 2021
bc69873
Merge branch 'develop' into feature/out-of-office
YairEn Feb 16, 2021
b0c3b22
Fix profile.html
YairEn Feb 16, 2021
83c7755
Change comments and typing , add date format function
YairEn Feb 22, 2021
1989a54
Merge branch 'develop' into feature/out-of-office
YairEn Feb 23, 2021
b29196b
Change user to user_db and user.id
YairEn Feb 23, 2021
a0856e2
Change indent to 2 spaces
YairEn Feb 23, 2021
c7478a4
Remove if main
YairEn Feb 23, 2021
3413899
Merge branch 'feature/out-of-office' of https://github.com/YairEn/cal…
YairEn Feb 23, 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
10 changes: 10 additions & 0 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ class Quote(Base):
author = Column(String)


class OutOfOffice(Base):
__tablename__ = "out_of_office"

id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
start_date = Column(DateTime, nullable=False)
end_date = Column(DateTime, nullable=False)
status = Column(String, nullable=False)
YairEn marked this conversation as resolved.
Show resolved Hide resolved


class Comment(Base):
__tablename__ = "comments"

Expand Down
13 changes: 12 additions & 1 deletion app/internal/event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
from typing import List, Set
from typing import List, Set, Tuple

from email_validator import EmailSyntaxError, validate_email
from fastapi import HTTPException
Expand Down Expand Up @@ -87,6 +87,7 @@ def get_messages(
session: Session,
event: Event,
uninvited_contacts: Set[str],
out_of_office_users: List[Tuple[str, str]],
) -> List[str]:
messages = []
if uninvited_contacts:
Expand All @@ -100,4 +101,14 @@ def get_messages(
f"Same event happened {weeks_diff} weeks before too. "
f"Want to create another one {weeks_diff} after too?",
)

if out_of_office_users:
username = 0
email = 1
YairEn marked this conversation as resolved.
Show resolved Hide resolved
messages.append("Out of office:")
for user in out_of_office_users:
messages.append(
f"Username: {user[username]}, " f"Email: {user[email]}",
)

return messages
125 changes: 125 additions & 0 deletions app/internal/out_of_office.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from datetime import datetime
from sqlalchemy.orm import Session
from typing import List

from app.database.models import User, OutOfOffice

DATETIME_FORMAT = "%Y-%m-%d %H:%M"
START_DATE = "start_date"
END_DATE = "end_date"
START_TIME = "start_time"
END_TIME = "end_time"


def get_who_is_out_of_office(
session: Session,
event_start_date: datetime,
invited_emails: List[str],
):
"""
Get who is out of office

Args:
session: db session
event_start_date: event start date
invited_emails: invited emails

Returns:
Users who are out of office at the event date
"""
out_of_office_users = (
session.query(User.username, User.email)
.join(OutOfOffice)
.filter(User.email.in_(invited_emails))
.filter(
OutOfOffice.start_date <= event_start_date,
OutOfOffice.end_date >= event_start_date,
)
.filter(OutOfOffice.status == "On")
.all()
)
return out_of_office_users


def insert_new_out_of_office(out_of_office_data, user: User, session: Session):
out = get_out_of_office_template(
user_id=user.id,
start_date=get_date_formatted(
out_of_office_data,
START_DATE,
START_TIME,
DATETIME_FORMAT,
),
end_date=get_date_formatted(
out_of_office_data,
END_DATE,
END_TIME,
DATETIME_FORMAT,
),
status="On",
)
session.add(out)
YairEn marked this conversation as resolved.
Show resolved Hide resolved


def get_out_of_office_template(
user_id,
start_date=None,
end_date=None,
status="Off",
):
return OutOfOffice(
user_id=user_id,
start_date=start_date,
end_date=end_date,
status=status,
)


def get_date_formatted(out_of_office_data, date, time, date_format):
return datetime.strptime(
out_of_office_data[date] + " " + out_of_office_data[time],
date_format,
)


def update_out_of_office(
out_of_office_data_from_req,
out_of_office_data_from_db,
):
activate_out_of_office = "1"

if out_of_office_data_from_req["outOfOffice"] == activate_out_of_office:
out_of_office_data_from_db.start_date = get_date_formatted(
out_of_office_data_from_req,
START_DATE,
START_TIME,
DATETIME_FORMAT,
)
out_of_office_data_from_db.end_date = get_date_formatted(
out_of_office_data_from_req,
END_DATE,
END_TIME,
DATETIME_FORMAT,
)
out_of_office_data_from_db.status = "On"
else:
out_of_office_data_from_db.status = "Off"


def update_out_of_office_status_to_off(out_of_office_data, session: Session):
"""
Update out of office status to off if out of office date passed

Args:
out_of_office_data: Out of office data from db
session: db session

Returns:
out_of_office_data object
"""
if out_of_office_data:
if out_of_office_data.status == "On":
if out_of_office_data.end_date < datetime.now():
out_of_office_data.status = "Off"
session.commit()
return out_of_office_data
YairEn marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 13 additions & 2 deletions app/routers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
)
from app.internal import comment as cmt
from app.internal.emotion import get_emotion
from app.internal.out_of_office import get_who_is_out_of_office
from app.internal.privacy import PrivacyKinds
from app.internal.utils import create_model, get_current_user
from app.routers.categories import get_user_categories

EVENT_DATA = Tuple[Event, List[Dict[str, str]], str]
TIME_FORMAT = "%Y-%m-%d %H:%M"
START_FORMAT = "%A, %d/%m/%Y %H:%M"

UPDATE_EVENTS_FIELDS = {
"title": str,
"start": dt,
Expand All @@ -44,7 +46,6 @@
"category_id": (int, type(None)),
}


router = APIRouter(
prefix="/event",
tags=["event"],
Expand Down Expand Up @@ -153,7 +154,17 @@ async def create_new_event(
privacy=privacy,
)

messages = get_messages(session, event, uninvited_contacts)
out_of_office_users = get_who_is_out_of_office(
session,
start,
invited_emails,
)
messages = get_messages(
session,
event,
uninvited_contacts,
out_of_office_users,
)
return RedirectResponse(
router.url_path_for("eventview", event_id=event.id)
+ f'?messages={"---".join(messages)}',
Expand Down
Loading