From 566683bc581a25bea505406e67b6c2204c5ff7c0 Mon Sep 17 00:00:00 2001 From: Connor Kormos Date: Thu, 13 Apr 2023 22:21:35 -0700 Subject: [PATCH] completed most methods and deliverables --- freebies.db | Bin 0 -> 8192 bytes lib/__pycache__/models.cpython-38.pyc | Bin 0 -> 11892 bytes lib/alembic.ini | 105 --- lib/debug.py | 264 +++++- lib/freebies.db | Bin 20480 -> 24576 bytes lib/migrations/README | 1 - lib/migrations/env.py | 79 -- lib/migrations/script.py.mako | 24 - .../5f72c58bf48c_create_companies_devs.py | 39 - .../versions/7a71dbf71c64_create_db.py | 24 - lib/models.py | 814 +++++++++++++++++- lib/seed.py | 56 ++ 12 files changed, 1109 insertions(+), 297 deletions(-) create mode 100644 freebies.db create mode 100644 lib/__pycache__/models.cpython-38.pyc delete mode 100644 lib/alembic.ini delete mode 100644 lib/migrations/README delete mode 100644 lib/migrations/env.py delete mode 100644 lib/migrations/script.py.mako delete mode 100644 lib/migrations/versions/5f72c58bf48c_create_companies_devs.py delete mode 100644 lib/migrations/versions/7a71dbf71c64_create_db.py diff --git a/freebies.db b/freebies.db new file mode 100644 index 0000000000000000000000000000000000000000..2034f9a9ab6b90c9eea70be1751fd13cc0bc5495 GIT binary patch literal 8192 zcmeIv%}N6?5C`zdc4fh~#GZ<$&QVbc>I=9^kZS!}7qll?Z7K`yu59DMqds2`>aj22 z3-keuLW){^f#p9WWF{E``Q_rgpBl}hqRbM*JG4SV&@M9(we0llbg^U;-xYQhKl1<5 z7IiNVn_tcHlHUOXBVkNGgo z@2;P8Qo7rIU9jGiGpfl6$%*`s(<*!c-T(mzKmY;|fB*y_009U<00I!Gwm?JpwCWi> bHo6R6>ouR+-_>^TUiVzF6}(x6hLiXNeW^in literal 0 HcmV?d00001 diff --git a/lib/__pycache__/models.cpython-38.pyc b/lib/__pycache__/models.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1c3dbc4a799ebbc1976221a618631361aa50d30b GIT binary patch literal 11892 zcmcIq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_2QFJO`Lu4dZ=}GOk@iyc0J?Ti8!BtNkSKn3UaFih0)E8E)5fI~7_jI+8ARNUn!N z`g1V;`-o1tcRvVs19<@OuHw~aBnw6nyFZk1;g3$xDA?;w&KlFT8-HueG;aSt)D3eH literal 0 HcmV?d00001 diff --git a/lib/alembic.ini b/lib/alembic.ini deleted file mode 100644 index 953863ddd..000000000 --- a/lib/alembic.ini +++ /dev/null @@ -1,105 +0,0 @@ -# A generic, single database configuration. - -[alembic] -# path to migration scripts -script_location = migrations - -# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s -# Uncomment the line below if you want the files to be prepended with date and time -# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file -# for all available tokens -# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s - -# sys.path path, will be prepended to sys.path if present. -# defaults to the current working directory. -prepend_sys_path = . - -# timezone to use when rendering the date within the migration file -# as well as the filename. -# If specified, requires the python-dateutil library that can be -# installed by adding `alembic[tz]` to the pip requirements -# string value is passed to dateutil.tz.gettz() -# leave blank for localtime -# timezone = - -# max length of characters to apply to the -# "slug" field -# truncate_slug_length = 40 - -# set to 'true' to run the environment during -# the 'revision' command, regardless of autogenerate -# revision_environment = false - -# set to 'true' to allow .pyc and .pyo files without -# a source .py file to be detected as revisions in the -# versions/ directory -# sourceless = false - -# version location specification; This defaults -# to migrations/versions. When using multiple version -# directories, initial revisions must be specified with --version-path. -# The path separator used here should be the separator specified by "version_path_separator" below. -# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions - -# version path separator; As mentioned above, this is the character used to split -# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. -# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. -# Valid values for version_path_separator are: -# -# version_path_separator = : -# version_path_separator = ; -# version_path_separator = space -version_path_separator = os # Use os.pathsep. Default configuration used for new projects. - -# the output encoding used when revision files -# are written from script.py.mako -# output_encoding = utf-8 - -sqlalchemy.url = sqlite:///freebies.db - - -[post_write_hooks] -# post_write_hooks defines scripts or Python functions that are run -# on newly generated revision scripts. See the documentation for further -# detail and examples - -# format using "black" - use the console_scripts runner, against the "black" entrypoint -# hooks = black -# black.type = console_scripts -# black.entrypoint = black -# black.options = -l 79 REVISION_SCRIPT_FILENAME - -# Logging configuration -[loggers] -keys = root,sqlalchemy,alembic - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = WARN -handlers = console -qualname = - -[logger_sqlalchemy] -level = WARN -handlers = -qualname = sqlalchemy.engine - -[logger_alembic] -level = INFO -handlers = -qualname = alembic - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = %(levelname)-5.5s [%(name)s] %(message)s -datefmt = %H:%M:%S diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..dc9bf9d32 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,265 @@ #!/usr/bin/env python3 -from sqlalchemy import create_engine +# from sqlalchemy import create_engine -from models import Company, Dev +from models import Company, Dev, Freebie +import ipdb; if __name__ == '__main__': - engine = create_engine('sqlite:///freebies.db') - import ipdb; ipdb.set_trace() + # engine = create_engine('sqlite:///freebies.db') + + print("Running debug.py...") + + print("--------------------") + print("--------------------") + print("--------------------") + + # ----- COMPANY CLASS ----- + print("Running Company Methods:") + + print("--------------------") + print("--------------------") + print("--------------------") + + print("Dropping companies table...") + Company.drop_table() + + print("--------------------") + + print("Creating companies table...") + Company.create_table() + + print("--------------------") + + print("Creating company instances...") + mlb = Company.create("MLB", 1905) + nba = Company.create("NBA", 1969) + nhl = Company.create("NGL", 1960) + monsanto = Company.create("Monsanto", 1940) + + print("--------------------") + + print("Updating companies...") + nhl.name = "NHL" + nhl.update() + + print("Deleting following companies...") + monsanto.delete() + + print("--------------------") + + print("Searching for oldest company...") + Company.oldest_company() + + print("--------------------") + print("--------------------") + print("--------------------") + + print("Running Dev Methods:") + + print("--------------------") + print("--------------------") + print("--------------------") + + print("Dropping devs table...") + Dev.drop_table() + + print("--------------------") + + print("Creating devs table...") + Dev.create_table() + + print("--------------------") + + print("Saving new devs and saving to database...") + connor = Dev.create("Connor") + wally = Dev.create("Bill") + the_rock = Dev.create("Dwayne") + bill = Dev.create("Wally") + + print("--------------------") + + print("Updating dev names...") + wally.name = "Wally" + wally.update() + bill.name = "Bill" + bill.update() + + print("--------------------") + + print("Deleting dev rows...") + the_rock.delete() + + print("--------------------") + print("--------------------") + print("--------------------") + + print("Running Freebie Methods:") + + print("--------------------") + print("--------------------") + print("--------------------") + + print("Dropping freebies table...") + Freebie.drop_table() + + print("--------------------") + + print("Creating freebies table...") + Freebie.create_table() + + print("--------------------") + + print("Creating and saving freebies...") + ugly_sweater = Freebie.create("Ugly Sweater", 1, 2, 3) + mlb_jersey = Freebie.create("Mike Trout jersey", 50, 1, 1) + basketball = Freebie.create("Basketball", 20, 2, 3) + hockey_puck = Freebie.create("Hockey Puck", 15, 3, 2) + sticker = Freebie.create("Sticker", 2, 2, 1) + + print("--------------------") + + print("Updating freebies...") + + mlb_jersey.value = 100 + mlb_jersey.update() + + print("--------------------") + + print("Deleting bad freebies...") + ugly_sweater.delete() + + print("--------------------") + + print("Finding company related to selected freebie...") + mlb_jersey_company = mlb_jersey.company() + basketball_company = basketball.company() + sticker_company = sticker.company() + hockey_puck_company = hockey_puck.company() + + print("--------------------") + + print("Finding dev related to selected freebie...") + + mlb_jersey_dev = mlb_jersey.dev() + + ipdb.set_trace() + + # print("Dropping companies table...") + # Company.drop_table() + + # print("--------------------") + + # print("Creating companies table...") + # Company.create_table() + + # print("--------------------") + + # print("Creating company 'test'...") + # test = Company.create("test", 2000) + # print("--------------------") + # print("Creating company 'test1'...") + # test1 = Company.create("test1", 2001) + # print("--------------------") + # print("Creating company 'test2'...") + # test2 = Company.create("test2", 2002) + # print("--------------------") + # print("Creating company 'test3'...") + # test3 = Company.create("test3", 2003) + # print("--------------------") + + # print("Updating 'test' to 'tester'...") + # test.name = "tester" + # test.update() + + # print("--------------------") + + # print("Finding company 'test' freebies...") + # test.freebies() + + # print("--------------------") + + # print("Finding company 'test' devs...") + # test.devs() + + # print("--------------------") + + # print("Deleting 'test2'...") + # test2.delete() + + # print("--------------------") + + # print("Returning oldest company...") + # Company.oldest_company() + + + + # DEV CLASS + + # print("Dropping 'devs' table...") + # Dev.drop_table() + + # print("--------------------") + + # print("Creating 'devs' table...") + # Dev.create_table() + + # print("--------------------") + + # print("Creating new_dev instance 'connor' and saving to database...") + # connor = Dev.create("Connor") + # print("--------------------") + # print("Creating new_dev instance 'simon' and saving to database...") + # simon = Dev.create("Simon") + # print("Creating new_dev instance 'will (be deleted)' and saving to database...") + # will = Dev.create("Will") + # print("--------------------") + # print("Creating new_dev instance 'steve' and saving to database...") + # steve = Dev.create("Steve") + + # print("--------------------") + + # print("Updated dev 'Steve' to 'Stephen'...") + # steve.name = "Stephen" + # steve.update() + + # print("--------------------") + + # print("Deleting 'will' from database...") + # will.delete() + + + # # FREEBIES TABLE + + # print("Dropping 'freebies' table...") + # Freebie.drop_table() + + # print("--------------------") + + # print("Creating 'freebies' table...") + # Freebie.create_table() + + # print("--------------------") + + # print("Creating Freebie instance 'goodies' and saving to database...") + # goodies = Freebie.create("goodies", 100.00, 1, 1) + # print("Creating Freebie instance 'tasty_food' and saving to database...") + # tasty_food = Freebie.create("tasty food", 14.99, 1, 2) + # print("Creating Freebie instance 'stickers' and saving to database...") + # stickers = Freebie.create("stickers", 2.50, 2, 1) + # print("Creating Freebie instance 'bad_freebie' and saving to database...") + # bad_freebie = Freebie.create("bad freebie", 0.01, 1, 1) + + # print("--------------------") + + # print("Updating freebies...") + # print("Original goodies value: ", goodies.value) + # goodies.value = 1000.00 + # goodies.update() + # print("Updated goodies value: ", goodies.value) + + # print("--------------------") + + # print("Deleting 'bad_freebie'...") + # bad_freebie.delete() + diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..45ac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nah)XBej3^j-~XmUNTs3=wwH}mlSP+(#Z S06OzN|HKK>Y`h>3!MOkst2W61 diff --git a/lib/migrations/README b/lib/migrations/README deleted file mode 100644 index 98e4f9c44..000000000 --- a/lib/migrations/README +++ /dev/null @@ -1 +0,0 @@ -Generic single-database configuration. \ No newline at end of file diff --git a/lib/migrations/env.py b/lib/migrations/env.py deleted file mode 100644 index c7aab9656..000000000 --- a/lib/migrations/env.py +++ /dev/null @@ -1,79 +0,0 @@ -from logging.config import fileConfig - -from sqlalchemy import engine_from_config -from sqlalchemy import pool - -from alembic import context - -# this is the Alembic Config object, which provides -# access to the values within the .ini file in use. -config = context.config - -# Interpret the config file for Python logging. -# This line sets up loggers basically. -if config.config_file_name is not None: - fileConfig(config.config_file_name) - -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -from models import Base -target_metadata = Base.metadata - -# other values from the config, defined by the needs of env.py, -# can be acquired: -# my_important_option = config.get_main_option("my_important_option") -# ... etc. - - -def run_migrations_offline() -> None: - """Run migrations in 'offline' mode. - - This configures the context with just a URL - and not an Engine, though an Engine is acceptable - here as well. By skipping the Engine creation - we don't even need a DBAPI to be available. - - Calls to context.execute() here emit the given string to the - script output. - - """ - url = config.get_main_option("sqlalchemy.url") - context.configure( - url=url, - target_metadata=target_metadata, - literal_binds=True, - dialect_opts={"paramstyle": "named"}, - ) - - with context.begin_transaction(): - context.run_migrations() - - -def run_migrations_online() -> None: - """Run migrations in 'online' mode. - - In this scenario we need to create an Engine - and associate a connection with the context. - - """ - connectable = engine_from_config( - config.get_section(config.config_ini_section), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) - - with connectable.connect() as connection: - context.configure( - connection=connection, target_metadata=target_metadata, render_as_batch=True, - ) - - with context.begin_transaction(): - context.run_migrations() - - -if context.is_offline_mode(): - run_migrations_offline() -else: - run_migrations_online() diff --git a/lib/migrations/script.py.mako b/lib/migrations/script.py.mako deleted file mode 100644 index 55df2863d..000000000 --- a/lib/migrations/script.py.mako +++ /dev/null @@ -1,24 +0,0 @@ -"""${message} - -Revision ID: ${up_revision} -Revises: ${down_revision | comma,n} -Create Date: ${create_date} - -""" -from alembic import op -import sqlalchemy as sa -${imports if imports else ""} - -# revision identifiers, used by Alembic. -revision = ${repr(up_revision)} -down_revision = ${repr(down_revision)} -branch_labels = ${repr(branch_labels)} -depends_on = ${repr(depends_on)} - - -def upgrade() -> None: - ${upgrades if upgrades else "pass"} - - -def downgrade() -> None: - ${downgrades if downgrades else "pass"} diff --git a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py b/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py deleted file mode 100644 index c191bb2f9..000000000 --- a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py +++ /dev/null @@ -1,39 +0,0 @@ -"""create companies, devs - -Revision ID: 5f72c58bf48c -Revises: 7a71dbf71c64 -Create Date: 2023-03-15 15:06:20.944586 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '5f72c58bf48c' -down_revision = '7a71dbf71c64' -branch_labels = None -depends_on = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('companies', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(), nullable=True), - sa.Column('founding_year', sa.Integer(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - op.create_table('devs', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('devs') - op.drop_table('companies') - # ### end Alembic commands ### diff --git a/lib/migrations/versions/7a71dbf71c64_create_db.py b/lib/migrations/versions/7a71dbf71c64_create_db.py deleted file mode 100644 index 23e0a655b..000000000 --- a/lib/migrations/versions/7a71dbf71c64_create_db.py +++ /dev/null @@ -1,24 +0,0 @@ -"""create db - -Revision ID: 7a71dbf71c64 -Revises: -Create Date: 2023-03-15 15:05:55.516631 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '7a71dbf71c64' -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade() -> None: - pass - - -def downgrade() -> None: - pass diff --git a/lib/models.py b/lib/models.py index 2681bee5a..a131ccd35 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,29 +1,801 @@ -from sqlalchemy import ForeignKey, Column, Integer, String, MetaData -from sqlalchemy.orm import relationship, backref -from sqlalchemy.ext.declarative import declarative_base +# lib/models.py -convention = { - "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", -} -metadata = MetaData(naming_convention=convention) +# ! Class.class_method +# ! Class#instance_method -Base = declarative_base(metadata=metadata) +# Relationships: +# Company has_many Freebies +# Company has_many Devs through Freebies -class Company(Base): - __tablename__ = 'companies' +# Dev has_many Freebies +# Dev has_many Companies through Freebies - id = Column(Integer(), primary_key=True) - name = Column(String()) - founding_year = Column(Integer()) +# Freebie belongs_to Company +# Freebie belongs_to Dev - def __repr__(self): - return f'' +# Therefore there is a Many to Many Relationship +# between the Company and Dev -class Dev(Base): - __tablename__ = 'devs' +import sqlite3 - id = Column(Integer(), primary_key=True) - name= Column(String()) +CONN = sqlite3.connect('./freebies.db') +CURSOR = CONN.cursor() - def __repr__(self): - return f'' + +class Company(): + + def __init__(self, name, founding_year, id=None): + self.name = name + self.founding_year = founding_year + self.id = id + + def get_name(self): + return self._name + + def set_name(self, name): + if type(name) == str and len(name) > 0: + self._name = name + else: + print("Improper name...") + raise Exception() + + def get_founding_year(self): + return self._founding_year + + def set_founding_year(self, founding_year): + if founding_year > 0: + self._founding_year = founding_year + else: + print("Improper founding year...") + raise Exception() + + # def get_id(self): + # return self.id + + # def set_id(self, id): + # if id > 0: + # self._id = id + # else: + # print("Improper ID...") + # raise Exception() + + # id = property(get_id, set_id) + + name = property(get_name, set_name) + founding_year = property(get_founding_year, set_founding_year) + + # ? ORM Table Methods + @classmethod + def create_table(cls): + create_table_sql = """ + CREATE TABLE IF NOT EXISTS companies ( + id INTEGER PRIMARY KEY, + name TEXT, + founding_year INTEGER + ) + """ + companies_table = CURSOR.execute(create_table_sql) + print("companies table: ==> ", companies_table) + + @classmethod + def drop_table(cls): + drop_table_sql = """ + DROP TABLE IF EXISTS companies + """ + dropped_companies_table = CURSOR.execute(drop_table_sql) + print("Dropped companies table: ==> ", dropped_companies_table) + + @classmethod + def new_from_companies_db(cls, row): + company_inst = cls(name=row[1], founding_year=row[2], id=row[0]) + print(f"Selected company instance is {company_inst.name} ==> {company_inst}") + return company_inst + + def save(self): + save_company_sql = """ + INSERT INTO companies (name, founding_year) + VALUES(?, ?) + """ + saved_company = CURSOR.execute(save_company_sql, (self.name, self.founding_year)) + CONN.commit() + self.id = CURSOR.lastrowid + + print(f"{self.name} row ==> ", saved_company) + + @classmethod + def create(cls, name, founding_year, id=None): + new_company_inst = cls(name, founding_year, id) + new_company_inst.save() + print(f"{name} instance ==>", new_company_inst) + return new_company_inst + + # ? Optional Useful Methods + def update(self): + update_company_sql = """ + UPDATE companies + SET name = ?, + founding_year = ? + WHERE id = ? + """ + updated_company = CURSOR.execute(update_company_sql, (self.name, self.founding_year, self.id)) + CONN.commit() + print("Updated company: ==> ", updated_company) + + def delete(self): + delete_company_sql = """ + DELETE FROM companies + WHERE id = ? + """ + deleted_company = CURSOR.execute(delete_company_sql, (self.id,)) + CONN.commit() + + print(f"{self.name} has been deleted ==>", deleted_company) + + # ? Relationship Methods + def freebies(self): + # Returns all the FREEBIE Instances associated with THIS Company + pass + + def devs(self): + # Returns all the DEV Instances associated with THIS Company + pass + + # ? Aggregate Methods + def give_freebie(self, dev, item_name, value): + pass + + @classmethod + def oldest_company(cls): + # Returns the COMPANY Instance of the oldest company + # ! Should not use any python searching, it will all be done in SQL + oldest_company_sql = """ + SELECT * FROM companies + ORDER BY founding_year + LIMIT 1 + """ + oldest_company_row = CURSOR.execute(oldest_company_sql).fetchone() + print(f"The oldest company is ==> {oldest_company_row}") + return cls.new_from_companies_db(oldest_company_row) + +# ! ================================ + + +class Dev(): + + def __init__(self, name, id=None): + self.name = name + self.id = id + + def get_name(self): + return self._name + + def set_name(self, name): + if type(name) == str and len(name) > 0: + self._name = name + else: + raise Exception("Improper name...") + + name = property(get_name, set_name) + + + # ? ORM Table Methods + @classmethod + def create_table(cls): + create_devs_sql = """ + CREATE TABLE IF NOT EXISTS devs ( + id INTEGER PRIMARY KEY, + name TEXT + ) + """ + devs_table = CURSOR.execute(create_devs_sql) + print("Devs table ==>", devs_table) + + @classmethod + def drop_table(cls): + drop_devs_sql = """ + DROP TABLE IF EXISTS devs + """ + dropped_devs_table = CURSOR.execute(drop_devs_sql) + print("Dropped devs table ==>", dropped_devs_table) + + @classmethod + def new_from_devs_db(cls, row): + dev_inst = cls(name=row[1], id=row[0]) + print(f"Selected company instance is {dev_inst.name} ==> {dev_inst}") + return dev_inst + + def save(self): + save_dev_sql = """ + INSERT INTO devs (name) + VALUES (?) + """ + saved_dev = CURSOR.execute(save_dev_sql, (self.name,)) + CONN.commit() + self.id = CURSOR.lastrowid + + print(f"Saved dev {self.name} ==> {saved_dev}") + print("SELF.ID", self.id) + + @classmethod + def create(cls, name, id=None): + new_dev_inst = cls(name, id) + new_dev_inst.save() + print("New dev instance ==>", new_dev_inst) + return new_dev_inst + + # ? Optional Useful Methods + def update(self): + update_dev_sql = """ + UPDATE devs + SET name = ? + WHERE id = ? + """ + updated_dev = CURSOR.execute(update_dev_sql, (self.name, self.id)) + CONN.commit() + print(f"{self.name} has been updated. ==> {updated_dev}") + + def delete(self): + delete_dev_sql = """ + DELETE FROM devs + WHERE id = ? + """ + deleted_dev = CURSOR.execute(delete_dev_sql, (self.id,)) + CONN.commit() + print(f"{self.name} has been deleted ==> {deleted_dev}") + + # ? Relationship Methods + def companies(self): + pass + + def freebies(self): + pass + + # ? Aggregate Methods + def recieved_one(self, item_name): + # Returns a BOOLEAN of whether or not THIS Dev has recieved + pass + + def give_away(self, other_dev, freebie): + # Gives this Freebie instance to other_dev + # + pass + +# ! ================================ + + +class Freebie(): + + # initializes with a: item_name, value, both belongs_to associations, id of None on init + def __init__(self, item_name, value, company_id, dev_id, id=None): + self.item_name = item_name + self.value = value + self.company_id = company_id + self.dev_id = dev_id + self.id = id + + def get_item_name(self): + return self._item_name + + def set_item_name(self, item_name): + if type(item_name) == str and len(item_name) > 0: + self._item_name = item_name + else: + raise Exception("Improper item_name for freebie.") + + + def get_value(self): + return self._value + + def set_value(self, value): + if value > 0: + self._value = value + else: + raise Exception("Improper value for freebie.") + + item_name = property(get_item_name, set_item_name) + value = property(get_value, set_value) + + + # ? ORM Table Methods + @classmethod + def create_table(cls): + create_freebies_sql = """ + CREATE TABLE IF NOT EXISTS freebies ( + id INTEGER PRIMARY KEY, + item_name TEXT, + value INTEGER, + company_id INTEGER, + dev_id INTEGER + ) + """ + created_freebies_table = CURSOR.execute(create_freebies_sql) + print(f"freebies table created ==> {created_freebies_table}") + + @classmethod + def drop_table(cls): + drop_freebies_sql = """ + DROP TABLE IF EXISTS freebies + """ + dropped_freebie_table = CURSOR.execute(drop_freebies_sql) + print(f"Freebies table dropped ==> {dropped_freebie_table}") + + @classmethod + def new_from_freebies_db(cls, row): + freebie_inst = cls(item_name=row[1], value=row[2], company_id=row[3], dev_id=row[4], id=row[0]) + print(f"Selected company instance is {freebie_inst.item_name} ==> {freebie_inst}") + return freebie_inst + + def save(self): + save_freebie_row_sql = """ + INSERT INTO freebies (item_name, value, company_id, dev_id) + VALUES (?, ?, ?, ?) + """ + saved_freebie = CURSOR.execute(save_freebie_row_sql, (self.item_name, self.value, self.company_id, self.dev_id)) + CONN.commit() + self.id = CURSOR.lastrowid + print(f"Saved {self.item_name} freebie ==> {saved_freebie}") + + @classmethod + def create(cls, item_name, value, company_id, dev_id): + new_freebie_inst = cls(item_name, value, company_id, dev_id) + new_freebie_inst.save() + return new_freebie_inst + + # ? Optional Useful Methods + def update(self): + update_freebie_sql = """ + UPDATE freebies + SET item_name = ?, + value = ?, + company_id = ?, + dev_id = ? + WHERE id = ? + """ + updated_freebie = CURSOR.execute(update_freebie_sql, (self.item_name, self.value, self.company_id, self.dev_id, self.id)) + CONN.commit() + print(f"{self.item_name} freebie row has been updated ==> {updated_freebie}") + + def delete(self): + delete_freebie_sql = """ + DELETE FROM freebies + WHERE id = ? + """ + deleted_freebie = CURSOR.execute(delete_freebie_sql, (self.id,)) + CONN.commit() + print(f"{self.item_name} freebie has been deleted ==> {deleted_freebie}") + + + # ? Relationship Methods + def company(self): + freebie_company_sql = """ + SELECT * FROM companies + WHERE id = ? + LIMIT 1 + """ + related_company = CURSOR.execute(freebie_company_sql, (self.company_id,)).fetchone() + related_comp_inst = Company.new_from_companies_db(related_company) + print(f"{related_comp_inst.name} instance ==> {related_comp_inst}") + return related_comp_inst + + + def dev(self): + freebie_dev_sql = """ + SELECT * FROM devs + WHERE id = ? + LIMIT 1 + """ + related_dev = CURSOR.execute(freebie_dev_sql, (self.dev_id,)).fetchone() + related_dev_inst = Dev.new_from_devs_db(related_dev) + print(f"{related_dev_inst.name} instance ==> {related_dev_inst}") + return related_dev_inst + + def print_details(self): + # Returns a String formatted like: + # {dev name} owns a {freebie item_name} from {company name} + print(Freebie.dev.name) + pass + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +# # lib/models.py + +# # ! Class.class_method +# # ! Class#instance_method + +# # Relationships: +# # Company has_many Freebies +# # Company has_many Devs through Freebies + +# # Dev has_many Freebies +# # Dev has_many Companies through Freebies + +# # Freebie belongs_to Company +# # Freebie belongs_to Dev + +# # Therefore there is a Many to Many Relationship +# # between the Company and Dev + +# import sqlite3 + +# CONN = sqlite3.connect('./freebies.db') +# CURSOR = CONN.cursor() + + +# class Company(): + +# def __init__(self, name, founding_year, id=None): +# self.name = name +# self.founding_year = founding_year +# self.id = id + +# # ? ORM Table Methods +# @classmethod +# def create_table(cls): +# create_sql = """ +# CREATE TABLE IF NOT EXISTS companies ( +# id INTEGER PRIMARY KEY, +# name TEXT, +# founding_year INTEGER +# ) +# """ +# companies_table = CURSOR.execute(create_sql) +# CONN.commit() + +# print(companies_table) +# return companies_table + +# @classmethod +# def drop_table(cls): +# drop_sql = """ +# DROP TABLE IF EXISTS companies +# """ +# dropped_companies_table = CURSOR.execute(drop_sql) +# CONN.commit() + +# print(dropped_companies_table) +# return dropped_companies_table + +# def save(self): +# save_sql = """ +# INSERT INTO companies (name, founding_year) +# VALUES (?, ?) +# """ +# CURSOR.execute(save_sql, (self.name, self.founding_year)) +# CONN.commit() + +# self.id = CURSOR.lastrowid + +# @classmethod +# def create(cls, name, founding_year, id=None): +# new_company = cls(name, founding_year, id) +# new_company.save() + +# print(new_company) +# return new_company + + +# # ? Optional Useful Methods +# def update(self): +# update_sql = """ +# UPDATE companies +# SET name = ?, +# founding_year = ? +# WHERE id = ? +# """ +# updated_company = CURSOR.execute(update_sql, (self.name, self.founding_year, self.id)) +# CONN.commit() +# print(updated_company) +# return updated_company + + +# def delete(self): +# delete_sql = """ +# DELETE FROM companies +# WHERE id = ? +# """ +# deleted_row = CURSOR.execute(delete_sql, (self.id,)) +# CONN.commit() +# print(deleted_row) +# return deleted_row + +# # ? Relationship Methods +# def freebies(self): +# # Returns all the FREEBIE Instances associated with THIS Company +# company_freebies_sql = """ +# SELECT * FROM freebies +# WHERE company_id = ? +# """ +# company_freebies = CURSOR.execute(company_freebies_sql, (self.id, )).fetchall() + +# print(company_freebies) +# return company_freebies + + +# def devs(self): +# # Returns all the DEV Instances associated with THIS Company + +# company_devs_sql = """ +# SELECT devs.id, devs.name FROM devs +# JOIN freebies +# ON devs.id = freebies.dev_id +# WHERE (freebies.company_id) = (?) +# """ + +# company_devs = CURSOR.execute(company_devs_sql, (self.id, )) +# print(company_devs) +# for dev in company_devs: +# print(dev) + + +# # ? Aggregate Methods +# def give_freebie(self, dev, item_name, value): +# pass + +# @classmethod +# def oldest_company(cls): +# # Returns the COMPANY Instance of the oldest company +# # ! Should not use any python searching, it will all be done in SQL +# oldest_comp_sql = """ +# SELECT * FROM companies +# ORDER BY founding_year DESC +# LIMIT 1 +# """ + +# oldest_company = CURSOR.execute(oldest_comp_sql).fetchone() +# print(oldest_company) +# return oldest_company + + + +# # ! ================================ + + +# class Dev(): + +# def __init__(self, name, id=None): +# self.name = name +# self.id = id + +# # ? ORM Table Methods +# @classmethod +# def create_table(cls): +# create_sql = """ +# CREATE TABLE IF NOT EXISTS devs ( +# id INTEGER PRIMARY KEY, +# name TEXT +# ) +# """ +# devs_table = CURSOR.execute(create_sql) +# CONN.commit() +# print(devs_table) +# return devs_table + +# @classmethod +# def drop_table(cls): +# drop_sql = """ +# DROP TABLE IF EXISTS devs +# """ +# dropped_devs_table = CURSOR.execute(drop_sql) +# CONN.commit() +# print(dropped_devs_table) +# return dropped_devs_table + +# def save(self): +# save_sql = """ +# INSERT INTO devs (name) +# VALUES (?) +# """ +# new_dev = CURSOR.execute(save_sql, (self.name,)) +# CONN.commit() +# self.id = CURSOR.lastrowid + +# # print(new_dev) +# return new_dev + +# @classmethod +# def create(cls, name, id=None): +# new_dev_inst = cls(name, id) +# new_dev_inst.save() + +# print(new_dev_inst) +# return new_dev_inst + +# # ? Optional Useful Methods +# def update(self): +# update_sql = """ +# UPDATE devs +# SET name = ? +# WHERE id = ? +# """ +# updated_dev = CURSOR.execute(update_sql, (self.name, self.id)) +# CONN.commit() + +# print(updated_dev) +# return updated_dev + +# def delete(self): +# delete_sql = """ +# DELETE FROM devs +# WHERE id = ? +# """ +# deleted_dev = CURSOR.execute(delete_sql, (self.id,)) +# CONN.commit() + +# print(deleted_dev) +# return deleted_dev + +# # ? Relationship Methods +# def companies(self): +# pass + +# def freebies(self): +# pass + +# # ? Aggregate Methods +# def recieved_one(self, item_name): +# # Returns a BOOLEAN of whether or not THIS Dev has recieved +# pass + +# def give_away(self, other_dev, freebie): +# # Gives this Freebie instance to other_dev +# # +# pass + +# # ! ================================ + + +# class Freebie(): + +# # initializes with a: item_name, value, both belongs_to associations, id of None on init +# def __init__(self, name, value, company_id, dev_id, id=None): +# self.name = name +# self.value = value +# self.company_id = company_id +# self.dev_id = dev_id +# self.id = id + +# # ? ORM Table Methods +# @classmethod +# def create_table(cls): +# create_sql = """ +# CREATE TABLE IF NOT EXISTS freebies ( +# id INTEGER PRIMARY KEY, +# name TEXT, +# value REAL, +# company_id INTEGER, +# dev_id INTEGER +# ) +# """ +# freebies_table = CURSOR.execute(create_sql) +# CONN.commit() + +# print(freebies_table) +# return freebies_table + +# @classmethod +# def drop_table(cls): +# drop_sql = """ +# DROP TABLE IF EXISTS freebies +# """ +# dropped_table = CURSOR.execute(drop_sql) +# CONN.commit() + +# print(dropped_table) +# return dropped_table + +# def save(self): +# save_sql = """ +# INSERT INTO freebies (name, value, company_id, dev_id) +# VALUES (?, ?, ?, ?) +# """ +# new_freebie_inst = CURSOR.execute(save_sql, (self.name, self.value, self.company_id, self.dev_id)) +# CONN.commit() +# self.id = CURSOR.lastrowid + +# print(new_freebie_inst) +# return new_freebie_inst + +# @classmethod +# def create(cls, name, value, company_id, dev_id, id=None): +# new_freebie_inst = cls(name, value, company_id, dev_id, id) +# new_freebie_inst.save() + +# print(new_freebie_inst) +# return new_freebie_inst + +# # ? Optional Useful Methods +# def update(self): +# update_sql = """ +# UPDATE freebies +# SET name = ?, +# value = ?, +# company_id = ?, +# dev_id = ? +# WHERE id = ? +# """ +# updated_freebie = CURSOR.execute(update_sql, (self.name, self.value, self.company_id, self.dev_id, self.id)) +# CONN.commit() + +# print(updated_freebie) +# return updated_freebie + + +# def delete(self): +# delete_sql = """ +# DELETE FROM freebies +# WHERE id = ? +# """ +# deleted_freebie = CURSOR.execute(delete_sql, (self.id,)) +# CONN.commit() + +# print(deleted_freebie) +# return deleted_freebie + +# # ? Relationship Methods +# def company(self): +# pass + +# def dev(self): +# pass + +# def print_details(self): +# # Returns a String formatted like: +# # {dev name} owns a {freebie item_name} from {company name} +# pass \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..cc920120c 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,59 @@ #!/usr/bin/env python3 +from models import Company, Dev, Freebie # Script goes here! +print("Seeding DB 🌱...") + +# ? Start By Dropping all the tables +print("Dropping Tables...") + +# ? Then recreate the tables +print("Creating Tables...") + +# ? Create some Test Companies +print("Creating Companies...") + + +# ? Create some Test Devs +print("Creating Devs...") + +# ? Create some Test Freebies +print("Creating Freebies...") + + +print("DONE!") + + + + + + + + + + + +# #!/usr/bin/env python3 +# from models import Company, Dev, Freebie + +# # Script goes here! +# print("Seeding DB 🌱...") + +# # ? Start By Dropping all the tables +# print("Dropping Tables...") + +# # ? Then recreate the tables +# print("Creating Tables...") + +# # ? Create some Test Companies +# print("Creating Companies...") + + +# # ? Create some Test Devs +# print("Creating Devs...") + +# # ? Create some Test Freebies +# print("Creating Freebies...") + + +# print("DONE!") \ No newline at end of file