From a32cc6aed63ab7a8174718979882d9b81d4fd7df Mon Sep 17 00:00:00 2001 From: imimouni Date: Tue, 9 Feb 2021 20:34:22 +0200 Subject: [PATCH 01/14] feat: calendar privacy frontend backend and tests --- app/database/models.py | 1 + app/internal/calendar_privacy.py | 21 +++++++++++++ app/routers/profile.py | 17 ++++++++++ app/templates/profile.html | 30 +++++++++++++++++- tests/test_calendar_privacy.py | 54 ++++++++++++++++++++++++++++++++ tests/test_profile.py | 17 ++++++++++ 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 app/internal/calendar_privacy.py create mode 100644 tests/test_calendar_privacy.py diff --git a/app/database/models.py b/app/database/models.py index c1a63450..63be2207 100644 --- a/app/database/models.py +++ b/app/database/models.py @@ -42,6 +42,7 @@ class User(Base): avatar = Column(String, default="profile.png") telegram_id = Column(String, unique=True) is_active = Column(Boolean, default=False) + privacy = Column(String, default="Private", nullable=False) events = relationship("UserEvent", back_populates="participants") diff --git a/app/internal/calendar_privacy.py b/app/internal/calendar_privacy.py new file mode 100644 index 00000000..fe78dc78 --- /dev/null +++ b/app/internal/calendar_privacy.py @@ -0,0 +1,21 @@ +from app.database.database import get_db +from app.database.models import User +# from app.internal.security.dependancies import ( +# current_user, CurrentUser) + +from fastapi import Depends + + +# TODO add privacy as an attribute in current user in app.internal.security.dependancies +def can_show_calendar(requested_user_username: str, db: Depends(get_db), current_user: User): + # to be added after user system is merged: CurrentUser = Depends(current_user)): + """Check whether current user can show the requested calendar""" + requested_user = db.query(User).filter(User.username == requested_user_username).first() + privacy = current_user.privacy + if privacy == 'Private' and current_user.username == requested_user.username: + return True + + elif privacy == 'Public': + return True + + return \ No newline at end of file diff --git a/app/routers/profile.py b/app/routers/profile.py index b4be7b6d..58832494 100644 --- a/app/routers/profile.py +++ b/app/routers/profile.py @@ -141,6 +141,23 @@ async def update_telegram_id( return RedirectResponse(url=url, status_code=HTTP_302_FOUND) +@router.post("/update_calendar_privacy") +async def update_calendar_privacy( + request: Request, session=Depends(get_db)): + + user = session.query(User).filter_by(id=1).first() + data = await request.form() + print(data) + new_privacy = data['privacy'] + + # Update database + user.privacy = new_privacy + session.commit() + + url = router.url_path_for("profile") + return RedirectResponse(url=url, status_code=HTTP_302_FOUND) + + async def process_image(image, user): img = Image.open(io.BytesIO(image)) width, height = img.size diff --git a/app/templates/profile.html b/app/templates/profile.html index 764bc2c1..37e645d1 100644 --- a/app/templates/profile.html +++ b/app/templates/profile.html @@ -21,7 +21,7 @@
  • @@ -39,6 +39,11 @@ Register telegram Id
  • +
  • + +
  • @@ -146,6 +151,29 @@ + + + diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py new file mode 100644 index 00000000..f038bb02 --- /dev/null +++ b/tests/test_calendar_privacy.py @@ -0,0 +1,54 @@ +from app.internal.calendar_privacy import can_show_calendar +# after user system is merged: from app.internal.security.dependancies import CurrentUser +from app.routers.user import create_user + +import pytest + + +USER_DATA = { + 'username': 'new_test_username', + 'email': 'new_test.email@gmail.com', + 'password': 'new_test_password', + 'language': 'english' +} + + +def test_can_show_calendar_public(session): + user = create_user( + session=session, + **USER_DATA + ) + user.privacy = "Public" + # to be replaced after user system is merged:current_user = CurrentUser(**user.__dict__) + current_user = user + result = can_show_calendar(requested_user_username='new_test_username', db=session, current_user=current_user) + assert result + session.delete(user) + session.commit() + + +def test_can_show_calendar_private(session): + user = create_user( + session=session, + **USER_DATA + ) + another_user = create_user( + session=session, + username='new_test_username2', + email='new_test.email2@gmail.com', + password='passpar_2', + language='english' + ) + + current_user = user + # to be replaced after user system is merged: current_user = CurrentUser(**user.__dict__) + + result_a = can_show_calendar(requested_user_username='new_test_username2', db=session, current_user=current_user) + result_b = can_show_calendar(requested_user_username='new_test_username', db=session, current_user=current_user) + assert not result_a + assert result_b + session.delete(user) + session.delete(another_user) + session.commit() + + diff --git a/tests/test_profile.py b/tests/test_profile.py index 4064d06e..14cc51ea 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -127,3 +127,20 @@ def test_upload_user_photo(profile_test_client): new_avatar_path = os.path.join(MEDIA_PATH, 'fake_user.png') assert Image.open(new_avatar_path).size == config.AVATAR_SIZE os.remove(new_avatar_path) + + +def test_update_calendar_privacy(profile_test_client): + new_privacy = { + 'privacy': "Public" + } + # Get profile page and initialize database + profile = profile_test_client.get('/profile') + + # Post new data + profile = profile_test_client.post( + '/profile/update_calendar_privacy', data=new_privacy) + assert profile.status_code == status.HTTP_302_FOUND + + # Get updated data + data = profile_test_client.get('/profile').content + assert b"Public" in data From e29a73e3c1c317e4640f06c99a2f846f8009e2f3 Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 01:06:54 +0200 Subject: [PATCH 02/14] fix: remove print and empty line profile.py --- app/routers/profile.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/routers/profile.py b/app/routers/profile.py index 58832494..1366d99f 100644 --- a/app/routers/profile.py +++ b/app/routers/profile.py @@ -144,10 +144,8 @@ async def update_telegram_id( @router.post("/update_calendar_privacy") async def update_calendar_privacy( request: Request, session=Depends(get_db)): - user = session.query(User).filter_by(id=1).first() data = await request.form() - print(data) new_privacy = data['privacy'] # Update database From 40008d2d396caa58ead0493f9f96f1ca86edccac Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 01:29:07 +0200 Subject: [PATCH 03/14] fix: flake8 --- app/internal/calendar_privacy.py | 24 +++++++++++++++++------- app/routers/profile.py | 4 +++- tests/test_calendar_privacy.py | 4 +--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/internal/calendar_privacy.py b/app/internal/calendar_privacy.py index fe78dc78..4bf2481c 100644 --- a/app/internal/calendar_privacy.py +++ b/app/internal/calendar_privacy.py @@ -6,16 +6,26 @@ from fastapi import Depends -# TODO add privacy as an attribute in current user in app.internal.security.dependancies -def can_show_calendar(requested_user_username: str, db: Depends(get_db), current_user: User): - # to be added after user system is merged: CurrentUser = Depends(current_user)): +# TODO add privacy as an attribute in current user +# in app.internal.security.dependancies +def can_show_calendar( + requested_user_username: str, + db: Depends(get_db), + current_user: User +): + # to be added after user system is merged: + # CurrentUser = Depends(current_user)): """Check whether current user can show the requested calendar""" - requested_user = db.query(User).filter(User.username == requested_user_username).first() + requested_user = db.query(User).filter( + User.username == requested_user_username + ).first() privacy = current_user.privacy - if privacy == 'Private' and current_user.username == requested_user.username: + if privacy == 'Private' and ( + current_user.username == requested_user.username + ): return True - + elif privacy == 'Public': return True - return \ No newline at end of file + return diff --git a/app/routers/profile.py b/app/routers/profile.py index 1366d99f..218bfd2a 100644 --- a/app/routers/profile.py +++ b/app/routers/profile.py @@ -143,7 +143,9 @@ async def update_telegram_id( @router.post("/update_calendar_privacy") async def update_calendar_privacy( - request: Request, session=Depends(get_db)): + request: Request, + session=Depends(get_db) +): user = session.query(User).filter_by(id=1).first() data = await request.form() new_privacy = data['privacy'] diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index f038bb02..22279ef6 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -49,6 +49,4 @@ def test_can_show_calendar_private(session): assert result_b session.delete(user) session.delete(another_user) - session.commit() - - + session.commit() \ No newline at end of file From 8f01051fff917ebe9a59079872b8fe0f88903fc3 Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 01:41:04 +0200 Subject: [PATCH 04/14] fix: flake8 --- app/internal/calendar_privacy.py | 4 ++-- app/routers/event.py | 2 +- tests/test_calendar_privacy.py | 28 ++++++++++++++++++---------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/internal/calendar_privacy.py b/app/internal/calendar_privacy.py index 4bf2481c..6768dc2b 100644 --- a/app/internal/calendar_privacy.py +++ b/app/internal/calendar_privacy.py @@ -13,7 +13,7 @@ def can_show_calendar( db: Depends(get_db), current_user: User ): - # to be added after user system is merged: + # to be added after user system is merged: # CurrentUser = Depends(current_user)): """Check whether current user can show the requested calendar""" requested_user = db.query(User).filter( @@ -24,7 +24,7 @@ def can_show_calendar( current_user.username == requested_user.username ): return True - + elif privacy == 'Public': return True diff --git a/app/routers/event.py b/app/routers/event.py index c5f1b4f2..dd20ef6f 100644 --- a/app/routers/event.py +++ b/app/routers/event.py @@ -273,7 +273,7 @@ def delete_event(event_id: int, url="/calendar", status_code=status.HTTP_200_OK) -def is_date_before(start_time: datetime, end_time: datetime) -> bool: +def is_date_before(start_time: dt, end_time: dt) -> bool: """Check if the start_date is smaller then the end_time""" try: return start_time < end_time diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index 22279ef6..258abd34 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -1,9 +1,8 @@ from app.internal.calendar_privacy import can_show_calendar -# after user system is merged: from app.internal.security.dependancies import CurrentUser +# after user system is merged: +# from app.internal.security.dependancies import CurrentUser from app.routers.user import create_user -import pytest - USER_DATA = { 'username': 'new_test_username', @@ -19,9 +18,12 @@ def test_can_show_calendar_public(session): **USER_DATA ) user.privacy = "Public" - # to be replaced after user system is merged:current_user = CurrentUser(**user.__dict__) + # to be replaced after user system is merged: + # current_user = CurrentUser(**user.__dict__) current_user = user - result = can_show_calendar(requested_user_username='new_test_username', db=session, current_user=current_user) + result = can_show_calendar( + requested_user_username='new_test_username', db=session, current_user=current_user + ) assert result session.delete(user) session.commit() @@ -39,14 +41,20 @@ def test_can_show_calendar_private(session): password='passpar_2', language='english' ) - current_user = user - # to be replaced after user system is merged: current_user = CurrentUser(**user.__dict__) + # to be replaced after user system is merged: + # current_user = CurrentUser(**user.__dict__) - result_a = can_show_calendar(requested_user_username='new_test_username2', db=session, current_user=current_user) - result_b = can_show_calendar(requested_user_username='new_test_username', db=session, current_user=current_user) + result_a = can_show_calendar( + requested_user_username='new_test_username2', + db=session, current_user=current_user + ) + result_b = can_show_calendar( + requested_user_username='new_test_username', + db=session, current_user=current_user + ) assert not result_a assert result_b session.delete(user) session.delete(another_user) - session.commit() \ No newline at end of file + session.commit() From 36f2480e8bd9852bac46b422783682e3e34745cc Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 01:44:33 +0200 Subject: [PATCH 05/14] fix: flake8 final --- tests/test_calendar_privacy.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index 258abd34..a35e3d1d 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -22,7 +22,8 @@ def test_can_show_calendar_public(session): # current_user = CurrentUser(**user.__dict__) current_user = user result = can_show_calendar( - requested_user_username='new_test_username', db=session, current_user=current_user + requested_user_username='new_test_username', + db=session, current_user=current_user ) assert result session.delete(user) From 072d124188a639ccd76f4d34f63834a3fce3ef42 Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 01:49:15 +0200 Subject: [PATCH 06/14] fix: test_calendar_privacy.py test user details --- tests/test_calendar_privacy.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index a35e3d1d..887bf4c6 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -8,7 +8,8 @@ 'username': 'new_test_username', 'email': 'new_test.email@gmail.com', 'password': 'new_test_password', - 'language': 'english' + 'language': 'english', + 'language_id': 1 } @@ -40,7 +41,8 @@ def test_can_show_calendar_private(session): username='new_test_username2', email='new_test.email2@gmail.com', password='passpar_2', - language='english' + language='english', + language_id=1 ) current_user = user # to be replaced after user system is merged: From 48704b01f9c5a1703b1ec221dfe3ceb65b7ffe54 Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 19:00:27 +0200 Subject: [PATCH 07/14] fix: comments, typing, user fixture, readability fix --- app/internal/calendar_privacy.py | 15 +++++++------ tests/test_calendar_privacy.py | 37 ++++++++------------------------ 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/app/internal/calendar_privacy.py b/app/internal/calendar_privacy.py index 6768dc2b..18c121a2 100644 --- a/app/internal/calendar_privacy.py +++ b/app/internal/calendar_privacy.py @@ -1,5 +1,6 @@ from app.database.database import get_db from app.database.models import User +# TODO switch to using this when the user system is merged # from app.internal.security.dependancies import ( # current_user, CurrentUser) @@ -8,24 +9,24 @@ # TODO add privacy as an attribute in current user # in app.internal.security.dependancies +# when user system is merged def can_show_calendar( requested_user_username: str, db: Depends(get_db), current_user: User -): - # to be added after user system is merged: - # CurrentUser = Depends(current_user)): + # TODO to be added after user system is merged: + # CurrentUser = Depends(current_user) +) -> bool: """Check whether current user can show the requested calendar""" requested_user = db.query(User).filter( User.username == requested_user_username ).first() privacy = current_user.privacy - if privacy == 'Private' and ( - current_user.username == requested_user.username - ): + is_current_user = current_user.username == requested_user.username + if privacy == 'Private' and is_current_user: return True elif privacy == 'Public': return True - return + return False diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index 887bf4c6..5f9893da 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -1,41 +1,23 @@ from app.internal.calendar_privacy import can_show_calendar -# after user system is merged: +# TODO after user system is merged: # from app.internal.security.dependancies import CurrentUser from app.routers.user import create_user -USER_DATA = { - 'username': 'new_test_username', - 'email': 'new_test.email@gmail.com', - 'password': 'new_test_password', - 'language': 'english', - 'language_id': 1 -} - - -def test_can_show_calendar_public(session): - user = create_user( - session=session, - **USER_DATA - ) +def test_can_show_calendar_public(session, user): user.privacy = "Public" - # to be replaced after user system is merged: + # TODO to be replaced after user system is merged: # current_user = CurrentUser(**user.__dict__) current_user = user result = can_show_calendar( - requested_user_username='new_test_username', + requested_user_username='test_username', db=session, current_user=current_user ) - assert result - session.delete(user) + assert result is True session.commit() -def test_can_show_calendar_private(session): - user = create_user( - session=session, - **USER_DATA - ) +def test_can_show_calendar_private(session, user): another_user = create_user( session=session, username='new_test_username2', @@ -45,7 +27,7 @@ def test_can_show_calendar_private(session): language_id=1 ) current_user = user - # to be replaced after user system is merged: + # TODO to be replaced after user system is merged: # current_user = CurrentUser(**user.__dict__) result_a = can_show_calendar( @@ -56,8 +38,7 @@ def test_can_show_calendar_private(session): requested_user_username='new_test_username', db=session, current_user=current_user ) - assert not result_a - assert result_b - session.delete(user) + assert result_a is False + assert result_b is True session.delete(another_user) session.commit() From 0f140605bc707930cf38d89204a017138a2db834 Mon Sep 17 00:00:00 2001 From: imimouni Date: Wed, 10 Feb 2021 19:48:03 +0200 Subject: [PATCH 08/14] fix: conflicts with new merge in develop --- app/internal/calendar_privacy.py | 2 +- tests/test_calendar_privacy.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/internal/calendar_privacy.py b/app/internal/calendar_privacy.py index 18c121a2..d827f0e4 100644 --- a/app/internal/calendar_privacy.py +++ b/app/internal/calendar_privacy.py @@ -1,4 +1,4 @@ -from app.database.database import get_db +from app.dependencies import get_db from app.database.models import User # TODO switch to using this when the user system is merged # from app.internal.security.dependancies import ( diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index 5f9893da..46cf5f94 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -35,7 +35,7 @@ def test_can_show_calendar_private(session, user): db=session, current_user=current_user ) result_b = can_show_calendar( - requested_user_username='new_test_username', + requested_user_username='test_username', db=session, current_user=current_user ) assert result_a is False From da153282f10fd86740ec75e4e59afa94c629f720 Mon Sep 17 00:00:00 2001 From: imimouni Date: Fri, 12 Feb 2021 18:30:12 +0200 Subject: [PATCH 09/14] fix: flake8 after merge --- app/routers/profile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/routers/profile.py b/app/routers/profile.py index 8a4a2ab6..f97311c9 100644 --- a/app/routers/profile.py +++ b/app/routers/profile.py @@ -154,6 +154,7 @@ async def update_calendar_privacy( url = router.url_path_for("profile") return RedirectResponse(url=url, status_code=HTTP_302_FOUND) + @router.get("/holidays/import") def import_holidays(request: Request): return templates.TemplateResponse("import_holidays.html", { From 52bbfaba29e9165775db7f63226928ca5a0e8246 Mon Sep 17 00:00:00 2001 From: imimouni Date: Fri, 12 Feb 2021 18:35:05 +0200 Subject: [PATCH 10/14] fix: test_calendar_privacy fix after merge - for pytest --- tests/test_calendar_privacy.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_calendar_privacy.py b/tests/test_calendar_privacy.py index 46cf5f94..641b8cf5 100644 --- a/tests/test_calendar_privacy.py +++ b/tests/test_calendar_privacy.py @@ -23,7 +23,6 @@ def test_can_show_calendar_private(session, user): username='new_test_username2', email='new_test.email2@gmail.com', password='passpar_2', - language='english', language_id=1 ) current_user = user From f4368503347b735e62c24432c9745a093b8d3606 Mon Sep 17 00:00:00 2001 From: imimouni Date: Sat, 13 Feb 2021 20:27:19 +0200 Subject: [PATCH 11/14] fix: correct title element in profile.py + css fix --- app/static/style.css | 6 +++++- app/templates/profile.html | 18 +++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/static/style.css b/app/static/style.css index c0eb0101..47369a6c 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -67,4 +67,8 @@ body { .subtitle { font-size: 20px; -} \ No newline at end of file +} + +h1.modal-title { + font-size: 1.25rem; +} diff --git a/app/templates/profile.html b/app/templates/profile.html index 69c81628..fdb7416a 100644 --- a/app/templates/profile.html +++ b/app/templates/profile.html @@ -60,7 +60,7 @@