From 98670bec321e7f2628208c7c16151064c1bc9117 Mon Sep 17 00:00:00 2001 From: itzik Date: Tue, 16 Feb 2021 01:42:44 +0200 Subject: [PATCH 01/10] feat: adding global variable --- app/main.py | 11 ++++++++-- app/routers/global_variable.py | 33 ++++++++++++++++++++++++++++++ app/templates/global_var_test.html | 7 +++++++ tests/client_fixture.py | 7 ++++++- tests/test_global_variable.py | 4 ++++ 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 app/routers/global_variable.py create mode 100644 app/templates/global_var_test.html create mode 100644 tests/test_global_variable.py diff --git a/app/main.py b/app/main.py index 0ba0bfee..4a39cc63 100644 --- a/app/main.py +++ b/app/main.py @@ -36,8 +36,8 @@ def create_tables(engine, psql_environment): from app.routers import ( # noqa: E402 agenda, calendar, categories, celebrity, currency, dayview, - email, event, four_o_four, invitation, profile, search, - weekview, telegram, whatsapp, + email, event, four_o_four, global_variable, invitation, profile, + search, weekview, telegram, whatsapp ) json_data_loader.load_to_db(next(get_db())) @@ -53,6 +53,7 @@ def create_tables(engine, psql_environment): email.router, event.router, four_o_four.router, + global_variable.router, invitation.router, profile.router, salary.router, @@ -61,6 +62,12 @@ def create_tables(engine, psql_environment): whatsapp.router, ] +user_id = 1 +db_gen = get_db() +session = next(db_gen) +user = session.query(models.User).filter_by(id=user_id).first() +templates.env.globals['user'] = user + for router in routers_to_include: app.include_router(router) diff --git a/app/routers/global_variable.py b/app/routers/global_variable.py new file mode 100644 index 00000000..2ef40010 --- /dev/null +++ b/app/routers/global_variable.py @@ -0,0 +1,33 @@ +from fastapi import APIRouter, Depends, Request +from sqlalchemy.orm import Session + +from app.database.models import User +from app.dependencies import get_db, templates + + +router = APIRouter( + prefix="/global-variable", + tags=["global-variable"], + responses={404: {"description": "Not found"}}, +) + + +@router.get("/") +async def global_var(request: Request, db: Session = Depends(get_db)): + user = User( + username='test_user', + email='my@email.po', + password='1a2s3d4f5g6', + full_name='My Name', + language_id=1, + telegram_id='', + ) + user_db = db.query(User).filter_by(username=user.username).first() + if not user_db: + db.add(user) + db.commit() + user = db.query(User).filter_by(username=user.username).first() + templates.env.globals['user'] = user + return templates.TemplateResponse("global_var_test.html", { + "request": request + }) \ No newline at end of file diff --git a/app/templates/global_var_test.html b/app/templates/global_var_test.html new file mode 100644 index 00000000..a6dcb2ac --- /dev/null +++ b/app/templates/global_var_test.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block content %} + +

username: {{ user.username }}

+ +{% endblock %} \ No newline at end of file diff --git a/tests/client_fixture.py b/tests/client_fixture.py index 3b72f296..f2aa1262 100644 --- a/tests/client_fixture.py +++ b/tests/client_fixture.py @@ -5,7 +5,7 @@ from app import main from app.database.models import Base, User -from app.routers import agenda, event, invitation, profile +from app.routers import agenda, event, invitation, profile, global_variable from app.routers.salary import routes as salary from tests.conftest import get_test_db, test_engine @@ -37,6 +37,11 @@ def create_test_client(get_db_function) -> Iterator[TestClient]: Base.metadata.drop_all(bind=test_engine) +@pytest.fixture(scope="session") +def global_var_test_client() -> Iterator[TestClient]: + yield from create_test_client(global_variable.get_db) + + @pytest.fixture(scope="session") def agenda_test_client() -> Iterator[TestClient]: yield from create_test_client(agenda.get_db) diff --git a/tests/test_global_variable.py b/tests/test_global_variable.py new file mode 100644 index 00000000..7c952470 --- /dev/null +++ b/tests/test_global_variable.py @@ -0,0 +1,4 @@ +def test_global_var(global_var_test_client): + response = global_var_test_client.get("/global-variable") + assert response.ok + assert b'test_user' in response.content From 59411dfe54886c227d595d344087854b4eade666 Mon Sep 17 00:00:00 2001 From: itzik Date: Tue, 16 Feb 2021 02:13:41 +0200 Subject: [PATCH 02/10] fix: fixed for flake8 --- app/routers/global_variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routers/global_variable.py b/app/routers/global_variable.py index 2ef40010..a6abaa66 100644 --- a/app/routers/global_variable.py +++ b/app/routers/global_variable.py @@ -30,4 +30,4 @@ async def global_var(request: Request, db: Session = Depends(get_db)): templates.env.globals['user'] = user return templates.TemplateResponse("global_var_test.html", { "request": request - }) \ No newline at end of file + }) From 2a57add86d2523914c43496e66c58d136b5ef5c0 Mon Sep 17 00:00:00 2001 From: itzik Date: Tue, 16 Feb 2021 02:56:43 +0200 Subject: [PATCH 03/10] fix: little fix, README file --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03b39dec..f7b48ef7 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ cp app/config.py.example app/config.py # Edit the variables' values. # Rendering JWT_KEY: python -c "import secrets; from pathlib import Path; f = Path('app/config.py'); f.write_text(f.read_text().replace('JWT_KEY_PLACEHOLDER', secrets.token_hex(32), 1));" +``` ### Running tox ```shell From 365e49ce1221ace0dd2612554baaac88d94d4b84 Mon Sep 17 00:00:00 2001 From: itzik Date: Tue, 16 Feb 2021 03:18:57 +0200 Subject: [PATCH 04/10] feat: added a link to the forum --- app/templates/base.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/templates/base.html b/app/templates/base.html index fdfe2989..d1f79287 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -54,6 +54,9 @@ + From 2890b742b40cdd9caa84787645addbddb2693d6b Mon Sep 17 00:00:00 2001 From: itzik Date: Thu, 18 Feb 2021 02:48:09 +0200 Subject: [PATCH 05/10] fix: improved code for requested changes --- app/demos/global_variable.py | 57 +++++++++++++++++++++++++++ app/main.py | 9 +---- app/routers/global_variable.py | 33 ---------------- app/templates/base.html | 3 -- app/templates/global_var_test.html | 63 ++++++++++++++++++++++++++++-- tests/client_fixture.py | 3 +- tests/test_global_variable.py | 14 ++++++- 7 files changed, 133 insertions(+), 49 deletions(-) create mode 100644 app/demos/global_variable.py delete mode 100644 app/routers/global_variable.py diff --git a/app/demos/global_variable.py b/app/demos/global_variable.py new file mode 100644 index 00000000..32b2fd0a --- /dev/null +++ b/app/demos/global_variable.py @@ -0,0 +1,57 @@ +from fastapi import APIRouter, Depends, Request +from sqlalchemy.orm import Session + +from app.database.models import User +from app.dependencies import get_db, templates + + +router = APIRouter( + prefix="/global-variable", + tags=["global-variable"], + responses={404: {"description": "Not found"}}, +) + + +def create_test_logged_user(session: Session): + user = User( + username='test_user', + email='my@email.po', + password='1a2s3d4f5g6', + full_name='My Name', + language_id=1, + telegram_id='', + ) + + user_query = session.query(User) + user_from_db = user_query.filter_by(username=user.username).first() + if not user_from_db: + session.add(user) + session.commit() + user = session.query(User).filter_by(username=user.username).first() + templates.env.globals['user'] = user + + +# The way it will be written on the page + +# getting the logged in user +session = next(get_db()) +create_test_logged_user(session) +user_from_db = session.query(User).filter_by(username="test_user").first() + +# Placing a global variable +templates.env.globals['user'] = user_from_db + + +@router.get("/") +async def global_var( + request: Request, + test_session: Session = Depends(get_db) +): + # for testing + user_query = test_session.query(User) + user_from_db = user_query.filter_by(username="test_user").first() + templates.env.globals['user'] = user_from_db + + return templates.TemplateResponse("global_var_test.html", { + "request": request + }) diff --git a/app/main.py b/app/main.py index 02b21eab..a43cab2c 100644 --- a/app/main.py +++ b/app/main.py @@ -43,9 +43,10 @@ def create_tables(engine, psql_environment): from app.routers import ( # noqa: E402 agenda, calendar, categories, celebrity, currency, dayview, - email, event, export, four_o_four, global_variable, invitation, login, + email, event, export, four_o_four, invitation, login, logout, profile, register, search, telegram, user, weekview, whatsapp, ) +from app.demos import global_variable # noqa: E402 json_data_loader.load_to_db(next(get_db())) @@ -91,12 +92,6 @@ async def swagger_ui_redirect(): whatsapp.router, ] -user_id = 1 -db_gen = get_db() -session = next(db_gen) -user = session.query(models.User).filter_by(id=user_id).first() -templates.env.globals['user'] = user - for router in routers_to_include: app.include_router(router) diff --git a/app/routers/global_variable.py b/app/routers/global_variable.py deleted file mode 100644 index a6abaa66..00000000 --- a/app/routers/global_variable.py +++ /dev/null @@ -1,33 +0,0 @@ -from fastapi import APIRouter, Depends, Request -from sqlalchemy.orm import Session - -from app.database.models import User -from app.dependencies import get_db, templates - - -router = APIRouter( - prefix="/global-variable", - tags=["global-variable"], - responses={404: {"description": "Not found"}}, -) - - -@router.get("/") -async def global_var(request: Request, db: Session = Depends(get_db)): - user = User( - username='test_user', - email='my@email.po', - password='1a2s3d4f5g6', - full_name='My Name', - language_id=1, - telegram_id='', - ) - user_db = db.query(User).filter_by(username=user.username).first() - if not user_db: - db.add(user) - db.commit() - user = db.query(User).filter_by(username=user.username).first() - templates.env.globals['user'] = user - return templates.TemplateResponse("global_var_test.html", { - "request": request - }) diff --git a/app/templates/base.html b/app/templates/base.html index d1f79287..fdfe2989 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -54,9 +54,6 @@ - diff --git a/app/templates/global_var_test.html b/app/templates/global_var_test.html index a6dcb2ac..8e3b366c 100644 --- a/app/templates/global_var_test.html +++ b/app/templates/global_var_test.html @@ -1,7 +1,62 @@ -{% extends "base.html" %} + + -{% block content %} + + + -

