Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask REST API API Protection Password hashing

stuck please assist

Challenge Task 3 of 3 Almost done!

Now I need you to update the create_user method so that it sets the User instance's password using the User.hash_password method you just created. You should see the TODO for where to add this.

import datetime

from peewee import *

from argon2 import PasswordHasher

DATABASE = SqliteDatabase('recipes.db')
HASHER = PasswordHasher()

class User(Model):
    username = CharField(unique=True)
    password = CharField()

    class Meta:
        database = DATABASE

    def create_user(cls, username, password):
        except cls.DoesNotExist:
            user = cls(username=username)
            # TODO: hash user password here?
            return user
            raise Exception("User already exists")        

    def hash_password(password):
        return HASHER.hash(password)

class Recipe(Model):
    name = CharField()
    created_at = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = DATABASE

class Ingredient(Model):
    name = CharField()
    description = CharField()
    quantity = DecimalField()
    measurement_type = CharField()
    recipe = ForeignKeyField(Recipe)

    class Meta:
        database = DATABASE

def initialize():
    DATABASE.create_tables([User, Recipe, Ingredient], safe=True)

1 Answer

Steven Parker
Steven Parker
217,557 Points

The instructions are pretty explicit for the last task, you just need to replace the "TODO" line (or add one after it) to assign the "password" property of the instance that was just created. Call the method as given in the password, and pass it the correct argument.

i figured it out #TODO==>> user.password = user.hash_password(password) Thanx

Steven Parker
Steven Parker
217,557 Points

Good job! :+1: You can mark a question solved by choosing a "best answer".
And happy coding!