def edit_profile(): if 'user' in session: form = updateProfileForm() if form.validate_on_submit(): Database.update_one(collection="users", query=[{'user_id': session['user'].uid}, {"$set": {"uname": form.username.data, "fname": form.fname.data, "lname": form.lname.data, "bio": form.bio.data, "phone": form.phone.data, "address": form.address.data, "city": form.city.data, "zipcode": form.zipcode.data, "state": form.state.data}}]) flash('Your profile has been updated.') # change session username to newone once update the database session['user'].name = form.username.data # print(session['user'].name) # return to myprofile page userProfile = User.get_by_id(session['user'].uid) return render_template('/myprofile.html', user=userProfile) elif request.method == 'GET': userProfile = User.get_by_id(session['user'].uid) form.username.data = userProfile.uname form.fname.data = userProfile.fname form.lname.data = userProfile.lname form.bio.data = userProfile.bio form.phone.data = userProfile.phone form.address.data = userProfile.address form.city.data = userProfile.city form.zipcode.data = userProfile.zip form.state.data = userProfile.state return render_template('/updateProfile.html', title='Update', form=form) else: return redirect(url_for('web.index'))
def setUp(self): db.create_all() db.session.add(User(email="*****@*****.**", password="******")) db.session.commit() with self.client: response = self.client.post('login', { email: '*****@*****.**', password: '******' }) property = Property()
def upload_file(): if 'user' in session: if 'profile_pic' in request.files: #make sure that the filename is unique profile_pic = request.files['profile_pic'] files = mongo.db.fs.files.find({'filename':profile_pic.filename}).count() #print(files) if files ==0: # if files is not empty #upload the pic mongo.save_file(profile_pic.filename, profile_pic) #get previous filename user = User.get_by_username(session['user'].name) previousFilename = user.profile_pic #update the profile query = { 'uname':session['user'].name} updates = { "$set": { "profile_pic": profile_pic.filename } } mongo.db.users.update_one(query, updates) # delete the old file if it isn't default if previousFilename != 'default': # get fs.files _id for previousfile #find_one doesn't work. fileobject = mongo.db.fs.files.find({'filename':previousFilename}) #print(fileobject[0]['_id']) #delete the old file chunks from fs.chunks mongo.db.fs.chunks.remove({ 'files_id' : fileobject[0]['_id'] }) #delete the old file record from fs.files mongo.db.fs.files.remove({ '_id' : fileobject[0]['_id'] }) else: flash('The filename has been used. Please choice a different file name.') return redirect(url_for('web.myprofile')) else: return redirect(url_for('web.index'))
def change_password(): if 'user' in session: form = changePasswordForm() if form.validate_on_submit(): #update the password Database.update_one(collection="users", query=[{'uname':session['user'].name},{"$set":{"password":form.password.data}}]) flash('Your password has been changed.') #back to profile page userProfile = User.get_by_username(session['user'].name) return render_template('/myprofile.html', user=userProfile) return render_template('/changePassword.html', title='Change Password', form=form) else: return redirect(url_for('web.index'))
def setUp(self): #db.drop_all() db.create_all() db.session.add(User(email="*****@*****.**", password="******",active=True)) db.session.commit()
def myprofile(): if 'user' in session: userProfile = User.get_by_id(session['user'].uid) return render_template('/myprofile.html', user=userProfile) else: return redirect(url_for('web.login'))