コード例 #1
0
ファイル: users.py プロジェクト: TechFitU/Price-Alerts
def edit_profile():
    def allowed_file(filename):
        return '.' in filename and \
               filename.rsplit('.', 1)[1].lower() in settings.ALLOWED_EXTENSIONS

    error = None

    if request.method == 'POST':
        theme = request.form['theme']
        first_name = request.form.get('first_name', None)
        last_name = request.form.get('last_name', None)
        phone = request.form.get('phone', None)

        picture = request.files.get('picture', None)
        bio = request.form.get('bio', None)
        tz = request.form.get('tz', None)
        if 'picture' not in request.files:
            flash('No file part', category='error')
            return redirect(request.url)

        if phone != '' and phone is not None and not parse_phone(phone):
            error = 'Invalid phone. Suggested: 800-887-3244'
        elif not first_name:
            error = 'First name is required.'
        elif not last_name:
            error = 'Last name is required.'
        elif not tz:
            error = 'Timezone must be selected'

        elif picture is not None and not allowed_file(picture.filename):
            error = 'Not valid picture extension. Valid ones {}'.format(
                ";".join(settings.ALLOWED_EXTENSIONS))

        if error is None:
            if current_user.profile is None:
                current_user.profile = ProfileModel()

            current_user.name = first_name + " " + last_name
            current_user.phone = phone
            current_user.theme = theme
            current_user.profile.bio = bio
            current_user.profile.timezone = tz

            filename = secure_filename(picture.filename)
            current_user.profile.picture = filename
            picture.save(os.path.join(settings.UPLOAD_FOLDER, filename))

            current_user.save_to_db()
            flash('User profile updated successfully', category='success')
            return redirect(request.url)  # stay in the same page

        else:
            flash(error, category='error')

    first_name, last_name = current_user.name.split(" ")
    return render_template('user/edit_profile.html',
                           user=current_user,
                           first_name=first_name,
                           last_name=last_name,
                           tz_data=TZ_DATA)
コード例 #2
0
def change_password():
    form = ChangePasswordForm()
    if form.validate_on_submit():
        if current_user.verify_hash(form.current_password.data,
                                    current_user.password):
            current_user.set_password(form.new_password.data)
            current_user.save_to_db(db)

            return redirect("/profile")
    return render_template("change_password.html", form=form)
コード例 #3
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)
コード例 #4
0
def edit_profile():
    form = ProfileEditForm()
    if form.validate_on_submit():
        hint = form.hint.data
        address = form.address.data
        first_name = form.first_name.data
        last_name = form.last_name.data
        current_user.hint = hint
        current_user.address = address
        current_user.first_name = first_name
        current_user.last_name = last_name

        current_user.save_to_db(db)

        return redirect("/profile")
    hint = current_user.hint
    address = current_user.address
    return render_template("edit_profile.html",
                           form=form,
                           hint=hint,
                           address=address)
コード例 #5
0
def logout():
    current_user.last_logged_out = datetime.now()
    current_user.save_to_db(db)
    logout_user()
    form = LoginForm()
    return render_template("login.html", form=form)