WEBVTT

1
00:00:00.630 --> 00:00:03.000
<v ->So let's go ahead and make the changes</v>

2
00:00:03.000 --> 00:00:05.550
to the database that we need to make.

3
00:00:05.550 --> 00:00:08.450
Let's start with our database changes in poll.py.

4
00:00:08.450 --> 00:00:11.010
We need get poll options and get poll.

5
00:00:11.010 --> 00:00:13.803
So I'm gonna go over to database.py down here.

6
00:00:14.750 --> 00:00:17.590
I'm gonna add a quick comment just to say polls,

7
00:00:17.590 --> 00:00:22.150
and then we're going to start creating our functions here.

8
00:00:22.150 --> 00:00:25.200
I'm actually going to move create poll up to the top.

9
00:00:25.200 --> 00:00:26.870
I'm gonna give you this file afterwards, by the way,

10
00:00:26.870 --> 00:00:29.570
so if you're a bit lost with these changes, don't worry.

11
00:00:29.570 --> 00:00:31.940
I'm gonna give you everything that we've changed over,

12
00:00:31.940 --> 00:00:33.820
and it's all in the e-book as well.

13
00:00:33.820 --> 00:00:35.730
So create poll is up here.

14
00:00:35.730 --> 00:00:37.530
We're gonna have get polls.

15
00:00:37.530 --> 00:00:40.050
We need one for getting a single poll,

16
00:00:40.050 --> 00:00:42.010
so we're gonna do get poll.

17
00:00:42.010 --> 00:00:45.570
Takes in a connection and a poll ID,

18
00:00:45.570 --> 00:00:47.150
and that should be an integer,

19
00:00:47.150 --> 00:00:51.780
and returns a poll, and then it just uses a connection

20
00:00:51.780 --> 00:00:55.560
with connection dot cursor as cursor,

21
00:00:55.560 --> 00:00:56.580
and execute.

22
00:00:56.580 --> 00:00:58.060
So cursor execute.

23
00:00:58.060 --> 00:00:59.670
We're gonna execute select poll,

24
00:00:59.670 --> 00:01:03.600
notice that doesn't exist, with poll ID.

25
00:01:03.600 --> 00:01:07.330
Then at the end we return cursor dot fetch one.

26
00:01:07.330 --> 00:01:09.283
Alright, so let's create select poll.

27
00:01:12.580 --> 00:01:15.720
Just like that, we're gonna select star from polls,

28
00:01:15.720 --> 00:01:19.000
where the ID is equal to the question mark.

29
00:01:19.000 --> 00:01:21.580
Or, actually, this should be percent S, my apologies.

30
00:01:21.580 --> 00:01:24.000
Still in SQLite mode.

31
00:01:24.000 --> 00:01:26.700
So there we have our query to select a single poll,

32
00:01:26.700 --> 00:01:29.610
and now we've got the get poll function there.

33
00:01:29.610 --> 00:01:32.100
Something important to note and that can be a bit confusing

34
00:01:32.100 --> 00:01:33.930
is this poll return here.

35
00:01:33.930 --> 00:01:37.140
Remember, this is the poll type that we defined,

36
00:01:37.140 --> 00:01:39.440
and it's not the poll class,

37
00:01:39.440 --> 00:01:41.030
because the database is still very much

38
00:01:41.030 --> 00:01:43.970
working with tuples and not with classes.

39
00:01:43.970 --> 00:01:45.850
That is what the model classes are for.

40
00:01:45.850 --> 00:01:48.030
We no longer need get poll details,

41
00:01:48.030 --> 00:01:50.090
but we do need get poll options,

42
00:01:50.090 --> 00:01:54.100
so we're going to change this into get poll options,

43
00:01:54.100 --> 00:01:55.670
and it does very much the same thing

44
00:01:55.670 --> 00:01:59.340
but now it returns a list of options.

45
00:01:59.340 --> 00:02:01.350
We do have to create that, and again,

46
00:02:01.350 --> 00:02:03.615
all it does is use the connection, use the cursor,

47
00:02:03.615 --> 00:02:06.520
and then it's gonna select poll options,

48
00:02:06.520 --> 00:02:09.080
not poll with options, passing in the poll ID

49
00:02:09.080 --> 00:02:10.320
and then fetch everything.

50
00:02:10.320 --> 00:02:12.670
We now have to go ahead and create these, too.

51
00:02:12.670 --> 00:02:15.260
So what we're gonna do is down here,

52
00:02:15.260 --> 00:02:20.090
we're gonna do option equal a tuple of INT S-T-R and INT.

53
00:02:20.090 --> 00:02:22.030
We no longer need poll with option,

54
00:02:22.030 --> 00:02:24.720
and we do need the query.

55
00:02:24.720 --> 00:02:27.120
So instead of select poll with options,

56
00:02:27.120 --> 00:02:29.060
we're just gonna do select poll options,

57
00:02:29.060 --> 00:02:30.650
and it's gonna be simpler.

58
00:02:30.650 --> 00:02:33.340
We just have to select star from options

