Skip to content
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

refactor/jinja-global-variable #302

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions app/demos/global_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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='[email protected]',
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
})
2 changes: 2 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ 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
issac211 marked this conversation as resolved.
Show resolved Hide resolved

json_data_loader.load_to_database(next(get_db()))

Expand Down Expand Up @@ -79,6 +80,7 @@ async def swagger_ui_redirect():
export.router,
four_o_four.router,
friendview.router,
global_variable.router,
google_connect.router,
invitation.router,
joke.router,
Expand Down
62 changes: 62 additions & 0 deletions app/templates/global_var_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
Copy link
Member

Choose a reason for hiding this comment

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

There shouldn't be a file used just for testing in the templates directory.
Check the documentation for functions that allow you to load templates from strings instead.

<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>
{% endif %}

{% if not user %}
<li>
<a href="#"> Sign In </a>
</li>
{% endif %}

{% if user %}
<li>
<a href="#"> Sign Out </a>
</li>
{% endif %}

{% if not user %}
<li>
<a href="#"> Sign Up </a>
</li>
{% endif %}

{% if user %}
<li>
<a href="#"> Agenda </a>
</li>
{% endif %}

{% if user %}
<li>
<a href="#"> Invitations </a>
</li>
{% endif %}

<li>
<a href="#">Search</a>
</li>

</ul>
</div>

<h2>username: {{ user.username }}</h2>

</body>
</html>
6 changes: 6 additions & 0 deletions tests/client_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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,
Expand Down Expand Up @@ -44,6 +45,11 @@ def create_test_client(get_db_function) -> Generator[Session, None, None]:
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() -> Generator[TestClient, None, None]:
yield from create_test_client(agenda.get_db)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_global_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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