Day 9: passing words

Roo Harrigan
Making Athena
Published in
3 min readNov 4, 2015

>>> Brief summary

Athen now has sign up, login, and logout enabled! I can sign up users, check them to be sure their emails are unique and not already stored in the database, allow them login and logout with appropriate navbar buttons, and restrict access to the site’s main features based on login with a nifty little @login_required app decorator. Woot!

I also tackled the password hashing challenge with the help of a wonderful mentor and a little bit of applied unicode knowledge. Here’s how it went down:

I was still getting the not-so-friendly “Error: Unicode object must be encoded before hashing” during my sign-in route, at the point where the app checks the user’s entered password string against the hashed password that is stored for them in the database. The password string going in (during the signup route) was getting hashed just fine, but when it came out of postgreSQL it was :: gasp :: not coming out as a string at all, but a unicode object. As they say at Hackbright: “What. What?”

I had defined my User class with an attribute that looked like this:

password_hash = db.Column(db.String, nullable=False)

So the first thing we did was drop the table, change the type to db.LargeInteger, and convert the password to binary before hashing it. It came out of the hash just fine (no unicode at all, just plain base-10 numbers all the way) and my sign in route was working. Hurray!

But that sort of irked me all day. Why would I have to turn a password into a binary string? It was going in to postgreSQL just fine, I wasn’t getting back out what I wanted. So I changed the check_password function to .encode the password as it came out of the hash:

def check_password(self, password): encoded_pw = self.password_hash.encode(‘utf-8’) return bcrypt.hashpw(password, encoded_pw) == encoded_pw

Boom. Unicode defeated. In this specific tiny instance.

>>> Where I struggled

Modal windows and WTForms with Jinja

Eventually, I want my signup/login form to be a modal window, and since a number of girls were working on getting those set up tonight, I thought I’d take a break and work on making one, so as to be in the company of peers. The problem is that I’m the only one using WTForms to make my forms, which means (I think) my app has to go back to the server to make the form object before it can render the form, and I couldn’t get that to happen and feed the form variables to the modal window for display. It was like the modal window action was happening first. I tried wrapping the modal button in an <a> tag that referenced the route, a form with that route as an action, and a bunch of other weird permutations. Sad failure on the modal window, but I quit at the hour mark. It’s not in the MVP anyway.

Better wikipedia page, broken seed

I found a much cleaner wikipedia page, off of which I’m hoping to grab countries and capitals in a nicer way from the API, but unfortunately switching over to it broke my seed.py and I have been mucking around with it all night. I’ve still got about half my countries in my database though so the quiz is functional for testing purposes. Still battling the unicode errors that are coming over from Wikipedia. I’m starting to better appreciate the mwparserfromhell library more fully. Goal for tomorrow: fix seed and get continent information in!

>>> Thoughtful takeaway

Eating Halloween candy, or really any nasty refined sugars, can really negatively affect your outlook on your own ability to write code. And your affects controls your actual ability. Things look a whole lot brighter after a good night’s sleep, a yoga class, and a good balance of protein and coffee in the morning.

--

--