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

Paper - Lauren Covington #64

Open
wants to merge 5 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Ada Library Information
*.ali

# Added w Audrey 5/26/21, 8:43am
venv/
Binary file added __pycache__/video_store.cpython-39.pyc
Binary file not shown.
167 changes: 162 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,169 @@
import requests
from video_store import VideoStore

URL = "http://127.0.0.1:5000"
BACKUP_URL = "https://retro-video-store-api.herokuapp.com"

def main():
print("WELCOME TO RETRO VIDEO STORE")
pass
def mark_intro():
print("\n**************************\n")

def list_menu_options():
menu = {
"1": "List all customers in Blockbuster's database",
"2": "Create a customer account in the Blockbuster database",
"3": "Select a customer account from the database",
"4": "Update a customer's account information",
"5": "Delete a customer account from the database",

if __name__ == "__main__":
main()
"6": "List all videos in Blockbuster's database",
"7": "Add a video to the Blockbuster database",
"8": "Select a video from the database",
"9": "Update a video's due date",
"10": "Delete a video from the database",

"11": "Check out a video to a customer",
"12": "Check in a video from a customer",

"13": "List all menu options",
"14": "Quit"
}

mark_intro()
print("WELCOME TO BLOCKBUSTER'S DATABASE\n")
print("Below is a list of actions you can take: \n")
for option in menu:
print(f"Option {option}. {menu[option]}")
print()
return menu

def select_action(menu, video_store):
choices_offered = menu.keys()
employee_choice = None

while employee_choice not in choices_offered:
print("What action are you looking to perform? ")
employee_choice = input("Use your keypad to make a selection: ")
if employee_choice in ["4", "5"] and video_store.selected_customer == None:
print("You have to select a customer account that exists before performing any work on it.")
print("Choose a customer by pressing 3. ")
employee_choice = 3
return employee_choice

def run_cli(play=True):
video_store = VideoStore(url="https://retro-video-store-api.herokuapp.com")
choices = list_menu_options()

while play == True:
employee_choice = select_action(choices, video_store)
if employee_choice in ["1","2","3","4","5"]:
video_store.show_selected_customer()
elif employee_choice in ["6","7","8","9","10"]:
video_store.show_selected_video()

if employee_choice == '1':
mark_intro()
for customer in video_store.list_customers():
print(customer)
elif employee_choice == '2':
print("We'll create a new customer account, then. ")
name = input("Enter the customer's name: ")
phone = input("Enter the customer's phone number: ")
postal_code = input("Enter the customer's postal code: ")
for_display = video_store.create_customer(name=name, phone=phone, postal_code=postal_code)
mark_intro()
print('FOR DISPLAY: ', for_display) # {'id': 116}
print("Customer created. Their account ID is: ", for_display["id"])
elif employee_choice == '3':
choice_param = input("Want to select a customer by name or ID? ")
if choice_param == "name":
name = input("Enter the name of the customer whose account you want to work with: ")
print("Customer account info you selected: ", video_store.get_customer(name=name))
elif choice_param == "id":
# print("LOOOOOK: ", type(video_store.selected_customer)) # <class 'NoneType'>
cli_id = input("Which customer ID would you like to select? ")

if (cli_id.isnumeric()) == False:
print("Use digits, please.")
elif (cli_id.isnumeric()) == True:
cli_id = int(cli_id)
print("Customer account info you selected: ", video_store.get_customer(id=cli_id))
else:
print("Could not select. Please enter the name or ID of an existing customer. ")
elif employee_choice == '4':
print(f"Then, let's update the account information inside this collection: {video_store.selected_customer}. ")
name = input("Enter the new name for the customer account: ")
phone = input("Enter the customer's new phone number: ")
postal_code = input("Enter the customer's new postal code: ")
for_update = video_store.update_customer(name=name, phone=phone, postal_code=postal_code)
mark_intro()
print("Here's the new account information: ", for_update) # Here's the new account information: {'id': 198, 'name': 'Maxine Waters',
# 'phone': '333-333-0000', 'postal_code': '20022',
# 'registered_at': 'Sat, 29 May 2021 01:56:21 GMT', 'videos_checked_out_count': 0}
elif employee_choice == '5':
video_store.delete_customer()
mark_intro()
print("This customer account has been deleted. See below: ")
mark_intro()
for customer in video_store.list_customers():
print(customer)
elif employee_choice == '6':
mark_intro()
for video in video_store.list_videos():
print(video)
elif employee_choice == '7':
print("We'll add a new video to the database, then. ")
title = input("Enter the video's title: ")
release_date = input("Enter the video's release date (format: YYYY-MM-DD): ")
total_inventory = input("Enter the number of copies that'll be added to the stock: ")
available_inventory = total_inventory

