1
1

00:00:01,710  -->  00:00:03,220
<v ->Hi, guys, and welcome back.</v>
2

2

00:00:03,220  -->  00:00:04,630
In this video, we're going to learn
3

3

00:00:04,630  -->  00:00:06,753
about decorators with parameters.
4

4

00:00:06,753  -->  00:00:09,110
Now, this is slightly different than the last video
5

5

00:00:09,110  -->  00:00:11,750
where we looked at decorating functions with parameters,
6

6

00:00:11,750  -->  00:00:13,310
now we're going to add parameters
7

7

00:00:13,310  -->  00:00:15,400
to the decorators themselves.
8

8

00:00:15,400  -->  00:00:16,300
Let's get started.
9

9

00:00:17,220  -->  00:00:19,880
Here, we have our make_secure decorator.
10

10

00:00:19,880  -->  00:00:23,530
And now, we are looking at two different functions.
11

11

00:00:23,530  -->  00:00:26,310
One get_admin_password that is decorated,
12

12

00:00:26,310  -->  00:00:29,510
and get_dashboard_password, which is also decorated.
13

13

00:00:29,510  -->  00:00:31,600
But now, I want the get_admin_password
14

14

00:00:31,600  -->  00:00:33,840
function to only be accessible by admins
15

15

00:00:33,840  -->  00:00:35,700
and the get_dashboard_password to only
16

16

00:00:35,700  -->  00:00:38,170
be accessible by users.
17

17

00:00:38,170  -->  00:00:41,180
So if you're a guest, you will get access to neither,
18

18

00:00:41,180  -->  00:00:43,740
but if you're a user, you'll get access to this one.
19

19

00:00:43,740  -->  00:00:46,490
And if you're an admin, you'll get access to this one.
20

20

00:00:46,490  -->  00:00:48,023
So how do we do this?
21

21

00:00:48,023  -->  00:00:52,040
Well, we have a make_secure decorator here,
22

22

00:00:52,040  -->  00:00:55,840
and notice that when this gets called,
23

23

00:00:55,840  -->  00:00:58,480
what happens is the make_secure decorator
24

24

00:00:58,480  -->  00:01:01,450
gets applied to get_admin_password.
25

25

00:01:01,450  -->  00:01:05,180
But there are no brackets after this make_secure.
26

26

00:01:05,180  -->  00:01:07,530
Now, what we're going to do is we're going to create
27

27

00:01:07,530  -->  00:01:11,660
a function that will return the decorator,
28

28

00:01:11,660  -->  00:01:14,680
but it will allow us to check for the access level.
29

29

00:01:14,680  -->  00:01:15,800
Now, here's that's gonna go.
30

30

00:01:15,800  -->  00:01:17,300
It's a little bit complicated.
31

31

00:01:18,210  -->  00:01:22,260
We're going to create another decorator,
32

32

00:01:22,260  -->  00:01:25,057
and this one is going to take in an access_level.
33

33

00:01:26,364  -->  00:01:30,390
The rest of all of this is going to go inside that.
34

34

00:01:30,390  -->  00:01:34,253
And this one here is going to return make_secure.
35

35

00:01:35,440  -->  00:01:38,870
So we have added a third level of function,
36

36

00:01:38,870  -->  00:01:40,869
and I know this is quite confusing,
37

37

00:01:40,869  -->  00:01:45,350
but now what we've got is not really a decorator.
38

38

00:01:45,350  -->  00:01:46,970
Now, I'm going to actually change the name
39

39

00:01:46,970  -->  00:01:49,800
of these functions because the outer function,
40

40

00:01:49,800  -->  00:01:52,400
it's not really a decorator anymore.
41

41

00:01:52,400  -->  00:01:55,040
This one here is a decorator now.
42

42

00:01:55,040  -->  00:01:57,640
This one is a factory, essentially,
43

43

00:01:57,640  -->  00:02:00,870
a function that is used to create a decorator.
44

44

00:02:00,870  -->  00:02:03,200
But what we have is a function call,
45

45

00:02:03,200  -->  00:02:05,040
that we give it an access_level,
46

46

00:02:05,040  -->  00:02:07,530
and then it creates a decorator and returns it.
47

47

00:02:07,530  -->  00:02:10,720
So now we have to put the brackets in here.
48

48

00:02:10,720  -->  00:02:13,240
And the order is very important.
49

49

00:02:13,240  -->  00:02:15,970
The function call happens first,
50

50

00:02:15,970  -->  00:02:19,690
and then the result is applied as a decorator.
51

51

00:02:19,690  -->  00:02:21,630
So the function call happens first.
52

52

00:02:21,630  -->  00:02:23,940
This will run in its entirety.
53

53

00:02:23,940  -->  00:02:26,330
We will define the decorator function,
54

54

00:02:26,330  -->  00:02:28,180
and we will return it.
55

55

