コード例 #1
0
def verify_reset_token(token):
    s = Serializer(app.secret_key)
    try:
        user_id = s.loads(token)['_id']
    except:
        return None
    return UserModel.find_by_id(user_id)
コード例 #2
0
def signup_view():
    if current_user.is_authenticated:
        return redirect('/')

    form = FormSignup(request.form)
    if request.method == "POST" and form.validate():

        # Set the user inputs
        # Force only the initial character in first name to be capitalised
        first_name = (form.firstname.data.lower()).capitalize()

        # Make sure the first letter is capitalised. Don't care about capitalisation on the rest
        # Can't use .capitalize() here because it changes all other characters to lowercase
        last_name = form.lastname.data
        last_name_first_letter = last_name[0].capitalize()
        last_name_remaining_letters = last_name[1:]
        last_name = last_name_first_letter + last_name_remaining_letters

        email = form.email.data

        # Check if the email address already exists
        # (Need to make sure this is not case sensitive)
        user = UserModel.find_by_email(email)
        if user:
            flash('Account already exists', 'danger')
            return render_template('signup.html', form=form)
        else:
            # Encrypt the password using bcrypt
            hashpass = bcrypt.hashpw(form.password.data.encode('utf-8'),
                                     bcrypt.gensalt())
            # Make the new user using the user model
            user = UserModel(first_name, last_name, form.email.data, hashpass)
            try:
                user.save_to_db()

            except:
                flash('Error saving user to database', 'danger')
                return render_template('signup.html', form=form)
            addDirectory(user.id)
            return redirect("login")
    else:
        return render_template('signup.html', form=form)
コード例 #3
0
def login():
    if current_user.is_authenticated:
        return redirect('/')

    form = FormLogin(request.form)
    if request.method == "POST" and form.validate():
        user = UserModel.find_by_email(form.email.data)

        if user is not None and bcrypt.checkpw(
                form.password.data.encode('utf-8'), user.password):
            login_user(user, remember=True)
            return redirect('/')
        else:
            flash('Invalid email or password', 'danger')
            return render_template('login.html', form=form)
    return render_template('login.html', form=form)
コード例 #4
0
def reset_password():
    form = FormRequestPasswordReset()
    if request.method == 'POST' and form.validate():
        email = form.email.data
        user = UserModel.find_by_email(email)

        if user is None:
            error = "wrong_email"
            return render_template('password_reset_request.html',
                                   form=form,
                                   error=error)
        else:
            send_reset_email(user)
            flash('Password reset email has been sent!', 'info')
            return redirect(url_for('login'))

    return render_template('password_reset_request.html', form=form)
コード例 #5
0
def user_settings_view():
    form = FormUpdateSettings()
    change = False
    if request.method == "POST":
        if form.validate():
            if form.userPicture.data:
                addPicture(form)
            if form.passwordUpdate.data:
                hashpass = bcrypt.hashpw(
                    form.passwordUpdate.data.encode('utf-8'), bcrypt.gensalt())
                current_user.password = hashpass
                change = True
            if form.emailUpdate.data:
                user = UserModel.find_by_email(form.emailUpdate.data)
                if user and user != current_user:
                    flash('Account already exists, no changes saved', 'danger')
                    return render_template('user_settings.html', form=form)
                else:
                    current_user.email = form.emailUpdate.data
                    change = True
            if form.firstnameUpdate.data:
                current_user.firstname = form.firstnameUpdate.data
                change = True
            if form.lastnameUpdate.data:
                current_user.lastname = form.lastnameUpdate.data
                change = True
            if form.colourSetting.data != current_user.backgroundColour:
                current_user.backgroundColour = form.colourSetting.data
                change = True
            if change == True:
                current_user.save_to_db()
                flash("Your account has been updated", "success")
                return redirect('/user_settings')
        else:
            return render_template('user_settings.html', form=form)
    elif request.method == "GET":
        form.emailUpdate.data = current_user.email
        form.firstnameUpdate.data = current_user.firstname
        form.lastnameUpdate.data = current_user.lastname
        form.colourSetting.data = current_user.backgroundColour
        form.colourSetting.data.rgb = rgbValueCorrection(
            current_user.backgroundColour)
    return render_template('user_settings.html', form=form)
コード例 #6
0
def assets_select():
    filter_tags = request.form.get('filterTags')
    # possible values: RECENT, OLD, RELEVANT
    sorting = request.form.get('sorting')
    limit = request.form.get('limit')

    if filter_tags:
        filter_tags = json.loads(filter_tags)

    if sorting:
        if not (sorting.lower() == "recent" or sorting.lower() == "old"
                or sorting.lower() == "relevant"):
            sorting = "recent"
    else:
        sorting = "recent"

    if limit:
        limit = int(limit)
    else:
        limit = 12

    # filtering
    unfiltered = [
        asset.json()
        for asset in UserModel.find_by_id(current_user.get_id()).assets.all()
    ]
    filtered = []
    if not (filter_tags == None or len(filter_tags) == 0):
        for asset in unfiltered:
            for filter_tag in filter_tags:
                if filter_tag in asset['tags']:
                    filtered.append(asset)
                    # move on to next asset
                    break
    else:
        filtered = unfiltered

    sorted_assets = []

    # sorting
    if sorting.lower() == "recent":
        sorted_assets = sorted(filtered,
                               key=itemgetter('date-created'),
                               reverse=True)
    elif sorting.lower() == "old":
        sorted_assets = sorted(filtered,
                               key=itemgetter('date-created'),
                               reverse=False)
    elif sorting.lower() == "relevant":
        assets_match_count = []
        for asset in filtered:
            asset['tag_match_count'] = 0
            if not (filter_tags is None or len(filter_tags) == 0):
                for filter_tag in filter_tags:
                    if filter_tag in asset['tags']:
                        asset['tag_match_count'] += 1
            assets_match_count.append(asset)
        sorted_assets = sorted(assets_match_count,
                               key=itemgetter('tag_match_count'),
                               reverse=True)
    sorted_assets = sorted_assets[0:limit]
    return dumps(sorted_assets, default=json_serial)
コード例 #7
0
def asset_management_view():
    user = UserModel.find_by_id(current_user.get_id())
    # desc => from most recent to oldest
    assets = user.assets.order_by(desc(AssetModel.dateCreated)).limit(12).all()
    return render_template('asset_management.html', assets=assets)