def preferences(request): """ An overview of all user preferences """ template_data = {} template_data.update(csrf(request)) redirect = False # Process the preferences form if request.method == 'POST': form = UserPreferencesForm(data=request.POST, instance=request.user.userprofile) form.user = request.user # Save the data if it validates if form.is_valid(): form.save() redirect = True else: data = { 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email } form = UserPreferencesForm(initial=data, instance=request.user.userprofile) # Process the email form if request.method == 'POST': email_form = UserPersonalInformationForm(data=request.POST, instance=request.user) if email_form.is_valid() and redirect: email_form.save() redirect = True else: redirect = False template_data['form'] = form if redirect: messages.success(request, _('Settings successfully updated')) return HttpResponseRedirect(reverse('core:user:preferences')) else: return render(request, 'user/preferences.html', template_data)
def preferences(request): ''' An overview of all user preferences ''' template_data = {} template_data.update(csrf(request)) redirect = False # Process the preferences form if request.method == 'POST': form = UserPreferencesForm(data=request.POST, instance=request.user.userprofile) form.user = request.user # Save the data if it validates if form.is_valid(): form.save() redirect = True else: form = UserPreferencesForm(instance=request.user.userprofile) # Process the email form # # this is a bit ugly, but we need to take special care here, only instatiating # the form when the user changes its password, otherwise the email form will # check the adress for uniqueness and fail, because it will find one user (the # current one) using it. But if he changes it, we want to check that nobody else # has that email if request.method == 'POST' and request.POST["email"] != request.user.email: email_form = UserEmailForm(data=request.POST, instance=request.user) if email_form.is_valid() and redirect: request.user.email = email_form.cleaned_data['email'] request.user.save() redirect = True else: email_form = UserEmailForm(instance=request.user) template_data['form'] = form template_data['email_form'] = email_form if redirect: messages.success(request, _('Settings successfully updated')) return HttpResponseRedirect(reverse('core:preferences')) else: return render(request, 'user/preferences.html', template_data)
def preferences(request): ''' An overview of all user preferences ''' template_data = {} template_data.update(csrf(request)) redirect = False # Process the preferences form if request.method == 'POST': form = UserPreferencesForm(data=request.POST, instance=request.user.userprofile) form.user = request.user # Save the data if it validates if form.is_valid(): form.save() redirect = True else: form = UserPreferencesForm(instance=request.user.userprofile) # Process the email form if request.method == 'POST': email_form = UserPersonalInformationForm(data=request.POST, instance=request.user) if email_form.is_valid() and redirect: email_form.save() redirect = True else: redirect = False else: email_form = UserPersonalInformationForm(instance=request.user) template_data['form'] = form template_data['email_form'] = email_form if redirect: messages.success(request, _('Settings successfully updated')) return HttpResponseRedirect(reverse('core:user:preferences')) else: return render(request, 'user/preferences.html', template_data)