1 00:00:00,150 --> 00:00:03,910 We used the select method on the model, before, to find records. 2 00:00:03,910 --> 00:00:06,990 We're gonna use that again, to find all of our diary entries. 3 00:00:06,990 --> 00:00:08,830 I think we should show the newest ones first, 4 00:00:08,830 --> 00:00:12,940 since I probably want to see things that I logged today or yesterday, by default. 5 00:00:12,940 --> 00:00:15,520 To do that, we're gonna have to use the order by method. 6 00:00:15,520 --> 00:00:17,120 Let's check it out in Workspaces. 7 00:00:17,120 --> 00:00:20,700 So, before we can work with our entries, obviously, 8 00:00:20,700 --> 00:00:23,280 we need to get them back out of the database. 9 00:00:23,280 --> 00:00:24,410 We have to be able to look at them. 10 00:00:24,410 --> 00:00:27,340 Or we have to have them, in order to look at them, right? 11 00:00:27,340 --> 00:00:30,620 So, in our view_entries function, 12 00:00:30,620 --> 00:00:37,060 let's say that entries is equal to entry.select, get all the entries. 13 00:00:37,060 --> 00:00:43,829 And then we're gonna order them by the timestamp. 14 00:00:43,829 --> 00:00:48,307 Okay, so, we get all of our entries, and we, 15 00:00:48,307 --> 00:00:54,270 we're gonna use the variable name entries, because well, that's what they are. 16 00:00:54,270 --> 00:00:55,320 And then we're gonna sort them so 17 00:00:55,320 --> 00:00:59,320 that the oldest ones show up last, so our newest one comes first. 18 00:00:59,320 --> 00:01:01,190 So, now we have them. 19 00:01:01,190 --> 00:01:01,760 Let's print them out. 20 00:01:03,050 --> 00:01:05,256 For entry in entries. 21 00:01:05,256 --> 00:01:09,680 Before I print one out, though, I wanna set up the timestamp. 22 00:01:09,680 --> 00:01:14,620 So, timestamp is gonna be entry.timestamp. 23 00:01:14,620 --> 00:01:16,356 And we're gonna do strftime. 24 00:01:16,356 --> 00:01:19,177 Remember, we wanna make a string out of this. 25 00:01:19,177 --> 00:01:26,085 And let's do %A %B %d, 26 00:01:26,085 --> 00:01:31,113 %Y %I:%M%p. 27 00:01:32,760 --> 00:01:33,760 Wow, what is all this stuff? 28 00:01:33,760 --> 00:01:41,390 Well, so, the %A here, that is the weekday name, so like Wednesday. 29 00:01:42,630 --> 00:01:47,838 The %B here, is the month name, so, January. 30 00:01:47,838 --> 00:01:51,700 The d, is for the number, so 22. 31 00:01:51,700 --> 00:01:55,690 And then the Y is the year, so, 2014. 32 00:01:55,690 --> 00:01:58,920 The I is the hour. 33 00:01:58,920 --> 00:02:00,000 The M is the minute. 34 00:02:00,000 --> 00:02:02,460 And this is the hour on a 12 hour clock. 35 00:02:02,460 --> 00:02:07,140 So, noon starts over, it goes to one 1AM. 36 00:02:07,140 --> 00:02:07,990 M is for the minutes. 37 00:02:07,990 --> 00:02:11,279 And this lowercase p is either AM or PM. 38 00:02:12,300 --> 00:02:14,860 In case you're wondering, yes, I had to look those all up. 39 00:02:15,890 --> 00:02:20,350 So, now that we have that let's print out the timestamp. 40 00:02:22,620 --> 00:02:26,380 And then let's print an equal sign, for 41 00:02:26,380 --> 00:02:29,830 however many characters are in the timestamp. 42 00:02:29,830 --> 00:02:31,780 So, equal sign times timestamp. 43 00:02:31,780 --> 00:02:36,770 If there's 15 characters in timestamp, we'll print out 15 equals signs. 44 00:02:36,770 --> 00:02:38,510 Kind of a cool shortcut. 45 00:02:38,510 --> 00:02:41,960 Sorry not time, timestamp, times len timestamp. 46 00:02:44,110 --> 00:02:47,860 Let's print out the entry.content. 47 00:02:47,860 --> 00:02:50,000 And then, let's print a little bit of a menu here, so 48 00:02:50,000 --> 00:02:54,010 they can go to the next entry, or so they can quit back to the main menu, 49 00:02:54,010 --> 00:02:55,650 because, that's probably what they wanna do, right? 50 00:02:56,720 --> 00:03:02,116 So, we're gonna print n for next entry. 51 00:03:02,116 --> 00:03:08,857 And then print q, return to main menu. 52 00:03:08,857 --> 00:03:11,961 And you know what? We're gonna make this n be our, 53 00:03:11,961 --> 00:03:15,080 default step, so let's capitalize that. 54 00:03:17,470 --> 00:03:19,580 All right, so, this is cool. 55 00:03:19,580 --> 00:03:20,520 That's what we're doing. 56 00:03:20,520 --> 00:03:26,185 So, now, let's do next action, and let's do an input of, 57 00:03:26,185 --> 00:03:30,517 we said action before, let's say action again. 58 00:03:30,517 --> 00:03:36,777 And our choices here, are n and q. 59 00:03:36,777 --> 00:03:38,772 Actually, you know what? If we're gonna do the cap n there, 60 00:03:38,772 --> 00:03:40,180 we'll do the lower case n here. 61 00:03:41,280 --> 00:03:41,950 Okay. 62 00:03:41,950 --> 00:03:47,810 So, n and q, and then we give them a bit of a spot there, and 63 00:03:47,810 --> 00:03:51,810 then we lowercase, and let's strip that too, just in case. 64 00:03:52,870 --> 00:03:56,870 So, if next action is equal to q. 65 00:03:58,160 --> 00:04:00,620 Then we want to, oops, not quit, we want to break. 66 00:04:01,690 --> 00:04:03,720 We wanna break our for loop. 67 00:04:03,720 --> 00:04:07,260 Otherwise, we're just gonna move on to the next thing in the for loop. 68 00:04:07,260 --> 00:04:10,670 All right, so, let's test this out. 69 00:04:10,670 --> 00:04:15,200 Let's come down here to dot slash diary. 70 00:04:15,200 --> 00:04:17,800 And you know what, I only have that one entry in here, so 71 00:04:17,800 --> 00:04:20,790 I'm gonna add another entry real quick. 72 00:04:20,790 --> 00:04:25,680 So we'll just say, It's great to be able to store. 73 00:04:25,680 --> 00:04:26,385 So. 74 00:04:26,385 --> 00:04:27,185 Many. 75 00:04:27,185 --> 00:04:28,581 Entries. 76 00:04:28,581 --> 00:04:31,580 All right, Ctrl+D. 77 00:04:31,580 --> 00:04:33,860 Yep, go ahead and save that. 78 00:04:33,860 --> 00:04:35,610 Cool, back to our menu. 79 00:04:35,610 --> 00:04:39,210 All right, let's view, our previous entries. 80 00:04:39,210 --> 00:04:40,730 So, here's the one that I just typed. 81 00:04:40,730 --> 00:04:42,870 It's great to be able to store so many entries. 82 00:04:42,870 --> 00:04:44,910 If I press a key, it goes and 83 00:04:44,910 --> 00:04:49,190 shows me my next one, which is the working with databases isn't really great. 84 00:04:49,190 --> 00:04:51,870 If I hit another key, it's gonna back to the menu, because, 85 00:04:51,870 --> 00:04:55,550 there's not anything else to see, but I'm gonna hit q, to go back there as well. 86 00:04:55,550 --> 00:04:59,390 So, that takes you back to the menu, and I can quit again. 87 00:04:59,390 --> 00:05:00,840 So, that's all great. 88 00:05:02,200 --> 00:05:08,520 But you know what, I'd like to be able to search through, my entries. 89 00:05:08,520 --> 00:05:10,930 I got a lot of, maybe, and when I get done with this, 90 00:05:10,930 --> 00:05:13,532 I've got, you know, 100 entries. 91 00:05:13,532 --> 00:05:15,830 I wanna be able to search through all of these. 92 00:05:15,830 --> 00:05:18,400 So let's add a search option. 93 00:05:18,400 --> 00:05:21,150 Let's go back to our menu, down here. 94 00:05:22,650 --> 00:05:24,450 And let's add a new thing. 95 00:05:24,450 --> 00:05:28,655 Let's say S, and we'll say search_entries. 96 00:05:30,800 --> 00:05:32,100 Okay? 97 00:05:32,100 --> 00:05:36,850 And this, of course, means that we have to add a new function, called search_entries. 98 00:05:36,850 --> 00:05:38,640 I'm gonna add it right here after view_entries, 99 00:05:38,640 --> 00:05:40,280 cuz I have a feeling they're gonna be pretty similar. 100 00:05:41,640 --> 00:05:44,270 So, search_entries. 101 00:05:44,270 --> 00:05:47,880 Before I actually define all that, though, let's come up here to view_entries. 102 00:05:47,880 --> 00:05:51,000 I'm gonna add in, let's make this one a little bit smarter, 103 00:05:51,000 --> 00:05:55,910 and let's make it to where we, by default, we view all the entries. 104 00:05:55,910 --> 00:05:57,700 But if we have a search query, 105 00:05:57,700 --> 00:06:01,280 then we filter all the entries by the search query before we loop through them. 106 00:06:01,280 --> 00:06:02,490 Because, looping through and 107 00:06:02,490 --> 00:06:06,320 printing out all the entries, is looping through and printing out all the entries. 108 00:06:06,320 --> 00:06:07,520 Right? 109 00:06:07,520 --> 00:06:13,250 So, let's say search_query, equals None, by default. 110 00:06:14,720 --> 00:06:19,070 And then, inside of here, after we get the entries. 111 00:06:19,070 --> 00:06:24,030 Let's say if search_query, so a search query came in. 112 00:06:24,030 --> 00:06:27,010 Then now let's say, entries equals entries. 113 00:06:27,010 --> 00:06:31,660 So, our original query, getting everything out and ordered. 114 00:06:31,660 --> 00:06:35,790 And then let's say where, which what let's us do the filter. 115 00:06:35,790 --> 00:06:41,120 Entry.content.contains, the search_query. 116 00:06:43,360 --> 00:06:47,440 All right, so, where let's us do it's not a sub query, but it's filtering. 117 00:06:49,470 --> 00:06:52,890 So we can make sure that all of the entries that we select, 118 00:06:52,890 --> 00:06:56,710 have our search_query in their content attribute. 119 00:06:56,710 --> 00:07:00,270 If this was written in an actual SQL, it would look something like, 120 00:07:00,270 --> 00:07:05,550 SELECT * FROM entry WHERE content LIKE, and then you'd have a quote, and 121 00:07:05,550 --> 00:07:10,880 a percent sign, the search query string, whatever that is, another percent sign, 122 00:07:10,880 --> 00:07:14,070 another quote, and then ORDER BY timestamp DESC. 123 00:07:15,560 --> 00:07:19,990 What this does though, is, it allows us to cha, change our entry's, 124 00:07:19,990 --> 00:07:24,370 query only if we actually have something that we want to search for. 125 00:07:24,370 --> 00:07:27,990 Otherwise, we get the four results, just like always. 126 00:07:27,990 --> 00:07:32,240 Okay. So, that's updating view_entries. 127 00:07:32,240 --> 00:07:34,940 Now we need to do search_entries. 128 00:07:34,940 --> 00:07:36,270 I almost forgot about this one. 129 00:07:37,280 --> 00:07:41,390 So anyway, search_entries, it's just a function, and we're gonna say, 130 00:07:41,390 --> 00:07:46,020 search entires for a string. 131 00:07:46,020 --> 00:07:50,120 That could probably be better written, but, that's all I'm gonna say for now. 132 00:07:50,120 --> 00:07:54,959 And we're actually just gonna call view_entries, but we're gonna ask for 133 00:07:54,959 --> 00:07:56,826 input, of a search_query. 134 00:07:56,826 --> 00:08:00,087 [BLANK_AUDIO] 135 00:08:00,087 --> 00:08:04,990 I think that covers this idea of using view_entries as much as possible. 136 00:08:04,990 --> 00:08:08,560 So we're just calling the view_entries function, but 137 00:08:08,560 --> 00:08:11,080 we get some input from the user first. 138 00:08:11,080 --> 00:08:12,740 So, let's try this. 139 00:08:13,990 --> 00:08:15,900 Come back down here. 140 00:08:15,900 --> 00:08:19,310 Clear and diary. 141 00:08:19,310 --> 00:08:21,000 All right, so, I wanna view. 142 00:08:22,620 --> 00:08:24,230 And, okay. 143 00:08:24,230 --> 00:08:28,150 So, I used the word store, only in that first post. 144 00:08:28,150 --> 00:08:32,100 So, let's say search, and I'm gonna search for the word, store. 145 00:08:33,590 --> 00:08:34,540 And there's that one. 146 00:08:34,540 --> 00:08:37,305 And if I, hit enter, there shouldn't be another entry, 147 00:08:37,305 --> 00:08:39,990 I should go straight back to the menu. 148 00:08:39,990 --> 00:08:44,080 And I do, coz there's not anything else, I only said store, in that one. 149 00:08:44,080 --> 00:08:48,360 So, that's awesome, I'm amazed that this works, it works great, actually. 150 00:08:48,360 --> 00:08:49,500 Our apps pretty solid. 151 00:08:49,500 --> 00:08:54,144 And we're still at a pretty small number of lines, it's at 87 lines, 152 00:08:54,144 --> 00:08:56,590 I might hit my 100 after all. 153 00:08:56,590 --> 00:08:59,950 It's great that we are able to write a view_entries functions smartly, so 154 00:08:59,950 --> 00:09:01,516 we could use it again, for searching through entries. 155 00:09:01,516 --> 00:09:05,610 I'm not 100% happy with the way some of our data is displayed in the app, 156 00:09:05,610 --> 00:09:07,550 but, let's worry about that later. 157 00:09:07,550 --> 00:09:10,160 For now, I wanna see about deleting entries, that I don't need or 158 00:09:10,160 --> 00:09:10,910 want any more.