-
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
Paper - Lauren Covington #56
base: main
Are you sure you want to change the base?
Conversation
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.
Great job!
I pointed out some ways to DRY up your code but overall everything looks good!
phone_number = db.Column(db.String(12)) | ||
register_at = db.Column(db.DateTime) | ||
videos_checked_out_count = db.Column(db.Integer, default=0) | ||
rental = db.relationship('Rental', backref='rental', lazy=True) |
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 should be rentals
since it's a collection of them:
rental = db.relationship('Rental', backref='rental', lazy=True) | |
rentals = db.relationship('Rental', backref='rental', lazy=True) |
"phone": self.phone_number, | ||
"postal_code": self.postal_code, | ||
"registered_at": check_registration, | ||
"videos_checked_out_count": self.videos_checked_out_count |
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.
You can use the relationship so that videos_checked_out_count
is just the length of the rentals
that haven't been checked in:
"videos_checked_out_count": self.videos_checked_out_count | |
"videos_checked_out_count": len([rental for rental in self.rentals if not rental.check_in_date]) |
check_out_date = db.Column('check_out_date', db.DateTime, default=datetime.now()) | ||
renter = db.relationship('Customer', backref='rentals', lazy=True, foreign_keys=customer_id) | ||
video = db.relationship('Video', backref='rentals', lazy=True, foreign_keys=video_id) | ||
|
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.
It can be helpful to add a check in date:
check_in_date = db.Column('check_out_date', db.DateTime, default=datetime.now()) |
total_inventory = db.Column(db.Integer) | ||
available_inventory = db.Column(db.Integer) | ||
renter = db.relationship('Rental', backref='customer', lazy=True) | ||
|
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.
Like with Customer
you can compute available_inventory
with a relationship:
rentals = db.relationship('Rental', back_populates='video', lazy=True) |
"title": self.title, | ||
"release_date": self.release_date, | ||
"total_inventory": self.total_inventory, | ||
"available_inventory": self.available_inventory |
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.
Like with Customer
the available_inventory
is just the total inventory minus length of the rentals
that haven't been checked in:
"available_inventory": self.available_inventory | |
"available_inventory": self.total_inventory - len([rental for rental in self.rentals if not rental.check_in_date]) |
single_customer = Customer.query.get(customer_id) | ||
if not single_customer: | ||
return make_response({"details": f"There is no customer in the database with ID #{customer_id}"}, 404) |
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.
You can simplify this with get_or_404
.
single_customer = Customer.query.get(customer_id) | |
if not single_customer: | |
return make_response({"details": f"There is no customer in the database with ID #{customer_id}"}, 404) | |
single_customer = Customer.query.get_or_404(customer_id) |
new_customer = Customer(name=request_body["name"], | ||
postal_code=request_body["postal_code"], | ||
phone_number=request_body["phone"], | ||
register_at=datetime.now()) |
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.
Style/formatting:
new_customer = Customer(name=request_body["name"], | |
postal_code=request_body["postal_code"], | |
phone_number=request_body["phone"], | |
register_at=datetime.now()) | |
new_customer = Customer( | |
name=request_body["name"], | |
postal_code=request_body["postal_code"], | |
phone_number=request_body["phone"], | |
register_at=datetime.now() | |
) |
if not videos: | ||
return jsonify(list_of_videos) |
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.
You don't actually need to do this check since the loop will leave the list empty if there is nothing in it.
request_body = request.get_json() | ||
|
||
# convert str to datetime object | ||
request_body["release_date"] = datetime.fromisoformat(request_body["release_date"]) |
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.
Nice find with fromisoformat
😄
customer_entity.append(entity) | ||
dictified_ce["id"] = entity.customer_id | ||
dictified_ce["name"] = entity.name | ||
dictified_ce["phone"] = entity.phone_number | ||
dictified_ce["postal_code"] = entity.postal_code | ||
dictified_ce["registered_at"] = entity.register_at | ||
dictified_ce["videos_checked_out_count"] = entity.videos_checked_out_count | ||
hold_dictified_entities.append(dictified_ce) |
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.
You can use to_json
instead of producing this dictionary manually (the fields match up):
customer_entity.append(entity) | |
dictified_ce["id"] = entity.customer_id | |
dictified_ce["name"] = entity.name | |
dictified_ce["phone"] = entity.phone_number | |
dictified_ce["postal_code"] = entity.postal_code | |
dictified_ce["registered_at"] = entity.register_at | |
dictified_ce["videos_checked_out_count"] = entity.videos_checked_out_count | |
hold_dictified_entities.append(dictified_ce) | |
customer_entity.append(entity) | |
hold_dictified_entities.append(entity.to_json()) |
No description provided.