username: {{ user.username }}

+ PyLendar global var test -{% endblock %} \ No newline at end of file + + + + +
+ +
+ +

username: {{ user.username }}

+ + + \ No newline at end of file diff --git a/tests/client_fixture.py b/tests/client_fixture.py index 1ea6721c..d464f2f8 100644 --- a/tests/client_fixture.py +++ b/tests/client_fixture.py @@ -5,7 +5,8 @@ from app import main from app.database.models import Base, User -from app.routers import agenda, event, invitation, profile, global_variable +from app.demos import global_variable +from app.routers import agenda, event, invitation, profile from app.routers.salary import routes as salary from tests.conftest import get_test_db, test_engine from . import security_testing_routes diff --git a/tests/test_global_variable.py b/tests/test_global_variable.py index 7c952470..76aeb3ed 100644 --- a/tests/test_global_variable.py +++ b/tests/test_global_variable.py @@ -1,4 +1,16 @@ -def test_global_var(global_var_test_client): +from app.demos.global_variable import create_test_logged_user + + +def test_global_var(global_var_test_client, session): + response = global_var_test_client.get("/global-variable") + assert response.ok + assert b'test_user' not in response.content + assert b'Sign In' in response.content + assert b'Sign Up' in response.content + + create_test_logged_user(session) response = global_var_test_client.get("/global-variable") assert response.ok assert b'test_user' in response.content + assert b'Sign In' not in response.content + assert b'Profile' in response.content From 7ac04f04694d303c2e95349566185e60659619f3 Mon Sep 17 00:00:00 2001 From: itzik Date: Thu, 18 Feb 2021 16:16:15 +0200 Subject: [PATCH 06/10] fix: Small correction --- app/demos/global_variable.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/demos/global_variable.py b/app/demos/global_variable.py index 32b2fd0a..b7d3dd0d 100644 --- a/app/demos/global_variable.py +++ b/app/demos/global_variable.py @@ -32,7 +32,6 @@ def create_test_logged_user(session: Session): # The way it will be written on the page - # getting the logged in user session = next(get_db()) create_test_logged_user(session) From 9b2f1216439d63f0c859a0b22812717b3a5a1f2c Mon Sep 17 00:00:00 2001 From: itzik Date: Thu, 18 Feb 2021 16:25:56 +0200 Subject: [PATCH 07/10] fix: Small correction --- tests/client_fixture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/client_fixture.py b/tests/client_fixture.py index d7c451e4..a1aad5db 100644 --- a/tests/client_fixture.py +++ b/tests/client_fixture.py @@ -1,4 +1,3 @@ -from app.demos import global_variable from app.routers import agenda, event, friendview, invitation, profile from app.routers import google_connect from typing import Iterator @@ -8,6 +7,7 @@ from app import main from app.database.models import Base, User +from app.demos import global_variable from app.routers.salary import routes as salary from tests.conftest import get_test_db, test_engine from . import security_testing_routes From 32be156db370e39c6d57c6abd49d255b9be45726 Mon Sep 17 00:00:00 2001 From: itzik Date: Mon, 22 Feb 2021 09:41:53 +0200 Subject: [PATCH 08/10] fix: made it more usable with the security module --- app/demos/global_variable.py | 56 ------------------------------ app/internal/global_variable.py | 21 +++++++++++ app/main.py | 2 -- app/templates/global_var_test.html | 29 ++++++---------- tests/client_fixture.py | 6 ++-- tests/global_var_testing_routes.py | 21 +++++++++++ tests/test_global_variable.py | 22 +++++++++--- 7 files changed, 72 insertions(+), 85 deletions(-) delete mode 100644 app/demos/global_variable.py create mode 100644 app/internal/global_variable.py create mode 100644 tests/global_var_testing_routes.py diff --git a/app/demos/global_variable.py b/app/demos/global_variable.py deleted file mode 100644 index b7d3dd0d..00000000 --- a/app/demos/global_variable.py +++ /dev/null @@ -1,56 +0,0 @@ -from fastapi import APIRouter, Depends, Request -from sqlalchemy.orm import Session - -from app.database.models import User -from app.dependencies import get_db, templates - - -router = APIRouter( - prefix="/global-variable", - tags=["global-variable"], - responses={404: {"description": "Not found"}}, -) - - -def create_test_logged_user(session: Session): - user = User( - username='test_user', - email='my@email.po', - password='1a2s3d4f5g6', - full_name='My Name', - language_id=1, - telegram_id='', - ) - - user_query = session.query(User) - user_from_db = user_query.filter_by(username=user.username).first() - if not user_from_db: - session.add(user) - session.commit() - user = session.query(User).filter_by(username=user.username).first() - templates.env.globals['user'] = user - - -# The way it will be written on the page -# getting the logged in user -session = next(get_db()) -create_test_logged_user(session) -user_from_db = session.query(User).filter_by(username="test_user").first() - -# Placing a global variable -templates.env.globals['user'] = user_from_db - - -@router.get("/") -async def global_var( - request: Request, - test_session: Session = Depends(get_db) -): - # for testing - user_query = test_session.query(User) - user_from_db = user_query.filter_by(username="test_user").first() - templates.env.globals['user'] = user_from_db - - return templates.TemplateResponse("global_var_test.html", { - "request": request - }) diff --git a/app/internal/global_variable.py b/app/internal/global_variable.py new file mode 100644 index 00000000..7753e9d3 --- /dev/null +++ b/app/internal/global_variable.py @@ -0,0 +1,21 @@ +from fastapi import Request + +from app.dependencies import templates +from app.internal.security.ouath2 import ( + Session, get_jwt_token, get_authorization_cookie +) +from app.internal.security.schema import CurrentUser + + + +async def get_user_for_global_var(db: Session, jwt: str) -> CurrentUser: + jwt_payload = await get_jwt_token(db, jwt) + username = jwt_payload.get("sub") + user_id = jwt_payload.get("user_id") + return CurrentUser(user_id=user_id, username=username) + + +async def set_global_user_var(request: Request, db: Session, temp: templates): + jwt = await get_authorization_cookie(request) + user = await get_user_for_global_var(db, jwt) + temp.env.globals['user'] = user diff --git a/app/main.py b/app/main.py index e931ecbe..203ce0a4 100644 --- a/app/main.py +++ b/app/main.py @@ -45,7 +45,6 @@ def create_tables(engine, psql_environment): google_connect, invitation, joke, login, logout, profile, register, search, telegram, user, weekview, weight, whatsapp, ) -from app.demos import global_variable # noqa: E402 json_data_loader.load_to_database(next(get_db())) @@ -80,7 +79,6 @@ async def swagger_ui_redirect(): export.router, four_o_four.router, friendview.router, - global_variable.router, google_connect.router, invitation.router, joke.router, diff --git a/app/templates/global_var_test.html b/app/templates/global_var_test.html index 8e3b366c..6297624a 100644 --- a/app/templates/global_var_test.html +++ b/app/templates/global_var_test.html @@ -17,35 +17,23 @@
  • Profile
  • - {% endif %} - - {% if not user %}
  • - Sign In + Sign Out
  • - {% endif %} - - {% if user %}
  • - Sign Out + Agenda
  • - {% endif %} - - {% if not user %}
  • - Sign Up + Invitations
  • {% endif %} - {% if user %} + {% if not user %}
  • - Agenda + Sign In
  • - {% endif %} - - {% if user %}
  • - Invitations + Sign Up
  • {% endif %} @@ -56,7 +44,10 @@ -

    username: {{ user.username }}

    + {% if user %} +

    username: {{ user.username }}

    +

    user_id: {{ user.user_id }}

    + {% endif %} \ No newline at end of file diff --git a/tests/client_fixture.py b/tests/client_fixture.py index 202863de..a7dbe4e5 100644 --- a/tests/client_fixture.py +++ b/tests/client_fixture.py @@ -6,15 +6,15 @@ from app import main from app.database.models import Base, User -from app.demos import global_variable from app.routers import ( agenda, categories, event, friendview, google_connect, invitation, profile, weight, ) from app.routers.salary import routes as salary -from tests import security_testing_routes +from tests import security_testing_routes, global_var_testing_routes from tests.conftest import get_test_db, test_engine +main.app.include_router(global_var_testing_routes.router) main.app.include_router(security_testing_routes.router) @@ -47,7 +47,7 @@ def create_test_client(get_db_function) -> Generator[Session, None, None]: @pytest.fixture(scope="session") def global_var_test_client() -> Iterator[TestClient]: - yield from create_test_client(global_variable.get_db) + yield from create_test_client(global_var_testing_routes.get_db) @pytest.fixture(scope="session") diff --git a/tests/global_var_testing_routes.py b/tests/global_var_testing_routes.py new file mode 100644 index 00000000..52c9ac0d --- /dev/null +++ b/tests/global_var_testing_routes.py @@ -0,0 +1,21 @@ +from sqlalchemy.orm import Session + +from app.dependencies import get_db, templates +from app.internal.global_variable import set_global_user_var +from fastapi import APIRouter, Depends, Request + + +router = APIRouter( + prefix="/global-variable", + tags=["global-variable"], + responses={404: {"description": "Not found"}}, +) + + +@router.get("/") +async def global_var(request: Request, db: Session = Depends(get_db)): + await set_global_user_var(request, db, templates) + + return templates.TemplateResponse("global_var_test.html", { + "request": request + }) diff --git a/tests/test_global_variable.py b/tests/test_global_variable.py index 76aeb3ed..8ab75457 100644 --- a/tests/test_global_variable.py +++ b/tests/test_global_variable.py @@ -1,16 +1,28 @@ -from app.demos.global_variable import create_test_logged_user +REGISTER_DETAIL = { + 'username': 'correct_user', 'full_name': 'full_name', + 'password': 'correct_password', 'confirm_password': 'correct_password', + 'email': 'example@email.com', 'description': ""} +LOGIN_DATA = {'username': 'correct_user', 'password': 'correct_password'} -def test_global_var(global_var_test_client, session): + +def test_global_var(global_var_test_client): response = global_var_test_client.get("/global-variable") + assert response.ok - assert b'test_user' not in response.content + assert b'correct_user' not in response.content assert b'Sign In' in response.content assert b'Sign Up' in response.content - create_test_logged_user(session) + global_var_test_client.post( + global_var_test_client.app.url_path_for('register'), + data=REGISTER_DETAIL) + global_var_test_client.post( + global_var_test_client.app.url_path_for('login'), + data=LOGIN_DATA) + response = global_var_test_client.get("/global-variable") assert response.ok - assert b'test_user' in response.content + assert b'correct_user' in response.content assert b'Sign In' not in response.content assert b'Profile' in response.content From df6be83098cd18bdfb753910dd4433f4104a5fce Mon Sep 17 00:00:00 2001 From: itzik Date: Mon, 22 Feb 2021 09:45:19 +0200 Subject: [PATCH 09/10] fix: for flake8 --- app/internal/global_variable.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/internal/global_variable.py b/app/internal/global_variable.py index 7753e9d3..94b615d5 100644 --- a/app/internal/global_variable.py +++ b/app/internal/global_variable.py @@ -7,7 +7,6 @@ from app.internal.security.schema import CurrentUser - async def get_user_for_global_var(db: Session, jwt: str) -> CurrentUser: jwt_payload = await get_jwt_token(db, jwt) username = jwt_payload.get("sub") From 4e8d5bf8b35c4236dc537bdfe96a461c28a57e6b Mon Sep 17 00:00:00 2001 From: itzik Date: Mon, 22 Feb 2021 10:09:32 +0200 Subject: [PATCH 10/10] fix: saves only the username to the global variables --- app/internal/global_variable.py | 6 ++---- app/templates/global_var_test.html | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/internal/global_variable.py b/app/internal/global_variable.py index 94b615d5..b1c1755d 100644 --- a/app/internal/global_variable.py +++ b/app/internal/global_variable.py @@ -4,14 +4,12 @@ from app.internal.security.ouath2 import ( Session, get_jwt_token, get_authorization_cookie ) -from app.internal.security.schema import CurrentUser -async def get_user_for_global_var(db: Session, jwt: str) -> CurrentUser: +async def get_user_for_global_var(db: Session, jwt: str) -> str: jwt_payload = await get_jwt_token(db, jwt) username = jwt_payload.get("sub") - user_id = jwt_payload.get("user_id") - return CurrentUser(user_id=user_id, username=username) + return username async def set_global_user_var(request: Request, db: Session, temp: templates): diff --git a/app/templates/global_var_test.html b/app/templates/global_var_test.html index 6297624a..16d23ada 100644 --- a/app/templates/global_var_test.html +++ b/app/templates/global_var_test.html @@ -45,8 +45,7 @@ {% if user %} -

    username: {{ user.username }}

    -

    user_id: {{ user.user_id }}

    +

    username: {{ user }}

    {% endif %}