From cf6c1765d3e0c5ec340943b56c8589981be3dd5b Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 29 Sep 2021 15:10:09 +0200 Subject: [PATCH 01/37] Add jinja2 dependency for templates --- poetry.lock | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 31f6b00..ad940e2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -199,6 +199,28 @@ category = "dev" optional = false python-versions = ">=3.5" +[[package]] +name = "jinja2" +version = "3.0.1" +description = "A very fast and expressive template engine." +category = "main" +optional = false +python-versions = ">=3.6" + +[package.dependencies] +MarkupSafe = ">=2.0" + +[package.extras] +i18n = ["Babel (>=2.7)"] + +[[package]] +name = "markupsafe" +version = "2.0.1" +description = "Safely add untrusted strings to HTML/XML markup." +category = "main" +optional = false +python-versions = ">=3.6" + [[package]] name = "mccabe" version = "0.6.1" @@ -568,7 +590,7 @@ python-versions = ">=3.7" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "76743b3324f5a77d33919708ffcf63515ea12024f618338503683ca1a1d8405b" +content-hash = "ec05e7cc8cbd5852060b8adf87951b541166397d72636c7f050e6ec7c7e86e86" [metadata.files] asgiref = [ @@ -750,6 +772,66 @@ idna = [ {file = "idna-3.2-py3-none-any.whl", hash = "sha256:14475042e284991034cb48e06f6851428fb14c4dc953acd9be9a5e95c7b6dd7a"}, {file = "idna-3.2.tar.gz", hash = "sha256:467fbad99067910785144ce333826c71fb0e63a425657295239737f7ecd125f3"}, ] +jinja2 = [ + {file = "Jinja2-3.0.1-py3-none-any.whl", hash = "sha256:1f06f2da51e7b56b8f238affdd6b4e2c61e39598a378cc49345bc1bd42a978a4"}, + {file = "Jinja2-3.0.1.tar.gz", hash = "sha256:703f484b47a6af502e743c9122595cc812b0271f661722403114f71a79d0f5a4"}, +] +markupsafe = [ + {file = "MarkupSafe-2.0.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:d8446c54dc28c01e5a2dbac5a25f071f6653e6e40f3a8818e8b45d790fe6ef53"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:36bc903cbb393720fad60fc28c10de6acf10dc6cc883f3e24ee4012371399a38"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2d7d807855b419fc2ed3e631034685db6079889a1f01d5d9dac950f764da3dad"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:add36cb2dbb8b736611303cd3bfcee00afd96471b09cda130da3581cbdc56a6d"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:168cd0a3642de83558a5153c8bd34f175a9a6e7f6dc6384b9655d2697312a646"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-win32.whl", hash = "sha256:99df47edb6bda1249d3e80fdabb1dab8c08ef3975f69aed437cb69d0a5de1e28"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:e0f138900af21926a02425cf736db95be9f4af72ba1bb21453432a07f6082134"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:f9081981fe268bd86831e5c75f7de206ef275defcb82bc70740ae6dc507aee51"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:0955295dd5eec6cb6cc2fe1698f4c6d84af2e92de33fbcac4111913cd100a6ff"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:0446679737af14f45767963a1a9ef7620189912317d095f2d9ffa183a4d25d2b"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:f826e31d18b516f653fe296d967d700fddad5901ae07c622bb3705955e1faa94"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:fa130dd50c57d53368c9d59395cb5526eda596d3ffe36666cd81a44d56e48872"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:905fec760bd2fa1388bb5b489ee8ee5f7291d692638ea5f67982d968366bef9f"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bf5d821ffabf0ef3533c39c518f3357b171a1651c1ff6827325e4489b0e46c3c"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:0d4b31cc67ab36e3392bbf3862cfbadac3db12bdd8b02a2731f509ed5b829724"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:baa1a4e8f868845af802979fcdbf0bb11f94f1cb7ced4c4b8a351bb60d108145"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-win32.whl", hash = "sha256:6c4ca60fa24e85fe25b912b01e62cb969d69a23a5d5867682dd3e80b5b02581d"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b2f4bf27480f5e5e8ce285a8c8fd176c0b03e93dcc6646477d4630e83440c6a9"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:0717a7390a68be14b8c793ba258e075c6f4ca819f15edfc2a3a027c823718567"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:6557b31b5e2c9ddf0de32a691f2312a32f77cd7681d8af66c2692efdbef84c18"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:49e3ceeabbfb9d66c3aef5af3a60cc43b85c33df25ce03d0031a608b0a8b2e3f"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:d7f9850398e85aba693bb640262d3611788b1f29a79f0c93c565694658f4071f"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:6a7fae0dd14cf60ad5ff42baa2e95727c3d81ded453457771d02b7d2b3f9c0c2"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:b7f2d075102dc8c794cbde1947378051c4e5180d52d276987b8d28a3bd58c17d"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e9936f0b261d4df76ad22f8fee3ae83b60d7c3e871292cd42f40b81b70afae85"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:2a7d351cbd8cfeb19ca00de495e224dea7e7d919659c2841bbb7f420ad03e2d6"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:60bf42e36abfaf9aff1f50f52644b336d4f0a3fd6d8a60ca0d054ac9f713a864"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-win32.whl", hash = "sha256:a30e67a65b53ea0a5e62fe23682cfe22712e01f453b95233b25502f7c61cb415"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-win_amd64.whl", hash = "sha256:611d1ad9a4288cf3e3c16014564df047fe08410e628f89805e475368bd304914"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:5bb28c636d87e840583ee3adeb78172efc47c8b26127267f54a9c0ec251d41a9"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:be98f628055368795d818ebf93da628541e10b75b41c559fdf36d104c5787066"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:1d609f577dc6e1aa17d746f8bd3c31aa4d258f4070d61b2aa5c4166c1539de35"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:7d91275b0245b1da4d4cfa07e0faedd5b0812efc15b702576d103293e252af1b"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:01a9b8ea66f1658938f65b93a85ebe8bc016e6769611be228d797c9d998dd298"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:47ab1e7b91c098ab893b828deafa1203de86d0bc6ab587b160f78fe6c4011f75"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:97383d78eb34da7e1fa37dd273c20ad4320929af65d156e35a5e2d89566d9dfb"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6fcf051089389abe060c9cd7caa212c707e58153afa2c649f00346ce6d260f1b"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:5855f8438a7d1d458206a2466bf82b0f104a3724bf96a1c781ab731e4201731a"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:3dd007d54ee88b46be476e293f48c85048603f5f516008bee124ddd891398ed6"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-win32.whl", hash = "sha256:023cb26ec21ece8dc3907c0e8320058b2e0cb3c55cf9564da612bc325bed5e64"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:984d76483eb32f1bcb536dc27e4ad56bba4baa70be32fa87152832cdd9db0833"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:2ef54abee730b502252bcdf31b10dacb0a416229b72c18b19e24a4509f273d26"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:3c112550557578c26af18a1ccc9e090bfe03832ae994343cfdacd287db6a6ae7"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux1_i686.whl", hash = "sha256:53edb4da6925ad13c07b6d26c2a852bd81e364f95301c66e930ab2aef5b5ddd8"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:f5653a225f31e113b152e56f154ccbe59eeb1c7487b39b9d9f9cdb58e6c79dc5"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux2010_i686.whl", hash = "sha256:4efca8f86c54b22348a5467704e3fec767b2db12fc39c6d963168ab1d3fc9135"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl", hash = "sha256:ab3ef638ace319fa26553db0624c4699e31a28bb2a835c5faca8f8acf6a5a902"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:f8ba0e8349a38d3001fae7eadded3f6606f0da5d748ee53cc1dab1d6527b9509"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c47adbc92fc1bb2b3274c4b3a43ae0e4573d9fbff4f54cd484555edbf030baf1"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:37205cac2a79194e3750b0af2a5720d95f786a55ce7df90c3af697bfa100eaac"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:1f2ade76b9903f39aa442b4aadd2177decb66525062db244b35d71d0ee8599b6"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-win32.whl", hash = "sha256:10f82115e21dc0dfec9ab5c0223652f7197feb168c940f3ef61563fc2d6beb74"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:693ce3f9e70a6cf7d2fb9e6c9d8b204b6b39897a2c4a1aa65728d5ac97dcc1d8"}, + {file = "MarkupSafe-2.0.1.tar.gz", hash = "sha256:594c67807fb16238b30c44bdf74f36c02cdf22d1c8cda91ef8a0ed8dabf5620a"}, +] mccabe = [ {file = "mccabe-0.6.1-py2.py3-none-any.whl", hash = "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42"}, {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, diff --git a/pyproject.toml b/pyproject.toml index 23c9219..e94b73c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ python = "^3.9" fastapi = "^0.68.1" uvicorn = {extras = ["standard"], version = "^0.15.0"} SQLAlchemy = "^1.4.25" +Jinja2 = "^3.0.1" [tool.poetry.dev-dependencies] pytest = "^5.2" From e7be2ec1d2a864d2ca7c7e4a0ce9d82c271ad166 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 29 Sep 2021 15:16:04 +0200 Subject: [PATCH 02/37] --no-amend --- papi/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/papi/main.py b/papi/main.py index aa664e6..b298a59 100644 --- a/papi/main.py +++ b/papi/main.py @@ -2,6 +2,7 @@ from typing import Optional, List from pydantic import BaseModel from fastapi import FastAPI, Request, Body, Depends +from fastapi.templating import Jinja2Templates from sqlalchemy.orm import Session from collections import defaultdict From e09f161b97e0db87234c40a2f4cea05d1f296014 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 29 Sep 2021 15:21:51 +0200 Subject: [PATCH 03/37] TDD --- tests/test_papi.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_papi.py b/tests/test_papi.py index 2d07cab..6329fae 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -148,6 +148,13 @@ def test_onchange_notification(): assert len(notifs.json()) == 1 +def test_template_rendering(): + id_sonde = "masonde_histo" + notifs_text = client.get(f"/notifications/{idsonde}.txt") + assert notifs_text.ok + # assert something in notif_text.body ? + + class CodeCoverageTestCase(TestCase): """ Get covergage to 100% """ From adb69278c846ce444865450c50ec2a696bca30ce Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 29 Sep 2021 15:34:25 +0200 Subject: [PATCH 04/37] Test routing ok --- papi/main.py | 7 ++++++- tests/test_papi.py | 7 ++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/papi/main.py b/papi/main.py index b298a59..230fc1e 100644 --- a/papi/main.py +++ b/papi/main.py @@ -72,11 +72,16 @@ def list_sonde(): return [x for x in sondes.values()] -@app.get("/notifications/{idsonde}/") +@app.get("/notifications/{idsonde}") def list_notification(idsonde: str): return notifications[idsonde][::-1] +@app.get("/notifications/{idsonde}/text") +def list_notification_as_text(idsonde: str): + return "Contenu" + + @app.post("/sonde/{idsonde}/") def post_sonde_data( idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) diff --git a/tests/test_papi.py b/tests/test_papi.py index 6329fae..da2e6a3 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -149,10 +149,11 @@ def test_onchange_notification(): def test_template_rendering(): - id_sonde = "masonde_histo" - notifs_text = client.get(f"/notifications/{idsonde}.txt") + idsonde = "masonde_histo" + notifs_text = client.get(f"/notifications/{idsonde}/text") assert notifs_text.ok - # assert something in notif_text.body ? + content = notifs_text + assert "contenu" in content.text.lower() class CodeCoverageTestCase(TestCase): From f05808b226bd69401932e771aa2ef1276a20b439 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 11:32:35 +0200 Subject: [PATCH 05/37] Fix report output formating --- papi/main.py | 7 +++++-- tests/test_papi.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/papi/main.py b/papi/main.py index 230fc1e..0e4a6cc 100644 --- a/papi/main.py +++ b/papi/main.py @@ -79,7 +79,9 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") def list_notification_as_text(idsonde: str): - return "Contenu" + content = notifications[idsonde][-1] + + return @app.post("/sonde/{idsonde}/") @@ -127,10 +129,11 @@ def get_rapport(idsonde): else: last = last.content last = utils.prepare(last) + for name, data in sorted( last["channels"].items(), key=lambda text: float(text[0].split("#")[0]) ): - yield f'{name} {data["status"]}' + yield f'{name.split("#", 1)[1]} {data["status"]}' @app.get("/sonde/{idsonde}/historique") diff --git a/tests/test_papi.py b/tests/test_papi.py index da2e6a3..206ea0b 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -62,7 +62,7 @@ def test_sample_report(): report = client.get(f"/sonde/{id_}/rapport") rjson = report.json() assert len(rjson) == 1 - assert rjson[0] == "-1#Test channel ok" + assert rjson[0] == "Test channel ok" data["channels"][0]["status"] = "error" response = client.post(f"/sonde/{id_}/", json=data) @@ -70,7 +70,7 @@ def test_sample_report(): report = client.get(f"/sonde/{id_}/rapport") rjson = report.json() assert len(rjson) == 1 - assert rjson[0] == "-1#Test channel error" + assert rjson[0] == "Test channel error" def test_historique(): From 1d0f011171b10b11ac1f6a805c24fc4f3c459fb8 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 12:10:30 +0200 Subject: [PATCH 06/37] Get emails from config --- conf_notifications.ini | 5 +++++ papi/main.py | 23 ++++++++++++++++++++++- tests/test_papi.py | 4 ++-- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 conf_notifications.ini diff --git a/conf_notifications.ini b/conf_notifications.ini new file mode 100644 index 0000000..b212278 --- /dev/null +++ b/conf_notifications.ini @@ -0,0 +1,5 @@ +[masonde_001] +email = email@fqdn + email@fqdn.co.uk + masonde_001@fqdn + diff --git a/papi/main.py b/papi/main.py index 0e4a6cc..28885b1 100644 --- a/papi/main.py +++ b/papi/main.py @@ -1,4 +1,6 @@ from typing import Optional, List +from configparser import ConfigParser + from pydantic import BaseModel from fastapi import FastAPI, Request, Body, Depends @@ -21,6 +23,21 @@ notifications = defaultdict(list) Base.metadata.create_all(bind=engine) +conf = ConfigParser() +conf.read("conf_notifications.ini") + + +def sondeid2notifsemails(idsonde, mapping=conf): + try: + section = mapping[idsonde] + except KeyError: + raise KeyError(f"{idsonde} non trouvé dans {list(mapping.keys())}") + + mails = section["email"] + if isinstance(mails, str): + mails = [x.strip() for x in mails.split("\n")] + return mails + def get_db(): db = SessionLocal() @@ -80,8 +97,12 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") def list_notification_as_text(idsonde: str): content = notifications[idsonde][-1] + recipients = sondeid2notifsemails(idsonde) + changements = content["changes"] + status = content["status"] - return + data = {"recipients": recipients, "changements": changements, "status": status} + return data @app.post("/sonde/{idsonde}/") diff --git a/tests/test_papi.py b/tests/test_papi.py index 206ea0b..9cba1c8 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -149,11 +149,11 @@ def test_onchange_notification(): def test_template_rendering(): - idsonde = "masonde_histo" + idsonde = "masonde_001" notifs_text = client.get(f"/notifications/{idsonde}/text") assert notifs_text.ok content = notifs_text - assert "contenu" in content.text.lower() + assert f"{idsonde}@fqdn" in content.text.lower() class CodeCoverageTestCase(TestCase): From e6bb90f999bdf0473a39a9ce6b45b983a236f3d9 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 13:43:17 +0200 Subject: [PATCH 07/37] sample template --- conf_notifications.ini | 3 +++ papi/main.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/conf_notifications.ini b/conf_notifications.ini index b212278..15d4c68 100644 --- a/conf_notifications.ini +++ b/conf_notifications.ini @@ -3,3 +3,6 @@ email = email@fqdn email@fqdn.co.uk masonde_001@fqdn + +[test] +email = notif_sondetest@monitoring.com diff --git a/papi/main.py b/papi/main.py index 28885b1..09307fe 100644 --- a/papi/main.py +++ b/papi/main.py @@ -17,6 +17,8 @@ from papi.sqlapp import schemas app = FastAPI() +templates = Jinja2Templates(directory="templates/") + mesures = defaultdict(list) notifications = defaultdict(list) @@ -95,14 +97,20 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") -def list_notification_as_text(idsonde: str): +def list_notification_as_text(request: Request, idsonde: str): content = notifications[idsonde][-1] - recipients = sondeid2notifsemails(idsonde) + try: + recipients = sondeid2notifsemails(idsonde) + except KeyError: + recipients = ["mail1@xxx", "mail2@xxx"] + changements = content["changes"] status = content["status"] data = {"recipients": recipients, "changements": changements, "status": status} - return data + return templates.TemplateResponse( + "notification_email.html", context={"request": request, "data": data} + ) @app.post("/sonde/{idsonde}/") From 61099a9ed120d419e90e398e6094c20b18e34ce5 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 13:46:37 +0200 Subject: [PATCH 08/37] Add email template --- templates/notification_email.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 templates/notification_email.html diff --git a/templates/notification_email.html b/templates/notification_email.html new file mode 100644 index 0000000..58e2965 --- /dev/null +++ b/templates/notification_email.html @@ -0,0 +1,15 @@ + + + + + Email de notificaiton + + + +

Destinataires {{ ''.join(data['recipients']) }}

+ + + + + + From 4bcc49ad5f4271226b1ddbd01f3a23c551795842 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 14:12:41 +0200 Subject: [PATCH 09/37] =?UTF-8?q?Maj=20template=20avec=20d=C3=A9tail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/notification_email.html | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/templates/notification_email.html b/templates/notification_email.html index 58e2965..bf2fd17 100644 --- a/templates/notification_email.html +++ b/templates/notification_email.html @@ -8,6 +8,19 @@

Destinataires {{ ''.join(data['recipients']) }}

+ {% if data['changements'] %} +

Changements

+ {% for changement in data['changements'] %} +

{{changement}}

+ {% endfor %} + {% endif %} + + {% if data['status'] %} +

État courant

+ {% for status in data['status'] %} +

{{status}}

+ {% endfor %} + {% endif %} From cd767708631942c6e9d13c71f4d7762bdfae5a35 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 14:43:15 +0200 Subject: [PATCH 10/37] plein de couleurs tout partout --- papi/main.py | 14 +++++++++++++- templates/notification_email.html | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/papi/main.py b/papi/main.py index 09307fe..05f9bf8 100644 --- a/papi/main.py +++ b/papi/main.py @@ -107,9 +107,21 @@ def list_notification_as_text(request: Request, idsonde: str): changements = content["changes"] status = content["status"] + def coloriser(message): + d = {"error": "red", "warning": "orange", "pending": "blue"} + for k, v in d.items(): + if message.endswith(k): + return "color: %s;" % v + return "" + data = {"recipients": recipients, "changements": changements, "status": status} return templates.TemplateResponse( - "notification_email.html", context={"request": request, "data": data} + "notification_email.html", + context={ + "request": request, + "data": data, + "coloriser": coloriser, + }, ) diff --git a/templates/notification_email.html b/templates/notification_email.html index bf2fd17..47929e6 100644 --- a/templates/notification_email.html +++ b/templates/notification_email.html @@ -11,14 +11,14 @@ {% if data['changements'] %}

Changements

{% for changement in data['changements'] %} -

{{changement}}

+

{{changement}}

{% endfor %} {% endif %} {% if data['status'] %}

État courant

{% for status in data['status'] %} -

{{status}}

+

{{status}}

{% endfor %} {% endif %} From 75840fde5282eeba22d6693442262b343a353257 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 14:44:43 +0200 Subject: [PATCH 11/37] Ajoute le retour de sonde --- papi/sonde.py | 1 + 1 file changed, 1 insertion(+) diff --git a/papi/sonde.py b/papi/sonde.py index 44b8fa8..04abdb0 100644 --- a/papi/sonde.py +++ b/papi/sonde.py @@ -54,6 +54,7 @@ def forward(data): for post_url in forward_urls: res = session.post(post_url, json=data) print(res.ok) + print(res.json()) def status2list(status: dict): From e1fecd666f90dfa5aa39563b93eae71555b9df01 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 15:02:58 +0200 Subject: [PATCH 12/37] All Colors Are Beautiful --- papi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 05f9bf8..6ccdf87 100644 --- a/papi/main.py +++ b/papi/main.py @@ -108,7 +108,7 @@ def list_notification_as_text(request: Request, idsonde: str): status = content["status"] def coloriser(message): - d = {"error": "red", "warning": "orange", "pending": "blue"} + d = {"error": "red", "warning": "orange", "pending": "yellow", "ok": "green"} for k, v in d.items(): if message.endswith(k): return "color: %s;" % v From 22ba748079de8917a0f154b417cbe1012e31a9bb Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 15:05:04 +0200 Subject: [PATCH 13/37] Noir desir --- templates/notification_email.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/notification_email.html b/templates/notification_email.html index 47929e6..3da0e7a 100644 --- a/templates/notification_email.html +++ b/templates/notification_email.html @@ -4,7 +4,7 @@ Email de notificaiton - +

Destinataires {{ ''.join(data['recipients']) }}

From 59aae08188d5ce3d1299926c32b5012d22b5cad1 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 15:09:07 +0200 Subject: [PATCH 14/37] Black on black is hard to read --- templates/notification_email.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/notification_email.html b/templates/notification_email.html index 3da0e7a..5955453 100644 --- a/templates/notification_email.html +++ b/templates/notification_email.html @@ -4,7 +4,7 @@ Email de notificaiton - +

Destinataires {{ ''.join(data['recipients']) }}

From dce90e6621446c20f2a6a8ccbb945694f09bcef0 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 15:58:51 +0200 Subject: [PATCH 15/37] Add testecase and rename --- papi/main.py | 33 +++++++++++++++++++++++++++++++++ tests/test_papi.py | 10 +++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 6ccdf87..3726154 100644 --- a/papi/main.py +++ b/papi/main.py @@ -195,3 +195,36 @@ def historique(idsonde, db: Session = Depends(get_db)): for d in diff["changements"]: line = date, *utils.clean_state(d) yield " ".join(line) + + +@app.get("/sonde/{idsonde}/historique/text") +def list_notification_as_text( + request: Request, idsonde: str, db: Session = Depends(get_db) +): + def coloriser(message): + d = {"error": "red", "warning": "orange", "pending": "yellow", "ok": "green"} + for k, v in d.items(): + if message.endswith(k): + return "color: %s;" % v + return "" + + if not (sonde := crud.get_sonde(db, idsonde)): + return + mesures = sonde.mesures + + for previous, present in zip(mesures, mesures[1:]): + date = present.content["date"] + previous = utils.prepare(previous.content) + present = utils.prepare(present.content) + all_channels = sorted(set((*previous["channels"], *present["channels"]))) + + diff = utils.compare(all_channels, previous, present) + res = [] + if diff: + for d in diff["changements"]: + line = date, *utils.clean_state(d) + res.append(" ".join(line)) + + return templates.TemplateResponse( + "historique.html", context={"request": request, "lines": res} + ) diff --git a/tests/test_papi.py b/tests/test_papi.py index 9cba1c8..4eb0f77 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -112,6 +112,14 @@ def test_historique(): assert perte_supervision[-1] == f"{data['date']} test_historique error absent" +def test_historique_rendering(): + idsonde = "masonde_001" + notifs_text = client.get(f"/sonde/{idsonde}/historique/text") + assert notifs_text.ok + content = notifs_text + assert f"absent" in content.text.lower() + + def test_onchange_notification(): id_sonde = "masonde_notif" histo = {"identifiant": id_sonde, "nom": "Test notification"} @@ -148,7 +156,7 @@ def test_onchange_notification(): assert len(notifs.json()) == 1 -def test_template_rendering(): +def test_notif_rendering(): idsonde = "masonde_001" notifs_text = client.get(f"/notifications/{idsonde}/text") assert notifs_text.ok From e39fa666cd13209c672f3ef87ba3b0744c1a48ef Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 16:01:21 +0200 Subject: [PATCH 16/37] Does not 404 --- templates/historique.html | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 templates/historique.html diff --git a/templates/historique.html b/templates/historique.html new file mode 100644 index 0000000..5955453 --- /dev/null +++ b/templates/historique.html @@ -0,0 +1,28 @@ + + + + + Email de notificaiton + + + +

Destinataires {{ ''.join(data['recipients']) }}

+ + {% if data['changements'] %} +

Changements

+ {% for changement in data['changements'] %} +

{{changement}}

+ {% endfor %} + {% endif %} + + {% if data['status'] %} +

État courant

+ {% for status in data['status'] %} +

{{status}}

+ {% endfor %} + {% endif %} + + + + + From 701017bdb48d1d4490d17e87f378b72b4ca173ab Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 16:07:30 +0200 Subject: [PATCH 17/37] fix history template --- templates/historique.html | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/templates/historique.html b/templates/historique.html index 5955453..22d2ac3 100644 --- a/templates/historique.html +++ b/templates/historique.html @@ -2,25 +2,15 @@ - Email de notificaiton + Historique -

Destinataires {{ ''.join(data['recipients']) }}

- {% if data['changements'] %}

Changements

- {% for changement in data['changements'] %} + {% for changement in lines %}

{{changement}}

{% endfor %} - {% endif %} - - {% if data['status'] %} -

État courant

- {% for status in data['status'] %} -

{{status}}

- {% endfor %} - {% endif %} From 9955170b8d46c9da846fdcb6549c219c5a5b08d6 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 16:09:07 +0200 Subject: [PATCH 18/37] Fix template --- papi/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 3726154..6bc7412 100644 --- a/papi/main.py +++ b/papi/main.py @@ -226,5 +226,6 @@ def list_notification_as_text( res.append(" ".join(line)) return templates.TemplateResponse( - "historique.html", context={"request": request, "lines": res} + "historique.html", + context={"request": request, "lines": res, "coloriser": coloriser}, ) From 5d39c0f6233be21ba969dda7313d1ddc0323d45c Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 16:08:57 +0200 Subject: [PATCH 19/37] Yield magic replaced at proper place --- papi/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 6bc7412..58c7116 100644 --- a/papi/main.py +++ b/papi/main.py @@ -212,6 +212,8 @@ def list_notification_as_text( return mesures = sonde.mesures + res = [] + for previous, present in zip(mesures, mesures[1:]): date = present.content["date"] previous = utils.prepare(previous.content) @@ -219,7 +221,6 @@ def list_notification_as_text( all_channels = sorted(set((*previous["channels"], *present["channels"]))) diff = utils.compare(all_channels, previous, present) - res = [] if diff: for d in diff["changements"]: line = date, *utils.clean_state(d) From a788958ac5216a30c128ce85dad863d9ad9ba1da Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 16:13:19 +0200 Subject: [PATCH 20/37] Antechronologique --- papi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 58c7116..08780ad 100644 --- a/papi/main.py +++ b/papi/main.py @@ -228,5 +228,5 @@ def list_notification_as_text( return templates.TemplateResponse( "historique.html", - context={"request": request, "lines": res, "coloriser": coloriser}, + context={"request": request, "lines": reversed(res), "coloriser": coloriser}, ) From 844aa68c572a92c22b908df07ef0e11dc3f1d91a Mon Sep 17 00:00:00 2001 From: cgte Date: Wed, 6 Oct 2021 13:59:39 +0000 Subject: [PATCH 21/37] fix orm issues, on report last 100 --- papi/main.py | 9 ++++++--- papi/sqlapp/crud.py | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/papi/main.py b/papi/main.py index 08780ad..58eaffa 100644 --- a/papi/main.py +++ b/papi/main.py @@ -98,7 +98,11 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") def list_notification_as_text(request: Request, idsonde: str): - content = notifications[idsonde][-1] + notifs = notifications[idsonde] + try: + content =notifs[-1] + except IndexError: + return try: recipients = sondeid2notifsemails(idsonde) except KeyError: @@ -207,10 +211,9 @@ def list_notification_as_text( if message.endswith(k): return "color: %s;" % v return "" - if not (sonde := crud.get_sonde(db, idsonde)): return - mesures = sonde.mesures + mesures = [x for x in crud.get_mesure(db, sonde.sonde_id, only_last=100)] res = [] diff --git a/papi/sqlapp/crud.py b/papi/sqlapp/crud.py index 5018e20..306e186 100644 --- a/papi/sqlapp/crud.py +++ b/papi/sqlapp/crud.py @@ -25,9 +25,11 @@ def create_mesure(db: Session, sonde_id: int, content: dict): return db_mesure -def get_mesure(db: Session, sonde_id: int, only_last=False): +def get_mesure(db: Session, sonde_id: int, only_last: int =0): q = db.query(models.Mesure).filter(models.Mesure.sonde_id == sonde_id) if not only_last: return q.all() else: - return [q.all()[-1]] # order by id desc limit 1 + q = q.order_by(models.Mesure.mesure_id.desc()).limit(only_last) + + return reversed(q.all()) # order by id desc limit 1 From 6c159bea468ffdbcab08725f51378f50be0821c4 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 6 Oct 2021 16:22:45 +0200 Subject: [PATCH 22/37] Add errors handling --- papi/main.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/papi/main.py b/papi/main.py index 58eaffa..3ced1bf 100644 --- a/papi/main.py +++ b/papi/main.py @@ -57,6 +57,15 @@ class Notifier: {"changes": changes, "status": [x for x in status]} ) + def error_sonde(idsonde, kind="perte_contact_api"): + notifications[idsonde].append( + { + "changes": ["perte contact api"], + "status": ["Le monitoring est inacessible"], + } + ) + breakpoint() + Notifier = Notifier() @@ -129,6 +138,17 @@ def list_notification_as_text(request: Request, idsonde: str): ) +@app.post("/sonde/{idsonde}/error") +def post_sonde_error( + idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) +): + if not (sonde := crud.get_sonde(db, idsonde)): + return + + Notifier.error_sonde(idsonde) + return + + @app.post("/sonde/{idsonde}/") def post_sonde_data( idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) From e2f11a52e2dae95415560bb2fb5dc637c6d966cd Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 6 Oct 2021 16:38:00 +0200 Subject: [PATCH 23/37] typo :) --- papi/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/papi/main.py b/papi/main.py index 3ced1bf..4d2a695 100644 --- a/papi/main.py +++ b/papi/main.py @@ -57,6 +57,7 @@ class Notifier: {"changes": changes, "status": [x for x in status]} ) + @staticmethod def error_sonde(idsonde, kind="perte_contact_api"): notifications[idsonde].append( { From d4d050372ea8f3e9815ba3f8c7ffed999f30075d Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Wed, 6 Oct 2021 16:38:26 +0200 Subject: [PATCH 24/37] cosmetic fix --- papi/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/papi/main.py b/papi/main.py index 4d2a695..cb47931 100644 --- a/papi/main.py +++ b/papi/main.py @@ -65,7 +65,6 @@ class Notifier: "status": ["Le monitoring est inacessible"], } ) - breakpoint() Notifier = Notifier() @@ -108,11 +107,11 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") def list_notification_as_text(request: Request, idsonde: str): - notifs = notifications[idsonde] + notifs = notifications[idsonde] try: - content =notifs[-1] + content = notifs[-1] except IndexError: - return + return try: recipients = sondeid2notifsemails(idsonde) except KeyError: @@ -232,9 +231,10 @@ def list_notification_as_text( if message.endswith(k): return "color: %s;" % v return "" + if not (sonde := crud.get_sonde(db, idsonde)): return - mesures = [x for x in crud.get_mesure(db, sonde.sonde_id, only_last=100)] + mesures = [x for x in crud.get_mesure(db, sonde.sonde_id, only_last=100)] res = [] From 02562bd899825e19ae0160919aea00a7348976ee Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 01:46:30 +0200 Subject: [PATCH 25/37] Merci le tuto pour les astuces --- Makefile | 2 +- tests/test_papi.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5756e3f..ee6e997 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ dev_serve: poetry run uvicorn papi.main:app --reload cleandb: - rm sql_app.db || echo ls + rm test_sql_app.db || echo ls .ONESHELL: test: cleandb diff --git a/tests/test_papi.py b/tests/test_papi.py index 4eb0f77..d0aebff 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -5,13 +5,39 @@ import pytest from fastapi import FastAPI from fastapi.testclient import TestClient -from papi.main import app +from papi.main import app, get_db from papi.main import sondes from papi import __version__ from . import utils as testutils +from papi.sqlapp.database import Base +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + + +SQLALCHEMY_DATABASE_URL = "sqlite:///./test_sql_app.db" + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) +TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + + +Base.metadata.create_all(bind=engine) + + +def override_get_db(): + try: + db = TestingSessionLocal() + yield db + finally: + db.close() + + +app.dependency_overrides[get_db] = override_get_db + client = TestClient(app) From d1cebb444c75c8d8884677a21671f70ea39ae4ed Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 12:22:37 +0200 Subject: [PATCH 26/37] cover pragma --- papi/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/papi/main.py b/papi/main.py index cb47931..582a3b8 100644 --- a/papi/main.py +++ b/papi/main.py @@ -32,7 +32,7 @@ conf.read("conf_notifications.ini") def sondeid2notifsemails(idsonde, mapping=conf): try: section = mapping[idsonde] - except KeyError: + except KeyError: # pragma: no cover raise KeyError(f"{idsonde} non trouvé dans {list(mapping.keys())}") mails = section["email"] @@ -41,7 +41,7 @@ def sondeid2notifsemails(idsonde, mapping=conf): return mails -def get_db(): +def get_db(): # pragma: no cover db = SessionLocal() try: yield db From f61d3ac0cf0d7e540cc46533f94d50fa6b49d259 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 13:51:00 +0200 Subject: [PATCH 27/37] Test pra --- papi/main.py | 16 +++++++++++++++- papi/sqlapp/crud.py | 4 ++-- tests/test_papi.py | 20 +++++++++++++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/papi/main.py b/papi/main.py index 582a3b8..90a0404 100644 --- a/papi/main.py +++ b/papi/main.py @@ -144,8 +144,22 @@ def post_sonde_error( ): if not (sonde := crud.get_sonde(db, idsonde)): return - Notifier.error_sonde(idsonde) + # create fake sample + last = list(crud.get_mesure(db, sonde.sonde_id, only_last=1))[0] + from copy import copy + + api_error = copy(last.content) + from datetime import datetime + + date = str(datetime.now()) + for channel in api_error["channels"]: + channel["status"] = "perte contact api" + + api_error["date"] = date + + crud.create_mesure(db, sonde.sonde_id, api_error) + return diff --git a/papi/sqlapp/crud.py b/papi/sqlapp/crud.py index 306e186..19bebbf 100644 --- a/papi/sqlapp/crud.py +++ b/papi/sqlapp/crud.py @@ -25,11 +25,11 @@ def create_mesure(db: Session, sonde_id: int, content: dict): return db_mesure -def get_mesure(db: Session, sonde_id: int, only_last: int =0): +def get_mesure(db: Session, sonde_id: int, only_last: int = 0): q = db.query(models.Mesure).filter(models.Mesure.sonde_id == sonde_id) if not only_last: return q.all() else: q = q.order_by(models.Mesure.mesure_id.desc()).limit(only_last) - return reversed(q.all()) # order by id desc limit 1 + return list(reversed(q.all())) # order by id desc limit 1 diff --git a/tests/test_papi.py b/tests/test_papi.py index d0aebff..f82059d 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -105,16 +105,16 @@ def test_historique(): assert client.post("/sonde/", json=histo).ok - data = testutils.probe_sample_body( + data_ok = testutils.probe_sample_body( channel_name="test_historique", channel_id=0, status="ok" ) - response = client.post(f"/sonde/{id_sonde}/", json=data) + response = client.post(f"/sonde/{id_sonde}/", json=data_ok) assert response.ok debut_supervision = client.get(f"/sonde/{id_sonde}/historique") debut_supervision = debut_supervision.json() assert len(debut_supervision) == 1 - assert debut_supervision[0] == f"{data['date']} test_historique absent ok" + assert debut_supervision[0] == f"{data_ok['date']} test_historique absent ok" data = testutils.probe_sample_body( channel_name="test_historique", channel_id=0, status="error" @@ -137,6 +137,20 @@ def test_historique(): perte_supervision = perte_supervision.json() assert perte_supervision[-1] == f"{data['date']} test_historique error absent" + # perte api et repise + from papi.main import notifications + + notifs = notifications[id_sonde] + + response = client.post(f"/sonde/{id_sonde}/", json=data_ok) + + perte_api = client.post(f"/sonde/{id_sonde}/error", json={}) + + perte_api_rapport = client.get(f"/sonde/{id_sonde}/rapport") + assert "perte contact api" in perte_api_rapport.text + assert perte_api.ok + response = client.post(f"/sonde/{id_sonde}/", json=data_ok) + def test_historique_rendering(): idsonde = "masonde_001" From bf04e4ddeda487f53be78bb30bb62a9ec65978ec Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 13:54:21 +0200 Subject: [PATCH 28/37] Send fake measure to api --- papi/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/papi/main.py b/papi/main.py index 90a0404..23e5356 100644 --- a/papi/main.py +++ b/papi/main.py @@ -158,8 +158,7 @@ def post_sonde_error( api_error["date"] = date - crud.create_mesure(db, sonde.sonde_id, api_error) - + post_sonde_data(idsonde, body=api_error, db=db) return From c5b6b0a26aec1fc7195a1aa4ce75b593b57fb5f8 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 14:10:13 +0200 Subject: [PATCH 29/37] cover 100% main and crud --- papi/main.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/papi/main.py b/papi/main.py index 23e5356..6a49a2e 100644 --- a/papi/main.py +++ b/papi/main.py @@ -110,11 +110,11 @@ def list_notification_as_text(request: Request, idsonde: str): notifs = notifications[idsonde] try: content = notifs[-1] - except IndexError: + except IndexError: # pragma: no cover return try: recipients = sondeid2notifsemails(idsonde) - except KeyError: + except KeyError: # pragma: no cover recipients = ["mail1@xxx", "mail2@xxx"] changements = content["changes"] @@ -125,7 +125,6 @@ def list_notification_as_text(request: Request, idsonde: str): for k, v in d.items(): if message.endswith(k): return "color: %s;" % v - return "" data = {"recipients": recipients, "changements": changements, "status": status} return templates.TemplateResponse( @@ -143,7 +142,7 @@ def post_sonde_error( idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) ): if not (sonde := crud.get_sonde(db, idsonde)): - return + return # pragma: no cover Notifier.error_sonde(idsonde) # create fake sample last = list(crud.get_mesure(db, sonde.sonde_id, only_last=1))[0] @@ -217,8 +216,8 @@ def get_rapport(idsonde): @app.get("/sonde/{idsonde}/historique") def historique(idsonde, db: Session = Depends(get_db)): if not (sonde := crud.get_sonde(db, idsonde)): - return - mesures = sonde.mesures + return # pragma: no cover + mesures = crud.get_mesure(db, sonde.sonde_id) for previous, present in zip(mesures, mesures[1:]): date = present.content["date"] @@ -238,15 +237,15 @@ def historique(idsonde, db: Session = Depends(get_db)): def list_notification_as_text( request: Request, idsonde: str, db: Session = Depends(get_db) ): + if not (sonde := crud.get_sonde(db, idsonde)): + return # pragma: no cover + def coloriser(message): d = {"error": "red", "warning": "orange", "pending": "yellow", "ok": "green"} for k, v in d.items(): if message.endswith(k): return "color: %s;" % v - return "" - if not (sonde := crud.get_sonde(db, idsonde)): - return mesures = [x for x in crud.get_mesure(db, sonde.sonde_id, only_last=100)] res = [] From 0be2ac469a71bf574e8d5b08b6e7432cb3098400 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 14:20:45 +0200 Subject: [PATCH 30/37] Add mailjet dependency, hide credentials --- .gitignore | 1 + poetry.lock | 27 +++++++++++++++++++++------ pyproject.toml | 1 + 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d8a1bb0..6d6b047 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ .coverage +credentials.py diff --git a/poetry.lock b/poetry.lock index ad940e2..394a001 100644 --- a/poetry.lock +++ b/poetry.lock @@ -74,7 +74,7 @@ uvloop = ["uvloop (>=0.15.2)"] name = "certifi" version = "2021.5.30" description = "Python package for providing Mozilla's CA Bundle." -category = "dev" +category = "main" optional = false python-versions = "*" @@ -82,7 +82,7 @@ python-versions = "*" name = "charset-normalizer" version = "2.0.6" description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." -category = "dev" +category = "main" optional = false python-versions = ">=3.5.0" @@ -195,7 +195,7 @@ test = ["Cython (==0.29.22)"] name = "idna" version = "3.2" description = "Internationalized Domain Names in Applications (IDNA)" -category = "dev" +category = "main" optional = false python-versions = ">=3.5" @@ -213,6 +213,17 @@ MarkupSafe = ">=2.0" [package.extras] i18n = ["Babel (>=2.7)"] +[[package]] +name = "mailjet-rest" +version = "1.3.4" +description = "Mailjet V3 API wrapper" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +requests = ">=2.4.3" + [[package]] name = "markupsafe" version = "2.0.1" @@ -433,7 +444,7 @@ python-versions = "*" name = "requests" version = "2.26.0" description = "Python HTTP for Humans." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*" @@ -518,7 +529,7 @@ python-versions = "*" name = "urllib3" version = "1.26.6" description = "HTTP library with thread-safe connection pooling, file post, and more." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" @@ -590,7 +601,7 @@ python-versions = ">=3.7" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "ec05e7cc8cbd5852060b8adf87951b541166397d72636c7f050e6ec7c7e86e86" +content-hash = "3ec44b61bba180c6ddba65f7112199812d58584ed5a7b9524d7ef37202e371b6" [metadata.files] asgiref = [ @@ -776,6 +787,10 @@ jinja2 = [ {file = "Jinja2-3.0.1-py3-none-any.whl", hash = "sha256:1f06f2da51e7b56b8f238affdd6b4e2c61e39598a378cc49345bc1bd42a978a4"}, {file = "Jinja2-3.0.1.tar.gz", hash = "sha256:703f484b47a6af502e743c9122595cc812b0271f661722403114f71a79d0f5a4"}, ] +mailjet-rest = [ + {file = "mailjet_rest-1.3.4-py3-none-any.whl", hash = "sha256:635d53ac3fd61020f309c24ee977ae3458654ab39f9c36fc4b50c74e5d8ad410"}, + {file = "mailjet_rest-1.3.4.tar.gz", hash = "sha256:e02663fa0369543bcd48c37a146e8143bb12b9f3512af2d5ba6dfbcc99e64a2d"}, +] markupsafe = [ {file = "MarkupSafe-2.0.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:d8446c54dc28c01e5a2dbac5a25f071f6653e6e40f3a8818e8b45d790fe6ef53"}, {file = "MarkupSafe-2.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:36bc903cbb393720fad60fc28c10de6acf10dc6cc883f3e24ee4012371399a38"}, diff --git a/pyproject.toml b/pyproject.toml index e94b73c..1873b89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ fastapi = "^0.68.1" uvicorn = {extras = ["standard"], version = "^0.15.0"} SQLAlchemy = "^1.4.25" Jinja2 = "^3.0.1" +mailjet-rest = "^1.3.4" [tool.poetry.dev-dependencies] pytest = "^5.2" From 05ad5828e436201988b36d04dedc875af894b2d3 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 14:31:29 +0200 Subject: [PATCH 31/37] Fix name --- templates/notification_email.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/notification_email.html b/templates/notification_email.html index 5955453..3a2d799 100644 --- a/templates/notification_email.html +++ b/templates/notification_email.html @@ -2,7 +2,7 @@ - Email de notificaiton + Email de notification From 68f03eabbc2b0a07e26ee26eaa449b6f987f508b Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Thu, 7 Oct 2021 15:03:02 +0200 Subject: [PATCH 32/37] Mail send ok --- papi/mail_sendermodel.py | 24 ++++++++++++++++++++++++ papi/main.py | 21 +++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 papi/mail_sendermodel.py diff --git a/papi/mail_sendermodel.py b/papi/mail_sendermodel.py new file mode 100644 index 0000000..594a077 --- /dev/null +++ b/papi/mail_sendermodel.py @@ -0,0 +1,24 @@ +from mailjet_rest import Client +import os + +from papi.credentials import api_key, api_secret + + +def sendmail(htmlpart): + + mailjet = Client(auth=(api_key, api_secret), version="v3.1") + data = { + "Messages": [ + { + "From": {"Email": "colin.goutte@free.fr", "Name": "Colin"}, + "To": [{"Email": "colin.goutte@free.fr", "Name": "Colin"}], + "Subject": "Monitoring API", + "TextPart": "My first Mailjet email", + "HTMLPart": "" + htmlpart, + "CustomID": "AppGettingStartedTest", + } + ] + } + result = mailjet.send.create(data=data) + print(result.status_code) + print(result.json()) diff --git a/papi/main.py b/papi/main.py index 6a49a2e..259877a 100644 --- a/papi/main.py +++ b/papi/main.py @@ -106,7 +106,7 @@ def list_notification(idsonde: str): @app.get("/notifications/{idsonde}/text") -def list_notification_as_text(request: Request, idsonde: str): +def last_notif_text(request: Request, idsonde: str): notifs = notifications[idsonde] try: content = notifs[-1] @@ -139,7 +139,10 @@ def list_notification_as_text(request: Request, idsonde: str): @app.post("/sonde/{idsonde}/error") def post_sonde_error( - idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) + request: Request, + idsonde: str, + body: dict = Body(...), + db: Session = Depends(get_db), ): if not (sonde := crud.get_sonde(db, idsonde)): return # pragma: no cover @@ -157,13 +160,16 @@ def post_sonde_error( api_error["date"] = date - post_sonde_data(idsonde, body=api_error, db=db) + post_sonde_data(request, idsonde, body=api_error, db=db) return @app.post("/sonde/{idsonde}/") def post_sonde_data( - idsonde: str, body: dict = Body(...), db: Session = Depends(get_db) + request: Request, + idsonde: str, + body: dict = Body(...), + db: Session = Depends(get_db), ): if not (sonde := crud.get_sonde(db, idsonde)): return @@ -189,6 +195,13 @@ def post_sonde_data( ) if content: Notifier(idsonde, content) + res = last_notif_text(request, idsonde) + html = res.body.decode("utf-8") + from papi.mail_sendermodel import sendmail + + cond = True + if cond: + sendmail(html) return { "count": len(mesures_) if "date" in mesures_[0].content.keys() From 588c3bf1dba35bd9b85d12faa7678809ddc20320 Mon Sep 17 00:00:00 2001 From: cgte Date: Mon, 11 Oct 2021 14:17:01 +0000 Subject: [PATCH 33/37] wip --- papi/main.py | 2 +- papi/utils.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/papi/main.py b/papi/main.py index 259877a..347a719 100644 --- a/papi/main.py +++ b/papi/main.py @@ -199,7 +199,7 @@ def post_sonde_data( html = res.body.decode("utf-8") from papi.mail_sendermodel import sendmail - cond = True + cond = False if cond: sendmail(html) return { diff --git a/papi/utils.py b/papi/utils.py index 9afaf2f..aeb7a71 100644 --- a/papi/utils.py +++ b/papi/utils.py @@ -56,7 +56,8 @@ def compare(channels, previous, current): def clean_state(state): # Ote l'identifiant en début de chaine - return state[0].split("#", 1)[1], *state[1:] + + return state[0].split("#", 1)[1], state[1], '->', state[2] def list_channels(p, c): From caac05b03a192855c209ab9ae7ad8687c28f998b Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Mon, 11 Oct 2021 16:17:31 +0200 Subject: [PATCH 34/37] Comment unused sections --- papi/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/papi/utils.py b/papi/utils.py index aeb7a71..cbc65a6 100644 --- a/papi/utils.py +++ b/papi/utils.py @@ -116,6 +116,7 @@ def make_id_key(channel, keys=None, sep="#", tuple_=False): return {kvalue: channel} +""" def raw_filename(): return "raw_" + str(datetime.date.today()).replace("-", "_") + ".json" @@ -147,4 +148,6 @@ def savediff(date, diff, *, filename=diff_filename): for d in diff: data = {"date": date} data.update(zip(("name", "before", "after"), d)) + writer.writerow(data) +""" From 8fd151b23abf977a855bc3fbb96623c00fac8f26 Mon Sep 17 00:00:00 2001 From: cgte Date: Mon, 11 Oct 2021 14:34:13 +0000 Subject: [PATCH 35/37] Format text --- tests/test_papi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_papi.py b/tests/test_papi.py index f82059d..863e3de 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -114,7 +114,7 @@ def test_historique(): debut_supervision = client.get(f"/sonde/{id_sonde}/historique") debut_supervision = debut_supervision.json() assert len(debut_supervision) == 1 - assert debut_supervision[0] == f"{data_ok['date']} test_historique absent ok" + assert debut_supervision[0] == f"{data_ok['date']} test_historique absent -> ok" data = testutils.probe_sample_body( channel_name="test_historique", channel_id=0, status="error" @@ -124,7 +124,7 @@ def test_historique(): ok_erreur_supervision = client.get(f"/sonde/{id_sonde}/historique") ok_erreur_supervision = ok_erreur_supervision.json() - assert ok_erreur_supervision[-1] == f"{data['date']} test_historique ok error" + assert ok_erreur_supervision[-1] == f"{data['date']} test_historique ok -> error" data = testutils.probe_sample_body( channel_name="test_historique", channel_id=0, status="error" @@ -135,7 +135,7 @@ def test_historique(): perte_supervision = client.get(f"/sonde/{id_sonde}/historique") perte_supervision = perte_supervision.json() - assert perte_supervision[-1] == f"{data['date']} test_historique error absent" + assert perte_supervision[-1] == f"{data['date']} test_historique error -> absent" # perte api et repise from papi.main import notifications From 38fa22477a6aff6ae0a41d2a1be99a03c727b1e6 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Mon, 11 Oct 2021 16:35:04 +0200 Subject: [PATCH 36/37] black --- papi/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papi/utils.py b/papi/utils.py index cbc65a6..a8b866c 100644 --- a/papi/utils.py +++ b/papi/utils.py @@ -57,7 +57,7 @@ def compare(channels, previous, current): def clean_state(state): # Ote l'identifiant en début de chaine - return state[0].split("#", 1)[1], state[1], '->', state[2] + return state[0].split("#", 1)[1], state[1], "->", state[2] def list_channels(p, c): From c845a579e725a89503a6eec93a37bc9a1190e9d7 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Mon, 11 Oct 2021 16:36:31 +0200 Subject: [PATCH 37/37] Fix test formatting --- tests/test_papi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_papi.py b/tests/test_papi.py index 863e3de..2d0e8c6 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -179,7 +179,7 @@ def test_onchange_notification(): jresp = response.json() assert jresp["notify"] - assert jresp["notify"][0].split(" ", 1)[1] == "test_historique absent ok" + assert jresp["notify"][0].split(" ", 1)[1] == "test_historique absent -> ok" data = testutils.probe_sample_body( channel_name="test_historique", channel_id=0, status="ok"