Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ jobs:
with:
python-path: ${{ env.PYTHON_BIN }}
pylance-version: latest-release

- name: Check code format
uses: astral-sh/ruff-action@v3
with:
args: check . --exit-non-zero-on-fix

- name: Check database
run: nb orm upgrade

- name: Build package
run: uv build # 生成构建产物到dist目录

Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ jobs:
with:
python-path: ${{ env.PYTHON_BIN }}
pylance-version: latest-release

- name: Check code format
uses: astral-sh/ruff-action@v3
with:
args: check . --exit-non-zero-on-fix

- name: Check database
run: nb orm upgrade

- name: Build package
run: uv build # 生成构建产物到dist目录

- name: Save build artifacts
uses: actions/upload-artifact@v4
with:
name: python-package
path: dist/* # 上传所有构建产物
8 changes: 1 addition & 7 deletions nonebot_plugin_value/api/api_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ async def get_or_create_account(
currency_id = (await _get_default()).id
async with get_session() as session:
data = await _go_account(user_id, currency_id, session)
return UserAccountData(
id=data.id,
uni_id=data.uni_id,
currency_id=data.currency_id,
balance=data.balance,
last_updated=data.last_updated,
)
return UserAccountData.model_validate(data, from_attributes=True)


async def batch_del_balance(
Expand Down
41 changes: 6 additions & 35 deletions nonebot_plugin_value/api/api_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ async def update_currency(currency_data: CurrencyData) -> CurrencyData:
"""
async with get_session() as session:
currency = await _update_currency(currency_data, session)
return CurrencyData(
id=currency.id,
allow_negative=currency.allow_negative,
display_name=currency.display_name,
symbol=currency.symbol,
)
return CurrencyData.model_validate(currency, from_attributes=True)


async def remove_currency(currency_id: str) -> None:
Expand All @@ -51,13 +46,7 @@ async def list_currencies() -> list[CurrencyData]:
async with get_session() as session:
currencies = await _currencies(session)
return [
CurrencyData(
id=currency.id,
allow_negative=currency.allow_negative,
display_name=currency.display_name,
symbol=currency.symbol,
default_balance=currency.default_balance,
)
CurrencyData.model_validate(currency, from_attributes=True)
for currency in currencies
]

Expand All @@ -75,13 +64,7 @@ async def get_currency(currency_id: str) -> CurrencyData | None:
currency = await _g_currency(currency_id, session)
if currency is None:
return None
return CurrencyData(
id=currency.id,
allow_negative=currency.allow_negative,
display_name=currency.display_name,
symbol=currency.symbol,
default_balance=currency.default_balance,
)
return CurrencyData.model_validate(currency, from_attributes=True)


async def get_default_currency() -> CurrencyData:
Expand All @@ -92,13 +75,7 @@ async def get_default_currency() -> CurrencyData:
"""
async with get_session() as session:
currency = await _default_currency(session)
return CurrencyData(
id=currency.id,
allow_negative=currency.allow_negative,
display_name=currency.display_name,
symbol=currency.symbol,
default_balance=currency.default_balance,
)
return CurrencyData.model_validate(currency, from_attributes=True)


async def create_currency(currency_data: CurrencyData) -> None:
Expand All @@ -115,7 +92,7 @@ async def create_currency(currency_data: CurrencyData) -> None:


async def get_or_create_currency(currency_data: CurrencyData) -> CurrencyData:
"""获取或者创建货币
"""获取或者创建货币(一般在初始化时使用)

Args:
currency_data (CurrencyData): 货币数据
Expand All @@ -125,10 +102,4 @@ async def get_or_create_currency(currency_data: CurrencyData) -> CurrencyData:
"""
async with get_session() as session:
currency, _ = await _get_or_create_currency(currency_data, session)
return CurrencyData(
id=currency.id,
allow_negative=currency.allow_negative,
display_name=currency.display_name,
symbol=currency.symbol,
default_balance=currency.default_balance,
)
return CurrencyData.model_validate(currency)
24 changes: 2 additions & 22 deletions nonebot_plugin_value/api/api_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,7 @@ async def get_transaction_history_by_time_range(
limit,
)
result_list: list[TransactionData] = [
TransactionData(
id=transaction.id,
account_id=transaction.account_id,
currency_id=transaction.currency_id,
amount=transaction.amount,
action=transaction.action,
source=transaction.source,
balance_before=transaction.balance_before,
balance_after=transaction.balance_after,
timestamp=transaction.timestamp,
)
TransactionData.model_validate(transaction, from_attributes=True)
for transaction in data
]
return result_list
Expand All @@ -67,17 +57,7 @@ async def get_transaction_history(
"""
async with get_session() as session:
return [
TransactionData(
id=transaction.id,
account_id=transaction.account_id,
currency_id=transaction.currency_id,
amount=transaction.amount,
action=transaction.action,
source=transaction.source,
balance_before=transaction.balance_before,
balance_after=transaction.balance_after,
timestamp=transaction.timestamp,
)
TransactionData.model_validate(transaction, from_attributes=True)
for transaction in await _transaction_history(
account_id,
session,
Expand Down
76 changes: 76 additions & 0 deletions nonebot_plugin_value/migrations/f3fc1c3ec51d_fix_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""fix_delete

迁移 ID: f3fc1c3ec51d
父迁移: 53691b7cf82d
创建时间: 2025-07-31 15:32:03.929207

"""

from __future__ import annotations

from collections.abc import Sequence

from alembic import op

revision: str = "f3fc1c3ec51d"
down_revision: str | Sequence[str] | None = "53691b7cf82d"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("transactions", schema=None) as batch_op:
batch_op.drop_constraint(
batch_op.f("fk_transactions_account_id_user_accounts"), type_="foreignkey"
)
batch_op.drop_constraint(
batch_op.f("fk_transactions_currency_id_currency_meta"), type_="foreignkey"
)
batch_op.create_foreign_key(
batch_op.f("fk_transactions_currency_id_currency_meta"),
"currency_meta",
["currency_id"],
["id"],
ondelete="CASCADE",
)
batch_op.create_foreign_key(
batch_op.f("fk_transactions_account_id_user_accounts"),
"user_accounts",
["account_id"],
["id"],
ondelete="CASCADE",
)

# ### end Alembic commands ###


def downgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("transactions", schema=None) as batch_op:
batch_op.drop_constraint(
batch_op.f("fk_transactions_account_id_user_accounts"), type_="foreignkey"
)
batch_op.drop_constraint(
batch_op.f("fk_transactions_currency_id_currency_meta"), type_="foreignkey"
)
batch_op.create_foreign_key(
batch_op.f("fk_transactions_currency_id_currency_meta"),
"currency_meta",
["currency_id"],
["id"],
ondelete="RESTRICT",
)
batch_op.create_foreign_key(
batch_op.f("fk_transactions_account_id_user_accounts"),
"user_accounts",
["account_id"],
["id"],
ondelete="RESTRICT",
)

# ### end Alembic commands ###
15 changes: 11 additions & 4 deletions nonebot_plugin_value/models/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sqlalchemy.orm import MappedColumn, mapped_column, relationship

from ..uuid_lib import NAMESPACE_VALUE
from .utils import OnDeleteEnum


class UserAccount(Model):
Expand All @@ -28,12 +29,14 @@ class UserAccount(Model):
# 用户ID
id: MappedColumn[str] = mapped_column(String(255))

# 账户是否冻结(等待实现)
# 账户是否冻结
frozen: MappedColumn[bool] = mapped_column(Boolean, default=False)

# 货币外键
currency_id: MappedColumn[str] = mapped_column(
String(255), ForeignKey("currency_meta.id", ondelete="CASCADE"), nullable=False
String(255),
ForeignKey("currency_meta.id", ondelete=OnDeleteEnum.CASCADE.value),
nullable=False,
)

# 账户余额
Expand Down Expand Up @@ -75,12 +78,16 @@ class Transaction(Model):

# 账户外键
account_id: MappedColumn[str] = mapped_column(
String(255), ForeignKey("user_accounts.id", ondelete="RESTRICT"), nullable=False
String(255),
ForeignKey("user_accounts.id", ondelete=OnDeleteEnum.CASCADE.value),
nullable=False,
)

# 货币外键
currency_id: MappedColumn[str] = mapped_column(
String(255), ForeignKey("currency_meta.id", ondelete="RESTRICT"), nullable=False
String(255),
ForeignKey("currency_meta.id", ondelete=OnDeleteEnum.CASCADE.value),
nullable=False,
)

# 交易金额
Expand Down
9 changes: 9 additions & 0 deletions nonebot_plugin_value/models/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class OnDeleteEnum(str, Enum):
CASCADE = "CASCADE"
RESTRICT = "RESTRICT"
NO_ACTION = "NO ACTION"
SET_NULL = "SET NULL"
SET_DEFAULT = "SET DEFAULT"
Loading
Loading