59
00:02:33.340 --> 00:02:35.730
where poll ID is equal to that.

60
00:02:35.730 --> 00:02:38.890
So we'll do where poll ID is equal to that.

61
00:02:38.890 --> 00:02:40.460
We don't need to join anymore,

62
00:02:40.460 --> 00:02:43.190
and we can actually make this a single string.

63
00:02:43.190 --> 00:02:45.250
So you can see that by removing the join,

64
00:02:45.250 --> 00:02:47.210
this query and that return is now

65
00:02:47.210 --> 00:02:49.110
only going to return options.

66
00:02:49.110 --> 00:02:52.630
So when we use that in here, that is just going

67
00:02:52.630 --> 00:02:54.820
to give us the option data,

68
00:02:54.820 --> 00:02:58.460
and we are then passing that into the option constructor

69
00:02:58.460 --> 00:03:00.720
which gives us an option object,

70
00:03:00.720 --> 00:03:02.410
and that is what we're putting

71
00:03:02.410 --> 00:03:04.840
into our list that we return.

72
00:03:04.840 --> 00:03:08.070
So later on, app.py is going to be dealing

73
00:03:08.070 --> 00:03:10.430
with poll and option objects.

74
00:03:10.430 --> 00:03:13.680
So that's everything for the poll.py class.

75
00:03:13.680 --> 00:03:15.550
Now we can move onto option.py,

76
00:03:15.550 --> 00:03:17.473
make sure that we've got everything that we need

77
00:03:17.473 --> 00:03:18.780
from the database.

78
00:03:18.780 --> 00:03:21.020
So we've got add option and get option

79
00:03:21.020 --> 00:03:23.270
and get votes for option that we need to add.

80
00:03:23.270 --> 00:03:26.160
So let's go into database.py and add that.

81
00:03:26.160 --> 00:03:28.580
So here underneath get poll options

82
00:03:28.580 --> 00:03:32.190
I'm going to add another comment for options,

83
00:03:32.190 --> 00:03:35.050
and note that we don't really need a lot of these things

84
00:03:35.050 --> 00:03:37.720
so we're gonna be deleting them in a moment,

85
00:03:37.720 --> 00:03:40.700
but I'm gonna go ahead and do get option.

86
00:03:40.700 --> 00:03:42.910
That takes a connection and an option ID,

87
00:03:42.910 --> 00:03:46.540
which is an integer, and returns an option,

88
00:03:46.540 --> 00:03:50.270
and we also need add option, which takes a connection,

89
00:03:50.270 --> 00:03:53.070
option text, poll ID, and again,

90
00:03:53.070 --> 00:03:55.210
we're gonna do pass in there.

91
00:03:55.210 --> 00:03:57.120
Once we've got that, what else are we missing?

92
00:03:57.120 --> 00:03:58.880
Get votes for option.

93
00:03:58.880 --> 00:04:01.560
So we're gonna be adding that as well.

94
00:04:01.560 --> 00:04:04.820
So if we go over to database.py down here,

95
00:04:04.820 --> 00:04:07.180
we're gonna add a comment for votes

96
00:04:07.180 --> 00:04:11.240
and we're gonna do get votes for option.

97
00:04:11.240 --> 00:04:14.650
Takes in a connection and an option ID as well,

98
00:04:14.650 --> 00:04:17.440
and is gonna return a list of votes.

99
00:04:17.440 --> 00:04:19.660
Alright, what else do we need?

100
00:04:19.660 --> 00:04:21.250
We need add poll vote.

101
00:04:21.250 --> 00:04:23.150
So we're gonna make sure that that's there,

102
00:04:23.150 --> 00:04:23.983
and we can see it.

103
00:04:23.983 --> 00:04:26.200
It's down here, so all we have to do now

104
00:04:26.200 --> 00:04:27.900
is delete the rest.

105
00:04:27.900 --> 00:04:29.400
We're no longer gonna use the database

106
00:04:29.400 --> 00:04:32.090
to get a random poll vote, and we're no longer

107
00:04:32.090 --> 00:04:34.380
gonna get poll and vote results together,

108
00:04:34.380 --> 00:04:36.540
so we can get rid of those.

109
00:04:36.540 --> 00:04:39.010
While I'm here I'm noticing this red underline here.

110
00:04:39.010 --> 00:04:43.310
This should just be returning a poll and not a list of polls

111
00:04:43.310 --> 00:04:45.780
so we actually have to change this query.

112
00:04:45.780 --> 00:04:48.600
Instead of select latest poll with options,

113
00:04:48.600 --> 00:04:52.340
now we're just gonna change this to select latest poll,

114
00:04:52.340 --> 00:04:55.850
just like that, and the query has to change slightly.

115
00:04:55.850 --> 00:04:59.253
We simply find a bit in that it no longer needs the join.

116
00:05:00.460 --> 00:05:03.303
Alright, let's make sure to use that down here.

117
00:05:08.700 --> 00:05:10.580
Cool, now let's go and implement

