WEBVTT

1
00:00:00.380 --> 00:00:01.800
<v Instructor>Hi guys, and welcome back,</v>

2
00:00:01.800 --> 00:00:03.290
in this video we're going to be adding

3
00:00:03.290 --> 00:00:06.860
the vote date to our votes in the polling app.

4
00:00:06.860 --> 00:00:07.963
So let's get started.

5
00:00:09.260 --> 00:00:11.880
We're gonna start off by opening database.py

6
00:00:11.880 --> 00:00:16.870
and we want to add a new column into the votes table.

7
00:00:16.870 --> 00:00:19.280
So, oh by the way, I've got an old import there,

8
00:00:19.280 --> 00:00:21.300
so we're gonna do that, in create votes,

9
00:00:21.300 --> 00:00:23.830
we have the username, the option ID,

10
00:00:23.830 --> 00:00:27.580
an now we're also gonna add the vote timestamp,

11
00:00:27.580 --> 00:00:31.010
which is going to be a integer.

12
00:00:31.010 --> 00:00:33.490
Make sure that because we're changing the table,

13
00:00:33.490 --> 00:00:35.530
we have to go into ElephantSQL,

14
00:00:35.530 --> 00:00:37.150
or your server of choice,

15
00:00:37.150 --> 00:00:40.210
and delete the votes table from there.

16
00:00:40.210 --> 00:00:43.285
Otherwise, whenever you try to insert a new vote,

17
00:00:43.285 --> 00:00:47.180
into this table, your gonna find that the existing table

18
00:00:47.180 --> 00:00:49.320
doesn't have enough columns.

19
00:00:49.320 --> 00:00:51.110
So something else that we have to do here of course,

20
00:00:51.110 --> 00:00:54.020
is go ahead and change the insert vote here

21
00:00:54.020 --> 00:00:56.410
as well as the username and option ID,

22
00:00:56.410 --> 00:00:59.076
we have to enter the vote timestamp

23
00:00:59.076 --> 00:01:04.076
and the values do require the third parameter there.

24
00:01:04.490 --> 00:01:07.681
Now let's go over to add poll vote down here.

25
00:01:07.681 --> 00:01:09.360
And we're just gonna make sure that,

26
00:01:09.360 --> 00:01:11.750
as well as the username and the option ID,

27
00:01:11.750 --> 00:01:14.760
we get the timestamp, I'm gonna put it down here

28
00:01:14.760 --> 00:01:17.240
in the middle, so I'm gonna do vote timestamp,

29
00:01:17.240 --> 00:01:20.080
it's gonna be a float, and we're gonna pass it in there.

30
00:01:20.080 --> 00:01:23.080
Notice that we're putting an integer into the database

31
00:01:23.080 --> 00:01:25.520
as opposed to a float, that's just gonna lose everything

32
00:01:25.520 --> 00:01:27.530
after the decimal place and that's okay,

33
00:01:27.530 --> 00:01:29.610
we don't really care about which microsecond

34
00:01:29.610 --> 00:01:31.680
somebody voted in, maybe we would,

35
00:01:31.680 --> 00:01:34.050
if we were doing something a bit more like politics,

36
00:01:34.050 --> 00:01:36.060
voting, or something like that.

37
00:01:36.060 --> 00:01:38.796
But for here, we can covert from floats to integers

38
00:01:38.796 --> 00:01:41.070
and lose everything after the decimal point

39
00:01:41.070 --> 00:01:44.420
and that's okay and then we're going to add this in,

40
00:01:44.420 --> 00:01:46.630
but I do realise that

41
00:01:47.810 --> 00:01:49.350
we need to put that in there

42
00:01:49.350 --> 00:01:50.970
and just because the username, option ID,

43
00:01:50.970 --> 00:01:52.570
and vote timestamp have to match

44
00:01:52.570 --> 00:01:55.440
the order of the columns that we're putting in here.

45
00:01:55.440 --> 00:01:58.820
All right so this is how we insert a vote.

46
00:01:58.820 --> 00:02:02.330
Now, whenever we're selecting a vote, from here,

47
00:02:02.330 --> 00:02:05.100
get votes for option, this is gonna return everything,

48
00:02:05.100 --> 00:02:07.790
so that means we're gonna get the timestamp in here as well.

49
00:02:07.790 --> 00:02:10.100
We're gonna deal with that in a moment.

