def profile(): """Update profile for current user.""" user = User.query.get_or_404(session[CURR_USER_KEY]) form = UpdateProfileForm(obj=user) if form.validate_on_submit(): username = form.username.data password = form.password.data if User.authenticate(username, password): user.username = username user.email = form.email.data user.image_url = form.image_url.data or '/static/images/default-pic.png' user.header_image_url = form.header_image_url.data or '/static/images/warbler-hero.jpg' user.bio = form.bio.data db.session.add(user) db.session.commit() return redirect(url_for('users_show', user_id=user.id)) else: # form.password.errors.append('Wrong password') flash('Wrong password', 'danger') # Hide urls if they are the default ones if form.image_url.data == '/static/images/default-pic.png': form.image_url.data = '' if form.header_image_url.data == '/static/images/warbler-hero.jpg': form.header_image_url.data = '' return render_template('users/edit.html', form=form, user_id=user.id)
def update_profile(request): # 更新简介函数 """ 1. 更新用户名(username) 2. 更新头像(avatar) """ print "request.POST过来的数据 =", request.POST print "request.POST过来的数据username ="******"username") print "request.POST过来的数据avater =", request.POST.get("avatar") # 如果没有就是空值 form = UpdateProfileForm(request.POST) if form.is_valid(): avatar = form.cleaned_data.get('avatar', None) username = form.cleaned_data.get('username', None) print "form中的数据username ="******"form中的数据avater =", avatar # 1.更新用户名 user = User.objects.get(username=username) print "修改的User ="******"": cmsuser.avatar = avatar else: pass cmsuser.save() return myjson.json_result() else: return form.get_error_response()
def edit_profile(username): # Create form instance form = UpdateProfileForm() # For post request, check that form was valid if form.validate_on_submit(): # Need the user in dictionary format for db user = current_user.get_user_dictionary() # Save image if one is uploaded if form.picture.data: picture_path = save_picture(form.picture.data) user['profile_picture'] = picture_path # Update fields user['email'] = form.email.data # Save changes to the user db.users.save(user) flash('Your account has been updated!', 'success') return redirect(url_for('profile', username=user['username'])) elif request.method == 'GET': # Set the field inputs to already have the current email for this user form.email.data = current_user.email return render_template('edit_profile.html', form=form)
def update_profile(): form = UpdateProfileForm() if form.validate_on_submit(): # update avatar if form.photo.data: file_name = secure_filename(form.photo.data.filename) extension = file_name.split('.')[-1] file_name = '.'.join([current_user.username, extension]) path = upload_s3(file_name, form.photo.data, app.config['S3_UPLOAD_DIRECTORY']) current_user.avatar = path current_user.fname = form.fname.data current_user.lname = form.lname.data current_user.school = form.school.data current_user.major = form.major.data current_user.intro = form.intro.data current_user.location = form.location.data try: pm.listUnsubscribe(id='8d4e6caca8', email_address=current_user.email) except Exception, e: pass current_user.email = form.email.data db.session.commit() try: pm.listSubscribe(id='8d4e6caca8', email_address=current_user.email, double_optin=False) except Exception, e: pass
def update_profile(request): """ 更新用户信息 1. 更新头像(avatar) 2. 更新用户名(username) """ form = UpdateProfileForm(request.POST) if form.is_valid(): print request.POST avatar = form.cleaned_data.get('avatar', None) username = form.cleaned_data.get('username', None) # user = request.user user = User.objects.all().first() # 中间件设置当前的user到request上 user.username = username user.save() # 1.如果CmsUser这个表里已经有数据和user对应了,就直接更新 cmsuser = CmsUser.objects.filter(user__pk=user.pk).first() if not cmsuser: # 2.如果CmsUser这个表里没有数据和user对应,就应该新建,然后保存进去 cmsuser = CmsUser(avatar=avatar, user=user) else: cmsuser.avatar = avatar cmsuser.save() return xtjson.json_result() else: return form.get_error_response()
def profile(): user = current_user form = UpdateProfileForm() if form.validate_on_submit(): print(form.name.data) print(form.email.data) print(form.password.data) if form.name.data != user.name: User.query.filter_by(id=user.id).update({'name': form.name.data}) db.session.commit() flash('Name successfully changed', 'success') if form.email.data != user.email: User.query.filter_by(id=user.id).update({'email': form.email.data}) db.session.commit() flash('Email successfully changed', 'success') if user.password != form.password.data: User.query.filter_by(id=user.id).update({ 'password': generate_password_hash(form.password.data, method='sha256') }) db.session.commit() flash('Password successfully changed', 'success') logout_user() return redirect(url_for('login')) return render_template('index.html', logged_in_user=current_user) return render_template('customer/update-profile.html', form=form, logged_in_user=current_user)
def profile(): """Update profile for current user.""" # IMPLEMENT THIS user = User.query.get_or_404(g.user.id) form = UpdateProfileForm(obj=user) if not g.user: flash("Access unauthorized.", "danger") return redirect("/") if form.validate_on_submit(): try: User.authenticate(user.username, form.password.data) user.username = form.username.data user.email = form.email.data user.image_url = form.image_url.data user.header_image_url = form.header_image_url.data user.bio = form.bio.data db.session.add(user) db.session.commit() return redirect(f'/users/{user.id}') except: flash('Wrong password or username') return render_template('users/edit.html', form=form, user=user) else: return render_template('users/edit.html', form=form, user=user)
def profile(): """Update profile for current user.""" # IMPLEMENT THIS ####################### form = UpdateProfileForm(obj=g.user) # user = User.query.get_or_404(user_id) if not g.user: flash("Access unauthorized.", "danger") return redirect("/") if form.validate_on_submit(): u = User.authenticate(g.user.username, form.password.data) if u: g.user.username = form.username.data g.user.email = form.email.data g.user.image_url = form.image_url.data g.user.header_image_url = form.header_image_url.data g.user.bio = form.bio.data db.session.commit() return redirect(f"/users/{g.user.id}") else: form.password.errors = ["Invalid Password"] return render_template("/users/edit.html", form=form) else: return render_template("/users/edit.html", form=form)
def update_profile(): user = User.query.get(current_user.id) #pprint.pprint(user.first_name) form = UpdateProfileForm(request.form, obj=user) if request.method == 'POST' and form.validate() and current_user.is_authenticated(): form.populate_obj(user) user.first_name = form.first_name.data user.last_name = form.last_name.data db.session.commit() flash('your data has been updated') return redirect(url_for('home')) return render_template('profile_update.html', form=form)
def user_update_prefs(): form = UpdateProfileForm() form.primary_team.choices = [(t.slug, t.name) for t in Team.query.all()] form.teams.choices = [(t.slug, t.name) for t in Team.query.all()] if form.validate_on_submit(): current_user.primary_team = request.form.get('primary_team') current_user.contact_card = request.form.get('contact_card') current_user.set_teams(request.form.getlist('teams')) db.session.commit() return Response(json.dumps({'result': 'success'}), mimetype='application/json')
def update_profile(self, requestForm): status, message = False, "" form = UpdateProfileForm(requestForm) if form.validate(): status, message = self.__is_your_email_unique(form.email.data) if status: message = 'You have successfully updated your profile!' self.users.update({'username': username}, { "email": email, "firsname": fistname, "lastname": lastname }, {upsert: True}) return UserDataAccess.return_output(status, message, {})
def change_password(): form = UpdateProfileForm() if form.validate_on_submit(): if check_password_hash(current_user.password, form.old_password.data): current_user.update_password(form.new_password.data) db.session.commit() alert = Alert("Your password has been changed.", type="alert-success") trips = current_user.trips.all() return render_template('my_trips.html', form=form, alert=alert, trips=trips) else: alert = Alert("Old password not correct.", type="alert-error") return render_template('change_password.html', form=form, alert=alert) return render_template('change_password.html', form=form)
def update_profile(user_id): form = UpdateProfileForm() if form.validate_on_submit(): user = User.query.filter_by(id = user_id).first() user.about_me = form.about_me.data try: db.session.add(user) db.session.commit() except: flash('更改失败!') return redirect(url_for('update_profile', user_id = user_id)) flash('更改成功!') return redirect(url_for('profile', user_id = user_id)) return render_template('update_profile.html', form = form)
def update_profile(request): form = UpdateProfileForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username', None) avatar = form.cleaned_data.get('avatar', None) user = request.user #user = User.objects.all().first() user.username = username user.save() cmsuser = CmsUser.objects.filter(user__pk=user.pk).first() if not cmsuser: cmsuser = CmsUser(avatar=avatar, user=user) else: cmsuser.avatar = avatar cmsuser.save() return xtjson.json_result() else: return xtjson.json_result(message=form.get_error())
def update_profile(username): """Allows logged in user to update their own profile""" user = get_user(username) posts = get_posts() user_id = mongo.db.users.find_one({"username": session["username"]})['_id'] username = mongo.db.users.find_one({"username": session["username"]})['username'] # get user if request.method == 'GET': form = UpdateProfileForm(data=user) return render_template('update_profile.html', title="Update Profile", user=user, username=username, form=form) form = UpdateProfileForm(request.form) if form.validate_on_submit(): users_db = mongo.db.users users_db.update_one({ '_id': ObjectId(user_id), }, { '$set': { 'email': request.form["email"], 'linkedin_url': request.form["linkedin_url"], 'website_url': request.form["website_url"] } }) flash("Profile Successfully Updated") return redirect( url_for('profile', title="Profile Updated", user=user, posts=posts, username=username, form=form)) return render_template('update_profile.html', title="Update Profile", user=user, username=username, form=form)
def updateprofile(): form = UpdateProfileForm(request.form) if request.method == 'POST': user = current_user user.firstname = form.firstname.data.title() user.lastname = form.lastname.data.title() db.session.commit() return redirect(url_for('profile')) # GET Method return render_template('updateprofile.html', form=form)
def calendar(): profile_form = UpdateProfileForm() profile_form.primary_team.choices = [(t.slug, t.name) for t in Team.query.all()] profile_form.teams.choices = [(t.slug, t.name) for t in Team.query.all()] profile_form.primary_team.data = current_user.primary_team profile_form.contact_card.data = current_user.contact_card profile_form.teams.data = [team.slug for team in current_user.teams] return render_template('calendar.html', selected_team=team, profile_form=profile_form, logged_in=current_user)
def updateProfile(): form = UpdateProfileForm() if session.get('loggedIn') == True: user = User.query.filter_by(username=session['username']).first() if form.validate_on_submit(): if request.method == 'POST': user.email = form.email.data user.name = form.name.data session['user.email'] = user.email if form.password.data: encryptedPassword = sha256_crypt.encrypt( form.password.data) user.password = encryptedPassword if form.profilePic.data: filename = secure_filename(form.profilePic.data.filename) ext = filename.split('.') ext = ext[1] newFilename = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(17) ]) newFilename = newFilename + '.' + ext form.profilePic.data.save('static/profilePics/' + newFilename) user.profilePic = newFilename db.session.commit() flash("Profile updated", "success") return redirect('/profile') if request.method == 'GET': return render_template('updateProfile.html', form=form, user=user) return render_template('updateProfile.html', form=form, user=user) else: flash("Please sign in first", "warning") return redirect("/login") return render_template('updateProfile.html', form=form)
def profileCall(id): user_name = session['username'] users = mongo.db.userlist exiting_user = users.find_one({'USERNAME': id}) if id == 'myself': exiting_user = users.find_one({'USERNAME': user_name}) user = User(exiting_user['NAMES'], exiting_user['USERNAME'], exiting_user['MAIL']) else: user = User(exiting_user['NAMES'], exiting_user['USERNAME'], exiting_user['MAIL']) form = UpdateProfileForm(request.form) if form.validate_on_submit(): users.update_one({'USERNAME': user_name}, { '$set': { 'NAMES': form.name.data, 'PASSWORDS': form.password.data } }) #print(request.files['userpic']) app.upload_picture(request=request, user=session['username']) return user, form, request
def update_profile(request): args = {} if request.method == 'POST': form = UpdateProfileForm(request.POST, instance=request.user) form.actual_user = request.user if form.is_valid(): form.save() return HttpResponseRedirect(reverse('update_profile_success')) else: form = UpdateProfileForm() args['form'] = form return render_to_response('registration/update_profile.html', args)
def profile(): change = PasswordForm() update_profile = UpdateProfileForm() # Change Password form if request.method == 'POST' and request.form[ 'form-check'] == 'Change Password': if change.password.data == change.confirm.data: temp = Users.query.filter_by( username=session['user_username']).first() hash_pass = hashlib.sha256( change.password.data.encode('utf-8')).hexdigest() temp.password = hash_pass db.session.commit() return redirect(url_for('logout')) # Update Profile form if request.method == 'POST' and request.form[ 'form-check'] == 'Update Profile': name = update_profile.name.data email = update_profile.email.data user = Users.query.filter_by(username=session['user_username']).first() user.name = name user.email = email db.session.commit() return redirect(url_for('profile')) user = Users.query.filter_by(username=session['user_username']).first() user_username = session['user_username'] name = user.name email = user.email return render_template('profile.html', user_username=user_username, change=change, update_profile=update_profile, name=name, email=email)
def edit_user(user_id): """ Function to load form for a user to be editted and render to html """ # Check if user logged in to edit details if g.user: the_user = mongo.db.users.find_one({"_id": ObjectId(user_id)}) if g.user == str(the_user["_id"]): form = UpdateProfileForm() if form.validate_on_submit(): username_exist = user_exists( "username", form.username.data.lower()) email_exist = user_exists("email", form.email.data.lower()) username_error = "That username already exists. Please choose a different one..." email_error = "That email already exists. Please choose a different one..." if username_exist and email_exist: if the_user["username"] != form.username.data.lower( ) and the_user["email"] != form.email.data.lower(): return render_template( "edituser.html", form=form, username_error=username_error, email_error=email_error, user=the_user, regular_user=g.user) elif the_user["username"] == form.username.data.lower() and the_user["email"] != form.email.data.lower(): return render_template( "edituser.html", form=form, email_error=email_error, user=the_user, regular_user=g.user) elif the_user["username"] != form.username.data.lower() and the_user["email"] == form.email.data.lower(): return render_template( "edituser.html", form=form, username_error=username_error, user=the_user, regular_user=g.user) elif username_exist and email_exist != True: if the_user["username"] != form.username.data.lower(): return render_template( "edituser.html", form=form, username_error=username_error, user=the_user, regular_user=g.user) elif username_exist != True and email_exist == True: if the_user["email"] != form.email.data.lower(): return render_template( "edituser.html", form=form, email_error=email_error, user=the_user, regular_user=g.user) flash( f"Account profile updated! You need to log in again...", "success") users = mongo.db.users users.update({"_id": ObjectId(user_id)}, { "fname": form.fname.data.lower(), "lname": form.lname.data.lower(), "email": form.email.data.lower(), "username": form.username.data.lower(), "password": the_user["password"], "csrf_token": form.csrf_token.data }) return redirect( url_for( 'close_session', _scheme="https", _external=True)) else: return render_template( "edituser.html", form=form, user=the_user, regular_user=g.user) else: flash( f"You cannot edit this user profile, as it belongs to someone else...", "danger") return redirect( url_for( 'get_users', _scheme="https", _external=True)) else: flash(f"You need to log in first...", "warning") return redirect( url_for( "log_user_in", _scheme="https", _external=True))