for_display = video_store.create_video(title=title, release_date=release_date, total_inventory=total_inventory,\
available_inventory=available_inventory)
mark_intro()
print("Video addition details: ", for_display) # {'id': 238}
elif employee_choice == '8':
choice_param = input("Want to select a video by title or ID? ")
if choice_param == "title":
title = input("Enter the title of the video you want to work with: ")
print("Video info you selected: ", video_store.get_video(title=title))
elif choice_param == "id": # address case issue later

Choose a reason for hiding this comment

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

You can use lower() to convert the case of the string:

Suggested change
elif choice_param == "id": # address case issue later
elif choice_param.lower() == "id": # address case issue later

Also, if you'd made the case match the prompt it would have been less of an issue (you asked for ID but only accepted id).

cli_id = input("Which video ID would you like to select? ")
if (cli_id.isnumeric()) == False:
print("Use digits, please.")
elif (cli_id.isnumeric()) == True:
cli_id = int(cli_id)
print("Video info you selected: ", video_store.get_video(id=cli_id)) # ... = {'id': 237, 'release_date': 'Sun, 12 May 2002 00:00:00 GMT',
# 'title': 'The Matrix', 'total_inventory': 5}
else:
print("Could not select. Please enter the title or ID of an existing video. ")
elif employee_choice == '9': # NOT WORKING 5/27/21, 8:27pm

Choose a reason for hiding this comment

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

I think you may have been struggling here because you misunderstood the requirements.

The goal was to update the video (the title, quantity, etc) and not to update a rental.