50
00:02:10.100 --> 00:02:13.560
The first thing though is gonna go to option.py

51
00:02:13.560 --> 00:02:16.920
and here, when we're voting, we need to change this slightly

52
00:02:16.920 --> 00:02:19.745
because now we have to pass in the timestamp.

53
00:02:19.745 --> 00:02:24.000
And we know that we have to pass in the UTC timestamp,

54
00:02:24.000 --> 00:02:25.570
not the local time.

55
00:02:25.570 --> 00:02:28.880
So let's do it, using what we learned in the last lecture.

56
00:02:28.880 --> 00:02:33.070
We can do current datetime as UTC and in the last video,

57
00:02:33.070 --> 00:02:36.310
we learned a way to get the current datetime as a UTC,

58
00:02:36.310 --> 00:02:39.470
which was getting the UTC timezone from pytz

59
00:02:39.470 --> 00:02:42.910
and then using that to localise the naive datetime,

60
00:02:42.910 --> 00:02:45.860
but we can do it with datetime.datetime.now

61
00:02:45.860 --> 00:02:47.970
and passing the timezone here directly,

62
00:02:47.970 --> 00:02:49.470
this is an alternative.

63
00:02:49.470 --> 00:02:53.343
So of course, we do have to import the datetime module,

64
00:02:54.190 --> 00:02:57.180
and the pytz module.

65
00:02:57.180 --> 00:03:00.210
These are just going up to the top and we're doing import

66
00:03:00.210 --> 00:03:02.860
pytz and import datetime.

67
00:03:02.860 --> 00:03:05.740
So there we have the current datetime in UTC,

68
00:03:05.740 --> 00:03:08.270
let's get the current timestamp in UTC,

69
00:03:08.270 --> 00:03:11.360
and that's current datetime UTC dot timestamp.

70
00:03:11.360 --> 00:03:14.240
And then we're gonna put that into the database.

71
00:03:14.240 --> 00:03:16.430
The reason I opted for putting the timestamp

72
00:03:16.430 --> 00:03:17.380
before the option ID,

73
00:03:17.380 --> 00:03:20.170
is just because I like seeing self.id there at the end.

74
00:03:20.170 --> 00:03:22.990
It seems to be not something we're doing everywhere,

75
00:03:22.990 --> 00:03:24.760
so it just makes sense to put it there.

76
00:03:24.760 --> 00:03:27.280
But it's up to you where you wanna put your parameters.

77
00:03:27.280 --> 00:03:30.240
Finally, we can display the vote dates in app.py.

78
00:03:30.240 --> 00:03:32.270
So what I'm gonna do is go down here

79
00:03:32.270 --> 00:03:33.600
to show all votes,

80
00:03:33.600 --> 00:03:35.367
and I'm gonna add a couple of lines to it.

81
00:03:35.367 --> 00:03:37.760
I'm gonna ask whether the user

82
00:03:37.760 --> 00:03:39.650
wants to view the vote log,

83
00:03:39.650 --> 00:03:40.750
so who's voted for what.

84
00:03:40.750 --> 00:03:44.310
I'm gonna say vote log equal

85
00:03:44.310 --> 00:03:48.450
input, something like would you want to see the vote log?

86
00:03:48.450 --> 00:03:51.170
And we're gonna default to no.

87
00:03:51.170 --> 00:03:52.420
Like that.

88
00:03:52.420 --> 00:03:54.830
And then, if the vote log

89
00:03:54.830 --> 00:03:56.180
is equal to yes,

90
00:03:56.180 --> 00:03:59.690
we're gonna print vote for options,

91
00:03:59.690 --> 00:04:02.550
and we're gonna pass in the options in here.

92
00:04:02.550 --> 00:04:05.810
Now we do have to create this function, so let's do that.

93
00:04:05.810 --> 00:04:06.643
I'm gonna do

94
00:04:07.630 --> 00:04:11.560
print votes for options like that.

95
00:04:11.560 --> 00:04:14.100
It's list and options list there.

96
00:04:14.100 --> 00:04:17.513
And this is gonna be a list of option.

97
00:04:18.610 --> 00:04:21.260
And what this is gonna do is go through the options

98
00:04:21.260 --> 00:04:22.710
and print out each vote

99
00:04:22.710 --> 00:04:24.390
and information about each vote.

100
00:04:24.390 --> 00:04:26.130
Basically if you wanna see who voted for what

101
00:04:26.130 --> 00:04:27.330
and when they did it,

