0

I have no idea what this means:

(success, admin_id) = login(email, password)

The login method looks something like this:

def login(email, password):
    try:
        user = db.session.query(Admin).filter_by(email=email).first()        

        valid_role = 'Admin'

        if valid_role in user.role:       
            return user
    except Exception as e:
        print e

    return None

It returns either a TypeError: 'Admin' object is not iterable or a ValueError: too many values to unpack when I try to return just the user.id. Thank you for any help.

joshuar500
  • 164
  • 1
  • 15

2 Answers2

2

To return multiple values (tuple) from your function you can do:

# when user is admin
return True, user.id

In the case the user is not an admin or there is an exception:

return False, None

So with (success, admin_id) = login(email, password), the success status is returned from your function as a boolean, and the admin_id is either a valid user id or None

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
2

Your function returns one object; either an Admin() instance:

if valid_role in user.role:       
    return user

or None at the end:

return None

Neither fits the expectation that the function returns two values, which is what this line expects:

(success, admin_id) = login(email, password)

Either adjust that line to only receive the user, or fix your function to return the user id and the success flag.

The latter would require that you rewrite your function to:

def login(email, password):
    try:
        user = db.session.query(Admin).filter_by(email=email).first()        

        valid_role = 'Admin'

        if valid_role in user.role:       
            return True, user.id
    except Exception as e:
        print e

    return False, None

Now your function always returns two items, either True and the user id (not the user object), or False and None.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343