Skip to content

Feature: currency rates #170

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 31 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9919f2e
help
advaa123 Jan 20, 2021
c6cb120
merging
advaa123 Jan 20, 2021
7349c9e
merging
advaa123 Jan 20, 2021
324a5f3
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
advaa123 Jan 24, 2021
e2c5b06
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
advaa123 Jan 25, 2021
8127128
updated develop
advaa123 Jan 27, 2021
f2bc4e9
currency
advaa123 Jan 28, 2021
85fceab
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
advaa123 Jan 28, 2021
5f11aeb
Currency feature
advaa123 Jan 31, 2021
323b16c
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
advaa123 Jan 31, 2021
e3267e0
Merge develop into feature/currency-js
advaa123 Jan 31, 2021
26e4df2
Merge develop into feature/currency-js
advaa123 Jan 31, 2021
e437c17
Merge develop into feature/currency-js
advaa123 Jan 31, 2021
0dd6c75
Merge develop into feature/currency-js
advaa123 Jan 31, 2021
c20ebdd
Currency rates daily feature
advaa123 Jan 31, 2021
057981e
Flake8 fixes
advaa123 Jan 31, 2021
23624b8
Flake8 fixes
advaa123 Jan 31, 2021
b6c6831
Flake8 fixes
advaa123 Jan 31, 2021
27af394
flake8 fixes
advaa123 Jan 31, 2021
4a39595
final fixes
advaa123 Jan 31, 2021
d5dc607
Split fixtures and tests to different files
advaa123 Jan 31, 2021
a746b1e
Split fixtures and tests to different files
advaa123 Jan 31, 2021
89e03ff
Fixes for Yam
advaa123 Feb 1, 2021
3e2ed01
Merge branch 'develop' into feature/currency-js
advaa123 Feb 1, 2021
2faf089
Fixes for Yam
advaa123 Feb 1, 2021
b6c30cf
Merge branch 'feature/currency-js' of https://github.com/advaa123/cal…
advaa123 Feb 1, 2021
e3e792b
Fixes for Yam
advaa123 Feb 2, 2021
c44d786
Merge branch 'develop' into feature/currency-js
advaa123 Feb 4, 2021
dc3e7ea
Collapse fix
advaa123 Feb 5, 2021
c873746
Merge branch 'feature/currency-js' of http://github.com/advaa123/cale…
advaa123 Feb 5, 2021
6885f88
Collapse fix
advaa123 Feb 5, 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
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app.dependencies import (logger, MEDIA_PATH, STATIC_PATH, templates)
from app.internal.quotes import daily_quotes, load_quotes
from app.routers import (
agenda, calendar, categories, dayview, email,
agenda, calendar, categories, currency, dayview, email,
event, invitation, profile, search, telegram, whatsapp
)
from app.telegram.bot import telegram_bot
Expand Down Expand Up @@ -38,6 +38,7 @@ def create_tables(engine, psql_environment):
agenda.router,
calendar.router,
categories.router,
currency.router,
dayview.router,
email.router,
event.router,
Expand Down
32 changes: 32 additions & 0 deletions app/routers/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import datetime

from app.dependencies import templates
from fastapi import APIRouter, Request


router = APIRouter()

# TODO: Add this as a feature to the calendar view/
# day view/features panel frontend


@router.get("/currency")
def today_currency(request: Request):
"""Current day currency router"""

date = datetime.date.today().strftime("%Y-%m-%d")
return currency(request, date)


@router.get("/currency/{date}")
def currency(request: Request, date: str):
"""Custom date currency router"""

# TODO: get user default/preferred currency
base = "USD"

return templates.TemplateResponse("currency.html", {
"request": request,
"base": base,
"date": date
})
6 changes: 6 additions & 0 deletions app/static/currency.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
div[data-visible='0'] {
display: none;
}
div[data-visible='1'] {
display: block;
}
71 changes: 71 additions & 0 deletions app/static/currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function getCurrency(baseCurrency, dateToday) {
const showCurrencyElement = document.getElementById("showCurrency")
const currencyViewElement = document.getElementById("currencyView");
const myUrl = 'https://api.exchangeratesapi.io/' + dateToday + '?base=' + baseCurrency

async function getResponse(myUrl) {
let response = await fetch(myUrl);
if (response.ok) {
let result = await response.json();
currencyViewElement.innerHTML = "";
showCurrencyElement.dataset.visible = "1";
return result;
}
}

getResponse(myUrl)
.then(result => buildCurrencyList(result));

function buildCurrencyList(data) {
function getListItem() {
const listItem = document.createElement("li");
listItem.className = "list-group-item title_size_small";
return listItem;
}

function createBoldListItem(fieldString, currViewElem) {
const listItem = getListItem();
const boldItem = document.createElement("strong");
boldItem.innerHTML = fieldString;
listItem.appendChild(boldItem);
currViewElem.appendChild(listItem);
}

function getAnchorItem() {
const linkItem = document.createElement("a");
linkItem.setAttribute("href", "javascript:void(0)");
linkItem.setAttribute("id", key);
linkItem.innerText = key;
return linkItem;
}

function getRatesItem(fieldString) {
let ratesItem = document.createElement("div");
ratesItem.setAttribute("style", "display: inline;")
ratesItem.innerHTML = fieldString;
return ratesItem;
}

function createAllElements(dataRates, baseCurr, currViewElem) {
for (key in dataRates) {
if (key !== baseCurr) {
let listItem = getListItem();
let linkItem = getAnchorItem();
let ratesItem = getRatesItem(": " + dataRates[key]);
listItem.appendChild(linkItem);
listItem.appendChild(ratesItem);
currViewElem.appendChild(listItem);
}
}
}

// Create Base currency list item
createBoldListItem("BASE: " + baseCurrency, currencyViewElement);
// Create all rates list items
createAllElements(data.rates, baseCurrency, currencyViewElement);
// Create date list item
createBoldListItem(dateToday, currencyViewElement);

return true;
}
}
43 changes: 43 additions & 0 deletions app/templates/currency.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="{{ url_for('static', path='/currency.css') }}">
<title>currency</title>
</head>

<body>
<div id="showCurrency" data-visible="0">
<p style="margin: 1rem;">
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample"
aria-expanded="false" aria-controls="collapseExample">
Currency</button>
<div class="row" style="margin: 1rem; width: 14rem;">
<div class="col">
<div class="collapse multi-collapse" id="collapseExample">
<ul class="list-group list-group-flush" id="currencyView">
<!-- JS currency content -->
</ul>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="{{ url_for('static', path='/currency.js')}}"></script>
<script type="text/javascript">
getCurrency("{{ base | safe }}", "{{ date | safe }}");
document.getElementById("currencyView").addEventListener("click", function (e) {
if (e.target && e.target.nodeName == "A") {
getCurrency(e.target.id, "{{ date | safe }}");
}
}, false);
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
</body>

</html>
10 changes: 10 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CURRENCY = '/currency'
CUSTOM_DATE = "/2021-1-3"


def test_router_good(client):
resp = client.get(CURRENCY)
assert resp.ok
resp = client.get(CURRENCY + CUSTOM_DATE)
assert resp.ok
assert b'Currency</button>' in resp.content