Compare commits
36
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18ce66e507 | ||
|
|
acce8d726b | ||
|
|
164e498476 | ||
|
|
48e0ca80bd | ||
|
|
0f4930f6c8 | ||
|
|
e0cade01c9 | ||
|
|
dd79dbc90b | ||
|
|
9e1ac86761 | ||
|
|
abfc8ebfb4 | ||
|
|
872c24e6ea | ||
|
|
6dcff91bf7 | ||
|
|
ef253de426 | ||
|
|
0819f51c3a | ||
|
|
d1cae5341f | ||
|
|
3b249fbad2 | ||
|
|
9648587850 | ||
|
|
a5523c1bf6 | ||
|
|
eb84621f23 | ||
|
|
b702229cb1 | ||
|
|
c722776bc0 | ||
|
|
bd7917ab20 | ||
|
|
db9006ea8b | ||
|
|
15f2048bcb | ||
|
|
b0079871b0 | ||
|
|
2bedcae9f1 | ||
|
|
341974fb35 | ||
|
|
d6bfbb0045 | ||
|
|
dc9338b991 | ||
|
|
6f6f7d4542 | ||
|
|
617f9c14ed | ||
|
|
54fdb0948c | ||
|
|
2fe7ab4f83 | ||
|
|
10c8ca4ca1 | ||
|
|
f079762b71 | ||
|
|
2e042d2597 | ||
|
|
49b57c9f0b |
+9
-42
@@ -12,7 +12,6 @@ from typing import Any, Generic, Literal, TypeVar, get_origin
|
|||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pydantic import BaseModel, ValidationError, root_validator
|
from pydantic import BaseModel, ValidationError, root_validator
|
||||||
from sqlalchemy import event
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
|
||||||
from sqlalchemy.sql import text
|
from sqlalchemy.sql import text
|
||||||
|
|
||||||
@@ -56,14 +55,6 @@ def compat_timestamp_placeholder(key: str):
|
|||||||
return f":{key}"
|
return f":{key}"
|
||||||
|
|
||||||
|
|
||||||
def get_placeholder(model: Any, field: str) -> str:
|
|
||||||
type_ = model.__fields__[field].type_
|
|
||||||
if type_ == datetime:
|
|
||||||
return compat_timestamp_placeholder(field)
|
|
||||||
else:
|
|
||||||
return f":{field}"
|
|
||||||
|
|
||||||
|
|
||||||
class Compat:
|
class Compat:
|
||||||
type: str | None = "<inherited>"
|
type: str | None = "<inherited>"
|
||||||
schema: str | None = "<inherited>"
|
schema: str | None = "<inherited>"
|
||||||
@@ -326,31 +317,7 @@ class Database(Compat):
|
|||||||
self.engine: AsyncEngine = create_async_engine(
|
self.engine: AsyncEngine = create_async_engine(
|
||||||
database_uri, echo=settings.debug_database
|
database_uri, echo=settings.debug_database
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.type in {POSTGRES, COCKROACH}:
|
|
||||||
|
|
||||||
@event.listens_for(self.engine.sync_engine, "connect")
|
|
||||||
def register_custom_types(dbapi_connection, *_):
|
|
||||||
def _parse_date(value) -> datetime:
|
|
||||||
if value is None:
|
|
||||||
value = "1970-01-01 00:00:00"
|
|
||||||
f = "%Y-%m-%d %H:%M:%S.%f"
|
|
||||||
if "." not in value:
|
|
||||||
f = "%Y-%m-%d %H:%M:%S"
|
|
||||||
# Parse and add UTC timezone info
|
|
||||||
return datetime.strptime(value, f).replace(tzinfo=timezone.utc)
|
|
||||||
|
|
||||||
dbapi_connection.run_async(
|
|
||||||
lambda connection: connection.set_type_codec(
|
|
||||||
"TIMESTAMP",
|
|
||||||
encoder=datetime,
|
|
||||||
decoder=_parse_date,
|
|
||||||
schema="pg_catalog",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.lock = asyncio.Lock()
|
self.lock = asyncio.Lock()
|
||||||
|
|
||||||
logger.trace(f"database {self.type} added for {self.name}")
|
logger.trace(f"database {self.type} added for {self.name}")
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
@@ -663,7 +630,7 @@ def insert_query(table_name: str, model: BaseModel) -> str:
|
|||||||
placeholders = []
|
placeholders = []
|
||||||
keys = model_to_dict(model).keys()
|
keys = model_to_dict(model).keys()
|
||||||
for field in keys:
|
for field in keys:
|
||||||
placeholders.append(get_placeholder(model, field))
|
placeholders.append(f":{field}")
|
||||||
# add quotes to keys to avoid SQL conflicts (e.g. `user` is a reserved keyword)
|
# add quotes to keys to avoid SQL conflicts (e.g. `user` is a reserved keyword)
|
||||||
fields = ", ".join([f'"{key}"' for key in keys])
|
fields = ", ".join([f'"{key}"' for key in keys])
|
||||||
values = ", ".join(placeholders)
|
values = ", ".join(placeholders)
|
||||||
@@ -681,9 +648,8 @@ def update_query(
|
|||||||
"""
|
"""
|
||||||
fields = []
|
fields = []
|
||||||
for field in model_to_dict(model).keys():
|
for field in model_to_dict(model).keys():
|
||||||
placeholder = get_placeholder(model, field)
|
|
||||||
# add quotes to keys to avoid SQL conflicts (e.g. `user` is a reserved keyword)
|
# add quotes to keys to avoid SQL conflicts (e.g. `user` is a reserved keyword)
|
||||||
fields.append(f'"{field}" = {placeholder}')
|
fields.append(f'"{field}" = :{field}')
|
||||||
query = ", ".join(fields)
|
query = ", ".join(fields)
|
||||||
return f"UPDATE {table_name} SET {query} {where}" # noqa: S608
|
return f"UPDATE {table_name} SET {query} {where}" # noqa: S608
|
||||||
|
|
||||||
@@ -701,7 +667,12 @@ def model_to_dict(model: BaseModel) -> dict:
|
|||||||
if model.__fields__[key].field_info.extra.get("no_database", False):
|
if model.__fields__[key].field_info.extra.get("no_database", False):
|
||||||
continue
|
continue
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
_dict[key] = value.timestamp()
|
if DB_TYPE == SQLITE:
|
||||||
|
_dict[key] = value.timestamp()
|
||||||
|
else:
|
||||||
|
# remove tz. postgres and cockroach TIMESTAMP is not tz aware
|
||||||
|
# so it will throw if we dont remove the UTC.
|
||||||
|
_dict[key] = value.replace(tzinfo=None)
|
||||||
continue
|
continue
|
||||||
if (
|
if (
|
||||||
type(type_) is type(BaseModel)
|
type(type_) is type(BaseModel)
|
||||||
@@ -757,11 +728,7 @@ def dict_to_model(_row: dict, model: type[TModel]) -> TModel: # noqa: C901
|
|||||||
if DB_TYPE == SQLITE:
|
if DB_TYPE == SQLITE:
|
||||||
_dict[key] = datetime.fromtimestamp(value, timezone.utc)
|
_dict[key] = datetime.fromtimestamp(value, timezone.utc)
|
||||||
else:
|
else:
|
||||||
# Ensure PostgreSQL datetime values have timezone info
|
_dict[key] = value.replace(tzinfo=timezone.utc)
|
||||||
if isinstance(value, datetime) and value.tzinfo is None:
|
|
||||||
_dict[key] = value.replace(tzinfo=timezone.utc)
|
|
||||||
else:
|
|
||||||
_dict[key] = value
|
|
||||||
continue
|
continue
|
||||||
if issubclass(type_, BaseModel):
|
if issubclass(type_, BaseModel):
|
||||||
_dict[key] = dict_to_submodel(type_, value)
|
_dict[key] = dict_to_submodel(type_, value)
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -71,6 +71,10 @@ window.app.component('lnbits-wallet-extra', {
|
|||||||
'lnbits.exchangeRate.' + this.g.wallet.currency,
|
'lnbits.exchangeRate.' + this.g.wallet.currency,
|
||||||
this.g.exchangeRate
|
this.g.exchangeRate
|
||||||
)
|
)
|
||||||
|
if (this.g.exchangeRate <= 0) {
|
||||||
|
this.g.fiatTracking = false
|
||||||
|
this.g.isFiatPriority = false
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(e => console.error(e))
|
.catch(e => console.error(e))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ window._lnbitsUtils = {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
notifyApiError(error) {
|
async notifyApiError(error) {
|
||||||
if (!error.response) {
|
if (!error.response) {
|
||||||
return console.error(error)
|
return console.error(error)
|
||||||
}
|
}
|
||||||
@@ -154,6 +154,10 @@ window._lnbitsUtils = {
|
|||||||
500: 'negative'
|
500: 'negative'
|
||||||
}
|
}
|
||||||
let messages = error.response.data.detail
|
let messages = error.response.data.detail
|
||||||
|
if (!messages) {
|
||||||
|
const text = await error.response.data?.text()
|
||||||
|
messages = this.parseJSONSafe(text)?.detail
|
||||||
|
}
|
||||||
if (messages) {
|
if (messages) {
|
||||||
messages = Array.isArray(messages)
|
messages = Array.isArray(messages)
|
||||||
? messages.map(e => e.msg + ` (${e.loc?.join('/')})`)
|
? messages.map(e => e.msg + ` (${e.loc?.join('/')})`)
|
||||||
|
|||||||
@@ -178,7 +178,7 @@
|
|||||||
"
|
"
|
||||||
:clickable="!!reviewsUrl"
|
:clickable="!!reviewsUrl"
|
||||||
@click="openReviews(extension)"
|
@click="openReviews(extension)"
|
||||||
/>
|
></lnbits-extension-rating>
|
||||||
<q-btn-group size="xs" style="margin: 5px 0">
|
<q-btn-group size="xs" style="margin: 5px 0">
|
||||||
<q-btn
|
<q-btn
|
||||||
v-if="extension.hasFreeRelease"
|
v-if="extension.hasFreeRelease"
|
||||||
|
|||||||
@@ -292,7 +292,8 @@ async def btc_rates(currency: str) -> list[tuple[str, float]]:
|
|||||||
async def btc_price(currency: str) -> float:
|
async def btc_price(currency: str) -> float:
|
||||||
rates = await btc_rates(currency)
|
rates = await btc_rates(currency)
|
||||||
if not rates:
|
if not rates:
|
||||||
raise ValueError("Could not fetch any Bitcoin price.")
|
logger.warning("Could not fetch any Bitcoin price.")
|
||||||
|
return 0.0
|
||||||
elif len(rates) == 1:
|
elif len(rates) == 1:
|
||||||
logger.warning("Could only fetch one Bitcoin price.")
|
logger.warning("Could only fetch one Bitcoin price.")
|
||||||
|
|
||||||
@@ -306,7 +307,8 @@ async def get_fiat_rate_and_price_satoshis(currency: str) -> tuple[float, float]
|
|||||||
f"btc-price-{currency}",
|
f"btc-price-{currency}",
|
||||||
settings.lnbits_exchange_rate_cache_seconds,
|
settings.lnbits_exchange_rate_cache_seconds,
|
||||||
)
|
)
|
||||||
return float(100_000_000 / price), price
|
rate = float(100_000_000 / price) if price > 0 else 0.0
|
||||||
|
return rate, price
|
||||||
|
|
||||||
|
|
||||||
async def get_fiat_rate_satoshis(currency: str) -> float:
|
async def get_fiat_rate_satoshis(currency: str) -> float:
|
||||||
@@ -316,9 +318,13 @@ async def get_fiat_rate_satoshis(currency: str) -> float:
|
|||||||
|
|
||||||
async def fiat_amount_as_satoshis(amount: float, currency: str) -> int:
|
async def fiat_amount_as_satoshis(amount: float, currency: str) -> int:
|
||||||
rate = await get_fiat_rate_satoshis(currency)
|
rate = await get_fiat_rate_satoshis(currency)
|
||||||
return int(amount * (rate))
|
if rate > 0:
|
||||||
|
return int(amount * rate)
|
||||||
|
raise ValueError(f"Could not get exchange rate for {currency}.")
|
||||||
|
|
||||||
|
|
||||||
async def satoshis_amount_as_fiat(amount: float, currency: str) -> float:
|
async def satoshis_amount_as_fiat(amount: float, currency: str) -> float:
|
||||||
rate = await get_fiat_rate_satoshis(currency)
|
rate = await get_fiat_rate_satoshis(currency)
|
||||||
return float(amount / rate)
|
if rate > 0:
|
||||||
|
return float(amount / rate)
|
||||||
|
raise ValueError(f"Could not get exchange rate for {currency}.")
|
||||||
|
|||||||
Reference in New Issue
Block a user