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
Since we can add entries, we need to be able to read them again later. And we're eventually going to get tired of paging through them, so we'll want to be able to search through our entries. Luckily both of these things are easy to add!
New terms
-
.where()
- method that lets us filter our.select()
results -
.contains()
- method that specifies the input should be inside the specified field
Links
We used the select method on the model,
before, to find records.
0:00
We're gonna use that again, to find all of
our diary entries.
0:03
I think we should show the newest ones
first,
0:06
since I probably want to see things that I
logged today or yesterday, by default.
0:08
To do that, we're gonna have to use the
order by method.
0:12
Let's check it out in Workspaces.
0:15
So, before we can work with our entries,
obviously,
0:17
we need to get them back out of the
database.
0:20
We have to be able to look at them.
0:23
Or we have to have them, in order to look
at them, right?
0:24
So, in our view_entries function,
0:27
let's say that entries is equal to
entry.select, get all the entries.
0:30
And then we're gonna order them by the
timestamp.
0:37
Okay, so, we get all of our entries, and
we,
0:43
we're gonna use the variable name entries,
because well, that's what they are.
0:48
And then we're gonna sort them so
0:54
that the oldest ones show up last, so our
newest one comes first.
0:55
So, now we have them.
0:59
Let's print them out.
1:01
For entry in entries.
1:03
Before I print one out, though, I wanna
set up the timestamp.
1:05
So, timestamp is gonna be entry.timestamp.
1:09
And we're gonna do strftime.
1:14
Remember, we wanna make a string out of
this.
1:16
And let's do %A %B %d,
1:19
%Y %I:%M%p.
1:26
Wow, what is all this stuff?
1:32
Well, so, the %A here, that is the weekday
name, so like Wednesday.
1:33
The %B here, is the month name, so,
January.
1:42
The d, is for the number, so 22.
1:47
And then the Y is the year, so, 2014.
1:51
The I is the hour.
1:55
The M is the minute.
1:58
And this is the hour on a 12 hour clock.
2:00
So, noon starts over, it goes to one 1AM.
2:02
M is for the minutes.
2:07
And this lowercase p is either AM or PM.
2:07
In case you're wondering, yes, I had to
look those all up.
2:12
So, now that we have that let's print out
the timestamp.
2:15
And then let's print an equal sign, for
2:22
however many characters are in the
timestamp.
2:26
So, equal sign times timestamp.
2:29
If there's 15 characters in timestamp,
we'll print out 15 equals signs.
2:31
Kind of a cool shortcut.
2:36
Sorry not time, timestamp, times len
timestamp.
2:38
Let's print out the entry.content.
2:44
And then, let's print a little bit of a
menu here, so
2:47
they can go to the next entry, or so they
can quit back to the main menu,
2:50
because, that's probably what they wanna
do, right?
2:54
So, we're gonna print n for next entry.
2:56
And then print q, return to main menu.
3:02
And you know what?
We're gonna make this n be our,
3:08
default step, so let's capitalize that.
3:11
All right, so, this is cool.
3:17
That's what we're doing.
3:19
So, now, let's do next action, and let's
do an input of,
3:20
we said action before, let's say action
again.
3:26
And our choices here, are n and q.
3:30
Actually, you know what?
If we're gonna do the cap n there,
3:36
we'll do the lower case n here.
3:38
Okay.
3:41
So, n and q, and then we give them a bit
of a spot there, and
3:41
then we lowercase, and let's strip that
too, just in case.
3:47
So, if next action is equal to q.
3:52
Then we want to, oops, not quit, we want
to break.
3:58
We wanna break our for loop.
4:01
Otherwise, we're just gonna move on to the
next thing in the for loop.
4:03
All right, so, let's test this out.
4:07
Let's come down here to dot slash diary.
4:10
And you know what, I only have that one
entry in here, so
4:15
I'm gonna add another entry real quick.
4:17
So we'll just say, It's great to be able
to store.
4:20
So.
4:25
Many.
4:26
Entries.
4:27
All right, Ctrl+D.
4:28
Yep, go ahead and save that.
4:31
Cool, back to our menu.
4:33
All right, let's view, our previous
entries.
4:35
So, here's the one that I just typed.
4:39
It's great to be able to store so many
entries.
4:40
If I press a key, it goes and
4:42
shows me my next one, which is the working
with databases isn't really great.
4:44
If I hit another key, it's gonna back to
the menu, because,
4:49
there's not anything else to see, but I'm
gonna hit q, to go back there as well.
4:51
So, that takes you back to the menu, and I
can quit again.
4:55
So, that's all great.
4:59
But you know what, I'd like to be able to
search through, my entries.
5:02
I got a lot of, maybe, and when I get done
with this,
5:08
I've got, you know, 100 entries.
5:10
I wanna be able to search through all of
these.
5:13
So let's add a search option.
5:15
Let's go back to our menu, down here.
5:18
And let's add a new thing.
5:22
Let's say S, and we'll say search_entries.
5:24
Okay?
5:30
And this, of course, means that we have to
add a new function, called search_entries.
5:32
I'm gonna add it right here after
view_entries,
5:36
cuz I have a feeling they're gonna be
pretty similar.
5:38
So, search_entries.
5:41
Before I actually define all that, though,
let's come up here to view_entries.
5:44
I'm gonna add in, let's make this one a
little bit smarter,
5:47
and let's make it to where we, by default,
we view all the entries.
5:51
But if we have a search query,
5:55
then we filter all the entries by the
search query before we loop through them.
5:57
Because, looping through and
6:01
printing out all the entries, is looping
through and printing out all the entries.
6:02
Right?
6:06
So, let's say search_query, equals None,
by default.
6:07
And then, inside of here, after we get the
entries.
6:14
Let's say if search_query, so a search
query came in.
6:19
Then now let's say, entries equals
entries.
6:24
So, our original query, getting everything
out and ordered.
6:27
And then let's say where, which what let's
us do the filter.
6:31
Entry.content.contains, the search_query.
6:35
All right, so, where let's us do it's not
a sub query, but it's filtering.
6:43
So we can make sure that all of the
entries that we select,
6:49
have our search_query in their content
attribute.
6:52
If this was written in an actual SQL, it
would look something like,
6:56
SELECT * FROM entry WHERE content LIKE,
and then you'd have a quote, and
7:00
a percent sign, the search query string,
whatever that is, another percent sign,
7:05
another quote, and then ORDER BY timestamp
DESC.
7:10
What this does though, is, it allows us to
cha, change our entry's,
7:15
query only if we actually have something
that we want to search for.
7:19
Otherwise, we get the four results, just
like always.
7:24
Okay.
So, that's updating view_entries.
7:27
Now we need to do search_entries.
7:32
I almost forgot about this one.
7:34
So anyway, search_entries, it's just a
function, and we're gonna say,
7:37
search entires for a string.
7:41
That could probably be better written,
but, that's all I'm gonna say for now.
7:46
And we're actually just gonna call
view_entries, but we're gonna ask for
7:50
input, of a search_query.
7:54
[BLANK_AUDIO]
7:56
I think that covers this idea of using
view_entries as much as possible.
8:00
So we're just calling the view_entries
function, but
8:04
we get some input from the user first.
8:08
So, let's try this.
8:11
Come back down here.
8:13
Clear and diary.
8:15
All right, so, I wanna view.
8:19
And, okay.
8:22
So, I used the word store, only in that
first post.
8:24
So, let's say search, and I'm gonna search
for the word, store.
8:28
And there's that one.
8:33
And if I, hit enter, there shouldn't be
another entry,
8:34
I should go straight back to the menu.
8:37
And I do, coz there's not anything else, I
only said store, in that one.
8:39
So, that's awesome, I'm amazed that this
works, it works great, actually.
8:44
Our apps pretty solid.
8:48
And we're still at a pretty small number
of lines, it's at 87 lines,
8:49
I might hit my 100 after all.
8:54
It's great that we are able to write a
view_entries functions smartly, so
8:56
we could use it again, for searching
through entries.
8:59
I'm not 100% happy with the way some of
our data is displayed in the app,
9:01
but, let's worry about that later.
9:05
For now, I wanna see about deleting
entries, that I don't need or
9:07
want any more.
9:10
You need to sign up for Treehouse in order to download course files.
Sign up