def edit_profile(request): """Edit user profile view.""" # Don't user request.user user = User.objects.get(pk=request.user.id) profile = user.userprofile user_groups = stringify_groups(profile.groups.all().order_by('name')) user_skills = stringify_groups(profile.skills.all().order_by('name')) user_languages = stringify_groups(profile.languages.all().order_by('name')) user_form = forms.UserForm(request.POST or None, instance=user) AccountsFormset = inlineformset_factory(UserProfile, ExternalAccount, extra=1) accounts_formset = AccountsFormset(request.POST or None, instance=profile) new_profile = False form = forms.ProfileForm if not profile.is_complete: new_profile = True form = forms.RegisterForm profile_form = form(request.POST or None, request.FILES or None, instance=profile, locale=request.locale, initial=dict(groups=user_groups, skills=user_skills, languages=user_languages)) email_form = forms.EmailForm(request.POST or None, initial={'email': request.user.email, 'user_id': request.user.id}) if (user_form.is_valid() and profile_form.is_valid() and accounts_formset.is_valid() and email_form.is_valid()): old_username = request.user.username user_form.save() profile_form.save() accounts_formset.save() # Notify the user that their old profile URL won't work. if new_profile: update_invites(request) messages.info(request, _(u'Your account has been created.')) elif user.username != old_username: messages.info(request, _(u'You changed your username; please note your ' 'profile URL has also changed.')) if email_form.email_changed(): return render(request, 'phonebook/verify_email.html', {'email': email_form.cleaned_data['email']}) return redirect('phonebook:profile_view', user.username) data = dict(profile_form=profile_form, user_form=user_form, accounts_formset = accounts_formset, email_form = email_form, user_groups=user_groups, my_vouches=UserProfile.objects.filter(vouched_by=profile), profile=request.user.userprofile, apps=user.apiapp_set.filter(is_active=True)) # If there are form errors, don't send a 200 OK. status = 400 if (profile_form.errors or user_form.errors) else 200 return render(request, 'phonebook/edit_profile.html', data, status=status)
def edit_profile(request): """Edit user profile view.""" # Don't user request.user user = User.objects.get(pk=request.user.id) profile = user.userprofile user_groups = profile.groups.all().order_by('name') user_skills = stringify_groups(profile.skills.all().order_by('name')) user_form = forms.UserForm(request.POST or None, instance=user) accounts_formset = forms.AccountsFormset(request.POST or None, instance=profile) new_profile = False form = forms.ProfileForm language_formset = forms.LanguagesFormset(request.POST or None, instance=profile, locale=request.locale) if not profile.is_complete: new_profile = True form = forms.RegisterForm profile_form = form(request.POST or None, request.FILES or None, instance=profile, initial=dict(skills=user_skills)) email_form = forms.EmailForm(request.POST or None, initial={ 'email': request.user.email, 'user_id': request.user.id }) all_forms = [ user_form, profile_form, accounts_formset, email_form, language_formset ] # Using ``list`` to force calling is_valid on all the forms, even if earlier # ones are not valid, so we detect and display all the errors. if all(list(f.is_valid() for f in all_forms)): old_username = request.user.username user_form.save() profile_form.save() accounts_formset.save() language_formset.save() # Notify the user that their old profile URL won't work. if new_profile: redeem_invite(profile, request.session.get('invite-code')) messages.info(request, _(u'Your account has been created.')) elif user.username != old_username: messages.info( request, _(u'You changed your username; please note your ' u'profile URL has also changed.')) if email_form.email_changed(): return render(request, 'phonebook/verify_email.html', {'email': email_form.cleaned_data['email']}) return redirect('phonebook:profile_view', user.username) data = dict(profile_form=profile_form, user_form=user_form, accounts_formset=accounts_formset, email_form=email_form, user_groups=user_groups, my_vouches=UserProfile.objects.filter(vouched_by=profile), profile=request.user.userprofile, apps=user.apiapp_set.filter(is_active=True), language_formset=language_formset, mapbox_id=settings.MAPBOX_MAP_ID) # If there are form errors, don't send a 200 OK. status = 400 if any(f.errors for f in all_forms) else 200 return render(request, 'phonebook/edit_profile.html', data, status=status)