102
00:04:27.330 --> 00:04:28.660
you print this out.

103
00:04:28.660 --> 00:04:30.980
So we'll do for option in options,

104
00:04:30.980 --> 00:04:34.050
and then I'm gonna print out the option text.

105
00:04:34.050 --> 00:04:36.500
So we'll do option.text like that.

106
00:04:36.500 --> 00:04:38.130
And then we'll do for vote

107
00:04:38.130 --> 00:04:41.159
in options., or rather options.vote.

108
00:04:41.159 --> 00:04:42.840
Then we're gonna grab the date

109
00:04:42.840 --> 00:04:44.620
and print the vote information.

110
00:04:44.620 --> 00:04:47.030
Oh by the way, this should be option.votes there

111
00:04:47.030 --> 00:04:49.260
otherwise we're trying to call the function.

112
00:04:49.260 --> 00:04:51.070
So we're gonna do naive daytime

113
00:04:51.070 --> 00:04:52.970
is what's coming from the database.

114
00:04:52.970 --> 00:04:55.597
So we'll do daytime.daytime

115
00:04:55.597 --> 00:04:58.340
UTC from timestamp

116
00:04:58.340 --> 00:05:01.010
and we'll do vote two.

117
00:05:01.010 --> 00:05:02.780
We do have to import the daytime module,

118
00:05:02.780 --> 00:05:04.280
so we'll do import this name,

119
00:05:04.280 --> 00:05:06.203
import the daytime module in there.

120
00:05:07.110 --> 00:05:09.130
Then we get the UTC date,

121
00:05:09.130 --> 00:05:12.420
which is pytz.utc.localize

122
00:05:12.420 --> 00:05:15.200
and put in the naive data.

123
00:05:15.200 --> 00:05:17.430
We do have to import the pytz module,

124
00:05:17.430 --> 00:05:19.660
so we'll do that as well.

125
00:05:19.660 --> 00:05:21.720
Now, we grab the local date,

126
00:05:21.720 --> 00:05:24.485
and that's UTC date astimezone

127
00:05:24.485 --> 00:05:26.880
and we're gonna do pytz.timezone

128
00:05:27.910 --> 00:05:29.933
Europe slash London.

129
00:05:31.060 --> 00:05:35.500
And then we're gonna do local date as a string

130
00:05:35.500 --> 00:05:37.750
is gonna be local date.strftime

131
00:05:38.810 --> 00:05:40.510
and we're gonna pass in here something like

132
00:05:40.510 --> 00:05:43.050
percent y, percent m, percent d,

133
00:05:43.050 --> 00:05:44.463
percent h, percent n.

134
00:05:45.390 --> 00:05:47.260
I'm actually okay with just calculating that

135
00:05:47.260 --> 00:05:48.093
to the end of there

136
00:05:48.093 --> 00:05:50.290
so that we get the local date as a string.

137
00:05:50.290 --> 00:05:51.860
So I'm just gonna do that instead,

138
00:05:51.860 --> 00:05:54.890
but it's up to you how far you wanna subdivide it.

139
00:05:54.890 --> 00:05:55.723
And finally at the end,

140
00:05:55.723 --> 00:05:57.740
we're gonna do printout,

141
00:05:57.740 --> 00:05:59.730
backslash d for a tab character,

142
00:05:59.730 --> 00:06:01.990
which gives us a few spaces,

143
00:06:01.990 --> 00:06:04.210
and then we'll do vote zero

144
00:06:04.210 --> 00:06:06.193
on local date.

145
00:06:07.400 --> 00:06:09.420
Something that is hard coded in here

146
00:06:09.420 --> 00:06:11.883
is of course the time zone information.

147
00:06:11.883 --> 00:06:14.280
What I would recommend when you're getting into

148
00:06:14.280 --> 00:06:16.413
this level of, you know, functionality,

149
00:06:16.413 --> 00:06:20.440
is probably to create a model for users,

150
00:06:20.440 --> 00:06:22.020
store them in their own table,

151
00:06:22.020 --> 00:06:25.280
store the timezone information alongside their data,

152
00:06:25.280 --> 00:06:27.550
and, you know, that way you can start to build out

153
00:06:27.550 --> 00:06:30.380
more functionality in this application.

154
00:06:30.380 --> 00:06:32.440
But for now, we're gonna go ahead and right click