118
00:05:10.580 --> 00:05:13.500
the get option and add option functions.

119
00:05:13.500 --> 00:05:15.240
I've gone ahead and written these in.

120
00:05:15.240 --> 00:05:17.250
We've got select option being called here

121
00:05:17.250 --> 00:05:19.320
to fetch one individual option,

122
00:05:19.320 --> 00:05:22.240
and add option just uses the insert option query

123
00:05:22.240 --> 00:05:24.700
that already existed, so all we have to do

124
00:05:24.700 --> 00:05:28.650
is go up here and find a way to select that single option,

125
00:05:28.650 --> 00:05:29.960
which we know how to do.

126
00:05:29.960 --> 00:05:33.010
We can just do select option equal

127
00:05:33.010 --> 00:05:38.010
select star from options where ID equal percent S.

128
00:05:38.830 --> 00:05:40.920
Finally, for get votes for option,

129
00:05:40.920 --> 00:05:44.090
all we have to do, again, is call the appropriate query.

130
00:05:44.090 --> 00:05:45.410
So that's select vote for options.

131
00:05:45.410 --> 00:05:48.770
That doesn't exist yet, but we can make it.

132
00:05:48.770 --> 00:05:50.740
We'll do select votes for option equal

133
00:05:51.750 --> 00:05:56.740
select star from votes where option ID equal percent S.

134
00:05:56.740 --> 00:06:00.510
So select poll vote details is no longer necessary,

135
00:06:00.510 --> 00:06:03.027
and you can start to see, the other thing we don't need

136
00:06:03.027 --> 00:06:05.260
is, of course, select random vote.

137
00:06:05.260 --> 00:06:06.620
We've got rid of that function,

138
00:06:06.620 --> 00:06:08.670
so we're gonna get rid of that query,

139
00:06:08.670 --> 00:06:12.020
and we're gonna calculate the random votes using Python.

140
00:06:12.020 --> 00:06:13.470
And something else we don't need

141
00:06:13.470 --> 00:06:15.550
is the poll results data type here,

142
00:06:15.550 --> 00:06:17.840
so I'm just gonna get rid of that.

143
00:06:17.840 --> 00:06:20.680
Something I forgot to change here when creating polls

144
00:06:20.680 --> 00:06:22.320
is that we no longer need to pass in

145
00:06:22.320 --> 00:06:24.810
a list of options in here, and indeed,

146
00:06:24.810 --> 00:06:27.690
the create poll only needs to grab

147
00:06:27.690 --> 00:06:29.890
the insert poll return ID.

148
00:06:29.890 --> 00:06:32.600
So what we're gonna do is we're going to delete that

149
00:06:32.600 --> 00:06:36.000
and then return poll ID.

150
00:06:36.000 --> 00:06:38.220
Now when we call this, we're gonna be doing that

151
00:06:38.220 --> 00:06:41.790
from poll.py, so do make sure that we fix this.

152
00:06:41.790 --> 00:06:45.780
Here we need to pass in a connection and that's about it.

153
00:06:45.780 --> 00:06:48.070
In option.py I actually forgot the same thing,

154
00:06:48.070 --> 00:06:50.500
so make sure to pass the connection in there.

155
00:06:50.500 --> 00:06:52.500
Just pay attention to the PyCharm type hints there

156
00:06:52.500 --> 00:06:53.910
that I clearly didn't,

157
00:06:53.910 --> 00:06:56.560
and then let's go over to add option here

158
00:06:56.560 --> 00:06:58.450
and we're gonna make sure that we're returning

159
00:06:58.450 --> 00:07:00.930
the ID that the option method needs.

160
00:07:00.930 --> 00:07:03.320
So we're gonna add returning ID here to the query name,

161
00:07:03.320 --> 00:07:05.560
and then we're gonna do option ID

162
00:07:05.560 --> 00:07:08.720
is equal to cursor dot fetch one zero,

163
00:07:08.720 --> 00:07:11.310
and we're gonna return option ID.

164
00:07:11.310 --> 00:07:13.660
Then I'm gonna copy that, go up to the top,

165
00:07:13.660 --> 00:07:15.560
and just rename this here.

166
00:07:15.560 --> 00:07:18.100
I'm actually gonna rename it to match poll there,

167
00:07:18.100 --> 00:07:19.500
so just return ID there.

168
00:07:19.500 --> 00:07:22.930
The values are gonna be percent S, percent S,

169
00:07:22.930 --> 00:07:26.370
and returning ID.

170
00:07:26.370 --> 00:07:28.100
Now let's copy that and go back down.

171
00:07:28.100 --> 00:07:29.760
Make sure that this is matching.

172
00:07:29.760 --> 00:07:32.290
It's good to have consistency there in the code.

173
00:07:32.290 --> 00:07:34.360
Alright, that's all the changes we have to make.

174
00:07:34.360 --> 00:07:37.140
Now we're ready to go over to app.py.

175
00:07:37.140 --> 00:07:39.723
So I'll see you in the next video for that.

