Exemple #1
0
def load_user(username, passwordhash):
    """Load a user based on the passwordhash; if the passwordhash doesn't match
    the username, then this should return None."""

    client = _get_client()
    q = client.query(kind=_USER_ENTITY)
    q.add_filter('username', '=', username)
    q.add_filter('passwordhash', '=', passwordhash)
    for user in q.fetch():
        return lmsdata.User(user['username'], user['email'], user['about'])
    return None
Exemple #2
0
def register_user():
    username = flask.request.form.get('username')
    password1 = flask.request.form.get('password1')
    password2 = flask.request.form.get('password2')
    email = flask.request.form.get('email')
    errors = []
    if password1 != password2:
        errors.append('Passwords do not match.')
    email_parts = parseaddr(email)
    if len(email_parts) != 2 or not email_parts[1]:
        errors.append('Invalid email address: ' + str(email))

    user = lmsdata.User(username, email)
    if errors:
        return show_page('/signup.html', 'Sign Up', errors=errors)
    else:
        passwordhash = get_password_hash(password1)
        lmsdatastore.save_user(user, passwordhash)
        flask.session['user'] = user.username
        return flask.redirect('/courses')
Exemple #3
0
def load_user(
    username, passwordhash
):  # note: index.yaml and our User object do not contain passwordhash (it's only in datastore)
    """Load a user based on the passwordhash; if the passwordhash doesn't match
    the username, then this should return None."""

    client = _get_client()  # get the datastore client
    q = client.query(
        kind=_USER_ENTITY)  # prep a query that looks at user entities

    # .add_filter('<property>', '<operator>', <value>)
    # Filter the query based on a property name, operator and a value.
    # Documentation: https://googleapis.dev/python/datastore/latest/queries.html#google.cloud.datastore.query.Query.add_filter
    q.add_filter('username', '=', username)  # must equal un
    q.add_filter('passwordhash', '=', passwordhash)  # must equal pwhash

    for user in q.fetch():  # fetch the information on the user
        # get the information from the datastore (accessing as one accesses a dictionary)
        # use that information as a parameter to insert in our python User object
        # return object
        return lmsdata.User(user['username'], user['email'], user['about'])
    return None  # if info doesn't exist return None