From 14d61eaf561408aee4a3b0d0581c199dfd105a3d Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Fri, 8 May 2020 21:12:31 +0200 Subject: [PATCH] feat(withdraw): type casting --- lnbits/extensions/withdraw/crud.py | 8 +++--- lnbits/extensions/withdraw/migrations.py | 1 + lnbits/extensions/withdraw/models.py | 7 +++++ lnbits/extensions/withdraw/static/js/index.js | 1 - .../templates/withdraw/_api_docs.html | 28 +++++++++---------- lnbits/extensions/withdraw/views_api.py | 5 +++- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lnbits/extensions/withdraw/crud.py b/lnbits/extensions/withdraw/crud.py index 462170d..128a655 100644 --- a/lnbits/extensions/withdraw/crud.py +++ b/lnbits/extensions/withdraw/crud.py @@ -58,14 +58,14 @@ def get_withdraw_link(link_id: str) -> Optional[WithdrawLink]: with open_ext_db("withdraw") as db: row = db.fetchone("SELECT * FROM withdraw_links WHERE id = ?", (link_id,)) - return WithdrawLink(**row) if row else None + return WithdrawLink.from_row(row) if row else None def get_withdraw_link_by_hash(unique_hash: str) -> Optional[WithdrawLink]: with open_ext_db("withdraw") as db: row = db.fetchone("SELECT * FROM withdraw_links WHERE unique_hash = ?", (unique_hash,)) - return WithdrawLink(**row) if row else None + return WithdrawLink.from_row(row) if row else None def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]: @@ -76,7 +76,7 @@ def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]: q = ",".join(["?"] * len(wallet_ids)) rows = db.fetchall(f"SELECT * FROM withdraw_links WHERE wallet IN ({q})", (*wallet_ids,)) - return [WithdrawLink(**row) for row in rows] + return [WithdrawLink.from_row(row) for row in rows] def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]: @@ -86,7 +86,7 @@ def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]: db.execute(f"UPDATE withdraw_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)) row = db.fetchone("SELECT * FROM withdraw_links WHERE id = ?", (link_id,)) - return WithdrawLink(**row) if row else None + return WithdrawLink.from_row(row) if row else None def delete_withdraw_link(link_id: str) -> None: diff --git a/lnbits/extensions/withdraw/migrations.py b/lnbits/extensions/withdraw/migrations.py index 26ec06d..efc9258 100644 --- a/lnbits/extensions/withdraw/migrations.py +++ b/lnbits/extensions/withdraw/migrations.py @@ -91,6 +91,7 @@ def m002_change_withdraw_table(db): row[9], # spent ), ) + db.execute("DROP TABLE withdraws") diff --git a/lnbits/extensions/withdraw/models.py b/lnbits/extensions/withdraw/models.py index 0895f03..4023859 100644 --- a/lnbits/extensions/withdraw/models.py +++ b/lnbits/extensions/withdraw/models.py @@ -1,5 +1,6 @@ from flask import url_for from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode +from sqlite3 import Row from typing import NamedTuple from lnbits.settings import FORCE_HTTPS @@ -19,6 +20,12 @@ class WithdrawLink(NamedTuple): open_time: int used: int + @classmethod + def from_row(cls, row: Row) -> "WithdrawLink": + data = dict(row) + data["is_unique"] = bool(data["is_unique"]) + return cls(**data) + @property def is_spent(self) -> bool: return self.used >= self.uses diff --git a/lnbits/extensions/withdraw/static/js/index.js b/lnbits/extensions/withdraw/static/js/index.js index b41b188..309908f 100644 --- a/lnbits/extensions/withdraw/static/js/index.js +++ b/lnbits/extensions/withdraw/static/js/index.js @@ -3,7 +3,6 @@ Vue.component(VueQrcode.name, VueQrcode); var locationPath = [window.location.protocol, '//', window.location.hostname, window.location.pathname].join(''); var mapWithdrawLink = function (obj) { - obj.is_unique = obj.is_unique == 1; obj._data = _.clone(obj); obj.date = Quasar.utils.date.formatDate(new Date(obj.time * 1000), 'YYYY-MM-DD HH:mm'); obj.min_fsat = new Intl.NumberFormat(LOCALE).format(obj.min_withdrawable); diff --git a/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html b/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html index f08ea22..11082e5 100644 --- a/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html +++ b/lnbits/extensions/withdraw/templates/withdraw/_api_docs.html @@ -8,20 +8,20 @@ group="api" dense expand-separator - label="List all withdraw links" + label="List withdraw links" > GET /withdraw/api/v1/linksGET /withdraw/api/v1/links
Headers
{"X-Api-Key": <invoice_key>}
Body (application/json)
- Returns 201 CREATED (application/json) + Returns 200 OK (application/json)
- {"lnurl": <string>} + [<withdraw_link_object>, ...]
Curl example
curl -X GET {{ request.url_root }}withdraw/api/v1/links -H @@ -34,13 +34,13 @@ group="api" dense expand-separator - label="List specific withdraw link" + label="Get a withdraw link" > GET - /withdraw/api/v1/links/<LNURL_id>GET + /withdraw/api/v1/links/<withdraw_id>
Headers
{"X-Api-Key": <invoice_key>}
@@ -52,7 +52,7 @@
Curl example
curl -X GET {{ request.url_root - }}withdraw/api/v1/links/<LNURL_id> -H "X-Api-Key: {{ + }}withdraw/api/v1/links/<withdraw_id> -H "X-Api-Key: {{ g.user.wallets[0].inkey }}"
@@ -67,7 +67,7 @@ POST + >POST /withdraw/api/v1/links
Headers
@@ -103,7 +103,7 @@ PUT + >PUT /withdraw/api/v1/links/<withdraw_id>
Headers
@@ -115,7 +115,7 @@ "wait_time": <integer>, "is_unique": <boolean>}
- Returns 201 CREATED (application/json) + Returns 200 OK (application/json)
{"lnurl": <string>}
Curl example
@@ -141,13 +141,13 @@ DELETE + >DELETE /withdraw/api/v1/links/<withdraw_id>
Headers
{"X-Api-Key": <admin_key>}
-
Returns 201 NO_CONTENT
- {"lnurl": <string>} +
Returns 204 NO CONTENT
+
Curl example
curl -X DELETE {{ request.url_root diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 57d0359..782741f 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -68,7 +68,10 @@ def api_link_retrieve(link_id): ) def api_link_create_or_update(link_id=None): if g.data["max_withdrawable"] < g.data["min_withdrawable"]: - return jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), HTTPStatus.BAD_REQUEST + return ( + jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), + HTTPStatus.BAD_REQUEST, + ) if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat: return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN