1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| import os from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Table, Column, Integer, Numeric, String, DateTime, ForeignKey, Boolean, CheckConstraint from datetime import datetime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker
dbname = os.path.basename(__file__).replace('.py', '.sqlite') engine = create_engine('sqlite:///{}'.format(dbname), echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Cookie(Base): __tablename__ = 'cookies' __table_args__ = (CheckConstraint( 'quantity >= 0', name='quantity_positive'),)
cookie_id = Column(Integer, primary_key=True) cookie_name = Column(String(50), index=True) cookie_recipe_url = Column(String(255)) cookie_sku = Column(String(55)) quantity = Column(Integer()) unit_cost = Column(Numeric(12, 2))
def __init__(self, name, recipe_url=None, sku=None, quantity=0, unit_cost=0.00): self.cookie_name = name self.cookie_recipe_url = recipe_url self.cookie_sku = sku self.quantity = quantity self.unit_cost = unit_cost
def __repr__(self): return "Cookie(cookie_name='{self.cookie_name}', " \ "cookie_recipe_url='{self.cookie_recipe_url}', " \ "cookie_sku='{self.cookie_sku}', " \ "quantity={self.quantity}, " \ "unit_cost={self.unit_cost})".format(self=self)
class User(Base): __tablename__ = 'users'
user_id = Column(Integer(), primary_key=True) username = Column(String(15), nullable=False, unique=True) email_address = Column(String(255), nullable=False) phone = Column(String(20), nullable=False) password = Column(String(25), nullable=False) created_on = Column(DateTime(), default=datetime.now) updated_on = Column(DateTime(), default=datetime.now, onupdate=datetime.now)
def __init__(self, username, email_address, phone, password): self.username = username self.email_address = email_address self.phone = phone self.password = password
def __repr__(self): return "User(username='{self.username}', " \ "email_address='{self.email_address}', " \ "phone='{self.phone}', " \ "password='{self.password}')".format(self=self)
class Order(Base): __tablename__ = 'orders' order_id = Column(Integer(), primary_key=True) user_id = Column(Integer(), ForeignKey('users.user_id')) shipped = Column(Boolean(), default=False)
user = relationship("User", backref=backref('orders', order_by=order_id))
def __repr__(self): return "Order(user_id={self.user_id}, " \ "shipped={self.shipped})".format(self=self)
class LineItem(Base): __tablename__ = 'line_items' line_item_id = Column(Integer(), primary_key=True) order_id = Column(Integer(), ForeignKey('orders.order_id')) cookie_id = Column(Integer(), ForeignKey('cookies.cookie_id')) quantity = Column(Integer()) extended_cost = Column(Numeric(12, 2))
order = relationship("Order", backref=backref( 'line_items', order_by=line_item_id)) cookie = relationship("Cookie", uselist=False)
def __repr__(self): return "LineItems(order_id={self.order_id}, " \ "cookie_id={self.cookie_id}, " \ "quantity={self.quantity}, " \ "extended_cost={self.extended_cost})".format( self=self)
Base.metadata.create_all(engine)
cookiemon = User('cookiemon', 'mon@cookie.com', '111-111-1111', 'password') cc = Cookie('chocolate chip', 'http://some.aweso.me/cookie/recipe.html', 'CC01', 12, 0.50) dcc = Cookie('dark chocolate chip', 'http://some.aweso.me/cookie/recipe_dark.html', 'CC02', 1, 0.75) session.add(cookiemon) session.add(cc) session.add(dcc)
o1 = Order() o1.user = cookiemon session.add(o1)
line1 = LineItem(order=o1, cookie=cc, quantity=9, extended_cost=4.50)
session.add(line1) session.commit() o2 = Order() o2.user = cookiemon session.add(o2)
line1 = LineItem(order=o2, cookie=cc, quantity=2, extended_cost=1.50) line2 = LineItem(order=o2, cookie=dcc, quantity=9, extended_cost=6.75)
session.add(line1) session.add(line2) session.commit()
|