def custom_user_edit(req): if (Cart.objects.filter(user=req.user, status=Cart.Status.CURRENT)).exists(): cart = Cart.objects.get(user=req.user, status=Cart.Status.CURRENT) else: cart = None user = req.user form = CustomUserChangeForm(instance=user) #initiates form with user info if req.method == 'POST': # will only show validation errors on POST, not GET form = CustomUserChangeForm(req.POST, instance=user) #'instance=user' ensures you are overwriting current user and NOT creating a new one with same info if form.is_valid(): user_update = form.save(commit=False) user_update.is_active = False # turns user to inactive and kicks them out form.save() # saves all the info # Send email to user email = req.user.email subject = f"Request to update your Roadstar Tire Wholesale profile received" message = f"Hello {user.full_name} – This is an email confirmation to inform you that a request was made to edit your Roadstar Tire Wholesale profile details. Your account will be temporarily inactve until a staff member verifies the changes.\nIf this is an error or is urgent, please call (905)-660-3209." send_mail( subject, message, 'settings.EMAIL_HOST_USER', [email], fail_silently=False ) # Send email to admin subject = f"{user.full_name} from {user.company_name} edited their profile" message = f"{user.full_name} from {user.company_name} edited their profile. Please log in to your admin account and verify their account.\nhttps://www.roadstartirewholesale.ca/admin/users/customuser/{user.id}/change/" mail_admins( subject, message, fail_silently=False ) return redirect('account') return render(req, 'custom_user_edit_form.html', {'form': form, 'cart': cart})
def custom_user_edit(req): user = req.user form = CustomUserChangeForm(instance=user) #initiates form with user info if req.method == 'POST': # will only show validation errors on POST, not GET form = CustomUserChangeForm(req.POST, instance=user) #'instance=user' ensures you are overwriting current user and NOT creating a new one with same info if form.is_valid(): user_update = form.save(commit=False) user_update.is_active = False # turns user to inactive and kicks them out form.save() # saves all the info #Info needed to send user email email = req.user.email subject = f"You have edited your Road Star Tire Wholesale account" message = f"A Road Star Tire staff member will have to re-verify your account before you can log in again to place an order. If this is an error or urgent, please call +1-905-660-3209." send_mail( subject, message, 'settings.EMAIL_HOST_USER', [email], fail_silently=False ) # INFO NEEDED FOR EMAIL subject = f"{user.company_name} edited their account" message = f"This company - {user.company_name}, {user.email} - edited their account and will need to be re-verified. Please log in to your admin account (http://www.roadstartirewholesale.ca/admin/login/) and re-verify their account." mail_admins( subject, message, fail_silently=False ) return redirect('account') return render(req, 'custom_user_edit_form.html', {'form': form})
def profile(request): user = request.user form = CustomUserChangeForm(instance=user) if request.method == 'POST': form = CustomUserChangeForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() context = {'form': form} return render(request, 'profile.html', context)
def profile(request): if request.method == 'POST': u_form = CustomUserChangeForm(request.POST, instance=request.user) if u_form.is_valid(): u_form.save() messages.success(request, f'Your account has been updated!') return redirect('profile') else: u_form = CustomUserChangeForm(instance=request.user) context = {'u_form': u_form} return render(request, 'account/profile.html', context)
def account(request): events_list = Event.objects.filter(created_by=request.user) if request.method == 'POST': print(request.POST) user_form = CustomUserChangeForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() else: user_form = CustomUserChangeForm(instance=request.user) context = {'events_list': events_list, 'form': user_form} template_name = 'eventFinderApp/account.html' return render(request, template_name, context)
def user_edit(request, pk): UserModel = get_user_model() user = get_object_or_404(UserModel, pk=pk) if request.method == "POST": form = CustomUserChangeForm(request.POST, instance=user) if form.is_valid(): form.save() return redirect('user_list') else: form = CustomUserChangeForm(instance=user) return render(request, 'blog/user_edit.html', {'form': form})
def account(request): events_list = Event.objects.filter(host=request.user) if request.method == "POST": print(request.POST) user_form = CustomUserChangeForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() else: user_form = CustomUserChangeForm(instance=request.user) context = {"events_list": events_list, "form": user_form} template_name = "eventFinderApp/account.html" return render(request, template_name, context)
def edit_profile(request): if request.method == 'POST': form = CustomUserChangeForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): form.save() messages.add_message(request, messages.SUCCESS, 'Profile updated!') return redirect('user:profile_edit') else: form = CustomUserChangeForm(instance=request.user) return render(request, 'users/profile_edit.html', {'form': form})
def edit_profile(request): if request.method == 'POST': form = CustomUserChangeForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.success( request, ('Wohoo! You have successfully edited your profile')) return redirect('main-urls:home') else: form = CustomUserChangeForm(instance=request.user) context = {'form': form} return render(request, 'users/edit_profile.html', context)
def user_edit(request): user = request.user if request.method == "POST": print(request.POST) form = CustomUserChangeForm(request.POST, instance=request.user) if form.is_valid(): user = form.save(commit=False) user.save() form.save_m2m() return redirect('user_detail') else: form = CustomUserChangeForm(instance=user) return render(request, 'user_form.html', {'form': form})
def account_profile_edit(request, pk): customuser = get_object_or_404(CustomUser, pk=pk) if request.method == "POST": form = CustomUserChangeForm(request.POST, instance=customuser) if form.is_valid(): customuser = form.save(commit=False) customuser.updated_date = timezone.now() customuser.save() customuser = CustomUser.objects.get(pk=pk) return render(request,'portfolio/accountprofile.html', {'customuser': customuser}) else: # edit form = CustomUserChangeForm(instance=customuser) return render(request, 'portfolio/accountprofile_edit.html', {'form': form})
def user_edit(request): """Display the edit form and handle the edit action.""" if request.method == 'POST': user_form = CustomUserChangeForm(instance=request.user, data=request.POST) if user_form.is_valid(): user_form.save() messages.success(request, 'Your profile has been updated successfully!') else: messages.error(request, 'Please correct the error below.') else: user_form = CustomUserChangeForm(instance=request.user) return render(request, 'management_tool/user_edit.html', {'user_form': user_form})
def test_edit_form_is_valid(self): form_data = { 'username': '******', 'birth_date': datetime.datetime.now(), 'random_number': '4' } form = CustomUserChangeForm(data=form_data) self.assertTrue(form.is_valid())
def minhaconta(request): if not request.user.is_authenticated: return redirect('index') else: if request.method == 'POST': form_profile = CustomUserChangeForm(request.POST, instance=request.user) if form_profile.is_valid(): form_profile.save() messages.success(request, ('Seu perfil foi atualizado com sucesso!')) return redirect('minhaconta') else: messages.error(request, ('Por favor, corrija os erros abaixo.')) else: form_profile = CustomUserChangeForm(instance=request.user) return render(request, 'minhaconta.html', { 'form_profile': form_profile })
def test_no_nick(self): valid_data = { 'username': '******', 'email': '*****@*****.**', 'nick': '' } form = CustomUserChangeForm(valid_data) self.assertFalse(form.is_valid())
def test_wrong_email(self): valid_data = { 'username': '******', 'email': 'new_nobody', 'nick': 'new_nobodys_nick' } form = CustomUserChangeForm(valid_data) self.assertFalse(form.is_valid())
def test_correct_data(self): valid_data = { 'username': '******', 'email': '*****@*****.**', 'nick': 'new_nobodys_nick' } form = CustomUserChangeForm(valid_data) self.assertTrue(form.is_valid())
def familleView(request, id_famille): user = get_object_or_404(CustomUser, pk=id_famille) famille = CustomUserChangeForm(instance=user) for field in famille.fields: famille.fields[field].widget.attrs['readonly'] = True return render(request, 'pages/famille.html', { 'famille': famille, 'nom': user.username })
def test_too_long_username(self): valid_data = { 'username': '******', 'email': '*****@*****.**', 'nick': 'new_nobodys_nick' } form = CustomUserChangeForm(valid_data) self.assertFalse(form.is_valid())
def testProfileEditAllFieldsValid(self): local_fields = ['username', 'gender', 'phone', 'about'] correct_data = {'username':'******' , 'gender':'Male', 'phone':'+17037561234', 'about':'I am a god at soccer'} self.form = CustomUserChangeForm(data={'username':'******' , 'gender':'Male', 'phone':'+17037561234', 'about':'I am a god at soccer'}, instance=self.user) if self.form.is_valid(): self.form.save() for field in local_fields: self.assertTrue(getattr(self.user, field, '') == correct_data[field] )
def test_form_invalid(db): form = CustomUserChangeForm( data={ "username": "", "first_name": "bad-data", "last_name": "", "email": "", "image": "", }) assert form.is_valid() is False
def edit_profile(request): if request.method == 'POST': u_form = CustomUserChangeForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() messages.success(request, 'Your profile has been saved!') return HttpResponseRedirect(reverse('resumes:edit-profile')) else: u_form = CustomUserChangeForm(instance=request.user) p_form = ProfileUpdateForm(instance=request.user.profile) context = { 'p_form': p_form, 'u_form': u_form, } return render(request, 'resumes/profile.html', context)
def test_valid_form(db): form = CustomUserChangeForm( data={ "username": "******", "first_name": "Walter", "last_name": "Simonson", "email": "*****@*****.**", "image": "user/walter.jpg", }) assert form.is_valid() is True
def test_valid_data(self): form = CustomUserChangeForm({ "username": "******", "email": "*****@*****.**", "favorite_team": 119, "password": "******", }) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual(user.username, "TurangaLeela") self.assertEqual(user.email, "*****@*****.**") self.assertEqual(user.favorite_team, 119)
def test_invalid_institutional_email(self): """ Ensure an email address from an unsupported institution domain is caught. """ with self.assertRaises(Institution.DoesNotExist): form = CustomUserChangeForm( data={ 'username': self.shibboleth_user.username, 'first_name': self.shibboleth_user.first_name, 'last_name': self.shibboleth_user.last_name, 'email': '*****@*****.**', 'is_shibboleth_login_required': True, 'date_joined': datetime.date.today(), }, instance=self.shibboleth_user, ) self.assertTrue(form.is_valid()) form.save()
def update(request, id): obj = None if id is not None: obj = User.objects.get(id=id) else: obj = User.objects.get(email=request.user.email) if obj is not None: form = CustomUserChangeForm(request.POST, instance=obj) if form.is_valid(): form.save() return redirect("/users?updated=true") else: err = form.errors print(err) return render(request, 'users/detail.html', { 'object': obj, 'errors': err }) else: login_user(request)
def test_user_update(self): """ Ensure the user update form works. """ first_name = 'John' last_name = 'Smith' email = '*****@*****.**' form = CustomUserChangeForm( data={ 'username': self.shibboleth_user.username, 'first_name': first_name, 'last_name': last_name, 'email': email, 'is_shibboleth_login_required': True, 'date_joined': datetime.date.today(), }, instance=self.shibboleth_user, ) self.assertTrue(form.is_valid()) form.save() self.assertEqual(self.shibboleth_user.first_name, first_name) self.assertEqual(self.shibboleth_user.last_name, last_name) self.assertEqual(self.shibboleth_user.email, email)
def test_custom_user_creation_form_email_field_label(self): form = CustomUserChangeForm() self.assertTrue(form.fields['email'].label == 'Email address')
def testFormEditAllFieldsValid(self): form = CustomUserChangeForm(data={'username':'******' , 'gender':'Male', 'phone':'+17037561234', 'about':'Some About'}) self.assertTrue(form.is_valid())
def testFormEditInvalidPhone(self): form = CustomUserChangeForm(data={'username':'******' , 'gender':'Male', 'phone':'+1703756123w', 'about':'Some About'}) self.assertFalse(form.is_valid())