These are going to be the solutions for the Unit 2 homework.
Remember, we had 2 parts to this.
We had the ROT13 tool, and then we had the sign up page
that tested for errors and that sort of thing.
I'll show you quickly my versions working,
and then I'll show you the code, so let's start with the ROT13.
Okay, here's my ROT13 running on my local machine here.
I could type in some text, and if I submit,
it gets ROT13'd inside the text box,
and if I submit again, it gets changed back to the original text,
and that's how you know it's working.
It escapes properly.
We can see that by typing in our closed text area there,
and it's preserving everything.
All right, so let's take a peek at the code.
So, I'm in an appengine.py file.
This is the base ROT13 handler that I wrote.
It inherits from this base handler class, the ROT13 class does.
That added some convenience functions.
First, I have a write function, and this is short hand,
so I can say self.write instead of self.response.out.write.
I'll just save some typing.
And then I started using something called templates, and I'm going to spend
a little bit more time on templates in Unit 3.
It saves me the effort of having to type in all of the string substitutions
that you probably did for this homework if you didn't already
get tired of that and figure out templates.
My base page--well, let's look at the URL handler first.
We've got /unit2/rot13 maps to the Rot13 class,
and so we come up here to our Rot13 class, and it's got 2 functions.
It's got a get function, and all this does is it renders
this file called rot13-form.html.
Now, your version, you may have printed a whole string.
This accomplishes that same thing, and I'll go ahead and show you that file.
It's right here.
We want to get in the habit going forward of starting to separate these things,
and I apologize for putting you through that heartache of
writing out long strings first, but you'll appreciate
the niceness of this once you've done it the hard way.
This just returns some HTML.
It's got our title.
It's got an h2, which makes some big text at the top of the page.
Here's our form. Its method is post.
And it's got a text area.
The text area has a name of text.
I added a little styling here to make it a little bigger.
This is just some basic CSS.
You're free to copy this from my demo when I put this source stuff online.
This syntax here, again, we'll talk about this more in the next lesson,
but instead of doing the string substitution,
this substitutes the variable text passed into the template,
and it automatically escapes it for me so I don't have to deal with that,
and this closes the text area, and then there's a submit button.
That's pretty simple.
If I were to turn off the auto escaping ability,
which is set by this parameter here, let me show you the behavior of that.
Okay, so here we've got some text in here.
Remember, I disabled the texting--I'm sorry.
I disabled the escaping, so let's see what happens when we submit this.
It works the first time, but the second time the text jumps outside the box.
Why did that happen?
Well, when we turn that ROT13 text area back into a closing text area,
it actually closed our text area in the HTML
and then put this text outside it.
If I were to view the source of this file, which you can do in Chrome easily,
we can see what happened.
You can see my template, and then you can see my text area gets closed
by the unescaped version of that text
and then "escapes properly" gets put outside the text area.
And then here is the closing text area for my template,
so we want to make sure if we go back to our editor here
and I turn on escaping again--this is something you're probably doing by hand,
which is how we learned to do it initially--and if I were to go to Chrome
and then reload this page--reload, that's going to confirm the form submission--
we can see that my text area actually got escaped.
And so this is what the template has, and this is how it renders.
Now, what's interesting to note is that even though our HTML
has the escaped version of less than and greater than,
it renders as less than and greater than, and when we submit the form,
it's as less than and greater than.
It's not as the escaped version, so hopefully you had a chance to
kind of experiment that and see for yourself,
but that's how that works.