From a06b72652931a399ece752c5ca26c64989923414 Mon Sep 17 00:00:00 2001 From: Ngoc Nguyen Date: Sat, 5 Aug 2023 16:38:57 -0500 Subject: [PATCH] completed --- lib/debug.py | 7 +- lib/freebies.db | Bin 20480 -> 40960 bytes ...a721b_add_company_dev_association_table.py | 34 ++++++ ...c035_add_print_detail_method_in_freebie.py | 28 +++++ .../6e7c522f3eea_added_freebie_model.py | 37 ++++++ lib/models.py | 107 +++++++++++++++++- lib/seed.py | 66 +++++++++++ 7 files changed, 276 insertions(+), 3 deletions(-) create mode 100644 lib/migrations/versions/42855bda721b_add_company_dev_association_table.py create mode 100644 lib/migrations/versions/66bc32f5c035_add_print_detail_method_in_freebie.py create mode 100644 lib/migrations/versions/6e7c522f3eea_added_freebie_model.py diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..8cbb90b2e 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,14 @@ #!/usr/bin/env python3 from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker -from models import Company, Dev +from models import Company, Dev, Freebie if __name__ == '__main__': + engine = create_engine('sqlite:///freebies.db') + Session = sessionmaker(bind=engine) + session = Session() + import ipdb; ipdb.set_trace() diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..b6d82158532f78046f2ed89b7be53109c1671efa 100644 GIT binary patch literal 40960 zcmeI5e~cVe9l&RI=FRNPym{Z*AJ^><%1tT9-O~2j>(L%R`fGc=mTP<0cJF9Gew=fA zb9YPkcFXQw+pCFKBFH~TGzLP5k{E(%l&ENeN&<#JPy#4|k{A>*qQr>d_xAOA zvjf#=OhEijI`i50*2>ne_(+^8Q-P^gZFfh1lSmX?)%z1@@odbn|;r@Y<(-RP# zyGj%P>LhX~O(iY4PY*GiR}89|(#rK2dUj7oOes|+g5&iY%KU0%%%5#ko8>bDZ_c78 zJg3A>*AgSUe0l6>oy;}$iUklmY0)Zb z{)y74&8QSBchXjkY%GB6g;Mc-S`5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XH;&2G9T+Km%w14WI!ufCkV2 z8u;KFAbB+wBL>+f0A#B`l57#6WU~Mxn*?-nnSezu6-bdw1XOadK$`Rk5VBDqK{g1O zTioMWCV1Q7H~;QfRLnsMqB}hfPhJuKuU8|Gv;WNA3?ME(bOz{R5ggU%&W}`!@uMz&|1ZxB1s;M|_*MVg@B)03e-_|A z_&(eZKZi%*cQ6UVa0`4KDzJs$1vm(w=HCoB3SZ!N06N(_>^JOn=w>g00qv0GpA`W1 z7Q2((&Z_JYevN;Oe{;wYiFGdSe9*N`#$(j zavUKVKm%w14WI!ufCkV28bAYR;6q@*(D=uHh>@3sZIa2Yl5~qC+bpSXlC&<9OkFCe zULu*kSd#QfCN@f%8=BPgizKaHNprm<^hj#!B-OQ&nQqC{8cAohWO|jPaiJv5NhZ4_ z?an4udV!?gA(=Q|GP6=rJx|hEA({3hpD7T^ULB(Pbq;TJy}5*q*j{}`$O diff --git a/lib/migrations/versions/42855bda721b_add_company_dev_association_table.py b/lib/migrations/versions/42855bda721b_add_company_dev_association_table.py new file mode 100644 index 000000000..059a416c8 --- /dev/null +++ b/lib/migrations/versions/42855bda721b_add_company_dev_association_table.py @@ -0,0 +1,34 @@ +"""Add company_dev Association Table + +Revision ID: 42855bda721b +Revises: 6e7c522f3eea +Create Date: 2023-08-05 10:41:11.281916 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '42855bda721b' +down_revision = '6e7c522f3eea' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('company_devs', + sa.Column('company_id', sa.Integer(), nullable=False), + sa.Column('dev_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], name=op.f('fk_company_devs_company_id_companies')), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], name=op.f('fk_company_devs_dev_id_devs')), + sa.PrimaryKeyConstraint('company_id', 'dev_id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('company_devs') + # ### end Alembic commands ### diff --git a/lib/migrations/versions/66bc32f5c035_add_print_detail_method_in_freebie.py b/lib/migrations/versions/66bc32f5c035_add_print_detail_method_in_freebie.py new file mode 100644 index 000000000..aed54043a --- /dev/null +++ b/lib/migrations/versions/66bc32f5c035_add_print_detail_method_in_freebie.py @@ -0,0 +1,28 @@ +"""Add print_detail() method in Freebie + +Revision ID: 66bc32f5c035 +Revises: 42855bda721b +Create Date: 2023-08-05 13:01:57.993992 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '66bc32f5c035' +down_revision = '42855bda721b' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/lib/migrations/versions/6e7c522f3eea_added_freebie_model.py b/lib/migrations/versions/6e7c522f3eea_added_freebie_model.py new file mode 100644 index 000000000..2be35187f --- /dev/null +++ b/lib/migrations/versions/6e7c522f3eea_added_freebie_model.py @@ -0,0 +1,37 @@ +"""Added Freebie model + +Revision ID: 6e7c522f3eea +Revises: 5f72c58bf48c +Create Date: 2023-08-05 08:04:25.210600 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6e7c522f3eea' +down_revision = '5f72c58bf48c' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('freebies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('item_name', sa.String(), nullable=True), + sa.Column('value', sa.Integer(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.Column('dev_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], name=op.f('fk_freebies_company_id_companies')), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], name=op.f('fk_freebies_dev_id_devs')), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('freebies') + # ### end Alembic commands ### diff --git a/lib/models.py b/lib/models.py index 2681bee5a..7f39cc13c 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,5 +1,5 @@ -from sqlalchemy import ForeignKey, Column, Integer, String, MetaData -from sqlalchemy.orm import relationship, backref +from sqlalchemy import ForeignKey, Table, Column, Integer, String, MetaData, create_engine +from sqlalchemy.orm import relationship, backref, sessionmaker from sqlalchemy.ext.declarative import declarative_base convention = { @@ -9,21 +9,124 @@ Base = declarative_base(metadata=metadata) +engine = create_engine("sqlite:///freebies.db") +Session = sessionmaker(bind=engine) +session = Session() + +company_dev = Table( + "company_devs", + Base.metadata, + Column("company_id", ForeignKey("companies.id"), primary_key=True), + Column("dev_id", ForeignKey("devs.id"), primary_key=True), + extend_existing=False, +) + + class Company(Base): + __tablename__ = 'companies' id = Column(Integer(), primary_key=True) name = Column(String()) founding_year = Column(Integer()) + freebies = relationship("Freebie", backref=backref("company")) + devs = relationship("Dev", secondary=company_dev, back_populates="companies") + def __repr__(self): + return f'' + + # Company.give_freebie(dev, item_name, value) takes a dev (an instance of the Dev class), + # an item_name (string), and a value as arguments, + # and creates a new Freebie instance associated with this company and the given dev. + def give_freebie(self, dev, item_name, value): + + freebie = Freebie(item_name=item_name, value=value, company_id=self.id,dev_id=dev.id) + session.add(freebie) + session.commit() + + return freebie + + # Class method Company.oldest_company()returns the Company instance with the earliest founding year. + @classmethod + def oldest_company(cls): + + return session.query(cls.name).order_by(cls.founding_year).limit(1).all() + class Dev(Base): + __tablename__ = 'devs' id = Column(Integer(), primary_key=True) name= Column(String()) + freebies = relationship("Freebie", backref=backref("dev")) + companies = relationship("Company", secondary=company_dev, back_populates="devs") + def __repr__(self): + return f'' + + # Dev.received_one(item_name) accepts an item_name (string) and returns True + # if any of the freebies associated with the dev has that item_name, otherwise returns False. + def received_one(self, item_name): + + items_owned_by_dev = session.query(Freebie).filter(Freebie.dev_id == self.id, + Freebie.item_name == item_name).all() + + if items_owned_by_dev: + return True + + return False + + # Dev.give_away(dev, freebie) accepts a Dev instance and a Freebie instance, + # changes the freebie's dev to be the given dev; your code should only make the change + # if the freebie belongs to the dev who's giving it away + def give_away(self, dev, freebie): + + if self.id == freebie.dev_id: + freebie.dev_id = dev.id + session.commit() + + +class Freebie(Base): + + __tablename__ = "freebies" + + id = Column(Integer(), primary_key=True) + item_name = Column(String()) + value = Column(Integer()) + + company_id = Column(Integer(), ForeignKey("companies.id")) + dev_id = Column(Integer(), ForeignKey("devs.id")) + + def __repr__(self): + + return f'