(I'm not sure updating rentals was supported by the API and would be much less straightforward if it was.)

print(f"Then, let's update the due date inside this collection: {video_store.selected_video}. ")
due_date = input("Enter the new due date for the video using format YYYY-MM-DD: ")
for_update = video_store.update_rental(due_date=due_date) # ERROR THROWN HERE. linked to line 107 in video_store.py
print("HEYOOOOO: ", for_update)
mark_intro()
print("Here's the video's new due date: ", for_update) # should be {"due_date": whatever the given date format is}
elif employee_choice == '10':
video_store.delete_video()
mark_intro()
print("This video has been deleted from the database. See below: ")
mark_intro()
for video in video_store.list_videos():
print(video)
elif employee_choice == '11': # CHECK-OUT PROCESS; call check_out func from video_store.py
pass
elif employee_choice == '12': # CHECK-IN PROCESS; call check_out func from video_store.py
pass
elif employee_choice == '13':
list_menu_options()
elif employee_choice == '14':
play=False
print("\nThanks for using the Video Store CLI. Byeeeeee! ")
mark_intro()


run_cli()

# change 'main' to '__run_cli__' and main() to run_cli() ?
#if __name__ == "__main__": # func holding menu options dict used to be called main()
# main()
147 changes: 147 additions & 0 deletions video_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import requests
import datetime

# - Customer create, edit, delete --- check
# - Video create, edit, delete --- check
# - List customers --- check
# - List videos --- check
# - Check out video to customer --- *missing
# - Check in video --- *missing

class VideoStore:
def __init__(self, url="https://retro-video-store-api.herokuapp.com", selected_video=None, selected_customer=None, selected_rental=None):
self.url = url
self.selected_video = selected_video
self.selected_customer = selected_customer
self.selected_rental = selected_rental # operate through this attribute or video?

def create_customer(self, name="Default Customer Name", postal_code="Default Postal Code", phone="Default Phone Number",\
register_at=str(datetime.datetime.now()), videos_checked_out_count=0):
relevant_params = {
"name": name,
"postal_code": postal_code,
"phone": phone,
"register_at": register_at,
"videos_checked_out_count": videos_checked_out_count
}
response = requests.post(self.url + "/customers", json=relevant_params)

Choose a reason for hiding this comment

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

It's a best practice to check the status code before you use a response. (Even if the API generally gives well formed responses.)

return response.json() # example return value: {'id': 116}

def create_video(self, title="Default Movie Title", release_date="Default Date", total_inventory="Default Total Inventory",\
available_inventory="Default Available Inventory"):
relevant_params = {
"title": title,
"release_date": release_date,
"total_inventory": total_inventory,
"available_inventory": available_inventory
}
response = requests.post(self.url + "/videos", json=relevant_params)
return response.json()

def list_customers(self):
response = requests.get(self.url + "/customers")
return response.json()

def list_videos(self):
response = requests.get(self.url + "/videos")
return response.json()

def get_customer(self, name=None, id=None):
for customer in self.list_customers():
if customer:
if customer["name"] == name:
# customer = {'id': 136, 'name': 'Ada Lovelace', 'phone': '333-333-3333', 'postal_code': '88890',
# 'registered_at': 'Fri, 28 May 2021 20:13:32 GMT', 'videos_checked_out_count': 1}
id = customer["id"]
self.selected_customer = customer
elif customer["id"] == id:
name = customer["name"]
self.selected_customer = customer
if self.selected_customer == None:
return "Could not find a customer by that name or ID."

response = requests.get(self.url + f"/customers/{id}")
return response.json()

def get_video(self, title=None, id=None):
for video in self.list_videos():
if video:
if video["title"] == title:
id = video["id"]
self.selected_video = video
elif video["id"] == id:
title = video["title"]
self.selected_video = video
if self.selected_video == None:
return "Could not find a video by that title or ID."
response = requests.get(self.url + f"/videos/{id}")
return response.json()

def update_customer(self, name=None, postal_code=None, phone=None):
if not name:
name = self.selected_customer["name"]
if not postal_code:
postal_code = self.selected_customer["postal_code"]
if not phone:
phone = self.selected_customer["phone_number"]

relevant_params = {
"name": name,
"postal_code": postal_code,
"phone": phone
}

response = requests.put(
self.url + f"/customers/{self.selected_customer['id']}",
json=relevant_params)
self.selected_customer = response.json()
print("Here's what we have now: ", self.selected_customer) # self.selected_customer = {'id': 198, 'name': 'Maxine Waters', 'phone': '222-333-4444',
# 'postal_code': '90211', 'registered_at': 'Sat, 29 May 2021 01:56:21 GMT',
# 'videos_checked_out_count': 0}
return response.json()

def update_rental(self, due_date=None): # Work on rental instance, not video(?)
if not due_date:
due_date = (self.selected_rental["check_out_date"] + (datetime.timedelta(days=7)))
relevant_param = {"due_date": due_date}
response = requests.put(self.url + f"/customers/{self.selected_customer['id']}/rentals", # issue is here: TypeError: 'NoneType' object is not subscriptable
json=relevant_param)
# can't see any of this bc path above is wrong
print("Here's what we have now: ", response)
self.selected_rental = response.json()
return response.json()

def delete_customer(self):
response = requests.delete(self.url + f"/customers/{self.selected_customer['id']}")
self.selected_customer = None
return response.json()

def delete_video(self):
response = requests.delete(self.url + f"/videos/{self.selected_video['id']}")
self.selected_video = None
return response.json()

def show_selected_customer(self):
if self.selected_customer:
print(f"Customer with ID {self.selected_customer['id']} is currently selected\n")

def show_selected_video(self):
if self.selected_video:
print(f"Video with ID {self.selected_video['id']} is currently selected\n")

# incomplete/incorrect logic 5/27/21, 9:03pm
def check_out(self, customer_id=None, video_id=None, check_out_date=None):
relevant_params = {
"customer_id": customer_id,
"video_id": video_id,
"check_out_date": check_out_date
}
if self.selected_customer == None:
return "Could not find a customer by that name or ID."
if self.selected_video == None:
return "Could not find a video by that title or ID."
Comment on lines +133 to +142

Choose a reason for hiding this comment

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

To check out a movie all you need is the customer and the video ids.

You can use your selected customer and video for that:

Suggested change
def check_out(self, customer_id=None, video_id=None, check_out_date=None):
relevant_params = {
"customer_id": customer_id,
"video_id": video_id,
"check_out_date": check_out_date
}
if self.selected_customer == None:
return "Could not find a customer by that name or ID."
if self.selected_video == None:
return "Could not find a video by that title or ID."
def check_out(self):
if self.selected_customer == None:
return "Could not find a customer by that name or ID."
if self.selected_video == None:
return "Could not find a video by that title or ID."
relevant_params = {
"customer_id": self.selected_customer["id"],
"video_id": self.selected_video["id"],
}

response = requests.post(self.url + "/rentals/check-out", json=relevant_params)
return response.json()

def check_in(self):
pass
Comment on lines +146 to +147

Choose a reason for hiding this comment

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

Check in winds up looking very similar to check out:

Suggested change
def check_in(self):
pass
def check_in(self):
if self.selected_customer == None:
return "Could not find a customer by that name or ID."
if self.selected_video == None:
return "Could not find a video by that title or ID."
relevant_params = {
"customer_id": self.selected_customer["id"],
"video_id": self.selected_video["id"],
}
response = requests.post(self.url + "/rentals/check-in", json=relevant_params)
return response.json()