155
00:06:32.440 --> 00:06:34.173
app.py and run it.

156
00:06:35.120 --> 00:06:36.670
I'm gonna load this from the dot m file,

157
00:06:36.670 --> 00:06:37.790
we're gonna create a poll.

158
00:06:37.790 --> 00:06:39.620
It's gonna be flash versus jango.

159
00:06:39.620 --> 00:06:42.280
Let's say poll owner is gonna be me.

160
00:06:42.280 --> 00:06:45.010
Flask and jango and it depends

161
00:06:45.010 --> 00:06:46.610
are gonna be our three options.

162
00:06:46.610 --> 00:06:48.410
And then we're gonna list the open ports,

163
00:06:48.410 --> 00:06:49.340
make sure this is working.

164
00:06:49.340 --> 00:06:50.830
You can see that it's there.

165
00:06:50.830 --> 00:06:52.080
We're gonna vote on a poll.

166
00:06:52.080 --> 00:06:53.500
That's the ID one,

167
00:06:53.500 --> 00:06:55.150
and obviously we're gonna vote for flask.

168
00:06:55.150 --> 00:06:56.763
And we're gonna vote as rolf.

169
00:06:57.640 --> 00:06:59.510
Now we're gonna show the poll votes.

170
00:06:59.510 --> 00:07:00.670
So we'll do show poll votes,

171
00:07:00.670 --> 00:07:01.770
we're gonna see poll one,

172
00:07:01.770 --> 00:07:04.080
and you can see that flask got one vote,

173
00:07:04.080 --> 00:07:05.930
and that's 100 percent of the total.

174
00:07:05.930 --> 00:07:08.250
And now we wanna see the vote log.

175
00:07:08.250 --> 00:07:10.123
So we'll press on the y here.

176
00:07:11.250 --> 00:07:16.110
And you can see that each option gets their sub menu there.

177
00:07:16.110 --> 00:07:19.440
And then here we see rolf on 2020-05-16.

178
00:07:19.440 --> 00:07:20.620
So that's today

179
00:07:20.620 --> 00:07:22.520
at 5:06.

180
00:07:22.520 --> 00:07:26.800
So two things that we've learned in this video, really.

181
00:07:26.800 --> 00:07:30.240
The first one is how to use the data and times

182
00:07:30.240 --> 00:07:32.230
from your database here in the polling application,

183
00:07:32.230 --> 00:07:33.240
which is great.

184
00:07:33.240 --> 00:07:34.930
But really the more important thing

185
00:07:34.930 --> 00:07:37.790
is how to change something in your database

186
00:07:37.790 --> 00:07:41.270
and propagate that change throughout your application.

187
00:07:41.270 --> 00:07:43.060
Usually the flow for a change like this

188
00:07:43.060 --> 00:07:46.350
is gonna start with the data you want to handle,

189
00:07:46.350 --> 00:07:49.270
or rather how you want the user to interact with it.

190
00:07:49.270 --> 00:07:51.470
So figure out the data you want to store

191
00:07:51.470 --> 00:07:53.260
and the data the users need,

192
00:07:53.260 --> 00:07:55.300
and then work backwards from the database

193
00:07:55.300 --> 00:07:56.620
to add the necessary flow.

194
00:07:56.620 --> 00:07:58.450
Change the database interaction function

195
00:07:58.450 --> 00:07:59.700
and the query.

196
00:07:59.700 --> 00:08:02.220
Change the model that's using that,

197
00:08:02.220 --> 00:08:05.188
and then change the user facing code that's using that.

198
00:08:05.188 --> 00:08:08.973
That way, you're always in a working state.

199
00:08:10.030 --> 00:08:10.900
But that's about it.

200
00:08:10.900 --> 00:08:12.750
Thank you for joining me in this section,

201
00:08:12.750 --> 00:08:13.620
I hope you have enjoyed it,

202
00:08:13.620 --> 00:08:16.270
I hope you've enjoyed learning about data and times,

203
00:08:16.270 --> 00:08:18.540
how to simplify our code, how to use models,

204
00:08:18.540 --> 00:08:21.226
and how we can create these larger python applications

205
00:08:21.226 --> 00:08:23.430
that use databases.

206
00:08:23.430 --> 00:08:24.600
Thanks guys for joining me,

207
00:08:24.600 --> 00:08:25.560
thank you for watching,

208
00:08:25.560 --> 00:08:27.210
and I'll see you in the next one.

