WEBVTT

1
00:00:00.000 --> 00:00:02.560
<v Jose>Now that we've got the poll class,</v>

2
00:00:02.560 --> 00:00:04.770
we're gonna go ahead and write the option class.

3
00:00:04.770 --> 00:00:07.270
So I'll open up the project, right click in here,

4
00:00:07.270 --> 00:00:11.450
create a new option.py file, and then close that up.

5
00:00:11.450 --> 00:00:13.930
And now we can get started with the option class.

6
00:00:13.930 --> 00:00:16.810
It's going to be very similar to the poll class,

7
00:00:16.810 --> 00:00:18.450
except it's gonna deal with the data

8
00:00:18.450 --> 00:00:20.170
that an option deals with.

9
00:00:20.170 --> 00:00:22.637
So here I'm gonna have written the initial code.

10
00:00:22.637 --> 00:00:25.020
We've got the list import, the database import,

11
00:00:25.020 --> 00:00:27.560
I've actually imported the createConnection function

12
00:00:27.560 --> 00:00:29.270
from the connections.py file.

13
00:00:29.270 --> 00:00:30.740
That doesn't exist yet, but we're gonna add it

14
00:00:30.740 --> 00:00:31.710
in just a moment.

15
00:00:31.710 --> 00:00:33.600
And then I've done the initial set up

16
00:00:33.600 --> 00:00:35.240
for the option class.

17
00:00:35.240 --> 00:00:37.950
An option takes in the text, the poll ID,

18
00:00:37.950 --> 00:00:40.370
and it has an ID, then just sets them there,

19
00:00:40.370 --> 00:00:43.530
and the repr() method returns the option text

20
00:00:43.530 --> 00:00:46.960
that we can use to recreate an option object.

21
00:00:46.960 --> 00:00:49.160
We're gonna need a save method as well,

22
00:00:49.160 --> 00:00:50.570
there's again creates the connection,

23
00:00:50.570 --> 00:00:51.980
calls database .option,

24
00:00:51.980 --> 00:00:53.200
which doesn't exist at the moment,

25
00:00:53.200 --> 00:00:55.960
but that just inserts a row into the database,

26
00:00:55.960 --> 00:01:00.280
and then sets the self ID to be the option returned.

27
00:01:00.280 --> 00:01:02.030
We're gonna add a get method,

28
00:01:02.030 --> 00:01:04.240
so that we can pass in an option ID,

29
00:01:04.240 --> 00:01:06.000
and it returns and option object.

30
00:01:06.000 --> 00:01:08.120
For example, if we have to find a specific option

31
00:01:08.120 --> 00:01:10.610
in the database, and then we also need

32
00:01:10.610 --> 00:01:12.690
a couple of methods to deal with voting.

33
00:01:12.690 --> 00:01:14.560
So I'm gonna add the vote method,

34
00:01:14.560 --> 00:01:16.720
that takes in a username and is gonna cast

35
00:01:16.720 --> 00:01:19.010
a vote for that username.

36
00:01:19.010 --> 00:01:20.719
So all we have to do here is again

37
00:01:20.719 --> 00:01:23.730
create the connection, then we're gonna

38
00:01:23.730 --> 00:01:26.940
do database@poll_vote, pass that connection in,

39
00:01:26.940 --> 00:01:30.875
the username, and self.ID

40
00:01:30.875 --> 00:01:34.370
and this already exists so all we're using here is the stuff

41
00:01:34.370 --> 00:01:36.578
that is already in our database.py file

42
00:01:36.578 --> 00:01:39.760
and then we do connection.close.

43
00:01:39.760 --> 00:01:42.305
Finally, I'm gonna add a property decorator

44
00:01:42.305 --> 00:01:46.850
for the votes, and that is going to return a list

45
00:01:46.850 --> 00:01:48.940
of database.vote.

46
00:01:48.940 --> 00:01:51.803
What this is gonna do is again create the connection,

47
00:01:53.490 --> 00:01:57.840
get the votes with the database.get_votes_for_option,

48
00:01:57.840 --> 00:01:58.820
which it doesn't exist yet,

49
00:01:58.820 --> 00:02:00.320
but it's gonna in a moment,

50
00:02:00.320 --> 00:02:02.370
and pass in the ID of this option.

51
00:02:02.370 --> 00:02:04.220
That's what we're gonna use to go into the database

