Example #1
0
def edit():
    error = None
    message = None
    user = Users.query.filter_by(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)  # prepopulates form with values in user
        if form.validate_on_submit():
            if user.username != form.username.data:  # user is changing their username
                if Users.query.filter_by(username=form.username.data).first():
                    error = "\"" + form.username.data + "\" is already in use"
                else:
                    session['username'] = form.username.data
            if user.email != form.email.data.lower(
            ):  # user is changing their email
                if Users.query.filter_by(
                        email=form.email.data.lower()).first():
                    error = "\"" + form.email.data + "\" is already in use"
                else:
                    form.email.data = form.email.data.lower()
            if not error:
                form.populate_obj(user)
                db.session.add(user)
                db.session.commit()
                message = "Your profile has been successfully updated"
        return render_template("users/edit.html",
                               form=form,
                               error=error,
                               message=message)
    else:
        abort(404)
Example #2
0
    def post(self):
        form = EditForm()
        if not form.validate_on_submit():
            return self.get()

        form.populate_obj(current_user)
        setattr(
            current_user, "birth_date",
            datetime.strptime(request.form["birth_date"], "%Y-%m-%d").date())

        current_user.save()
        return redirect(url_for('users.profile'))