00:02:28,180  -->  00:02:31,460
Oh, sorry, this should be decorator there,
56

56

00:02:31,460  -->  00:02:33,600
because we wanna return that function.
57

57

00:02:33,600  -->  00:02:36,830
So this will run and return the decorator,
58

58

00:02:36,830  -->  00:02:41,490
and that is the result of calling make_secure,
59

59

00:02:41,490  -->  00:02:45,770
and then we will apply that decorator into this function.
60

60

00:02:45,770  -->  00:02:48,610
At the end of the day, the result is the same,
61

61

00:02:48,610  -->  00:02:53,310
but now we can pass in something like admin in there,
62

62

00:02:53,310  -->  00:02:56,630
and something like guest in there.
63

63

00:02:56,630  -->  00:03:01,200
Notice that because this factory does take in a argument,
64

64

00:03:01,200  -->  00:03:04,313
you need to put the argument and the brackets in there.
65

65

00:03:05,180  -->  00:03:07,810
The benefit of that is now inside here,
66

66

00:03:07,810  -->  00:03:09,620
we can check that access level.
67

67

00:03:09,620  -->  00:03:11,690
So we can say that if the user's access level
68

68

00:03:11,690  -->  00:03:16,690
is equal to the access level that we want to secure for,
69

69

00:03:17,460  -->  00:03:19,340
then we have access to the function.
70

70

00:03:19,340  -->  00:03:21,980
Otherwise, we can say that this user
71

71

00:03:21,980  -->  00:03:25,520
has no access level permissions, or for that.
72

72

00:03:25,520  -->  00:03:28,720
So there are no access level permissions for that user.
73

73

00:03:28,720  -->  00:03:31,010
Now if we want to run these functions, we can do.
74

74

00:03:31,010  -->  00:03:33,001
Here I've got the get_admin_password
75

75

00:03:33,001  -->  00:03:34,227
and get_dashboard_password.
76

76

00:03:34,227  -->  00:03:37,032
We should get no access for either of those.
77

77

00:03:37,032  -->  00:03:39,212
And then I'm going to change the user to an admin
78

78

00:03:39,212  -->  00:03:41,730
and run them again, and now we should allow this one,
79

79

00:03:41,730  -->  00:03:43,216
but not that one.
80

80

00:03:43,216  -->  00:03:45,419
Let me save that and run it,
81

81

00:03:45,419  -->  00:03:49,022
and you'll see that you get that problem exactly.
82

82

00:03:49,022  -->  00:03:51,593
Indeed, we actually have access to the user dashboard there.
83

83

00:03:51,593  -->  00:03:54,750
We wanna change that, make sure that's for users only
84

84

00:03:54,750  -->  00:03:56,900
since that was the original intention.
85

85

00:03:56,900  -->  00:03:58,040
Let's run that again.
86

86

00:03:58,040  -->  00:04:00,700
And you get no admin permissions for Jose,
87

87

00:04:00,700  -->  00:04:02,379
no user permissions for Jose.
88

88

00:04:02,379  -->  00:04:04,980
But Anna does have admin permissions,
89

89

00:04:04,980  -->  00:04:06,743
but no user permissions.
90

90

00:04:08,130  -->  00:04:09,730
something that you may wanna look into
91

91

00:04:09,730  -->  00:04:12,470
to improve this further is, instead of using strings
92

92

00:04:12,470  -->  00:04:14,520
for your access levels, you can use integers.
93

93

00:04:14,520  -->  00:04:17,280
So guest can be zero, user can be one,
94

94

00:04:17,280  -->  00:04:18,650
and admin can be two.
95

95

00:04:18,650  -->  00:04:20,380
And then, instead of checking a quality here,
96

96

00:04:20,380  -->  00:04:21,940
you can actually do greater than.
97

97

00:04:21,940  -->  00:04:24,470
And that'll let admins access the user passwords
98

98

00:04:24,470  -->  00:04:25,813
as well, if you want that.
99

99

00:04:28,010  -->  00:04:29,010
That's it for this video.
100

100

00:04:29,010  -->  00:04:31,520
I wanted to show you how to create decorators
101

101

00:04:31,520  -->  00:04:32,810
that have parameters.
102

102

00:04:32,810  -->  00:04:35,240
And I know that they are a bit confusing.
103

103

00:04:35,240  -->  00:04:37,640
There are three levels of functions,
104

104

00:04:37,640  -->  00:04:41,040
which make these the most complicated Python code
105

105

00:04:41,040  -->  00:04:42,740
that you'll see in most Python courses,
106

106

00:04:42,740  -->  00:04:45,910
and certainly throughout your programming, as well.
107

107

00:04:45,910  -->  00:04:47,010
But hope you've learned something.
108

108

00:04:47,010  -->  00:04:48,040
Hope you've enjoyed it.
109

109

00:04:48,040  -->  00:04:49,790
And I'll see you in the next video.
