Skip to content

Feature/out of office #311

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 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
15 changes: 13 additions & 2 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from typing import Any, Dict

from sqlalchemy import (
Boolean, Column, DateTime, DDL, event, Float, ForeignKey, Index, Integer,
JSON, String, Time, UniqueConstraint)
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
Expand Down Expand Up @@ -298,6 +299,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)


class Comment(Base):
__tablename__ = "comments"

Expand Down
13 changes: 11 additions & 2 deletions 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 @@ -73,7 +73,8 @@ def find_pattern(session, event):

def get_messages(session: Session,
event: Event,
uninvited_contacts: Set[str]) -> List[str]:
uninvited_contacts: Set[str],
out_of_office_users: List[Tuple[str, str]]) -> List[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it be none?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what did you mean?

messages = []
if uninvited_contacts:
messages.append(f'Forgot to invite '
Expand All @@ -83,4 +84,12 @@ def get_messages(session: Session,
for weeks_diff in pattern:
messages.append(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
messages.append('Out of office:')
for user in out_of_office_users:
messages.append(f'Username: {user[username]}, '
f'Email: {user[email]}')
return messages
93 changes: 93 additions & 0 deletions app/internal/out_of_office.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from datetime import datetime
from sqlalchemy.orm import Session

from app.database.models import User, OutOfOffice


def get_who_is_out_of_office(session: Session,
event_start_date,
invited_emails):
"""
This func check who is out of office
:param session: db session
:param event_start_date: event start date
:param invited_emails: invited users
:return: all users that cant be at the meeting
"""
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,
session):
out = get_out_of_office_template(1,
user_id=user.id,
start_date=datetime.
strptime(
out_of_office_data['start_date']
+ ' ' +
out_of_office_data['start_time'],
'%Y-%m-%d %H:%M'),
end_date=datetime.strptime(
out_of_office_data['end_date']
+ ' ' +
out_of_office_data['end_time'],
'%Y-%m-%d %H:%M'),
status='On')
session.add(out)


def get_out_of_office_template(out_of_office_id,
user_id,
start_date=None,
end_date=None,
status='Off'):
return OutOfOffice(
id=out_of_office_id,
user_id=user_id,
start_date=start_date,
end_date=end_date,
status=status
)


def update_out_of_office(out_of_office_data_from_req,
out_of_office_data_from_db):
activate_out_of_office = '1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add TODO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain, please :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could you please explain this line? why you set always '1'?

if out_of_office_data_from_req['outOfOffice'] == activate_out_of_office:
out_of_office_data_from_db.start_date = datetime.strptime(
out_of_office_data_from_req['start_date']
+ ' ' +
out_of_office_data_from_req['start_time'],
'%Y-%m-%d %H:%M')
out_of_office_data_from_db.end_date = datetime.strptime(
out_of_office_data_from_req['end_date']
+ ' ' +
out_of_office_data_from_req['end_time'],
'%Y-%m-%d %H:%M')
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):
"""
This func check if out of office date passed and changed the status to off
:param out_of_office_data: Out of office data from db
:param session:
:return: 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():
# update status to off
out_of_office_data.status = 'Off'
session.commit()
return out_of_office_data
14 changes: 10 additions & 4 deletions app/routers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
)
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.utils import create_model, get_current_user


EVENT_DATA = Tuple[Event, List[Dict[str, str]], str, str]
TIME_FORMAT = '%Y-%m-%d %H:%M'
START_FORMAT = '%A, %d/%m/%Y %H:%M'
Expand Down Expand Up @@ -107,7 +107,13 @@ async def create_new_event(request: Request,
category_id=category_id,
availability=availability)

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)}',
status_code=status.HTTP_302_FOUND)
Expand Down Expand Up @@ -304,8 +310,8 @@ def sort_by_date(events: List[Event]) -> List[Event]:

def get_attendees_email(session: Session, event: Event):
return (
session.query(User.email).join(UserEvent)
.filter(UserEvent.events == event).all()
(session.query(User.email).join(UserEvent).
filter(UserEvent.events == event).all())
)


Expand Down
46 changes: 44 additions & 2 deletions app/routers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
from sqlalchemy.exc import SQLAlchemyError

from app import config
from app.database.models import User
from app.database.models import User, OutOfOffice

from app.dependencies import get_db, MEDIA_PATH, templates, GOOGLE_ERROR
from app.internal.on_this_day_events import get_on_this_day_events
from app.internal.import_holidays import (get_holidays_from_file,
save_holidays_to_db)
from app.internal.on_this_day_events import get_on_this_day_events
from app.internal.out_of_office import (insert_new_out_of_office,
update_out_of_office,
update_out_of_office_status_to_off)

PICTURE_EXTENSION = config.PICTURE_EXTENSION
PICTURE_SIZE = config.AVATAR_SIZE
Expand Down Expand Up @@ -48,6 +52,11 @@ async def profile(
session.commit()
user = session.query(User).filter_by(id=1).first()

out_of_office_data = session.query(OutOfOffice).filter_by(id=1).first()
out_of_office_updated_data = (
update_out_of_office_status_to_off
(out_of_office_data, session))

signs = ['Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo',
'Virgo', 'Libra', 'Scorpio', 'Sagittarius',
'Capricorn', 'Aquarius', 'Pisces']
Expand All @@ -60,6 +69,7 @@ async def profile(
"signs": signs,
'google_error': GOOGLE_ERROR,
"on_this_day_data": on_this_day_data,
"out_of_office_data": out_of_office_updated_data,
})


Expand Down Expand Up @@ -182,6 +192,38 @@ def get_image_crop_area(width, height):
return 0, delta, width, width + delta


@router.post("/out_of_office")
async def out_of_office(
request: Request, session=Depends(get_db)):
activate_out_of_office = '1'
user = session.query(User).filter_by(id=1).first()

# TODO: Check if the user exist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could check it with an existing function.


out_of_office_data_from_req = await request.form()

out_of_office_data_from_db = (session.query(OutOfOffice).
filter_by(id=1).first())

# insert new out of office
if not out_of_office_data_from_db:
if (out_of_office_data_from_req['outOfOffice']
== activate_out_of_office):
insert_new_out_of_office(out_of_office_data_from_req,
user,
session)

# update out of office
else:
update_out_of_office(out_of_office_data_from_req,
out_of_office_data_from_db)

session.commit()

url = router.url_path_for("profile")
return RedirectResponse(url=url, status_code=HTTP_302_FOUND)


@router.post("/holidays/update")
async def update(
file: UploadFile = File(...), session=Depends(get_db)):
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions app/static/out_of_office/out_of_office.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function findselected() {
let deactivate = 0;
let result = document.querySelector('input[name="outOfOffice"]:checked').value;
if (result == deactivate) {
document.getElementById("start_date").setAttribute('disabled', true);
document.getElementById("end_date").setAttribute('disabled', true);
document.getElementById("start_time").setAttribute('disabled', true);
document.getElementById("end_time").setAttribute('disabled', true);
}
else {
document.getElementById("start_date").removeAttribute('disabled');
document.getElementById("end_date").removeAttribute('disabled');
document.getElementById("start_time").removeAttribute('disabled');
document.getElementById("end_time").removeAttribute('disabled');
}
}
Loading