-
Notifications
You must be signed in to change notification settings - Fork 177
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
base: main
Are you sure you want to change the base?
Changes from all commits
2821ca8
cd39b84
4b8e5cc
a5b87fb
9358182
61d09b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn 'app:create_app()' |
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 |
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) | ||
phone_number = db.Column(db.String) | ||
registered_at = db.Column(db.DateTime, nullable=True) | ||
videos_checked_out_count = db.Column(db.Integer, default=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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).