def profile(request): if request.method == "POST": userform = UserForm(request.POST, instance=request.user) passwordform = PasswordChangeForm(request.user, request.POST) error = False if userform.has_changed() and userform.is_valid(): userform.save() elif not userform.has_changed(): userform = UserForm(instance=request.user) else: error = True if passwordform.has_changed() and passwordform.is_valid(): passwordform.save() elif not passwordform.has_changed(): passwordform = PasswordChangeForm(user=request.user) else: error = True if not error: redirect('login') else: userform = UserForm(instance=request.user) passwordform = PasswordChangeForm(user=request.user) ctx = { 'userform': userform, 'passwordform': passwordform, } return render(request, 'profile.html', ctx)
def profile(request): user = request.user profile = user.profile # unbound forms to be used later unbound_update_bitbucket_form = ProfileUpdateBitbucketForm(instance=profile, initial={'bitbucket_account': profile.bitbucket_account, 'bitbucket_repository': profile.bitbucket_repository}) unbound_password_change_form = PasswordChangeForm(user=user) # handle forms if request.method == 'POST': update_bitbucket_form = ProfileUpdateBitbucketForm(data=request.POST, instance=profile, initial={'bitbucket_account': profile.bitbucket_account, 'bitbucket_repository': profile.bitbucket_repository}) password_change_form = PasswordChangeForm(data=request.POST, user=user) # for it to be regarded as successful (such that the user gets # redirected back to profile), all changed forms must be valid and saved # already; otherwise, render the view with all forms again (with # unchanged ones replaced by unbound ones, so as to clean their errors), # in order for the user to know what errors have occurred n_undone_forms = update_bitbucket_form.has_changed() + password_change_form.has_changed() if update_bitbucket_form.has_changed(): if update_bitbucket_form.is_valid(): update_bitbucket_form.save() n_undone_forms -= 1 messages.success(request, 'Bitbucket settings successfully updated.') else: update_bitbucket_form = unbound_update_bitbucket_form if password_change_form.has_changed(): if password_change_form.is_valid(): password_change_form.save() n_undone_forms -= 1 messages.success(request, 'Password successfully changed. Please log in again.') else: password_change_form = unbound_password_change_form if n_undone_forms == 0: return HttpResponseRedirect(reverse('judge:profile')) else: update_bitbucket_form = unbound_update_bitbucket_form password_change_form = unbound_password_change_form return render(request, 'judge/profile.html', {'profile': profile, 'update_bitbucket_form': update_bitbucket_form, 'password_change_form': password_change_form})
def password(request): group = str(request.user.groups.get()) if request.method == 'POST': form = PasswordChangeForm(user=request.user, data=request.POST) if form.is_valid() and form.has_changed(): form.save() update_session_auth_hash(request, form.user) messages.success(request, "Password has been changed successfully!") return redirect('employers:password') else: messages.error(request, "You did not make any changes.") return redirect('employers:password') else: form = PasswordChangeForm(user=request.user) if group == "Employers": employer = Employer.objects.values().get(user_id=request.user.id) return render(request, 'employers/password.html', { 'form': form, 'employer': employer, 'group': group }) else: individual = Individual.objects.values().get( user_id=request.user.id) return render(request, 'employers/password.html', { 'form': form, 'individual': individual, 'group': group })
def profile(request): user = request.user profile = user.profile # unbound forms to be used later unbound_update_github_form = ProfileUpdateGithubForm( instance=profile, initial={ 'github_account': profile.github_account, 'github_repository': profile.github_repository }) unbound_password_change_form = PasswordChangeForm(user=user) # handle forms if request.method == 'POST': update_github_form = ProfileUpdateGithubForm( data=request.POST, instance=profile, initial={ 'github_account': profile.github_account, 'github_repository': profile.github_repository }) password_change_form = PasswordChangeForm(data=request.POST, user=user) # for it to be regarded as successful (such that the user gets # redirected back to profile), all changed forms must be valid and saved # already; otherwise, render the view with all forms again (with # unchanged ones replaced by unbound ones, so as to clean their errors), # in order for the user to know what errors have occurred n_undone_forms = update_github_form.has_changed( ) + password_change_form.has_changed() if update_github_form.has_changed(): if update_github_form.is_valid(): update_github_form.save() n_undone_forms -= 1 messages.success(request, 'GitHub settings successfully updated.') else: update_github_form = unbound_update_github_form if password_change_form.has_changed(): if password_change_form.is_valid(): password_change_form.save() n_undone_forms -= 1 messages.success( request, 'Password successfully changed. Please log in again.') else: password_change_form = unbound_password_change_form if n_undone_forms == 0: return HttpResponseRedirect(reverse('judge:profile')) else: update_github_form = unbound_update_github_form password_change_form = unbound_password_change_form return render( request, 'judge/profile.html', { 'profile': profile, 'update_github_form': update_github_form, 'password_change_form': password_change_form })