Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Give users the ability to update and delete books in the database.
Challenges
- How could you combine the two menu functions to create one menu function?
- Is there a way to have the user tell you which values they want to change and then only prompt them to change those values instead of asking about each one?
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Our app is looking great, but
0:00
I think we can add a few more features to
make this even more useful for our users.
0:02
What happens if they make
a mistake when entering a book or
0:08
what if the price changes?
0:11
Right now you can't do anything about it.
0:13
Also, what if you want to remove a book?
0:16
Let's introduce these
two features to our app.
0:20
We can add them as a part
of our search feature.
0:23
When someone searches for a book,
we can give them the option to edit,
0:26
delete or return to the main menu.
0:30
Let's handle this like our other menu,
by giving it its own function.
0:33
To keep similar things together,
0:37
let's create our sub menu
right below our main menu.
0:39
I can actually remove these, There we go,
0:45
submenu, and
we can actually copy this here.
0:53
And if you want, you could actually
make a single menu function and
1:00
then pass in values for the options and
the list of numbers and all that jazz.
1:05
All right, so we only have three options
and it's no longer programming books,
1:15
we can also get rid of that.
1:20
One will be edit, two will be delete,
1:22
and three will be return to main menu.
1:27
And then we can get rid of four and
five, so we only have three options, and
1:33
then we can get rid of five,
and switch it to three.
1:38
Wonderful.
1:42
Now we can create the structure for
each response in our app.
1:43
So let's jump down to app.py.
1:47
Okay, so we have search for book.
1:56
And then instead of this input,
we're going to give them a submenu.
1:58
So I'm gonna call it sub_choice = submenu.
2:04
And then, if sub_choice
2:10
== '1' this will be edit,
2:14
pass for right now.
2:19
And then, elif, sub_ choice = '2'.
2:23
This will be delete.
2:30
And I'll still put pass for right now.
2:32
And then we actually don't need to do
one for three because of our loop,
2:34
it'll automatically jump
it back to the top.
2:38
Now it's time to tackle edit.
2:42
Let's think about what we need
to happen here a bit first.
2:45
We'll need to print out the current value
for each book's title, date, etc., so
2:48
the user can see what
the value currently is.
2:53
Then we'll need to ask them
to update the information.
2:56
And we'll need to repeat this for
each column, the title, author,
3:00
price, date, all of them.
3:05
And the keyword here is repeat.
3:08
We're going to need to do this task for
each value.
3:10
So that means we're going to need
a function to handle this task so
3:15
that we can call it for each column.
3:19
I'm going to place this new function
after our cleaning functions and
3:22
before the CSV function.
3:26
Right here.
3:31
I'm gonna call it edit_check, and inside,
3:37
we're going to get the column_name.
3:41
This is because for
the price and for the date,
3:46
we're gonna need to do something different
compared to the author and title.
3:48
And then we want the current_value.
3:54
Okay.
3:59
And inside of our edit_check function,
let's print out the column names so
4:01
they know which column
they're dealing with.
4:04
I make this an F string and
I start it on a new line,
4:08
and I give a couple stars,
I'm gonna do EDIT and
4:13
then column_name, And
a few stars, close it out.
4:17
Cool.
4:24
Now we need to show the current value for
each column.
4:26
But remember if it's the price or
the published date,
4:29
we're going to need to show it how we
want to receive the information too.
4:32
So for instance,
let's do if column_name == 'Price'.
4:36
We're going to print,
An f string of the current value.
4:43
Current Value, and
we're going to need the current_value and
4:52
it needs to be divided by 100.
4:59
So it's now back into that 1099
price instead of the price in cents,
5:03
cuz that's how we want
to receive the price.
5:08
So if we model it here for the user,
5:12
then it kinda shows what we want
the information to come in as too.
5:14
So let's do the same thing for the date.
5:18
Okay, so if the column_name == 'Date',
we still want to print an f string,
5:28
and this is going to say same thing,
Current Value.
5:36
And to translate the date
into the format we want,
5:42
we're going to need to use
current_value.strftime to convert it.
5:46
And oops,
5:52
we need to make sure to use double quotes
because we're inside of single quotes.
5:52
So we want to make sure we
use double quotes here.
5:57
And to get the month converted to
the name of the month, we want %B,
6:00
and then, we need the day, that's %d,
and then we need the year,
6:06
that's %Y to get the full year, Okay?
6:11
And then, we need an else.
6:18
So if it's any of the other columns,
we're just going to print the value.
6:20
String, do a return, Current Value.
6:26
And that's just going to be current_value.
6:32
Great.
6:36
Okay.
6:37
Now we're also going to need to do
something a little special if it's
6:39
the published date or the price because we
need to use our clean date and clean price
6:43
functions again, so that we can turn their
string input into the correct format.
6:48
So, I'm gonna just to help break
it up a little bit, I'm gonna add
6:54
a space here to help differentiate
between these kind of two sections.
6:58
So we printed out the information so
they can see it and now we're gonna
7:02
get into actually asking them for
their input and interacting with it.
7:06
So if column_name == 'Date' or
7:10
if the column_name == 'Price',
7:14
then we're going to want
to do some stuff in here.
7:18
And it's going to be inside of a while
loop because we have to work with
7:23
the clean date functions and
we need to keep asking them for
7:27
information until they give it
to us in the correct format.
7:30
I'm just gonna put pass inside for
right now.
7:33
Okay, and then on the outside, we're gonna
need an else, and we can just return
7:36
if it's not one of these two columns, that
means it's either the author or the title.
7:41
And so we can just ask for the input and
7:47
we can just return their
response right away.
7:49
Because it's in a string format and
that's all it needs to be.
7:52
So 'What would you like
to change the value?
7:58
' Okay, so
now let's handle inside of our while loop.
8:05
We need to ask the same question.
8:11
So I'm gonna call it changes.
8:13
And I'm actually just gonna
use the exact same question.
8:15
There we go.
8:22
Okay.
8:23
And then we need to call clean date for
the date and clean price for the price.
8:23
And now we want to break it up then
into either the date or the price.
8:27
So column_name == 'Date'.
8:31
Then we're going to do something
very similar to what we did before.
8:35
And say the changes are now equal
8:40
to the clean version of changes.
8:45
And if the type(changes) == datetime.date,
8:50
then we can return changes,
8:57
cuz remember return
automatically stops a loop.
9:00
And that's why we're using while
true instead of using a variable to
9:07
control our loop.
9:11
Okay, so that takes care of the date and
9:13
we're gonna do essentially
the exact same thing for price.
9:16
== 'Price' to changes
9:21
= clean_price.
9:27
And we're gonna pass in changes.
9:31
And then if type(changes) is
9:33
an integer then we can return our changes.
9:38
And let's save.
9:47
Awesome.
9:49
Now let's pop down into the app.pi and
call our new function.
9:52
Here we are in edit.
10:00
Now remember when we're updating a value,
10:02
we need to set the current
value equal to something else.
10:05
And we've already grabbed
our book previously.
10:09
We have it here in
a variable called the_book.
10:11
So we just need to set
the_book.title equal to and
10:15
then we'll call our edit_check('Title')
to pass in the column name.
10:19
And then we can actually just copy
this because it's the same thing,
10:27
the_book.title to pass
in the current value.
10:31
And we'll do that for each column.
10:34
So the_book.author, and
I'm gonna copy that.
10:36
Goes edit_check('Author'),
and then the_book.author.
10:42
And the next one the_book,
10:48
and then we'll do .published_date,
10:52
copy that, = edit_check('Date'),
10:56
and then fill in the_book.published_date.
11:02
And lastly the_book.price,
11:08
Copy that, and =edit_check('Price').
11:13
And then the_book.price.
11:19
And then below here let's
do a print(session.dirty)
11:22
to see if the changes are being grabbed
by our session, just to make sure.
11:27
So let's make sure we saved.
11:36
And I'm gonna pull this up so
you can see more of our console.
11:39
And let's run the file.
11:43
Oops, I have an IndentationError, line 29.
11:46
Let's go check it out.
11:50
Popping up to line 29.
11:52
Yes.
11:59
Somehow we got menu inside of, oops,
I don't know how that happened.
12:01
We don't need that.
12:06
There we go.
12:07
That should be much better.
12:09
There we go.
12:16
Okay.
12:17
So let's search for a book.
12:18
Let's do book 11 cuz we
know that's my silly one.
12:20
Jethro's Favorites, let's edit it.
12:24
So I'm gonna do one.
12:27
Okay, so
the current value is Jethro's Favorites.
12:29
What would you like to
change the value to?
12:31
And here in my console,
I can copy and paste.
12:33
I know in some consoles it
doesn't always let you.
12:36
I'm just gonna leave that.
12:39
I'm gonna change this
value to Jethro Amendola.
12:40
What do I want to change the date to?
12:45
Let's change the date to, actually,
let's test this, 23rd 2003.
12:46
It should give me an error.
12:52
Cool, and that should ask me again.
12:54
What would you like to
change the value to?
12:57
Awesome.
12:58
Now let's give it a real date.
13:00
Let's do June 14, 2020.
13:01
And now we're on Price, perfect.
13:06
So it took it.
13:08
Let's give it an incorrect price here.
13:09
So let's do $4 but with the dollar symbol.
13:11
And, yep, gave us that error.
13:14
Let's press enter to try again.
13:17
And let's just keep it at 5.99.
13:19
Cool.
13:22
And you can see we have
our identity set here,
13:23
which means that it is
captured by session.
13:26
And we have Jethro's Favorites,
we can see the change to the author,
13:29
the change to the published date,
and the price is the same.
13:32
Amazing job.
13:35
So our changes are being added to session.
13:38
But to make the changes reflected in the
database, let's make sure to commit them.
13:40
And then let's print out a nice message so
13:50
that the user knows that
our book was updated.
13:53
And then let's do a time.sleep, 1.5.
13:58
Save, and amazing,
our edit functions are working.
14:05
Now let's get to delete.
14:10
This one is 1,000 times easier, I promise.
14:13
Are you ready?
14:16
We already know we still
have access to the book.
14:18
So we'll do a session.delete(the_book),
14:21
session.commit, and
14:27
then we can just copy our message here so
14:30
they get the same message,
except this will say deleted.
14:35
And that's it.
14:44
That's the code.
14:47
Let me save this to Python app.pi.
14:49
Let's do 3 and 11,
since that's our silly book.
14:53
And scroll up so
you can see that it's Jethro's Favorites.
15:01
And let's do 2 for delete, book deleted.
15:07
And then we can check by running,
view all books and
15:11
we only have ten books in the database,
amazing.
15:14
Pat yourself on the back, you've tackled
a ton of code so far in this course.
15:19
Don't forget to take time to
review what you've written and
15:24
maybe go line by line and try to
explain to yourself what's happening.
15:27
This is a great way to get used
to thinking about how your code
15:31
is working and
is a skill used in technical interviews.
15:34
So use every opportunity to practice.
15:38
Don't forget, run your Git commands,
add, commit and
15:41
push your changes up to your repo,
15:45
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up