diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c96..bd2b4de09 100644 Binary files a/lib/freebies.db and b/lib/freebies.db differ diff --git a/lib/migrations/versions/3a9b975a6961_add_freebie_model.py b/lib/migrations/versions/3a9b975a6961_add_freebie_model.py new file mode 100644 index 000000000..6225d78db --- /dev/null +++ b/lib/migrations/versions/3a9b975a6961_add_freebie_model.py @@ -0,0 +1,45 @@ +"""Add freebie model + +Revision ID: 3a9b975a6961 +Revises: 5f72c58bf48c +Create Date: 2023-09-02 10:45:10.488928 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '3a9b975a6961' +down_revision = '5f72c58bf48c' +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') + ) + 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('dev_id', sa.Integer(), nullable=True), + sa.Column('company_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') + op.drop_table('company_devs') + # ### end Alembic commands ### diff --git a/lib/models.py b/lib/models.py index 2681bee5a..12a756161 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,13 +1,19 @@ -from sqlalchemy import ForeignKey, Column, Integer, String, MetaData +from sqlalchemy import ForeignKey, Column, Integer, String, MetaData, Table from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +from sqlalchemy import create_engine convention = { "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", } metadata = MetaData(naming_convention=convention) +creator_engine = create_engine('sqlite:///freebies.db') +Session = sessionmaker(bind=creator_engine) +session = Session() Base = declarative_base(metadata=metadata) +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)) class Company(Base): __tablename__ = 'companies' @@ -16,6 +22,10 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + freebies = relationship('Frieebies', backref=backref('company')) + devs = relationship('Dev', secondary=company_dev, back_populates='companies') + + def __repr__(self): return f'' @@ -25,5 +35,45 @@ class Dev(Base): id = Column(Integer(), primary_key=True) name= Column(String()) + freebies = relationship('Freebie', back_populates='dev') + companies = relationship('Company', secondary='freebies', back_populates='devs') + + def __repr__(self): return f'' + +class Freebie(Base): + + __tablename__ = 'freebies' + + id = Column(Integer(), primary_key=True) + item_name = Column(String()) + value = Column(Integer()) + dev_id = Column(Integer(), ForeignKey('devs.id')) + # The backref and back_populates keywords generate attributes in the related data model that map to records from the current data model. + dev = relationship('Devs', back_populates='freebies') + + company_id = Column(Integer(), ForeignKey('companies.id')) + company = relationship('Company', back_populates='freebies') + + def print_details(self): + return f"{self.dev.name} owns a {self.item_name} from {self.company.name}" + + @classmethod + def give_away(cls, dev, freebie): + if freebie.dev == dev: + freebie.dev = None + + @classmethod + def received_one(cls, item_name): + return cls.query.filter_by(item_name=item_name).first() is not None + + @classmethod + def create_freebie(cls, dev, company, item_name, value): + freebie = cls(dev=dev, company=company, item_name=item_name, value=value) + # session.add(freebie) + # session.commit() + + @classmethod + def oldest_company(cls): + return Company.query.order_by(Company.founding_year).first() \ No newline at end of file