Skip to content

Feat/jinja global variable #354

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
43b3f1b
WIP register - not finished
kobyfogel Jan 19, 2021
d211377
register before updated pull
kobyfogel Jan 22, 2021
2f0501a
register for review
kobyfogel Jan 22, 2021
a5a1a04
register fixed errors
kobyfogel Jan 23, 2021
5d2c52e
register fixed flake-8
kobyfogel Jan 23, 2021
eafbe37
register fixed tests flake8
kobyfogel Jan 23, 2021
56d0ca3
register fixed tests issues
kobyfogel Jan 23, 2021
00d9e7d
register placeholders fix
kobyfogel Jan 23, 2021
c9c777e
register flake8 fix
kobyfogel Jan 23, 2021
bb2b52d
register flake8 more fixes
kobyfogel Jan 23, 2021
a32c9c4
fixed CR suggestions
kobyfogel Jan 24, 2021
deee638
first commit
kobyfogel Jan 24, 2021
7ca40e1
fixed check_jwt_token
kobyfogel Jan 25, 2021
1bdbc54
Not Finshed
kobyfogel Jan 26, 2021
eed52e2
minor fix
kobyfogel Jan 26, 2021
b793e2c
starting dependancy
kobyfogel Jan 31, 2021
46820e1
dependancies working
kobyfogel Feb 1, 2021
68e40fe
redirectin and user messages
kobyfogel Feb 1, 2021
b7e7e60
async fixing
kobyfogel Feb 1, 2021
e3c620a
exception handler
kobyfogel Feb 1, 2021
b6b7441
quary parameters
kobyfogel Feb 2, 2021
d1849bb
Documentation added
kobyfogel Feb 3, 2021
8d7f135
after pull and fixing conflicts
kobyfogel Feb 3, 2021
84db349
fixed pep8
kobyfogel Feb 3, 2021
d9ae477
pep8 more fixes
kobyfogel Feb 3, 2021
d7465b5
pep8 config fix
kobyfogel Feb 3, 2021
9d735b1
pep8 jwt fix
kobyfogel Feb 3, 2021
4f5a127
pep8 jwt final fix
kobyfogel Feb 3, 2021
4392974
pep8 final fix
kobyfogel Feb 3, 2021
b9ee9d3
CR fixes, tests added
kobyfogel Feb 7, 2021
94eb29a
after merge conflicts
kobyfogel Feb 7, 2021
97d8aff
flake8 fixes
kobyfogel Feb 7, 2021
8b54f67
flake8 fixes2
kobyfogel Feb 7, 2021
b46d679
updating requirements
kobyfogel Feb 7, 2021
73f53c1
test added
kobyfogel Feb 7, 2021
c15a451
conflicts fixed
kobyfogel Feb 7, 2021
c3cad9d
flake8 fix
kobyfogel Feb 7, 2021
befd5f9
CR fixing
kobyfogel Feb 9, 2021
063489f
conflicts fixed
kobyfogel Feb 9, 2021
da12351
flake8 fixes
kobyfogel Feb 9, 2021
cc4fdae
flake8 fix2
kobyfogel Feb 9, 2021
0ae794a
flake8 fixes3
kobyfogel Feb 9, 2021
7a47985
flake8 fixes4
kobyfogel Feb 9, 2021
e7bc55a
flake8 fixes5
kobyfogel Feb 9, 2021
8fbe36a
CR fix
kobyfogel Feb 9, 2021
f203c38
Revert "CR fix"
kobyfogel Feb 9, 2021
bbb241b
CR fixes
kobyfogel Feb 10, 2021
b00439d
conflicts fix
kobyfogel Feb 10, 2021
84a6fa7
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
kobyfogel Feb 10, 2021
d1eabd2
works, before tests
kobyfogel Feb 11, 2021
7701386
testing are not done
kobyfogel Feb 14, 2021
baaba6f
tests not finished
kobyfogel Feb 14, 2021
ad8cdeb
first push
kobyfogel Feb 14, 2021
ad01dff
WIP - conflicts fixed
kobyfogel Feb 15, 2021
bf56dcf
conflicts fixed
kobyfogel Feb 15, 2021
2977af1
before pull from develop
kobyfogel Feb 16, 2021
57df976
redefining send_mail
kobyfogel Feb 16, 2021
47106c7
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
kobyfogel Feb 16, 2021
b5471c4
docstring added
kobyfogel Feb 16, 2021
a592296
send_reset_password_mail function updated
kobyfogel Feb 17, 2021
3440353
conflicts fix, new authenticate_user_by_email function
kobyfogel Feb 17, 2021
22736de
CR fix
kobyfogel Feb 18, 2021
2d9066f
tests fixed
kobyfogel Feb 18, 2021
f8044d7
fix conflicts
kobyfogel Feb 20, 2021
ff411b6
conflicts fix
kobyfogel Feb 20, 2021
e0daa16
CR and more fixes
kobyfogel Feb 22, 2021
3bbd43a
conflicts fixed
kobyfogel Feb 22, 2021
fff3683
CR fixes
kobyfogel Feb 23, 2021
fd42eef
upgraded security in dependencies
kobyfogel Feb 23, 2021
0ee0876
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
kobyfogel Feb 23, 2021
5da9a1d
CR and get_jwt_token function fixes
kobyfogel Feb 24, 2021
3cab7bd
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
kobyfogel Feb 24, 2021
e505c5e
CR fixes
kobyfogel Feb 25, 2021
846f7d2
conflicts fixed
kobyfogel Feb 25, 2021
1aeb219
CR small fix
kobyfogel Feb 25, 2021
fadb54a
html fix
kobyfogel Feb 25, 2021
692436f
annotation and docstring
kobyfogel Feb 28, 2021
07edfcb
test fixed
kobyfogel Feb 28, 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
1 change: 1 addition & 0 deletions app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
templates = Jinja2Templates(directory=TEMPLATES_PATH)
templates.env.add_extension("jinja2.ext.i18n")


