def edit_profile(request): template_name = 'users/edit.html' if request.method == 'POST': form = EditForm(request.POST, instance=request.user) if form.is_valid(): form.save() return redirect('view_profile') else: form = EditForm(instance=request.user) context = { 'page_title': 'EDIT PROFILE', 'form': form, } return render(request, template_name, context)
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)
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'))
def user_edit(request, pk): form = EditForm(request.POST or None, instance=User.objects.get(pk=pk)) if form.is_valid(): form.save() info(request, 'User details changed.') return HttpResponseRedirect(reverse("user-list")) return direct_to_template(request, "user_edit.html", extra_context={ "form": form, "nav": { "selected": "users", }, })
def edit(request): """ This function handles the user information editing form submissions. :param request: The request object from the user. :return: """ # Default context context = {} if 'logged' not in request.session: return redirect('/article/index/') # The user has filled the form itself. if request.method == 'POST': # Create new form with data received from the user. edit_form = EditForm(request.POST, request.FILES) if edit_form.is_valid(): # Get the currently logged in user. current_user_name = request.session['username'] # Get the current user object current_user = Users.objects.get(user_name=current_user_name) # Update the user's name if it was filled in the fields. if 'user_name' in request.POST and request.POST['user_name'] != "": current_user.user_name = request.POST['user_name'] request.session['username'] = request.POST['user_name'] # Update the user's email if it was filled in the fields. if 'user_email' in request.POST and request.POST[ 'user_email'] != "": current_user.user_email = request.POST['user_email'] # Update the user's profile pic if it was filled in the fields. if 'user_image' in request.FILES: # Get the just uploaded image. user_image = request.FILES['user_image'] # Get the image's extension image_extension = user_image.name.split('.')[1] # Assign the image a new name, with p_user_id as its name. user_image.name = 'p_' + str( current_user.user_id) + '.' + image_extension # Assign the modified image to the current user image path. current_user.user_image_path = user_image # A flag that indicates that the image has been altered. changed_image = True else: # Indicate that the image has not changed. changed_image = False # Save updates to the current user. current_user.save() # The image has changed, I needed to have saved the object first to get the true URL for the image if changed_image: # Save the current profile picture in the session variable. request.session['user_image'] = str( current_user.user_image_path) context = {'edit_status': True} # The user has navigated to the form. Display the empty form. else: # Create new form. edit_form = EditForm() # Add form to the context variable context = {'edit_form': edit_form} return render(request, 'edit_info.html', context)
def get(self): form = EditForm(obj=current_user) return render_template( 'users/edit_profile.html', form=form, orgs=Organization.get_attached_to_user(current_user))