Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3067c2f
feat: add a joke generator to the online calendar.
fandomario Jan 28, 2021
cc9c58c
fix: according to flake8
fandomario Jan 28, 2021
0823233
fix: seperate the js code to a different file in static
fandomario Jan 29, 2021
779de2d
feat: add i18n support (#115)
Gonzom Jan 30, 2021
6370ef2
Revert "feat: add i18n support (#115)" (#161)
yammesicka Jan 30, 2021
afaa4b2
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 4, 2021
fac77dd
Merge branch 'develop' into feature/getjoke
fandomario Feb 4, 2021
0554c88
fix: flake8
fandomario Feb 4, 2021
05d87c8
Merge branch 'main' of https://github.com/PythonFreeCourse/calendar i…
fandomario Feb 4, 2021
40cfeee
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 4, 2021
ea08c94
Merge branch 'feature/getjoke' of https://github.com/fandomario/calen…
fandomario Feb 5, 2021
cd979eb
fix: flake8
fandomario Feb 5, 2021
905eb0d
fix: add db tests
fandomario Feb 5, 2021
4fd7a4e
fix: flake8
fandomario Feb 5, 2021
7f38da9
Update our production site. (#209)
yammesicka Feb 5, 2021
024cf85
feat: add a joke generator to the online calendar.
fandomario Feb 5, 2021
c730e26
Merge branch 'main' of https://github.com/PythonFreeCourse/calendar i…
fandomario Feb 5, 2021
8f4ccad
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 5, 2021
39fc16a
fix: trying to debug the test error
fandomario Feb 5, 2021
5fb3333
fix: debug
fandomario Feb 5, 2021
979f691
fix: debugging
fandomario Feb 5, 2021
f4ea217
fix: add a test
fandomario Feb 5, 2021
cc09511
fix: debug1
fandomario Feb 5, 2021
3a71bc2
fix: debug2
fandomario Feb 5, 2021
22c14ad
fix: debug3
fandomario Feb 5, 2021
f477a89
fix: debug4
fandomario Feb 5, 2021
f6cdb37
fix: add a test
fandomario Feb 5, 2021
1c56f78
fix: debug5
fandomario Feb 5, 2021
830d690
feat: add a joke generator to the online calendar.
fandomario Feb 5, 2021
85bca46
fix: change to url_for
fandomario Feb 9, 2021
67534af
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 9, 2021
74a2e83
fix: flake8 1
fandomario Feb 9, 2021
211cbad
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 9, 2021
b1f1b8b
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 13, 2021
9a89069
fix: fixing tests
fandomario Feb 13, 2021
745a533
fix: according to cr (wasn't the right file)
fandomario Feb 17, 2021
7b1842d
fix: accordind to CR
fandomario Feb 17, 2021
45eb8bc
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
fandomario Feb 17, 2021
4a73725
fix: flake8
fandomario Feb 17, 2021
483c4d5
fix: change "alert" to SweetAlert2
fandomario Feb 18, 2021
fb1109f
Merge branch 'develop' into feature/getjoke
fandomario Feb 18, 2021
485715b
Merge branch 'feature/getjoke' of https://github.com/fandomario/calen…
fandomario Feb 19, 2021
8a52104
fix: change the way jokes are loaded and use json_data_loader
fandomario Feb 19, 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
7 changes: 7 additions & 0 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@ def __repr__(self):
)


class Joke(Base):
__tablename__ = "jokes"

id = Column(Integer, primary_key=True, index=True)
text = Column(String, nullable=False)


# insert language data

# Credit to adrihanu https://stackoverflow.com/users/9127249/adrihanu
Expand Down
24 changes: 24 additions & 0 deletions app/internal/jokes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Dict, Optional

from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import func

from app.database.models import Joke


def get_joke(joke_: Dict[str, Optional[str]]) -> Joke:
"""Returns a Joke object from the dictionary data.

Args:
joke_: A dictionary joke related information.

Returns:
A new Joke object.
"""
return Joke(
text=joke_['text'],
)


def get_a_joke(session: Session):
return session.query(Joke).order_by(func.random()).first()
11 changes: 9 additions & 2 deletions app/internal/json_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from loguru import logger
from sqlalchemy.orm import Session

from app.database.models import Base, Quote, Zodiac
from app.internal import daily_quotes, zodiac
from app.database.models import Base, Joke, Quote, Zodiac
from app.internal import daily_quotes, jokes, zodiac


def load_to_database(session: Session) -> None:
Expand Down Expand Up @@ -35,6 +35,13 @@ def load_to_database(session: Session) -> None:
daily_quotes.get_quote,
)

_insert_into_database(
session,
'app/resources/jokes.json',
Joke,
jokes.get_joke,
)


def _insert_into_database(
session: Session,
Expand Down
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_tables(engine, psql_environment):
from app.routers import ( # noqa: E402
about_us, agenda, calendar, categories, celebrity, credits,
currency, dayview, email, event, export, four_o_four, friendview,
google_connect, invitation, login, logout, profile,
google_connect, invitation, joke, login, logout, profile,
register, search, telegram, user, weekview, whatsapp,
)

Expand Down Expand Up @@ -82,6 +82,7 @@ async def swagger_ui_redirect():
four_o_four.router,
google_connect.router,
invitation.router,
joke.router,
login.router,
logout.router,
profile.router,
Expand Down
368 changes: 368 additions & 0 deletions app/resources/jokes.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions app/routers/joke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from fastapi import APIRouter, Depends, Request
from app.internal import jokes
from sqlalchemy.orm import Session
from app.dependencies import get_db


router = APIRouter()


@router.get("/joke")
async def joke(request: Request, db: Session = Depends(get_db)):
return jokes.get_a_joke(db)
14 changes: 14 additions & 0 deletions app/static/joke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function makejoke() {
fetch('/joke')
.then(response => response.json())
.then(data => Swal.fire(data.text));
}


function addEventsAfterPageLoaded() {
const element = document.getElementById("a-joke");
element.addEventListener("click", makejoke, false);
}


document.addEventListener("DOMContentLoaded", addEventsAfterPageLoaded);
6 changes: 6 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<li class="nav-item">
<a class="nav-link" href="{{ url_for('friendview') }}">Friend View</a>
</li>
<li class="nav-item">
<button id="a-joke" class="btn btn-link">Make me Laugh</button>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('about') }}">{{ gettext("About Us") }}</a>
</li>
Expand All @@ -75,6 +78,9 @@
<!-- This bootstrap version is needed here because of the toggler bug in the beta version-->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/js/bootstrap.bundle.min.js" integrity="sha384-BOsAfwzjNJHrJ8cZidOg56tcQWfp6y72vEJ8xQ9w6Quywb24iOsW913URv1IS4GD" crossorigin="anonymous"></script>
<script src="{{ url_for('static', path='/horoscope.js') }}"></script>
<script src="{{ url_for('static', path='/joke.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

</body>

</html>
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'smtpdfix',
'tests.quotes_fixture',
'tests.zodiac_fixture',
'tests.jokes_fixture',
'tests.comment_fixture',
]

Expand Down
20 changes: 20 additions & 0 deletions tests/jokes_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from sqlalchemy.orm import Session

from app.database.models import Joke
from app.internal.utils import create_model, delete_instance


def add_joke(session: Session, id_joke: int, text: str) -> Joke:
joke = create_model(session, Joke, id=id_joke, text=text)
yield joke
delete_instance(session, joke)


@pytest.fixture
def joke(session: Session) -> Joke:
yield from add_joke(
session=session,
id_joke=1,
text='Chuck Norris can slam a revolving door.',
)
17 changes: 17 additions & 0 deletions tests/test_joke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from app.database.models import Joke
from app.internal import jokes


def get_jokes_amount(session):
return session.query(Joke).count()


def test_get_a_joke(session, joke):
assert jokes.get_a_joke(session).text == joke.text


def test_jokes_not_load_twice_to_db(session):
jokes.get_a_joke(session)
first_load_amount = get_jokes_amount(session)
jokes.get_a_joke(session)
assert first_load_amount == get_jokes_amount(session)
4 changes: 4 additions & 0 deletions tests/test_joke_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_joke(client, session):
resp = client.get('/joke')
assert resp.ok
assert resp.json