52
00:02:04.220 --> 00:02:05.660
and fetch those votes.

53
00:02:05.660 --> 00:02:07.590
Then we'll close,

54
00:02:07.590 --> 00:02:08.970
and we're actually going to return

55
00:02:08.970 --> 00:02:11.440
those votes which again is a list of tuples,

56
00:02:11.440 --> 00:02:13.690
it's not a list of objects anymore

57
00:02:13.690 --> 00:02:15.360
because we've decided not to create

58
00:02:15.360 --> 00:02:17.540
a model class for votes.

59
00:02:17.540 --> 00:02:18.830
Let's open up our project

60
00:02:18.830 --> 00:02:21.260
and we're gonna create a new Python file

61
00:02:21.260 --> 00:02:23.546
inside there beside @.py, in database.py

62
00:02:23.546 --> 00:02:26.370
I'm gonna call it connections.py

63
00:02:26.370 --> 00:02:29.159
and all this is gonna have is a very simple function

64
00:02:29.159 --> 00:02:31.480
to create a connection.

65
00:02:31.480 --> 00:02:35.380
This we have to import from both poll.py and option.py.

66
00:02:35.380 --> 00:02:37.230
So I'm going to go ahead and do that.

67
00:02:38.971 --> 00:02:41.550
You can see that PyCharm is complaining from this import,

68
00:02:41.550 --> 00:02:44.630
that's because we have to do models.option there.

69
00:02:44.630 --> 00:02:46.580
So up to now, we've got everything

70
00:02:46.580 --> 00:02:49.370
except the database.py changes.

71
00:02:49.370 --> 00:02:51.028
Something important to note,

72
00:02:51.028 --> 00:02:54.740
when we swap out from using the database

73
00:02:54.740 --> 00:02:57.490
directly in our .py and now we're gonna

74
00:02:57.490 --> 00:02:59.146
change all this to use the models,

75
00:02:59.146 --> 00:03:02.670
is that the models are usually only concerned

76
00:03:02.670 --> 00:03:05.860
with themselves and the data they can

77
00:03:05.860 --> 00:03:08.530
put into the database and how they can use the data

78
00:03:08.530 --> 00:03:11.340
from the database to affect themselves.

79
00:03:11.340 --> 00:03:14.470
That greatly simplifies the thinking

80
00:03:14.470 --> 00:03:16.649
for us as developers around these models,

81
00:03:16.649 --> 00:03:19.760
but it can limit us when it comes to,

82
00:03:19.760 --> 00:03:23.330
you know, using the database for its maximum efficiency.

83
00:03:23.330 --> 00:03:25.580
That's why, now you're going to see

84
00:03:25.580 --> 00:03:27.670
in database.py, we've got a lot of stuff

85
00:03:27.670 --> 00:03:30.270
that simply deals with a single table.

86
00:03:30.270 --> 00:03:34.270
We no longer are gonna have many joins, for example.

87
00:03:34.270 --> 00:03:37.310
Database.create_poll just affects the polls table.

88
00:03:37.310 --> 00:03:39.425
Database.get_poll_options just get options

89
00:03:39.425 --> 00:03:40.850
from the options table.

90
00:03:40.850 --> 00:03:44.870
Database.get_poll just uses a polls table, and so on.

91
00:03:44.870 --> 00:03:46.544
Similarly in the options table,

92
00:03:46.544 --> 00:03:48.620
it's very much the same stuff.

93
00:03:48.620 --> 00:03:51.232
Database.add_option just adds to the options table.

94
00:03:51.232 --> 00:03:52.760
Get_option gets from the options table.

95
00:03:52.760 --> 00:03:55.860
Get_votes_for_option just affects the votes table.

96
00:03:55.860 --> 00:03:59.300
You can see that there's not that many joins going on.

97
00:03:59.300 --> 00:04:02.100
So, that is the trade-off that we make

98
00:04:02.100 --> 00:04:03.940
when we use this models approach.

99
00:04:03.940 --> 00:04:06.288
We lose power in the database side,

100
00:04:06.288 --> 00:04:09.883
but we gain in maintainability of our code.

101
00:04:10.780 --> 00:04:14.280
Let's start working on database.py in the next video.

102
00:04:14.280 --> 00:04:15.230
I'll see you there.

