def cart(): """ 商品加入购物车 :return: """ order = request.json session = Session() amazon = None global sites print(f'收到来自IP[{request.remote_addr}]的Cart请求,使用账号[{order["amazon_account"]}]开始进行处理!') try: amazon_account: AmazonAccount = session.query(AmazonAccount).filter( AmazonAccount.email == order['amazon_account'], or_( AmazonAccount.amazon_account_status_id == AmazonAccountStatus.IDLE, AmazonAccount.amazon_account_status_id == AmazonAccountStatus.USING ) ).one() if amazon_account.email in sites: amazon = sites[amazon_account.email] else: amazon = sites[amazon_account.email] = Amazon(amazon_account.email, amazon_account.password) try: amazon.visit(amazon.cart_page) except WebDriverException: # 被人为手动关掉的浏览器 clean_up(amazon) amazon = sites[amazon_account.email] = Amazon(amazon_account.email, amazon_account.password) amazon.login() amazon.clear_cart() # 将商品加入购物车 for asin in order['order_details'].keys(): amazon.get(asin) amazon.visit(amazon.cart_page) products = amazon.cart(order['order_details']) result = { 'result': True, 'products': products } except Exception as exception: result = clean_up(amazon, exception) return jsonify(result)
def checkout(): """ 填写地址 :return: """ print(f'收到来自IP[{request.remote_addr}]的Checkout请求,使用账号[{request.json["amazon_account"]}]开始进行处理!') session = Session() amazon = None global sites try: amazon_account: AmazonAccount = session.query(AmazonAccount).filter( AmazonAccount.email == request.json['amazon_account'], AmazonAccount.amazon_account_status_id == AmazonAccountStatus.USING ).one() amazon = sites[amazon_account.email] address = { 'name': amazon_account.depository_address.name, 'postcode': amazon_account.depository_address.postcode, 'prefecture': amazon_account.depository_address.prefecture.name, 'address1': amazon_account.depository_address.address1, 'address2': amazon_account.depository_address.address2, 'company': amazon_account.depository_address.company, 'tel': amazon_account.depository_address.tel } price, summaries = amazon.checkout(address) result = { 'result': True, 'price': price, 'summaries': summaries } except Exception as exception: result = clean_up(amazon, exception) return jsonify(result)
class Post(Base): __tablename__ = 'posts' id = Column(Integer, primary_key=True) number = Column(String(13), nullable=False, unique=True) delivery_type_id = Column(SmallInteger, ForeignKey('delivery_types.id'), nullable=False) postage = Column(Integer, nullable=False) name = Column(String(100), nullable=False) postcode = Column(String(6), nullable=False) address = Column(Text, nullable=False) tel = Column(String(20), nullable=False) image = Column(String(20), nullable=False) note = Column(Text, nullable=True, server_default=None) created = Column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP')) updated = Column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) modifier_id = Column(Integer, ForeignKey('administrators.id'), nullable=True, server_default=None) deleted = Column(TIMESTAMP, nullable=True, server_default=None) delivery_type = relationship('DeliveryType', backref='posts') modifier = relationship('Administrator', backref='modified_posts') if __name__ == '__main__': from Config.datasource import Session # noinspection PyUnresolvedReferences from AmazonAccountStatus import AmazonAccountStatus session = Session()
# -*- coding: utf-8 -*- from sqlalchemy import Column, text, SmallInteger, Integer, String, TIMESTAMP from Config.datasource import Base class AmazonAccountStatus(Base): __tablename__ = 'amazon_account_statuses' id = Column(SmallInteger, primary_key=True) name = Column(String(255), nullable=False, unique=True) sort = Column(Integer, nullable=False, unique=True) created = Column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP')) updated = Column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) deleted = Column(TIMESTAMP, nullable=True, server_default=None) STOPPED = 1 # 停止 IDLE = 2 # 未使用 USING = 3 # 使用中 ERROR = 4 # 错误 if __name__ == '__main__': from Config.datasource import Session session = Session() for amazon_account_status in session.query(AmazonAccountStatus).all(): print(amazon_account_status.sort)
nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) modifier_id = Column(Integer, ForeignKey('administrators.id'), nullable=True, server_default=None) depository_address = relationship('DepositoryAddress', backref='amazon_accounts') amazon_account_status = relationship('AmazonAccountStatus', backref='amazon_accounts') creator = relationship('Administrator', backref='created_amazon_accounts', foreign_keys=creator_id) modifier = relationship('Administrator', backref='modified_amazon_accounts', foreign_keys=modifier_id) if __name__ == '__main__': from Config.datasource import Session # noinspection PyUnresolvedReferences from AmazonAccountStatus import AmazonAccountStatus from Administrator import Administrator from Sex import Sex session = Session() for amazon_account in session.query(AmazonAccount).all(): print(amazon_account.email)