-
Notifications
You must be signed in to change notification settings - Fork 52
refactor/jinja-global-variable #302
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
base: develop
Are you sure you want to change the base?
Changes from all commits
98670be
e04d7c1
59411df
ce3fa34
2a57add
365e49c
914ba31
2890b74
cb0e0f9
d3b5258
7ac04f0
9b2f121
e7fb749
eb56e7c
32be156
df6be83
4e8d5bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from fastapi import Request | ||
|
||
from app.dependencies import templates | ||
from app.internal.security.ouath2 import ( | ||
Session, get_jwt_token, get_authorization_cookie | ||
) | ||
|
||
|
||
async def get_user_for_global_var(db: Session, jwt: str) -> str: | ||
jwt_payload = await get_jwt_token(db, jwt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make the site query the database with every request to the site. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So maybe I could pull the token out of the cookie, and if the token is in the right length (and maybe more tests), I wiil set a global var: 'is_token'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! You can also take the user details from the JWT, not from the database. |
||
username = jwt_payload.get("sub") | ||
return 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There shouldn't be a file used just for testing in the |
||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
|
||
<title>PyLendar global var test</title> | ||
|
||
</head> | ||
|
||
<body> | ||
|
||
<div> | ||
<ul> | ||
{% if user %} | ||
<li> | ||
<a href="#"> Profile </a> | ||
</li> | ||
<li> | ||
<a href="#"> Sign Out </a> | ||
</li> | ||
<li> | ||
<a href="#"> Agenda </a> | ||
</li> | ||
<li> | ||
<a href="#"> Invitations </a> | ||
</li> | ||
{% endif %} | ||
|
||
{% if not user %} | ||
<li> | ||
<a href="#"> Sign In </a> | ||
</li> | ||
<li> | ||
<a href="#"> Sign Up </a> | ||
</li> | ||
{% endif %} | ||
|
||
<li> | ||
<a href="#">Search</a> | ||
</li> | ||
|
||
</ul> | ||
</div> | ||
|
||
{% if user %} | ||
<h2>username: {{ user }}</h2> | ||
{% endif %} | ||
|
||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from sqlalchemy.orm import Session | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this file can also be deleted |
||
|
||
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 | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
REGISTER_DETAIL = { | ||
'username': 'correct_user', 'full_name': 'full_name', | ||
'password': 'correct_password', 'confirm_password': 'correct_password', | ||
'email': '[email protected]', 'description': ""} | ||
|
||
LOGIN_DATA = {'username': 'correct_user', 'password': 'correct_password'} | ||
|
||
|
||
def test_global_var(global_var_test_client): | ||
response = global_var_test_client.get("/global-variable") | ||
|
||
assert response.ok | ||
assert b'correct_user' not in response.content | ||
assert b'Sign In' in response.content | ||
assert b'Sign Up' in response.content | ||
|
||
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'correct_user' in response.content | ||
assert b'Sign In' not in response.content | ||
assert b'Profile' in response.content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use git precommit hooks to order that right and add a trailing comma