Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scissors: Mai Truong #55

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn 'app:create_app()'
33 changes: 33 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import os
from dotenv import load_dotenv


db = SQLAlchemy()
migrate = Migrate()
load_dotenv()

def create_app(test_config=None):
app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

if test_config is None:
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI")
else:
app.config["TESTING"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_TEST_DATABASE_URI")

# Import models here for Alembic setup
from app.models.customer import Customer
from app.models.video import Video
from app.models.rental import Rental

db.init_app(app)
migrate.init_app(app, db)

# Register Blueprints here
from .routes import video_bp
app.register_blueprint(video_bp)
from .routes import customer_bp
app.register_blueprint(customer_bp)
from .routes import rental_bp
app.register_blueprint(rental_bp)

return app
Empty file added app/models/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions app/models/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from flask import current_app
from app import db
from sqlalchemy.orm import relationship


class Customer(db.Model):
__tablename__ = "customer"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
postal_code = db.Column(db.Integer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is fine but it causes the code to fail 2 smoke tests (there was inconsistency around the postal_code in the tests).

phone_number = db.Column(db.String)
registered_at = db.Column(db.DateTime, nullable=True)
videos_checked_out_count = db.Column(db.Integer, default=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull videos_checked_out_count from the rental table rather than storing it directly in the customer table?

video = relationship("Rental", back_populates="customer")

def to_json(self):
customer_dict = {
"id": self.id,
"name": self.name,
"postal_code": self.postal_code,
"phone": self.phone_number,
"registered_at": self.registered_at,
"videos_checked_out_count": self.videos_checked_out_count
}
return customer_dict
23 changes: 23 additions & 0 deletions app/models/rental.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import current_app
from app import db
from sqlalchemy.orm import relationship
from datetime import datetime, timedelta


class Rental(db.Model):
__tablename__ = "rental"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), primary_key=True)
video_id = db.Column(db.Integer, db.ForeignKey('video.id'), primary_key=True)
due_date = db.Column(db.DateTime, nullable=True)
video = relationship("Video", back_populates="customer")
customer = relationship("Customer", back_populates="video")


# def to_json(self):
# rental_dict = {
# "customer_id": self.customer_id,
# "video_id": self.video_id,
# "due_date": self.due_date,
# }
# return rental_dict
22 changes: 22 additions & 0 deletions app/models/video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import current_app
from app import db
from sqlalchemy.orm import relationship


class Video(db.Model):
__tablename__ = "video"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String)
release_date = db.Column(db.DateTime, nullable=True)
total_inventory = db.Column(db.Integer)
available_inventory = db.Column(db.Integer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull available_inventory from the rental table rather than storing it directly in this table?

customer = relationship("Rental", back_populates="video")

def to_json(self):
video_dict = {
"video_id": self.id,
"title": self.title,
"release_date": self.release_date,
"total_inventory": self.total_inventory,
}
return video_dict
Loading