-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/favorite quotes #299
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
subscorp
wants to merge
74
commits into
PythonFreeCourse:develop
Choose a base branch
from
subscorp:feature/favorite-quotes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+407
−94
Open
Changes from 52 commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
fef70ba
feature: add favorite quotes
subscorp da92003
fix: fixed conflicts
subscorp 4d4ac98
fix: fixed flake8 error
subscorp 9a103e6
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
subscorp 8392bb2
fix: fixed pytest issues
subscorp c728f0c
a small fix
subscorp f62f8d3
fix: fixed display issue in favorite_quotes.html
subscorp 82d79fd
fix: moved favorite quotes from navigation bar to profile
subscorp 26f6a91
fix: fixed issues according to comments
subscorp 076ab07
fix: fixed conflicts
subscorp 701228e
fix: fixed issues according to comments
subscorp a9fe632
fix: fixed conflicts
subscorp fbe286c
fix: fixed flake8 issues
subscorp 3026dd7
fix: fixed issue in pytest
subscorp 2238a8d
fix: a tiny improvement
subscorp d8ce7b5
fix: small fix
subscorp 47b0b5f
fix: another tiny improvement
subscorp 1308754
fix: fixed issues according to comments
subscorp fef81a7
fix: fixed conflicts
subscorp e53fab9
fix: fixed pytest issues
subscorp fedc394
fix: a small fix
subscorp ee7ec43
fix: another small fix
subscorp c012d64
Update: now using the new login system
subscorp ac4a818
Update: now using the new login system
subscorp 1800655
fix: fixed low coverage in pytest
subscorp a975a1c
fix: moved styles to css
subscorp bdba51f
fix: fixed conflicts
subscorp 34d1ee6
Update: moved Favorite Quotes to profile
subscorp 3098862
fix: fixed issues according to comments
subscorp 1791fa6
fix: fixed conflicts
subscorp b722eea
fix: fixed pytest issue
subscorp 5e5f6c3
fix: fixed issues in pytest
subscorp 5d243e3
fix: small fix
subscorp 56b0988
fix: fixed conflicts
subscorp 097271a
fix: fixed issues according to comments
subscorp 667b4a3
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
subscorp 8a0464e
fix: a small fix
subscorp 1fa587a
fix: fixed issues in pytest
subscorp de61e04
fix: flake8 error fix
subscorp f6355dc
fix: flake8 error fix
subscorp 9c3a795
fix: flake8 error fix
subscorp f2c7d7a
fix: fixed conflicts
subscorp 0e0f704
fix: revert some changes due to a bug
subscorp 6bb30e1
Update: moved functions out of main
subscorp 0a846f7
fix: a small fix
subscorp a059d81
Updates: A. heart now appears only when logged in. B. landing page no…
subscorp 9a76d3f
fix: some small fixes and mainly added tests to both the audio featur…
subscorp 37ec594
fix: fixed conflicts
subscorp 0ea5847
fix: fixed issues according to comments
subscorp 9f55f15
fix: fixed issues according to comments
subscorp 99f5e33
fix: a small, final fix
subscorp c390fea
fix: fixed issues according to comments
subscorp 3e33aa2
fix: fixed merge issue :)
subscorp 79a8e6d
fix: fixed conflicts
subscorp 19aae78
fix: trying to fix a bug in pytest
subscorp c671d5f
fix: trying to fix the bug
subscorp 1e4c089
fix: trying to fix the bug
subscorp dccf688
fix: trying to fix the bug
subscorp 095fcea
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
subscorp 60af145
trying to fix the bug
subscorp 07f6cc6
fix: trying to fix the bug (it's only in github)
subscorp 970f296
fix: trying to fix the bug (it's only in github)
subscorp e5bcdc5
fix: trying to fix the bug
subscorp 0504e22
fix: trying to fix the bug
subscorp 12481b4
fix: trying to fix the bug
subscorp 0d4042f
fix: trying to fix the bug
subscorp de05d1a
fix: trying to fix the bug
subscorp 4abe14d
fix: trying to fix the bug
subscorp 404e195
fix: trying to fix the bug
subscorp 23becd6
fix: trying to fix the bug
subscorp 943162a
fix: trying to fix the bug
subscorp 9f5244e
fix: trying to fix the bug
subscorp cac2432
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
subscorp 5fd6929
fix: fixed issues according to comments
subscorp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from fastapi import APIRouter, Depends, Form, Request | ||
from sqlalchemy.orm import Session | ||
|
||
from app.database import models | ||
from app.dependencies import get_db, templates | ||
from app.internal import daily_quotes | ||
from app.internal.security.dependencies import current_user | ||
|
||
router = APIRouter( | ||
prefix="/quotes", | ||
tags=["quotes"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.post("/save") | ||
async def save_quote( | ||
user: models.User = Depends(current_user), | ||
quote_id: int = Form(...), | ||
db: Session = Depends(get_db), | ||
) -> None: | ||
"""Saves a quote in the database.""" | ||
record = ( | ||
db.query(models.UserQuotes) | ||
.filter( | ||
models.UserQuotes.user_id == user.user_id, | ||
models.UserQuotes.quote_id == quote_id, | ||
) | ||
.first() | ||
) | ||
if not record: | ||
db.merge(models.UserQuotes(user_id=user.user_id, quote_id=quote_id)) | ||
db.commit() | ||
|
||
|
||
@router.delete("/delete") | ||
async def delete_quote( | ||
user: models.User = Depends(current_user), | ||
quote_id: int = Form(...), | ||
db: Session = Depends(get_db), | ||
) -> None: | ||
"""Deletes a quote from the database.""" | ||
db.query(models.UserQuotes).filter( | ||
models.UserQuotes.user_id == user.user_id, | ||
models.UserQuotes.quote_id == quote_id, | ||
).delete() | ||
db.commit() | ||
|
||
|
||
@router.get("/favorites") | ||
async def favorite_quotes( | ||
request: Request, | ||
db: Session = Depends(get_db), | ||
user: models.User = Depends(current_user), | ||
) -> templates.TemplateResponse: | ||
"""html page for displaying the users' favorite quotes.""" | ||
quotes = daily_quotes.get_quotes(db, user.user_id) | ||
return templates.TemplateResponse( | ||
"favorite_quotes.html", | ||
{ | ||
"request": request, | ||
"quotes": quotes, | ||
"full_heart": daily_quotes.FAVORITES_PATH, | ||
}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const FULL_HEART = "../../media/full_heart.png"; | ||
const EMPTY_HEART = "../../media/empty_heart.png"; | ||
|
||
// Adding event listener | ||
window.addEventListener("load", function () { | ||
const quoteContainer = document.getElementById("quote-container"); | ||
if (!quoteContainer) { | ||
return; | ||
} | ||
const isConnected = quoteContainer.dataset.connected; | ||
if (isConnected !== "True") { | ||
return; | ||
} | ||
const fullHeart = document.getElementsByClassName("full-heart")[0]; | ||
const emptyHeart = document.getElementsByClassName("empty-heart")[0]; | ||
if (fullHeart) { | ||
fullHeart.classList.toggle("full-heart"); | ||
} else if (emptyHeart) { | ||
emptyHeart.classList.toggle("empty-heart"); | ||
} | ||
|
||
let hearts = Array.from(document.getElementsByClassName("heart")); | ||
hearts.forEach((heart_element) => { | ||
if (heart_element) { | ||
heart_element.addEventListener("click", function () { | ||
onHeartClick(heart_element); | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* @summary This function is a handler for the event of heart-click. | ||
* Whenever a user clicks on a heart icon, in case of empty heart: | ||
* saves quote in favorites, as well as changing | ||
* the heart icon from empty to full. | ||
* In case of full heart: | ||
* Removes it and switch back to empty heart icon. | ||
* Uses the save_or_remove_quote function to handle db operations. | ||
*/ | ||
function onHeartClick(heart_element) { | ||
const quote_id = heart_element.dataset.qid; | ||
if (heart_element.dataset.heart == "off") { | ||
heart_element.src = FULL_HEART; | ||
heart_element.dataset.heart = "on"; | ||
save_or_remove_quote(quote_id, true); | ||
} else { | ||
heart_element.src = EMPTY_HEART; | ||
heart_element.dataset.heart = "off"; | ||
save_or_remove_quote(quote_id, false); | ||
if (heart_element.classList.contains("favorites")) { | ||
heart_element.parentNode.parentNode.remove(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @summary Saves or removes a quote from favorites. | ||
*/ | ||
function save_or_remove_quote(quote_id, to_save) { | ||
const method = to_save ? "post" : "delete"; | ||
const url = method == "post" ? "/quotes/save" : "/quotes/delete"; | ||
const xhr = new XMLHttpRequest(); | ||
quote_id = parseInt(quote_id); | ||
xhr.open(method, url); | ||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | ||
xhr.send(`quote_id=${quote_id}`); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<div class="d-flex position-relative" id="quote-container" data-connected="True"> | ||
<div class="container mx-auto gap-3"> | ||
<h2 id="favorite-quotes">Favorite Quotes</h2> | ||
{% for quote in quotes %} | ||
<p id="p-quote"><cite><q id="quote">{{ quote.text }}</q></cite> | ||
{% if quote.author %} | ||
<span id="author"> \ {{ quote.author }} | ||
{% else %} | ||
<span> | ||
{% endif %} | ||
<img src="{{full_heart}}" data-heart="on" data-qid="{{quote.id}}" class="heart favorites"> | ||
</span> | ||
</p> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
{% endblock %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.