Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Navigation based on logged in status and demo wrap up

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now, notice up here we have this home, albums, contact, right,
0:03 as we click around, the other thing I'd like to do is
0:06 add just a little bit of a concept of a logged in user.
0:10 We are going to get to user accounts and all that stuff later,
0:13 but let's start talking about the template a little bit up here.
0:16 Now notice, right now I have this part hidden, but if I put it back,
0:20 we have this section that says should you sign in or register
0:24 or are you already signed in, you can view account or log out.
0:28 And notice there is no real actions here but just from a template perspective,
0:31 we would like to show some or the other, so let's go back over here,
0:35 our base controller here, and let's add some kind of behavior here,
0:41 so we are going to have a property,
0:43 a property is basically a function that you interact with, like a field
0:47 but in fact it's a function call, and it's also read-only, so we have this,
0:52 we'll say is_logged_in, and we are going to "return" right now,
0:57 let's just "return True", yes, they are logged in.
1:01 So over here, let's go back for a moment,
1:04 our base controller has all of these attributes, or fields
1:09 and then it has these properties, our albums controller derives from that
1:13 so our albums controller has those properties as well,
1:17 so it has is_logged_in and so on, so that means over here I can do this,
1:23 remember, when speaking of the controller we say view.,
1:27 so we'll say, it's tal condition, is_logged_in,
1:30 actually, we don't want this, we want the opposite of that, so we'll say "not"
1:33 and then put this on those, we want this in the case you are logged in,
1:38 in here we want to when you are not logged in.
1:42 So let's rerun it, because we changed the Python code
1:45 which means we've got to restart the web app,
1:47 so over here you can see we have our account,
1:49 we have our logout and that's because in the base controller
1:52 we said yes, they are logged in, right now,
1:55 so if we come over here and look we have account,
1:57 and log out, because those are logged in users, if this was false,
2:01 if they weren't logged in... we refresh this, rerun the code and refresh it,
2:05 you can see now we have sign in and register,
2:07 so that's how we work with the template attribute language
2:11 with Chameleon templates and so on, let's just review.
2:14 We go from our albums controller, we get to our action method,
2:18 we do our data access, web service access, whatever calculations we want,
2:21 we return that as a dictionary, as a model, to the template.
2:25 Great, so the template then has access to the view
2:29 which is the instance of the controller class and all of its properties and fields and so on,
2:34 so we can say "tal conditional" don't show this if they are logged in,
2:38 do show this if they are logged in, or show this if they're not logged in rather.
2:42 And then we come down here and we say create little prototypical HTML block
2:48 that we want to work with and then we'll say "tal repeat"
2:51 and we'll create a local variable for each time through the loop
2:54 so here we are going to create - now that we are floating this we don't need this anymore,
2:57 we are going to create an image
3:00 and we are going to use the image URL from the "a" each album coming through
3:04 again, we want to set the title, use the tal conditional on whether or not it has a preview
3:09 and then we are going to go the albums tracks and loop over those as well.
3:14 And you can see that our albums are on the screen in a pretty decent way
3:19 that represents the data that is backing them.
3:22 Like I said, we'll make this more beautiful later but it's a good start.