def member_add(request): """ Show a member profile and his sharing history. """ if request.method == "POST": profile = Profile() form_user = UserForm(request.POST) form_profile = ProfileForm(request.POST) if form_user.is_valid() and form_profile.is_valid(): confirm_password = form_user.cleaned_data["confirm_password"] if confirm_password == form_user.cleaned_data["password"]: user = User.objects.create_user( form_user.cleaned_data["username"], form_user.cleaned_data["email"], form_user.cleaned_data["password"], ) profile.user = user profile.save() messages.add_message(request, messages.SUCCESS, _("The new user has been successfully added !")) form_user = UserForm() form_profile = ProfileForm() else: messages.add_message(request, messages.ERROR, _("The two passwords are different.")) else: form_user = UserForm() form_profile = ProfileForm() return render(request, "sharing/member_form.html", {"form_user": form_user, "form_profile": form_profile})
def profile_edit(request): """ Show a member's profile and allows him to edit it. """ profile = get_object_or_404(Profile, user__id=request.user.id) if request.method == "POST": form_user = UserEditForm(request.POST, instance=request.user) form_profile = ProfileForm(request.POST, request.FILES, instance=profile) if form_user.is_valid() and form_profile.is_valid(): confirm_password = form_user.cleaned_data["confirm_password"] password = form_user.cleaned_data["password"] # Either both passwords have been sent and are equal # Or none has been sent if (not confirm_password and not password) or confirm_password == password: user = form_user.save(commit=False) # Only update password if both have been sent otherwise it will logout the user. if confirm_password and password: user.set_password(password) user.save() user = authenticate(username=user.username, password=password) if user is not None: if user.is_active: login(request, user) # We otherwise save the email modifications. else: user.save() profile = form_profile.save() translation_activate(profile.locale) request.session[LANGUAGE_SESSION_KEY] = profile.locale messages.add_message(request, messages.SUCCESS, _("Your profile has been successfully updated !")) return redirect(reverse("profile_show", args=[request.user.id])) else: messages.add_message(request, messages.ERROR, _("The two passwords are different or you forgot one.")) else: form_user = UserEditForm(instance=request.user) form_profile = ProfileForm(instance=profile) return render(request, "sharing/member_form.html", {"form_user": form_user, "form_profile": form_profile})