Flask-Peewee and User Creation doesn't work

164 Views Asked by At

I am currently working on my administration interface for a website. However, when I test adding a new user, I get the following traceback:

Traceback (most recent call last):
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 170, in login
    form.password.data,
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 128, in authenticate
    if not user.check_password(password):
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 25, in check_password
    return check_password(password, self.password)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/utils.py", line 138, in check_password
    salt, hsh = enc_password.split('$', 1)
ValueError: need more than 1 value to unpack

Here is the code for the various related areas of my program: model.py

class User(db.Model):
    id = IntegerField(primary_key=True)
    username = CharField()
    password = CharField()
    email = CharField(120)
    admin = BooleanField(default=False)
    active = BooleanField(default=True)

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    # Required for administrative interface
    def __unicode__(self):
        return self.username
    def __repr__(self):
        return '<User %r>' % (self.username)

app.py

class UserView(ModelAdmin):
    columns = ('username', 'email', 'admin', 'active')
    filter_exclude = ('id', 'password')
    exclude = ('id')

I'm sure I am missing something obvious, but I thought I'd post it in case I'm not. If I am, what am I doing wrong here?

Here is a few screenshots of what the admin interface looks like when I'm doing it. screenshot from 2014-05-21 12 04 05 screenshot from 2014-05-21 12 04 20 screenshot from 2014-05-21 12 04 44

1

There are 1 best solutions below

0
On

According to Flask-Pewee documentation, if you define a custom User Model which you do in your case, you must extend the BaseUser Class. That will ensure that you password is created as a tuple of (salt,hash) according to this code

Now, you are getting the error on this line

salt, hsh = enc_password.split('$', 1)

which is trying to split the tuple that never existed

So, short answer is that you User Model should be defined as:

class User(db.Model, BaseUser):