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

update the create_user method so that it sets the User instance's password using the User.hash_password method you just

update the create_user method so that it sets the User instance's password using the User.hash_password method you just created.

models.py
import datetime

from flask import PasswordHasher

from peewee import *

DATABASE = SqliteDatabase('courses.sqlite')
HASHER = PasswordHasher()


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

    @classmethod
    def create_user(cls, username, password):
        try:
            cls.get(cls.username**username)
        except cls.DoesNotExist:
           user = cls(username=username)
           user.password = cls.hash_password(password) #Here the class instance is calling the static method we just made
           user.save()
        return user

    @staticmethod
    def hash_password(password):   #function takes in user password and hashes it
        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.connect()
    DATABASE.create_tables([User, Recipe, Ingredient], safe=True)
    DATABASE.close()

2 Answers

Your code for task 2 and task 3 looks good. However I'm not sure how you got past task 1.

Here is your code

import datetime

from flask import PasswordHasher

from peewee import *

DATABASE = SqliteDatabase('courses.sqlite')
HASHER = PasswordHasher()

And here is the code provided

import datetime

from peewee import *

DATABASE = SqliteDatabase('recipes.db')

For task 1 you are to import the PasswordHasher class from argon2. The database is also different.

task 3 is failing, help pliz

Help please