11from datetime import datetime
22
33from pydantic import BaseModel
4+ from sqladmin import ModelView
45from sqlalchemy import Column , Integer , Float , DateTime , ForeignKey , func , CheckConstraint , Enum , String
56from sqlalchemy .orm import relationship
67
@@ -17,21 +18,34 @@ class Buy(Base):
1718
1819 id = Column (Integer , primary_key = True )
1920 buyer_id = Column (Integer , ForeignKey ('users.id' ), nullable = True )
20- buyer = relationship (' User' , backref = ' buys' )
21+ buyer = relationship (" User" , back_populates = " buys" )
2122 total_price = Column (Float , nullable = False )
2223 buy_datetime = Column (DateTime , default = func .now ())
2324 status = Column (Enum (BuyStatus ), nullable = False )
2425 coupon_id = Column (Integer , ForeignKey ('coupons.id' ), nullable = True )
26+ coupon = relationship ("Coupon" , back_populates = "buys" )
2527 discount = Column (Float , nullable = False , default = 0.0 )
2628 shipping_address = Column (String , nullable = True )
2729 track_number = Column (String , nullable = True )
2830 shipping_option_id = Column (Integer , ForeignKey ('shipping_options.id' ), nullable = True )
31+ shipping_option = relationship ("ShippingOption" , back_populates = "buys" )
32+ buy_items = relationship (
33+ "BuyItem" ,
34+ back_populates = "buy" ,
35+ cascade = "all, delete-orphan"
36+ )
2937
3038 __table_args__ = (
31- CheckConstraint ('quantity > 0' , name = 'check_quantity_positive' ),
3239 CheckConstraint ('total_price > 0' , name = 'check_total_price_positive' ),
3340 )
3441
42+ def __repr__ (self ):
43+ return get_text (Language .EN , BotEntity .USER , "purchase_history_item" ).format (
44+ buy_id = self .id ,
45+ total_price = self .total_price ,
46+ currency_sym = config .CURRENCY .get_localized_symbol ()
47+ )
48+
3549
3650class BuyDTO (BaseModel ):
3751 id : int | None = None
@@ -60,3 +74,20 @@ class RefundDTO(BaseModel):
6074 item_ids : list [int ] | None = None
6175 buy_id : int | None = None
6276 language : Language
77+
78+
79+ class BuyAdmin (ModelView , model = Buy ):
80+ column_exclude_list = [Buy .shipping_option_id ,
81+ Buy .coupon_id ,
82+ Buy .buyer_id ]
83+ column_sortable_list = [Buy .id ,
84+ Buy .total_price ,
85+ Buy .buy_datetime ,
86+ Buy .status ,
87+ Buy .discount ,
88+ Buy .shipping_address ,
89+ Buy .track_number ]
90+ can_create = False
91+ can_delete = False
92+ can_edit = False
93+ can_export = False
0 commit comments