Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Leveraging a base controller demo

Login or purchase this course to watch this video and the rest of the course contents.
0:02 There are few things over here that were not so smooth. For one you saw me forget to do this because in my web apps
0:09 I don't ever have these initializers on the top level controllers, but also this build_cache_id stuff, this is not so great,
0:17 so let's see how we can use a base controller to make all these shared pieces, you can see everyone is going to need to have the request,
0:24 it's going to have this initializer, we are going to want to use this build_cache_id all over the place
0:29 and as this complexity in features of our app grow, that requirement to share all these things is going to absolutely grow as well.
0:36 So what we are going to do is we are going to add a base controller. Like so, and all it's going to do is have the various common pieces
0:49 so we are going to start up by moving this down here, like so and then inheriting from BaseController
1:01 PyCharm imported it, you can see it's added the import line up here and, this built cache id thing, let's move this around,
1:08 so we can go down here and say "self_.'build_cache_id', so we are going to set basically field here that is this implementation,
1:18 like so, and we need to import this like we did above, here is our static cache utility method that we are going to put here,
1:26 we could actually take and move the implementation down here but I am not a big fan of doing that.
1:31 So now we can come up here and delete this, make this much simpler and do it similarly for these others, now when I run this,
1:42 it's not going to work quite as well as you would think, it's going to be missing the build_cache_id,
1:47 because of the way we are expressing it in the templates. See this "NameError: build_cache_id", it turns out before we were passing in the model
1:55 and now we have associated it with the underlined view, but that's no problem, we just need to make a small tweak.
2:02 Let's go over here, we can do a replace, we'll replace "this" with "view.this", so because we are storing it on the controller
2:13 it's now considered to be part of the view, remember the way the nomenclature pyramid is, controller is a view basically.
2:19 So if I do this and I say "replace all". There, I have replaced in all of them let's just look at this one, so now anytime we want to use our cache id
2:27 we'll just come here and say view.build_cache_id now let's try this again, perfect, everything is back the way we wanted it,
2:35 but we have a much simpler normal controllers, here you can see we just have the action decorator to set the template,
2:43 and then we just return a basic model, whatever is special about this method and the base controller handles the initialization,
2:51 it's going to handle common layout, it's handling these other utility features that we need, like this static cache id utility and so on,
2:59 So, like I said, as your app grows, this base controller is going to be the place where your common shared functionality lives,
3:06 and it's going to keep your normal methods you write much simpler and make the maintenance of your app across various action methods
3:14 and controllers much simpler.


Talk Python's Mastodon Michael Kennedy's Mastodon