def admin_edit_user(request, user_id): """Edit user.""" # Security check. if not request.user.is_superuser: return render_to_response("error.html", {"error": "You must be superuser"}, context_instance=RequestContext(request)) user = get_object_or_404(Profile, pk=user_id) if request.method == "POST": form = forms.ProfileForm(request.POST, instance=user) if form.is_valid(): user = form.save() # Auditing. log_activity("A", "Edited user %s" % user.username, request) return HttpResponseRedirect( reverse("users.views.admin_show_user", args=(user.id, ))) else: form = forms.ProfileForm(instance=user) return render_to_response("admin/edit_user.html", { "form": form, "user": user }, context_instance=RequestContext(request))
def get(self, request, pk, *args, **kwargs): user = shortcuts.get_object_or_404(auth_models.User, pk=pk, profile__isnull=False) context = { 'user_form': forms.UserUpdateForm(instance=user), 'profile_form': forms.ProfileForm(instance=user.profile) } return shortcuts.render(request, self.template_name, context)
def post(self, request, pk, *args, **kwargs): user = shortcuts.get_object_or_404(auth_models.User, pk=pk, profile__isnull=False) user_form = forms.UserUpdateForm(request.POST, instance=user) profile_form = forms.ProfileForm(request.POST, request.FILES, instance=user.profile) if user_form.is_valid() and profile_form.is_valid(): with transaction.atomic(): user_form.save() profile_form.save() return shortcuts.redirect('users:user_detail', pk=user_form.instance.id) else: context = { 'user_form': user_form, 'profile_form': profile_form } return shortcuts.render(request, self.template_name, context)
def post(self, request, *args, **kwargs): user_form = forms.UserCreateForm(request.POST) profile_form = forms.ProfileForm(request.POST, request.FILES) if user_form.is_valid() and profile_form.is_valid(): with transaction.atomic(): user_form.save() profile_form.instance.user = user_form.instance profile_form.save() return shortcuts.redirect('users:user_detail', pk=user_form.instance.id) else: context = { 'user_form': user_form, 'profile_form': profile_form } return shortcuts.render(request, self.template_name, context)
def get(self, request): user_instance = get_object_or_404(klass=User, pk=request.user.id) profile_instance = get_object_or_404(klass=users_models.Profile, user=request.user) form_user = forms.UserForm(instance=user_instance) form_profile = forms.ProfileForm(instance=profile_instance) return render(request=request, template_name='users/user_form.html', context={ 'form_user': form_user, 'form_profile': form_profile, 'action': 'Edit Profile' })
def post(self, request): form_user = forms.UserForm(request.POST) form_profile = forms.ProfileForm(request.POST) if form_user.is_valid() and form_profile.is_valid(): form_user.save() user = form_user.save() profile = form_profile.save(commit=False) profile.user = user profile.save() login(request=request, user=user) messages.success(request, message='Account created successfully') return HttpResponseRedirect(redirect_to=reverse('profile-view')) return render(request=request, template_name='users/user_form.html', context={ 'form_user': form_user, 'form_profile': form_profile, 'action': 'Create Profile' })
def profile(request): context = {} try: profile = UserProfile.objects.get(user=request.user) print(profile.address_line_1) except UserProfile.DoesNotExist: profile = None form = forms.ProfileForm(request.POST or None, request.FILES or None, instance=profile or None) if request.method == 'POST': if form.is_valid(): data = form.save(commit=False) data.user = request.user data.save() messages.success(request, 'Profile Updated') return HttpResponseRedirect("") context['form'] = form context['profile'] = profile template_name = 'users/profile.html' return render(request, template_name, context)
def post(self, request): user_instance = get_object_or_404(klass=User, pk=request.user.id) profile_instance = get_object_or_404(klass=users_models.Profile, user=request.user) form_user = forms.UserForm(request.POST, instance=user_instance) form_profile = forms.ProfileForm(request.POST, instance=profile_instance) if form_user.is_valid() and form_profile.is_valid(): form_profile.save() form_user.save() messages.success( request, message= f'Updated data for {user_instance.username} successfully') return render(request=request, template_name='users/user_form.html', context={ 'form_user': form_user, 'form_profile': form_profile, 'action': 'Edit Profile' })
def get(self, request, *args, **kwargs): context = { 'user_form': forms.UserCreateForm(), 'profile_form': forms.ProfileForm() } return shortcuts.render(request, self.template_name, context)
def test_form_user_profile(self): user = User.objects.create_user('*****@*****.**', 'password') user_form = forms.UserForm(data={'user': user}) profile_form = forms.ProfileForm(data={'user': user, 'phone': '9999999999'}) assert (user_form.is_valid() and profile_form.is_valid()) is True
def test_blank_form(self): form = forms.ProfileForm(data={}) assert form.is_valid() is True