From 97856a84cc7252af4e845c3af17dcc25d5b1c67e Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:05:04 +0300 Subject: [PATCH 1/6] added app.py --- app.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 000000000..7299dcfcd --- /dev/null +++ b/app.py @@ -0,0 +1,16 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate + + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///freebie_tracker.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +db = SQLAlchemy(app) +migrate = Migrate(app, db) + +from models import Company, Dev, Freebie + +if __name__ == '__main__': + app.run(debug=True) From 49753a1951330a5f959098358f3d26e0c9b4d936 Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:05:29 +0300 Subject: [PATCH 2/6] added the three models freebie dev and company --- models.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 models.py diff --git a/models.py b/models.py new file mode 100644 index 000000000..1a86afbe1 --- /dev/null +++ b/models.py @@ -0,0 +1,68 @@ +from app import db +from sqlalchemy import ForeignKey +from sqlalchemy.orm import relationship + + +class Company(db.Model): + __tablename__ = 'companies' + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String, nullable=False) + founding_year = db.Column(db.Integer, nullable=False) + + + freebies = relationship('Freebie', backref='company') + + + devs = relationship('Dev', secondary='freebies', backref='companies') + + @classmethod + def oldest_company(cls): + return cls.query.order_by(cls.founding_year).first() + + def give_freebie(self, dev, item_name, value): + + freebie = Freebie(dev_id=dev.id, company_id=self.id, item_name=item_name, value=value) + db.session.add(freebie) + db.session.commit() + + +class Dev(db.Model): + __tablename__ = 'devs' + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String, nullable=False) + + + freebies = relationship('Freebie', backref='dev') + + + companies = relationship('Company', secondary='freebies', backref='devs') + + def received_one(self, item_name): + + return any(freebie.item_name == item_name for freebie in self.freebies) + + def give_away(self, dev, freebie): + + if freebie.dev_id == self.id: + freebie.dev_id = dev.id + db.session.commit() + + +class Freebie(db.Model): + __tablename__ = 'freebies' + + id = db.Column(db.Integer, primary_key=True) + item_name = db.Column(db.String, nullable=False) + value = db.Column(db.Integer, nullable=False) + + dev_id = db.Column(db.Integer, ForeignKey('devs.id'), nullable=False) + company_id = db.Column(db.Integer, ForeignKey('companies.id'), nullable=False) + + + dev = relationship('Dev', backref='freebies') + company = relationship('Company', backref='freebies') + + def print_details(self): + return f"{self.dev.name} owns a {self.item_name} from {self.company.name}" From b876dd06464e6214b4fac3c7c75a26e6853819e8 Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:05:37 +0300 Subject: [PATCH 3/6] added seed file --- seed.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 seed.py diff --git a/seed.py b/seed.py new file mode 100644 index 000000000..7fe6b3892 --- /dev/null +++ b/seed.py @@ -0,0 +1,26 @@ +from app import db +from models import Company, Dev + +def seed_data(): + + dev1 = Dev(name="Alice") + dev2 = Dev(name="Bob") + + company1 = Company(name="TechCorp", founding_year=2000) + company2 = Company(name="DevWorks", founding_year=2010) + + db.session.add(dev1) + db.session.add(dev2) + db.session.add(company1) + db.session.add(company2) + db.session.commit() + + + company1.give_freebie(dev1, "Laptop", 1500) + company2.give_freebie(dev2, "Tablet", 800) + company2.give_freebie(dev1, "Smartwatch", 300) + + db.session.commit() + +if __name__ == "__main__": + seed_data() From 6b62bb0b0410c2cfa51811d747b0b783af3a27a1 Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:05:49 +0300 Subject: [PATCH 4/6] added debug file --- debug.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 debug.py diff --git a/debug.py b/debug.py new file mode 100644 index 000000000..e9c3eeff8 --- /dev/null +++ b/debug.py @@ -0,0 +1,16 @@ +from app import app, db +from models import Dev, Company, Freebie + +with app.app_context(): + + dev = Dev.query.filter_by(name="Alice").first() + print(dev.freebies) + print(dev.companies) + + company = Company.query.filter_by(name="TechCorp").first() + company.give_freebie(dev, "Headphones", 100) + print(dev.freebies) + + + freebie = Freebie.query.first() + print(freebie.print_details()) From fd4ebdecfadfcc47f41049f5693b4bb8e8f4a6af Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:06:05 +0300 Subject: [PATCH 5/6] migrations --- instance/freebie_tracker.db | Bin 0 -> 24576 bytes migrations/README | 1 + migrations/alembic.ini | 50 ++++++++ migrations/env.py | 113 ++++++++++++++++++ migrations/script.py.mako | 24 ++++ .../e7b3a26cea78_create_freebies_table.py | 50 ++++++++ 6 files changed, 238 insertions(+) create mode 100644 instance/freebie_tracker.db create mode 100644 migrations/README create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/e7b3a26cea78_create_freebies_table.py diff --git a/instance/freebie_tracker.db b/instance/freebie_tracker.db new file mode 100644 index 0000000000000000000000000000000000000000..eba642d6187a513131f8f822093ced6360bf9e19 GIT binary patch literal 24576 zcmeI&O^?z*7{Kv%c@rXwmklSfbJ>IjW8#vCi7^pLcO@dY1x@zU6c|h+Eh}$vkGt_p z=|}L>c=V=IptvQl7jNW0VbWoyGkxYa)58SX7Y%nR#b7k{oT(^lk2GD^UJ0RTT0-Td z%3+GDLXg9xhlP8t3GIphBb)oJB_96Na@pMXLofSB6-fvnfB*srAb2?TdP|W1gFqXtp|{`JvHx zEL8PWdbaO)QhcmhwKvsh>)aX6`EB@URkge~V+`Y|{Lztq^I~BZGz2*j zv0C1~(`hHs=5#W~O(B-bA0O+t>BZyLfv*ek$b;9__pH*+rw>Lmzwi2=>}%%%vP_cY!L^fHi)ogdHs@i{I zV+8x^b5BGPx# literal 0 HcmV?d00001 diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..0e0484415 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Single-database configuration for Flask. diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..ec9d45c26 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,50 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic,flask_migrate + +[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 + +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + +[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/migrations/env.py b/migrations/env.py new file mode 100644 index 000000000..4c9709271 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,113 @@ +import logging +from logging.config import fileConfig + +from flask import current_app + +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. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + + +def get_engine(): + try: + # this works with Flask-SQLAlchemy<3 and Alchemical + return current_app.extensions['migrate'].db.get_engine() + except (TypeError, AttributeError): + # this works with Flask-SQLAlchemy>=3 + return current_app.extensions['migrate'].db.engine + + +def get_engine_url(): + try: + return get_engine().url.render_as_string(hide_password=False).replace( + '%', '%%') + except AttributeError: + return str(get_engine().url).replace('%', '%%') + + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option('sqlalchemy.url', get_engine_url()) +target_db = current_app.extensions['migrate'].db + +# 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 get_metadata(): + if hasattr(target_db, 'metadatas'): + return target_db.metadatas[None] + return target_db.metadata + + +def run_migrations_offline(): + """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=get_metadata(), literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + conf_args = current_app.extensions['migrate'].configure_args + if conf_args.get("process_revision_directives") is None: + conf_args["process_revision_directives"] = process_revision_directives + + connectable = get_engine() + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=get_metadata(), + **conf_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 000000000..2c0156303 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${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(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/e7b3a26cea78_create_freebies_table.py b/migrations/versions/e7b3a26cea78_create_freebies_table.py new file mode 100644 index 000000000..59adcf1b3 --- /dev/null +++ b/migrations/versions/e7b3a26cea78_create_freebies_table.py @@ -0,0 +1,50 @@ +"""Create freebies table + +Revision ID: e7b3a26cea78 +Revises: +Create Date: 2025-05-25 17:34:45.563928 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'e7b3a26cea78' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('companies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('founding_year', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('devs', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('freebies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('item_name', sa.String(), nullable=False), + sa.Column('value', sa.Integer(), nullable=False), + sa.Column('dev_id', sa.Integer(), nullable=False), + sa.Column('company_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.ForeignKeyConstraint(['dev_id'], ['devs.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('freebies') + op.drop_table('devs') + op.drop_table('companies') + # ### end Alembic commands ### From 2d9ebe0d1ff52f1f4216c75a9b22e58da5b8da05 Mon Sep 17 00:00:00 2001 From: Salman Date: Sun, 25 May 2025 18:08:19 +0300 Subject: [PATCH 6/6] readme addition --- README.md | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/README.md b/README.md index b598b7784..1f5aa6fd9 100644 --- a/README.md +++ b/README.md @@ -180,3 +180,245 @@ data). - `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 + + + +Freebie Tracker +This is a Python-based application built using Flask and SQLAlchemy to track freebies given by companies to developers. The application uses object-oriented principles to define models for Company, Dev (developer), and Freebie (item received by a developer). + +Features +A company can give freebies to developers. + +A developer can receive and give away freebies. + +Each freebie links a developer and a company. + +The system allows for tracking which freebies a developer has received from which company. + +Table of Contents +Installation + +Usage + +Models + +Company Model + +Dev Model + +Freebie Model + +Methods + +Company Methods + +Dev Methods + +Freebie Methods + +Database Setup + +Testing + +Installation +Follow the steps below to set up the application on your local machine. + +Prerequisites +Python 3.x + +Flask + +SQLAlchemy + +Flask-Migrate + +Steps to Install +Clone the repository: + +bash +Copy code +git clone https://github.com/your-repository/freebie-tracker.git +Navigate into the project folder: + +bash +Copy code +cd freebie-tracker +Create a virtual environment: + +bash +Copy code +python3 -m venv .venv +Activate the virtual environment: + +On macOS/Linux: + +bash +Copy code +source .venv/bin/activate +On Windows: + +bash +Copy code +.venv\Scripts\activate +Install the dependencies: + +bash +Copy code +pip install -r requirements.txt +Usage +To run the application: + +Set the environment variable: + +bash +Copy code +export FLASK_APP=app.py +export FLASK_ENV=development +Run the application: + +bash +Copy code +flask run +The application should now be running on http://127.0.0.1:5000/. + +Models +Company Model +This model represents a company in the system and tracks the freebies it gives out. + +python +Copy code +class Company(db.Model): + __tablename__ = 'companies' + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String, nullable=False) + founding_year = db.Column(db.Integer, nullable=False) + + freebies = relationship('Freebie', backref='company') # One-to-many with Freebie + devs = relationship('Dev', secondary='freebies', backref='companies') # Many-to-many with Dev +Attributes: + +id: Unique identifier for the company. + +name: Name of the company. + +founding_year: Year the company was founded. + +Relationships: + +One-to-many relationship with Freebie. + +Many-to-many relationship with Dev through Freebie. + +Dev Model +This model represents a developer who collects freebies from companies. + +python +Copy code +class Dev(db.Model): + __tablename__ = 'devs' + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String, nullable=False) + + freebies = relationship('Freebie', backref='dev') # One-to-many with Freebie + companies = relationship('Company', secondary='freebies', backref='devs') # Many-to-many with Company +Attributes: + +id: Unique identifier for the developer. + +name: Name of the developer. + +Relationships: + +One-to-many relationship with Freebie. + +Many-to-many relationship with Company through Freebie. + +Freebie Model +This model represents a freebie given by a company to a developer. + +python +Copy code +class Freebie(db.Model): + __tablename__ = 'freebies' + id = db.Column(db.Integer, primary_key=True) + item_name = db.Column(db.String, nullable=False) + value = db.Column(db.Integer, nullable=False) + + dev_id = db.Column(db.Integer, ForeignKey('devs.id'), nullable=False) + company_id = db.Column(db.Integer, ForeignKey('companies.id'), nullable=False) + + dev = relationship('Dev', backref='freebies') + company = relationship('Company', backref='freebies') +Attributes: + +id: Unique identifier for the freebie. + +item_name: Name of the freebie. + +value: Value of the freebie. + +dev_id: Foreign key to the devs table. + +company_id: Foreign key to the companies table. + +Relationships: + +One-to-one relationship with Dev and Company. + +Methods +Company Methods +give_freebie(dev, item_name, value): This method creates a new Freebie and associates it with a developer and company. + +oldest_company(): This class method returns the company with the earliest founding year. + +Dev Methods +received_one(item_name): This method checks if a developer has received a freebie with a given item name. + +give_away(dev, freebie): This method allows a developer to transfer a freebie to another developer. + +Freebie Methods +print_details(): This method returns a string formatted as: + +css +Copy code +"{dev name} owns a {freebie item_name} from {company name}" +Database Setup +Migrations +Initialize Migration Folder: + +bash +Copy code +flask db init +Generate Migrations: + +bash +Copy code +flask db migrate -m "Added Freebie table and relationships" +Apply Migrations: + +bash +Copy code +flask db upgrade +Seed Data: +Run seed.py to add initial Company, Dev, and Freebie data for testing. + +Testing +Testing the Relationships: +Use python debug.py to verify relationships between Dev, Company, and Freebie. + +Example to test if a developer has received a freebie: + +python +Copy code +dev = Dev.query.filter_by(name="Alice").first() +print(dev.freebies) # Check the freebies the dev has received +Test Methods: + +Ensure that methods like give_freebie, received_one, and give_away work as expected by calling them in debug.py or during test execution. + +Contributing +Feel free to fork the repository and submit pull requests. Any issues or improvements can be reported in the issues section. + +License +This project is licensed under the MIT License - see the LICENSE file for details. +