def settings(request): if request.method == 'POST': form = SettingsForm(request.POST) if form.is_valid(): #requires old password + new password and confirmation to change old_passwd = form.cleaned_data['old_passwd'] new_passwd1 = form.cleaned_data['new_passwd1'] new_passwd2 = form.cleaned_data['new_passwd2'] user = auth.authenticate(username=request.user.username, password=old_passwd) if user is not None: if new_passwd1 == new_passwd2: user.set_password(new_passwd1) user.save() auth.login(request, user) # log in user for redirect messages.success(request, 'Password changed') else: messages.error(request, 'Passwords do not match') else: messages.error(request, 'Incorrect password') else: messages.error(request, 'Invalid entry') return redirect('accounts:settings') else: form = SettingsForm() if DEBUG == True: return render(request, 'accounts/settings.html', {'form': form, 'user': request.user}) else: return render(request, 'accounts/settings.min.html', {'form': form, 'user': request.user})
def test_settings_form_valid_data(self): """ Original password, a new password, and a matching password confirmation confirmation are required for validation. """ form = SettingsForm({ 'old_passwd': 'test12345', 'new_passwd1': 'XAGEasdh!ad', 'new_passwd2': 'XAGEasdh!ad', }) self.assertTrue(form.is_valid())
def test_settings_form_invalid_data(self): """ Original password, a new password, and a matching password confirmation confirmation are required for validation. """ form = SettingsForm({ 'old_passwd': 'test12345', 'new_passwd1': 'XAGEasdh!ad', }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { 'new_passwd2': ['This field is required.'], })
def post(self, request): form = SettingsForm(self.request.POST) if form.is_valid(): services.apply_changes(self.request, form) context = { 'form': SettingsForm(), 'saved': form.is_valid(), 'notification_option': self.request.user.profile.notification_option, 'home_view': self.request.user.profile.home_view, 'theme': self.request.user.profile.theme, 'push_time': self.request.user.profile.push_time, } return render(self.request, 'accounts/settings.html', context=context)
def settings_view(request): if not request.user.is_authenticated: return not_authenticated(request) user = request.user profile = Profile.objects.get(user=user) if request.method == 'POST': form = SettingsForm(request.POST, request.FILES) if form.is_valid(): description = form.data.get('description', None) avatar = request.FILES.get('avatar', None) if description is not None: if description is not profile.description: profile.description = description if avatar is not None: if avatar.size > 2097152: form.add_error('avatar', "Image is too big! Max 2MB.") return render(request, 'accounts/settings.html', {'form': form}) if avatar is not profile.avatar: profile.avatar = avatar profile.save() return redirect('accounts-profile', username=user.username) else: form = SettingsForm(initial={ 'description': profile.description, 'avatar': profile.avatar }) return render(request, 'accounts/settings.html', {'form': form})
def post(self, request, username=None, mode=None): context = {} if username is not None: return self.get(request, username, mode) context['detailsform'] = DetailsForm(instance=request.user.details) context['userform'] = UserForm(instance=request.user) context['settingsform'] = SettingsForm(instance=request.user.settings) if mode == "details": form = DetailsForm(request.POST, instance=request.user.details) if form.is_valid(): form.save() messages.success(request, "Persoonsgegevens veranderd") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['detailsform'] = form elif mode == "profile": user = UserForm(request.POST, instance=request.user) if user.is_valid(): user.save() messages.success(request, "Profiel opgeslagen!") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['userform'] = user elif mode == "settings": settings = SettingsForm(request.POST, request.FILES, instance=request.user.settings) if settings.is_valid(): settings.save() messages.success(request, "Intelling gewijzigd") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['settingsform'] = settings return render(request, "account/profile.html", context)
def account_settings(request): """ *GET* Allows a user to change an account. *POST* Saves account with selected attributes *TEMPLATES* 'accounts/settings.html' """ from accounts.forms import SettingsForm if request.method == "GET": form = SettingsForm(request.user) redirect_to = "/home/#settings" if "next" in request.GET: redirect_to = request.GET["next"] return render_with_user(request, "accounts/settings.html", {"form": form, "redirect_to": redirect_to}) if request.method == "POST": form = SettingsForm(request.user, request.POST) form_valid = False redirect_to = request.GET.get("next", "/home/#settings") if form.is_valid(): # FORM IS VALID, CREATE USER form_valid = True form.save() return redirect(redirect_to) return render_with_user( request, "accounts/settings.html", {"form": form, "form_valid": form_valid, "redirect_to": redirect_to} )
def get(self, request): context = { 'form': SettingsForm(), 'saved': False, 'notification_option': self.request.user.profile.notification_option, 'home_view': self.request.user.profile.home_view, 'theme': self.request.user.profile.theme, 'push_time': self.request.user.profile.push_time, } print(context) return render(self.request, 'accounts/settings.html', context=context)
def settings_view(request): if request.method == 'POST': form = SettingsForm(request.POST, request.FILES) if not form.is_valid(): return render(request, 'accounts/settings.html', {'form': form}) user = request.user check_user = authenticate(request, username=user.username, password=form.cleaned_data['password']) if check_user is None: return render(request, 'accounts/settings.html', { 'form': form, 'error': 'Wrong password' }) User.objects.update_user(user, form) return redirect('accounts:settings') form = SettingsForm() if request.user.is_authenticated: return render(request, 'accounts/settings.html', {'form': form}) return redirect('articles:Cat In Hat')