1 00:00:04,960 --> 00:00:19,999 [Music] 2 00:00:20,560 --> 00:00:25,800 to yure 2 with three Jango talks the 3 00:00:23,119 --> 00:00:27,400 first one is called Jango rest and Open 4 00:00:25,800 --> 00:00:30,800 PI a gentle 5 00:00:27,400 --> 00:00:35,840 introduction and it's by Paul 6 00:00:30,800 --> 00:00:38,040 waer um I hate glasses uh Paul is a six 7 00:00:35,840 --> 00:00:40,160 foot tall ape descendant and nobody is 8 00:00:38,040 --> 00:00:42,200 currently trying to bypass through his 9 00:00:40,160 --> 00:00:45,360 home he works for an open source company 10 00:00:42,200 --> 00:00:47,800 and in spare time he maintains websites 11 00:00:45,360 --> 00:00:49,480 rides electric motorbike teaches Iris 12 00:00:47,800 --> 00:00:51,320 set dancing plays the piano reads 13 00:00:49,480 --> 00:00:53,640 Science Fiction and Fantasy 3D prints 14 00:00:51,320 --> 00:00:54,680 and collects esoteric Hobbies I wish I 15 00:00:53,640 --> 00:01:00,160 had that 16 00:00:54,680 --> 00:01:00,160 much welcome to your talk thank you 17 00:01:02,800 --> 00:01:08,759 thank you and greetings from Nal country 18 00:01:05,199 --> 00:01:11,320 where I come from um I have been 19 00:01:08,759 --> 00:01:15,640 programming for a fair while uh which is 20 00:01:11,320 --> 00:01:18,320 why I use boring uh default uh Libra 21 00:01:15,640 --> 00:01:20,360 office impress templates uh and 22 00:01:18,320 --> 00:01:21,880 currently I work at a small uh open 23 00:01:20,360 --> 00:01:25,360 source company that few people have 24 00:01:21,880 --> 00:01:28,439 heard of uh working on an API that only 25 00:01:25,360 --> 00:01:32,040 our customers can actually access uh so 26 00:01:28,439 --> 00:01:34,439 instead I thought I'd do my demo on one 27 00:01:32,040 --> 00:01:38,119 of my other Hobbies uh which is mixing 28 00:01:34,439 --> 00:01:42,320 electronic music um and I published them 29 00:01:38,119 --> 00:01:45,360 on uh my website uh each mix has a list 30 00:01:42,320 --> 00:01:47,880 of tracks uh is distributed through a 31 00:01:45,360 --> 00:01:51,560 number of files and can be linked to one 32 00:01:47,880 --> 00:01:54,840 or more Styles so I want to provide um 33 00:01:51,560 --> 00:01:58,119 an API for that uh so I'm going to 34 00:01:54,840 --> 00:02:02,719 assume that you know mostly about Python 35 00:01:58,119 --> 00:02:05,799 and Django uh and on the other hand if 36 00:02:02,719 --> 00:02:10,560 you already know a lot about Django rest 37 00:02:05,799 --> 00:02:13,400 framework uh open API uh and using that 38 00:02:10,560 --> 00:02:18,000 um the exits are there um I'll be 39 00:02:13,400 --> 00:02:21,599 leading the way um so let's talk a bit 40 00:02:18,000 --> 00:02:24,800 about what apis actually are uh a good 41 00:02:21,599 --> 00:02:27,720 talk on Friday I think by Ash said that 42 00:02:24,800 --> 00:02:30,000 uh rest is a really overused term and it 43 00:02:27,720 --> 00:02:32,440 probably doesn't mean what it used to 44 00:02:30,000 --> 00:02:36,800 mean basically all we're talking about 45 00:02:32,440 --> 00:02:39,959 here is an interface for programs to 46 00:02:36,800 --> 00:02:43,200 read data from something you publish 47 00:02:39,959 --> 00:02:46,720 usually a website um so that means that 48 00:02:43,200 --> 00:02:48,800 the data is going to come not in a nice 49 00:02:46,720 --> 00:02:51,480 template 50 00:02:48,800 --> 00:02:54,000 um the camera crew really wants me to 51 00:02:51,480 --> 00:02:56,080 stay in one place so I'm going to have 52 00:02:54,000 --> 00:03:01,319 to fight my instinct to point all over 53 00:02:56,080 --> 00:03:03,959 the place um so the um 54 00:03:01,319 --> 00:03:06,519 uh the programs are going to read this 55 00:03:03,959 --> 00:03:09,120 uh output so it's going to be in a 56 00:03:06,519 --> 00:03:12,159 standard format uh it's not going to be 57 00:03:09,120 --> 00:03:15,799 rendered nicely in HTML and things like 58 00:03:12,159 --> 00:03:18,239 that uh and in Django we have standard 59 00:03:15,799 --> 00:03:21,159 methods of doing this you've got models 60 00:03:18,239 --> 00:03:24,640 which store the data uh and then those 61 00:03:21,159 --> 00:03:26,720 views um get referenced through the URL 62 00:03:24,640 --> 00:03:29,439 grab the data from the models and 63 00:03:26,720 --> 00:03:32,159 present it in templates D Jango rest 64 00:03:29,439 --> 00:03:35,360 frame work works very similarly the only 65 00:03:32,159 --> 00:03:38,040 real change is that we use serializers 66 00:03:35,360 --> 00:03:42,080 instead of templates uh and I'll cover 67 00:03:38,040 --> 00:03:44,480 those in a sec so for my website uh if 68 00:03:42,080 --> 00:03:47,159 you go to mixers uh you get the list of 69 00:03:44,480 --> 00:03:49,920 all the mixers and if you go to mix 70 00:03:47,159 --> 00:03:52,920 number one you get the first mix that I 71 00:03:49,920 --> 00:03:54,480 produced uh and if you go to 72 00:03:52,920 --> 00:03:57,799 mixstyle 73 00:03:54,480 --> 00:04:01,519 SLR then you get all of mixes just of 74 00:03:57,799 --> 00:04:05,280 that style and we want to Pro provide uh 75 00:04:01,519 --> 00:04:08,239 the mixers API um which lists which has 76 00:04:05,280 --> 00:04:11,519 sort of very similar things we have a 77 00:04:08,239 --> 00:04:14,720 list of mixes and we want to publish um 78 00:04:11,519 --> 00:04:18,239 details on mix one and so we've all 79 00:04:14,720 --> 00:04:20,959 written view code like this um grabbing 80 00:04:18,239 --> 00:04:24,280 all of the mixes the latest news the 81 00:04:20,959 --> 00:04:29,479 last data mix was completed um and 82 00:04:24,280 --> 00:04:30,280 render that using a template uh likewise 83 00:04:29,479 --> 00:04:33,360 um 84 00:04:30,280 --> 00:04:35,479 mix details uh this is where we get a 85 00:04:33,360 --> 00:04:37,880 bit complicated and we might want to 86 00:04:35,479 --> 00:04:39,720 change this at some point because uh 87 00:04:37,880 --> 00:04:42,960 mixers have a number but they can also 88 00:04:39,720 --> 00:04:46,280 have multiple Parts I some mixers I did 89 00:04:42,960 --> 00:04:49,400 two parts on the same day uh some mixes 90 00:04:46,280 --> 00:04:51,280 I have gone back and reworked and so the 91 00:04:49,400 --> 00:04:56,080 rework is mix 92 00:04:51,280 --> 00:04:59,039 1.2 um so we've all written view code a 93 00:04:56,080 --> 00:05:01,520 bit like this now one of the things that 94 00:04:59,039 --> 00:05:04,520 I really love about Jango Jango and I 95 00:05:01,520 --> 00:05:09,400 think more Frameworks out there should 96 00:05:04,520 --> 00:05:12,840 embrace this principle is don't repeat 97 00:05:09,400 --> 00:05:16,960 yourself but we seem to have written 98 00:05:12,840 --> 00:05:20,080 that code a lot for view list views and 99 00:05:16,960 --> 00:05:22,000 detail views and search views and you 100 00:05:20,080 --> 00:05:24,240 know create views all of those sort of 101 00:05:22,000 --> 00:05:26,800 things it's the same code over and over 102 00:05:24,240 --> 00:05:29,160 again and so normally when we go through 103 00:05:26,800 --> 00:05:31,160 this Evolution uh we get to we start 104 00:05:29,160 --> 00:05:32,800 writing our own views and then we find 105 00:05:31,160 --> 00:05:35,479 out that someone's done this before so 106 00:05:32,800 --> 00:05:37,800 we get generic views and then we we hit 107 00:05:35,479 --> 00:05:40,400 the limitations on generic views really 108 00:05:37,800 --> 00:05:42,160 quickly uh and usually we get told yeah 109 00:05:40,400 --> 00:05:44,160 actually you have to go back and 110 00:05:42,160 --> 00:05:49,280 reimplement all of your 111 00:05:44,160 --> 00:05:52,479 stuff so Jango rest framework has uh an 112 00:05:49,280 --> 00:05:54,639 idea called view sets um I'm going to 113 00:05:52,479 --> 00:05:57,000 talk about that uh as one of the 114 00:05:54,639 --> 00:05:59,479 fundamental things to take away from 115 00:05:57,000 --> 00:06:03,160 Dango rest framework what do they look 116 00:05:59,479 --> 00:06:07,599 like like so here's uh a really basic 117 00:06:03,160 --> 00:06:11,400 view set that I wrote um for mixers um 118 00:06:07,599 --> 00:06:15,280 we're using the readon model view set so 119 00:06:11,400 --> 00:06:17,800 that um will introspect my model I just 120 00:06:15,280 --> 00:06:21,479 tell it the query set that I want to 121 00:06:17,800 --> 00:06:23,800 look at and then the serializer class 122 00:06:21,479 --> 00:06:25,880 that is going to take that data and 123 00:06:23,800 --> 00:06:30,639 present it to the outside 124 00:06:25,880 --> 00:06:33,199 world that's it that has all the inbuilt 125 00:06:30,639 --> 00:06:35,319 code to handle the list View and the 126 00:06:33,199 --> 00:06:38,280 detail view it's also got all the 127 00:06:35,319 --> 00:06:40,840 inbuilt code to say if someone tries to 128 00:06:38,280 --> 00:06:43,720 say create a new mix and this is a 129 00:06:40,840 --> 00:06:48,280 readon model view set um then they get a 130 00:06:43,720 --> 00:06:52,280 405 method not allowed also really nice 131 00:06:48,280 --> 00:06:55,759 if I want to change uh how that mix set 132 00:06:52,280 --> 00:06:58,440 is um presents its detail so instead of 133 00:06:55,759 --> 00:07:00,080 the um number I want to present a much 134 00:06:58,440 --> 00:07:02,319 more readable slide 135 00:07:00,080 --> 00:07:05,360 all I have to do is just add the lookup 136 00:07:02,319 --> 00:07:07,960 field Slug and that will know to get the 137 00:07:05,360 --> 00:07:10,599 right field out of the mix qu the query 138 00:07:07,960 --> 00:07:14,840 set when it's looking for that detail 139 00:07:10,599 --> 00:07:19,919 view all the 404s everything handled 140 00:07:14,840 --> 00:07:19,919 just fine that's really really 141 00:07:20,039 --> 00:07:24,120 compact so let's talk about that 142 00:07:22,280 --> 00:07:26,039 serializer that I was kind of glossing 143 00:07:24,120 --> 00:07:30,560 over before 144 00:07:26,039 --> 00:07:33,120 um D Jango uh we have template to Output 145 00:07:30,560 --> 00:07:34,840 data from the internal representation 146 00:07:33,120 --> 00:07:37,560 out to the outside world and we've got 147 00:07:34,840 --> 00:07:40,479 forms to take data from the outside 148 00:07:37,560 --> 00:07:44,960 world and turn take it into our Jango 149 00:07:40,479 --> 00:07:47,879 model template so for um Django rest 150 00:07:44,960 --> 00:07:50,919 framework the serializer does both of 151 00:07:47,879 --> 00:07:53,280 those functions what does a serializer 152 00:07:50,919 --> 00:07:56,120 look like well here's the mixed 153 00:07:53,280 --> 00:07:59,159 serializer and we give it we based on 154 00:07:56,120 --> 00:08:02,080 the model serializer class uh we give it 155 00:07:59,159 --> 00:08:04,520 the model and we tell it which fields we 156 00:08:02,080 --> 00:08:06,599 want to 157 00:08:04,520 --> 00:08:10,319 Output 158 00:08:06,599 --> 00:08:12,599 um the complication here remember I 159 00:08:10,319 --> 00:08:15,840 talked about that number and part those 160 00:08:12,599 --> 00:08:17,960 two Fields aren't really completely 161 00:08:15,840 --> 00:08:21,199 independent we'd like to present them as 162 00:08:17,960 --> 00:08:25,199 one thing um there's a couple of ways we 163 00:08:21,199 --> 00:08:28,759 can do that one is we can take the 164 00:08:25,199 --> 00:08:31,360 describe the the field a bit more and we 165 00:08:28,759 --> 00:08:35,279 can use a serializer method 166 00:08:31,360 --> 00:08:38,880 field um that looks for a get uncore 167 00:08:35,279 --> 00:08:42,640 field name method in the serializer uh 168 00:08:38,880 --> 00:08:45,680 and then outputs the number or the part 169 00:08:42,640 --> 00:08:49,839 if it has a part but that's only read 170 00:08:45,680 --> 00:08:52,480 only we can't um put data back into that 171 00:08:49,839 --> 00:08:55,240 so instead what we might do if we want 172 00:08:52,480 --> 00:08:56,880 to uh input at some point I might 173 00:08:55,240 --> 00:09:00,040 actually like to have an external 174 00:08:56,880 --> 00:09:04,120 program create these things for me 175 00:09:00,040 --> 00:09:07,399 um we Define our own field and the all 176 00:09:04,120 --> 00:09:10,920 the field really needs to do is do two 177 00:09:07,399 --> 00:09:14,720 things one is take the internal view the 178 00:09:10,920 --> 00:09:16,640 internal mix and um output the 179 00:09:14,720 --> 00:09:19,760 representation we want the external 180 00:09:16,640 --> 00:09:22,560 world to see the other is uh taking the 181 00:09:19,760 --> 00:09:27,200 external representation and using the 182 00:09:22,560 --> 00:09:32,079 magic of regexes and named Rix 183 00:09:27,200 --> 00:09:35,680 um Fields uh uh and group dict uh we 184 00:09:32,079 --> 00:09:38,839 just return a dictionary um if we got a 185 00:09:35,680 --> 00:09:42,240 match uh and that dictionary contains 186 00:09:38,839 --> 00:09:44,320 the number and the part Fields uh and if 187 00:09:42,240 --> 00:09:47,160 we didn't match then there's some kind 188 00:09:44,320 --> 00:09:49,519 of validation error so raise that and 189 00:09:47,160 --> 00:09:51,160 that's all we need for our mixed number 190 00:09:49,519 --> 00:09:54,200 field we just make sure that we 191 00:09:51,160 --> 00:09:57,120 reference mix number in the fields list 192 00:09:54,200 --> 00:10:00,720 um as opposed to the number and the 193 00:09:57,120 --> 00:10:02,360 number and part Fields like likewise uh 194 00:10:00,720 --> 00:10:05,320 we can have a track 195 00:10:02,360 --> 00:10:07,560 serializer um that just is based on the 196 00:10:05,320 --> 00:10:11,519 track and outputs the number and artist 197 00:10:07,560 --> 00:10:14,800 and title and we can include that track 198 00:10:11,519 --> 00:10:18,279 serializer as a field in our mix 199 00:10:14,800 --> 00:10:20,079 serializer We Tell It many equals true 200 00:10:18,279 --> 00:10:22,839 which means that that is going to Output 201 00:10:20,079 --> 00:10:25,279 a list of fields not just a single field 202 00:10:22,839 --> 00:10:28,160 and we give it the source which will be 203 00:10:25,279 --> 00:10:31,560 the foreign key relationship from the 204 00:10:28,160 --> 00:10:34,800 mix if you're you're doing uh in the you 205 00:10:31,560 --> 00:10:37,760 know Django uh you've got Django shell 206 00:10:34,800 --> 00:10:39,880 uh and you do mix. track set that will 207 00:10:37,760 --> 00:10:43,279 give you the query set of tracks within 208 00:10:39,880 --> 00:10:46,160 the mix um so that's the that's the 209 00:10:43,279 --> 00:10:49,720 source we're looking at that's all I 210 00:10:46,160 --> 00:10:53,680 need to do to include all of that data 211 00:10:49,720 --> 00:10:57,519 as a su list in my actual 212 00:10:53,680 --> 00:10:59,920 mix that is really convenient I have to 213 00:10:57,519 --> 00:11:04,079 say Okay so 214 00:10:59,920 --> 00:11:06,720 um we've got our mix view set um how do 215 00:11:04,079 --> 00:11:10,600 we actually present that to the outside 216 00:11:06,720 --> 00:11:15,959 world so in Django uh we would uh create 217 00:11:10,600 --> 00:11:19,839 the URLs list uh as a list of paths um 218 00:11:15,959 --> 00:11:23,760 which link the actual um text fragment 219 00:11:19,839 --> 00:11:26,160 to the view method or class um and that 220 00:11:23,760 --> 00:11:30,880 looks something like this so we have the 221 00:11:26,160 --> 00:11:34,120 index view um based on the blank path um 222 00:11:30,880 --> 00:11:36,160 linking the to the views. index function 223 00:11:34,120 --> 00:11:39,480 uh and they're named so we can refer to 224 00:11:36,160 --> 00:11:41,480 them in tests uh likewise we have two 225 00:11:39,480 --> 00:11:44,519 linking to the mixed page depending on 226 00:11:41,480 --> 00:11:47,920 whether we got a number or a number. 227 00:11:44,519 --> 00:11:51,800 part in D Jango rest framework this 228 00:11:47,920 --> 00:11:55,560 works slightly differently um we it has 229 00:11:51,800 --> 00:11:59,399 the concept of a router and routers 230 00:11:55,560 --> 00:12:04,160 gather view sets and they can enumerate 231 00:11:59,399 --> 00:12:07,560 the um methods provided by that view set 232 00:12:04,160 --> 00:12:10,360 and turn them into 233 00:12:07,560 --> 00:12:12,560 URLs that in your code will look 234 00:12:10,360 --> 00:12:16,000 something like this so we import the 235 00:12:12,560 --> 00:12:19,000 routers uh we create a simple router and 236 00:12:16,000 --> 00:12:22,680 we register against the mix path 237 00:12:19,000 --> 00:12:25,600 fragment uh we register the mix view set 238 00:12:22,680 --> 00:12:30,240 and then we just include that router. 239 00:12:25,600 --> 00:12:33,160 URLs in our API path 240 00:12:30,240 --> 00:12:38,120 um because I've been doing this a while 241 00:12:33,160 --> 00:12:41,000 and I don't do live demos very well um 242 00:12:38,120 --> 00:12:47,920 this is the copy and paste of my live 243 00:12:41,000 --> 00:12:51,959 demo um and that uh query basically so 244 00:12:47,920 --> 00:12:54,839 we mapped mix and then it looked at yes 245 00:12:51,959 --> 00:12:57,480 there's actually a part after this so 246 00:12:54,839 --> 00:12:59,760 this must be a detail view that's the 247 00:12:57,480 --> 00:13:03,519 slug that we gave it 248 00:12:59,760 --> 00:13:06,480 it takes the slug puts it into the um 249 00:13:03,519 --> 00:13:07,399 query set finds a single item so we 250 00:13:06,480 --> 00:13:10,720 didn't get a 251 00:13:07,399 --> 00:13:13,680 404 um gets that item feeds it through 252 00:13:10,720 --> 00:13:19,199 the serializer output outputs 253 00:13:13,680 --> 00:13:22,560 that um the so the view set and the 254 00:13:19,199 --> 00:13:25,920 router has known how to map all of that 255 00:13:22,560 --> 00:13:27,959 path information back into uh an actual 256 00:13:25,920 --> 00:13:31,040 query and we haven't had to spell that 257 00:13:27,959 --> 00:13:34,360 out at all uh so that works for the the 258 00:13:31,040 --> 00:13:38,240 list view works for the detail view we 259 00:13:34,360 --> 00:13:41,079 can also add extra actions so here we 260 00:13:38,240 --> 00:13:43,440 use the action decorator um and we tell 261 00:13:41,079 --> 00:13:45,560 it at detail equals false we're going to 262 00:13:43,440 --> 00:13:50,399 return a just the mixes and a random 263 00:13:45,560 --> 00:13:51,959 order I don't know it's a demo um and 264 00:13:50,399 --> 00:13:56,560 here we need to do a little bit more 265 00:13:51,959 --> 00:13:59,240 work uh this is not um a simple thing um 266 00:13:56,560 --> 00:14:02,160 in terms you know compared to just using 267 00:13:59,240 --> 00:14:04,880 the default list output but all this 268 00:14:02,160 --> 00:14:08,480 this is what your standard this is sort 269 00:14:04,880 --> 00:14:12,000 of the list or detail um methods would 270 00:14:08,480 --> 00:14:14,240 look like this so U they start with the 271 00:14:12,000 --> 00:14:17,759 request um we're going to return a 272 00:14:14,240 --> 00:14:19,759 response object we get our serializer we 273 00:14:17,759 --> 00:14:22,959 give it the query set that we want it to 274 00:14:19,759 --> 00:14:25,680 serialize We Tell It many equals true so 275 00:14:22,959 --> 00:14:28,040 that it it will output a list and then 276 00:14:25,680 --> 00:14:29,839 we ask for the data out of that 277 00:14:28,040 --> 00:14:32,959 serializer 278 00:14:29,839 --> 00:14:37,560 the response then feeds that through the 279 00:14:32,959 --> 00:14:43,560 renderer to Output Jason or yaml or CSV 280 00:14:37,560 --> 00:14:46,160 or whatever um and so out out of that um 281 00:14:43,560 --> 00:14:48,519 comes a chunk of 282 00:14:46,160 --> 00:14:51,720 Json but most 283 00:14:48,519 --> 00:14:55,800 importantly when I add that I don't have 284 00:14:51,720 --> 00:14:58,120 to change the router or the URLs at all 285 00:14:55,800 --> 00:14:59,639 the router automatically introspects 286 00:14:58,120 --> 00:15:02,959 finds that action 287 00:14:59,639 --> 00:15:05,720 adds that to the set of paths that it 288 00:15:02,959 --> 00:15:08,079 recognizes that is don't repeat 289 00:15:05,720 --> 00:15:10,880 yourself okay so where was I up to we 290 00:15:08,079 --> 00:15:13,320 need to talk a bit about uh 291 00:15:10,880 --> 00:15:15,360 authentication um this is a little bit 292 00:15:13,320 --> 00:15:18,440 more complicated these are the things 293 00:15:15,360 --> 00:15:22,519 that I found myself stumbling over a 294 00:15:18,440 --> 00:15:25,600 little bit um but if we wanted something 295 00:15:22,519 --> 00:15:29,120 where I could um write a program to add 296 00:15:25,600 --> 00:15:33,839 new mixes um but everyone else just sees 297 00:15:29,120 --> 00:15:36,639 standard view um we there are two phases 298 00:15:33,839 --> 00:15:39,639 to Jango rest Frameworks sort of 299 00:15:36,639 --> 00:15:41,519 authentication permission system one is 300 00:15:39,639 --> 00:15:44,360 the authentication so it looks at the 301 00:15:41,519 --> 00:15:45,800 request you've made sees if there's any 302 00:15:44,360 --> 00:15:49,480 uh information in there that 303 00:15:45,800 --> 00:15:52,959 authenticates you grabs that tries um 304 00:15:49,480 --> 00:15:56,600 tries to see if it's valid uh and then 305 00:15:52,959 --> 00:15:59,120 if it is one of those authentication 306 00:15:56,600 --> 00:16:01,880 authentication classes will return and 307 00:15:59,120 --> 00:16:04,600 your request object will now have an or 308 00:16:01,880 --> 00:16:06,240 property which has the authentication 309 00:16:04,600 --> 00:16:10,079 information in 310 00:16:06,240 --> 00:16:14,880 it um the permission 311 00:16:10,079 --> 00:16:17,279 class uh then checks which of the 312 00:16:14,880 --> 00:16:20,279 whether you're actually allowed to do 313 00:16:17,279 --> 00:16:22,319 the request that you have asked to do so 314 00:16:20,279 --> 00:16:25,120 here I'm using the standard is 315 00:16:22,319 --> 00:16:28,319 authenticator or if is authenticated or 316 00:16:25,120 --> 00:16:31,199 readon permission um does kind of what 317 00:16:28,319 --> 00:16:33,560 it says on the tin um there's only one 318 00:16:31,199 --> 00:16:35,839 authenticated user on my website and 319 00:16:33,560 --> 00:16:38,519 that's me so that's really simple I 320 00:16:35,839 --> 00:16:43,279 didn't have to work out who uh who else 321 00:16:38,519 --> 00:16:45,920 was allowed to see uh or create mixes um 322 00:16:43,279 --> 00:16:48,199 OB if you are if you need more 323 00:16:45,920 --> 00:16:51,639 information uh in your permission class 324 00:16:48,199 --> 00:16:54,880 about exactly who is allow allowed to um 325 00:16:51,639 --> 00:16:58,079 access exactly what you'd modify uh your 326 00:16:54,880 --> 00:17:00,040 own permission class the trick with 327 00:16:58,079 --> 00:17:02,519 permission class is where where the 328 00:17:00,040 --> 00:17:06,079 authentication class the first one of 329 00:17:02,519 --> 00:17:09,360 those to authenticate wins and it says 330 00:17:06,079 --> 00:17:13,839 yes authenticated uh the permission 331 00:17:09,360 --> 00:17:16,919 class um if the if any one of those 332 00:17:13,839 --> 00:17:20,640 returns false then you're 333 00:17:16,919 --> 00:17:24,120 denied uh you can actually a and and 334 00:17:20,640 --> 00:17:26,640 these together which is pretty neat U so 335 00:17:24,120 --> 00:17:30,280 you can make a combination of permission 336 00:17:26,640 --> 00:17:33,120 classes um but the idea with your 337 00:17:30,280 --> 00:17:36,720 permission class is that if there's if 338 00:17:33,120 --> 00:17:40,320 it if it doesn't say 339 00:17:36,720 --> 00:17:43,320 yes then and it says no Then whichever 340 00:17:40,320 --> 00:17:44,360 one of those uh says no will win and 341 00:17:43,320 --> 00:17:49,480 you'll get 342 00:17:44,360 --> 00:17:51,880 denied okay testing um there's not a lot 343 00:17:49,480 --> 00:17:55,039 to say about testing everyone here is 344 00:17:51,880 --> 00:17:59,679 obviously uh doing all of their tests on 345 00:17:55,039 --> 00:18:01,640 their Dango um systems so the only real 346 00:17:59,679 --> 00:18:04,919 thing that I want yes I lots of heads 347 00:18:01,640 --> 00:18:07,080 nodding thumbs up yeah agreed um so the 348 00:18:04,919 --> 00:18:12,200 only thing I really want to point out 349 00:18:07,080 --> 00:18:15,720 here is uh the reverse name um that has 350 00:18:12,200 --> 00:18:19,559 been created by the router out of the 351 00:18:15,720 --> 00:18:23,360 path fragment that we gave it um or a 352 00:18:19,559 --> 00:18:27,720 base name if we supplied that and the 353 00:18:23,360 --> 00:18:30,799 view name so list handles lists details 354 00:18:27,720 --> 00:18:36,679 handles detailed View use 355 00:18:30,799 --> 00:18:42,240 um when we um post to if we want to 356 00:18:36,679 --> 00:18:46,480 create uh a mix We post to the mix uh 357 00:18:42,240 --> 00:18:49,640 list View and so that's kind of uh how 358 00:18:46,480 --> 00:18:53,320 that it's not named mixor create at that 359 00:18:49,640 --> 00:18:56,840 point um but those names come for free 360 00:18:53,320 --> 00:18:58,960 again we don't repeat ourselves that's 361 00:18:56,840 --> 00:19:04,400 nice as well 362 00:18:58,960 --> 00:19:08,080 okay so open API uh what is it um it 363 00:19:04,400 --> 00:19:10,720 used to be called Swagger um and it's 364 00:19:08,080 --> 00:19:14,919 basically in the same way that apis 365 00:19:10,720 --> 00:19:19,280 allow programs to talk to your 366 00:19:14,919 --> 00:19:22,280 um website and get information um the 367 00:19:19,280 --> 00:19:25,240 open API standard allows the program 368 00:19:22,280 --> 00:19:27,760 talking to you to see what that data is 369 00:19:25,240 --> 00:19:31,200 going to look like what queries it can 370 00:19:27,760 --> 00:19:34,720 um give make on your you know what what 371 00:19:31,200 --> 00:19:38,120 creery parameters what the uh example 372 00:19:34,720 --> 00:19:41,720 data that you might Supply looks like uh 373 00:19:38,120 --> 00:19:45,559 that often is supplied as yaml uh here's 374 00:19:41,720 --> 00:19:48,799 an example um and it basically lists the 375 00:19:45,559 --> 00:19:51,520 paths uh combined with a 376 00:19:48,799 --> 00:19:56,240 description um which you might 377 00:19:51,520 --> 00:20:00,320 recognize um from the model that we 378 00:19:56,240 --> 00:20:03,320 um uh from what the model we supplied 379 00:20:00,320 --> 00:20:04,799 and then it lists content it lists a 380 00:20:03,320 --> 00:20:06,080 whole bunch of other stuff don't worry 381 00:20:04,799 --> 00:20:10,360 too much about 382 00:20:06,080 --> 00:20:12,720 that uh so I'm using the uh package drf 383 00:20:10,360 --> 00:20:15,799 spectacular um there used to be an old 384 00:20:12,720 --> 00:20:20,039 one drf YK uh they only coped with open 385 00:20:15,799 --> 00:20:22,960 API 2 uh spectacular is the three 386 00:20:20,039 --> 00:20:25,919 variant um that you need a little bit 387 00:20:22,960 --> 00:20:28,720 more setup you um install it and then 388 00:20:25,919 --> 00:20:32,080 you need to plug in um the spectacular 389 00:20:28,720 --> 00:20:34,360 settings in your settings Pi file uh the 390 00:20:32,080 --> 00:20:37,840 main obviously fill in your title and 391 00:20:34,360 --> 00:20:41,000 description uh don't use mine um the 392 00:20:37,840 --> 00:20:44,200 main one to point out here that I really 393 00:20:41,000 --> 00:20:47,799 like is serve public normally that 394 00:20:44,200 --> 00:20:50,480 defaults to true what that does is it 395 00:20:47,799 --> 00:20:53,720 will uh when you request the open API 396 00:20:50,480 --> 00:20:55,280 schema you it is you see every 397 00:20:53,720 --> 00:20:59,080 possible 398 00:20:55,280 --> 00:21:01,200 um method every possible path every 399 00:20:59,080 --> 00:21:05,320 possible 400 00:21:01,200 --> 00:21:07,200 um uh serializer information that that 401 00:21:05,320 --> 00:21:11,000 would come out of your website when you 402 00:21:07,200 --> 00:21:14,880 set that to false the view the user gets 403 00:21:11,000 --> 00:21:16,720 of your open API schema is what they can 404 00:21:14,880 --> 00:21:19,360 do on the 405 00:21:16,720 --> 00:21:21,679 website which means you if you're 406 00:21:19,360 --> 00:21:23,799 providing a Reon schema to 407 00:21:21,679 --> 00:21:26,240 unauthenticated users they don't get 408 00:21:23,799 --> 00:21:28,960 confused as to why they might be able to 409 00:21:26,240 --> 00:21:30,960 post or delete website and they don't 410 00:21:28,960 --> 00:21:32,360 get confused when they then get told no 411 00:21:30,960 --> 00:21:35,159 you're not 412 00:21:32,360 --> 00:21:37,360 allowed okay we also need to plug in a 413 00:21:35,159 --> 00:21:40,320 couple of things in our uh this is my 414 00:21:37,360 --> 00:21:43,200 main uh root paths file so we've include 415 00:21:40,320 --> 00:21:47,679 the mixers URLs and we just include we 416 00:21:43,200 --> 00:21:51,240 use the spectacular API view um and then 417 00:21:47,679 --> 00:21:54,320 we uh use if you're using the Dango rest 418 00:21:51,240 --> 00:21:58,080 drf spectacular sidecar package you get 419 00:21:54,320 --> 00:22:00,279 a Swagger API for free um you include 420 00:21:58,080 --> 00:22:03,080 that and you point it to the URL name 421 00:22:00,279 --> 00:22:04,880 that we declared for the other uh open 422 00:22:03,080 --> 00:22:09,360 API 423 00:22:04,880 --> 00:22:12,799 URL uh and that allows us to generate a 424 00:22:09,360 --> 00:22:16,880 nice content and because that is a demo 425 00:22:12,799 --> 00:22:19,880 this is a demo um it gives you this nice 426 00:22:16,880 --> 00:22:20,720 uh spec that you can go in and try these 427 00:22:19,880 --> 00:22:25,240 things 428 00:22:20,720 --> 00:22:27,640 out so there's lots more uh information 429 00:22:25,240 --> 00:22:30,600 this has really just been a very basic 430 00:22:27,640 --> 00:22:33,000 introduction to how to use uh Jango rest 431 00:22:30,600 --> 00:22:36,600 framework Dr spectacular those are the 432 00:22:33,000 --> 00:22:39,360 bits that I stumbled most on um thank 433 00:22:36,600 --> 00:22:39,360 you very much for your 434 00:22:43,880 --> 00:22:51,480 time thank you very much Paul about um 435 00:22:48,440 --> 00:22:53,799 but uh that's just that's why we have 436 00:22:51,480 --> 00:22:56,640 those two particular views uh if you 437 00:22:53,799 --> 00:22:59,880 don't want to provide that particular 438 00:22:56,640 --> 00:23:03,159 output you only use the first open API 439 00:22:59,880 --> 00:23:05,919 schema and that's all you need to 440 00:23:03,159 --> 00:23:09,520 present cool we have time for one more 441 00:23:05,919 --> 00:23:09,520 question up the back 442 00:23:11,880 --> 00:23:17,080 there hi thanks for the talk let's say 443 00:23:14,840 --> 00:23:20,159 you join a hypothetical company and they 444 00:23:17,080 --> 00:23:23,320 give you ajango app and the view.py is 445 00:23:20,159 --> 00:23:25,480 6,500 lines long what would you do to 446 00:23:23,320 --> 00:23:29,600 dry it 447 00:23:25,480 --> 00:23:34,799 up I that is I'm totally sympathetic to 448 00:23:29,600 --> 00:23:37,559 that particular situation um so that's 449 00:23:34,799 --> 00:23:39,640 where I what I really like and the 450 00:23:37,559 --> 00:23:45,440 reason I approached this from the point 451 00:23:39,640 --> 00:23:48,960 of view of um taking my existing website 452 00:23:45,440 --> 00:23:53,360 and adding an API to it is it's really 453 00:23:48,960 --> 00:23:57,360 easy to just bolt on that uh mix view 454 00:23:53,360 --> 00:24:03,640 set use the router and get a simple 455 00:23:57,360 --> 00:24:06,200 output um and you can supply that as uh 456 00:24:03,640 --> 00:24:09,520 say SL API 457 00:24:06,200 --> 00:24:12,480 slv2 and now you can do ab comparison 458 00:24:09,520 --> 00:24:16,159 between uh version one and version two 459 00:24:12,480 --> 00:24:20,039 of your API to see if the queries match 460 00:24:16,159 --> 00:24:24,279 and the responses match um and I would 461 00:24:20,039 --> 00:24:29,919 just chisel away gradually adding mixes 462 00:24:24,279 --> 00:24:34,279 sorry adding um models adding view sets 463 00:24:29,919 --> 00:24:36,000 one by one um so don't try to do a whole 464 00:24:34,279 --> 00:24:39,840 don't try to replace the whole thing in 465 00:24:36,000 --> 00:24:42,679 one go just um add little bits at a time 466 00:24:39,840 --> 00:24:45,120 and the fact that uh for example with 467 00:24:42,679 --> 00:24:48,960 the track list 468 00:24:45,120 --> 00:24:52,720 um the that I can add that as a single 469 00:24:48,960 --> 00:24:57,320 field that gets expressed as a a list 470 00:24:52,720 --> 00:25:00,760 within the Json output there uh means I 471 00:24:57,320 --> 00:25:05,200 don't actually have to try to replace 472 00:25:00,760 --> 00:25:09,559 every single output in that 473 00:25:05,200 --> 00:25:12,039 um uh that big set of URLs maybe I just 474 00:25:09,559 --> 00:25:15,960 need one piece of data that contains all 475 00:25:12,039 --> 00:25:18,679 of the information that a person like or 476 00:25:15,960 --> 00:25:20,159 a a script like the UI would want to 477 00:25:18,679 --> 00:25:24,720 know about that 478 00:25:20,159 --> 00:25:27,440 mix so see yeah again uh I like the the 479 00:25:24,720 --> 00:25:29,840 approach of try to do as little as 480 00:25:27,440 --> 00:25:32,679 possible in that sort of situation see 481 00:25:29,840 --> 00:25:36,399 how uh see how 482 00:25:32,679 --> 00:25:38,120 few um view sets you can use to Pro 483 00:25:36,399 --> 00:25:40,559 provide the information that your 484 00:25:38,120 --> 00:25:40,559 customers 485 00:25:40,679 --> 00:25:48,819 want thank you very much 486 00:25:43,400 --> 00:25:48,819 [Applause]