Skip to content

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

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
11 changes: 9 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def create_tables(engine, psql_environment):

from app.routers import ( # noqa: E402
agenda, calendar, categories, celebrity, currency, dayview,
email, event, export, four_o_four, google_connect,
invitation, login, logout, profile,
email, event, export, four_o_four, global_variable,
google_connect, invitation, login, logout, profile,
register, search, telegram, user, weekview, whatsapp,
)

Expand Down Expand Up @@ -79,6 +79,7 @@ async def swagger_ui_redirect():
event.router,
export.router,
four_o_four.router,
global_variable.router,
google_connect.router,
invitation.router,
login.router,
Expand All @@ -92,6 +93,12 @@ 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)

Expand Down
33 changes: 33 additions & 0 deletions app/routers/global_variable.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]',
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
})
3 changes: 3 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<li class="nav-item">
<a class="nav-link" href="{{ url_for('search') }}">Search</a>
</li>
<li class="nav-item">
<a class="nav-link fw-bolder" href="https://forums.pythonic.guru/">The Forum</a>
</li>
</ul>
</div>
</nav>
Expand Down
7 changes: 7 additions & 0 deletions app/templates/global_var_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}

{% block content %}

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

{% endblock %}
10 changes: 9 additions & 1 deletion tests/client_fixture.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from app.routers import agenda, event, invitation, profile, google_connect
from app.routers import (
agenda, event, invitation, profile,
global_variable, google_connect
)
from typing import Iterator

from fastapi.testclient import TestClient
Expand Down Expand Up @@ -41,6 +44,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)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_global_variable.py
Original file line number Diff line number Diff line change
@@ -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