# Configure logger
logger = LoggerCustomizer.make_logger(
config.LOG_PATH,
Expand Down
20 changes: 20 additions & 0 deletions app/internal/security/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from fastapi import Depends, HTTPException
from starlette.requests import Request
from starlette.status import HTTP_401_UNAUTHORIZED
Expand Down Expand Up @@ -90,3 +92,21 @@ async def current_user(
detail="Your token is not valid. Please log in again",
)
return schema.CurrentUser(user_id=user_id, username=username)


def get_jinja_current_user(request: Request) -> Optional[schema.CurrentUser]:
"""Return the currently logged in user.
Returns logged in User object if exists, None if not.
Set as a jinja global parameter.
"""
if "Authorization" not in request.cookies:
return None
jwt_payload = get_jwt_token(request.cookies["Authorization"])
username = jwt_payload.get("sub")
user_id = jwt_payload.get("user_id")
if not user_id:
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Your token is not valid. Please log in again",
)
return schema.CurrentUser(user_id=user_id, username=username)
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
from fastapi.staticfiles import StaticFiles
from sqlalchemy.orm import Session

import app.internal.features as internal_features
from app import config
from app.database import engine, models
from app.dependencies import (
MEDIA_PATH,
SOUNDS_PATH,
STATIC_PATH,
UPLOAD_PATH,
SessionLocal,
get_db,
logger,
templates,
SessionLocal,
)
from app.internal import daily_quotes, json_data_loader
import app.internal.features as internal_features
from app.internal.languages import set_ui_language
from app.internal.security.dependencies import get_jinja_current_user
from app.internal.security.ouath2 import auth_exception_handler
from app.routers.salary import routes as salary
from app.utils.extending_openapi import custom_openapi
Expand Down Expand Up @@ -51,6 +52,7 @@ def create_tables(engine, psql_environment):
app.logger = logger

app.add_exception_handler(status.HTTP_401_UNAUTHORIZED, auth_exception_handler)
templates.env.globals["jinja_current_user"] = get_jinja_current_user

# This MUST come before the app.routers imports.
set_ui_language()
Expand Down
33 changes: 18 additions & 15 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@
</div>
<div class="collapse navbar-collapse" id="navbarToggler">
<ul class="navbar-nav mr-auto mb-2 mb-lg-0 ">
<li class="nav-item">
<a class="nav-link" href="{{ url_for('profile') }}">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('login') }}">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logout') }}">{{ gettext("Sign Out") }}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('register') }}">Sign Up</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('agenda') }}">Agenda</a>
</li>
{% if jinja_current_user(request) %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('profile') }}">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logout') }}">Sign Out</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('agenda') }}">Agenda</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('login') }}">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('register') }}">Sign Up</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for( 'audio_settings') }}">Audio Settings</a>
</li>
Expand Down
32 changes: 32 additions & 0 deletions tests/test_jinja_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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_user_not_logged_in(session, security_test_client):
security_test_client.get(security_test_client.app.url_path_for("logout"))
response = security_test_client.get("/about")
assert b"Sign Out" not in response.content
assert b"Sign In" in response.content


def test_user_is_logged_in(session, security_test_client):
security_test_client.get(security_test_client.app.url_path_for("logout"))
security_test_client.post(
security_test_client.app.url_path_for("register"),
data=REGISTER_DETAIL,
)
security_test_client.post(
security_test_client.app.url_path_for("login"),
data=LOGIN_DATA,
)
response = security_test_client.get("/about")
assert b"Sign Out" in response.content
assert b"Sign In" not in response.content