def register(request): """Registers Users. Pulls out an invite code if it exists and auto validates the user if so. Single-purpose view. """ # TODO already vouched users can be re-vouched? if 'code' in request.GET: request.session['invite-code'] = request.GET['code'] if request.user.is_authenticated(): if not request.user.userprofile.is_vouched: redeem_invite(request.user.userprofile, request.session['invite-code']) else: messages.info(request, _("You've been invited to join Mozillians.org! " "Sign in and then you can create a profile.")) return redirect('phonebook:home')
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={'skills': user_skills, 'saveregion': True if profile.geo_region else False, 'savecity': True if profile.geo_city else False, 'lat': profile.lat, 'lng': profile.lng}) 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() 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: # Notify the user that their old profile URL won't work. 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, profile=request.user.userprofile, apps=user.apiapp_set.filter(is_active=True), language_formset=language_formset, mapbox_id=settings.MAPBOX_PROFILE_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)
def edit_profile(request): """Edit user profile view.""" # Don't use request.user user = User.objects.get(pk=request.user.id) profile = user.userprofile user_groups = profile.groups.all().order_by('name') emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL) accounts_qs = ExternalAccount.objects.exclude(type=ExternalAccount.TYPE_EMAIL) sections = { 'registration_section': ['user_form', 'registration_form'], 'basic_section': ['user_form', 'basic_information_form'], 'groups_section': ['groups_privacy_form'], 'skills_section': ['skills_form'], 'email_section': ['email_privacy_form', 'alternate_email_formset'], 'languages_section': ['language_privacy_form', 'language_formset'], 'accounts_section': ['accounts_formset'], 'location_section': ['location_form'], 'irc_section': ['irc_form'], 'contribution_section': ['contribution_form'], 'tshirt_section': ['tshirt_form'], } curr_sect = next((s for s in sections.keys() if s in request.POST), None) def get_request_data(form): if curr_sect and form in sections[curr_sect]: return request.POST return None ctx = {} ctx['user_form'] = forms.UserForm(get_request_data('user_form'), instance=user) ctx['registration_form'] = forms.RegisterForm(get_request_data('registration_form'), request.FILES or None, instance=profile) basic_information_data = get_request_data('basic_information_form') ctx['basic_information_form'] = forms.BasicInformationForm(basic_information_data, request.FILES or None, instance=profile) ctx['accounts_formset'] = forms.AccountsFormset(get_request_data('accounts_formset'), instance=profile, queryset=accounts_qs) ctx['location_form'] = forms.LocationForm(get_request_data('location_form'), instance=profile) ctx['language_formset'] = forms.LanguagesFormset(get_request_data('language_formset'), instance=profile, locale=request.locale) language_privacy_data = get_request_data('language_privacy_form') ctx['language_privacy_form'] = forms.LanguagesPrivacyForm(language_privacy_data, instance=profile) ctx['skills_form'] = forms.SkillsForm(get_request_data('skills_form'), instance=profile) ctx['contribution_form'] = forms.ContributionForm(get_request_data('contribution_form'), instance=profile) ctx['tshirt_form'] = forms.TshirtForm(get_request_data('tshirt_form'), instance=profile) ctx['groups_privacy_form'] = forms.GroupsPrivacyForm(get_request_data('groups_privacy_form'), instance=profile) ctx['irc_form'] = forms.IRCForm(get_request_data('irc_form'), instance=profile) ctx['email_privacy_form'] = forms.EmailPrivacyForm(get_request_data('email_privacy_form'), instance=profile) alternate_email_formset_data = get_request_data('alternate_email_formset') ctx['alternate_email_formset'] = forms.AlternateEmailFormset(alternate_email_formset_data, instance=profile, queryset=emails) ctx['autocomplete_form_media'] = ctx['registration_form'].media + ctx['skills_form'].media forms_valid = True if request.POST: if not curr_sect: raise Http404 curr_forms = map(lambda x: ctx[x], sections[curr_sect]) forms_valid = all(map(lambda x: x.is_valid(), curr_forms)) if forms_valid: old_username = request.user.username for f in curr_forms: f.save() # Spawn task to check for spam if not profile.can_vouch: params = { 'instance_id': profile.id, 'user_ip': request.META.get('REMOTE_ADDR'), 'user_agent': request.META.get('HTTP_USER_AGENT'), 'referrer': request.META.get('HTTP_REFERER'), 'comment_author': profile.full_name, 'comment_author_email': profile.email, 'comment_content': profile.bio } check_spam_account.delay(**params) next_section = request.GET.get('next') next_url = urlparams(reverse('phonebook:profile_edit'), next_section) if curr_sect == 'registration_section': settings_url = reverse('phonebook:profile_edit') settings_link = '<a href="{0}">settings</a>'.format(settings_url) msg = _(u'Your registration is complete. ' u'Feel free to visit the {0} page to add ' u'additional information to your profile.'.format(settings_link)) messages.info(request, mark_safe(msg)) redeem_invite(profile, request.session.get('invite-code')) next_url = reverse('phonebook:profile_view', args=[user.username]) elif user.username != old_username: msg = _(u'You changed your username; ' u'please note your profile URL has also changed.') messages.info(request, _(msg)) return HttpResponseRedirect(next_url) ctx.update({ 'user_groups': user_groups, 'profile': request.user.userprofile, 'vouch_threshold': settings.CAN_VOUCH_THRESHOLD, 'appsv2': profile.apps.filter(enabled=True), 'forms_valid': forms_valid }) return render(request, 'phonebook/edit_profile.html', ctx)
def edit_profile(request): """Edit user profile view.""" # Don't use request.user user = User.objects.get(pk=request.user.id) profile = user.userprofile user_groups = profile.groups.all().order_by('name') emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL) accounts_qs = ExternalAccount.objects.exclude( type=ExternalAccount.TYPE_EMAIL) sections = { 'registration_section': ['user_form', 'registration_form'], 'basic_section': ['user_form', 'basic_information_form'], 'groups_section': ['groups_privacy_form'], 'skills_section': ['skills_form'], 'email_section': ['email_privacy_form', 'alternate_email_formset'], 'languages_section': ['language_privacy_form', 'language_formset'], 'accounts_section': ['accounts_formset'], 'irc_section': ['irc_form'], 'location_section': ['location_form'], 'contribution_section': ['contribution_form'], 'tshirt_section': ['tshirt_form'], 'developer_section': ['developer_form'] } curr_sect = next((s for s in sections.keys() if s in request.POST), None) def get_request_data(form): if curr_sect and form in sections[curr_sect]: return request.POST return None ctx = {} ctx['user_form'] = forms.UserForm(get_request_data('user_form'), instance=user) ctx['registration_form'] = forms.RegisterForm( get_request_data('registration_form'), request.FILES or None, instance=profile) basic_information_data = get_request_data('basic_information_form') ctx['basic_information_form'] = forms.BasicInformationForm( basic_information_data, request.FILES or None, instance=profile) ctx['accounts_formset'] = forms.AccountsFormset( get_request_data('accounts_formset'), instance=profile, queryset=accounts_qs) ctx['language_formset'] = forms.LanguagesFormset( get_request_data('language_formset'), instance=profile, locale=request.locale) language_privacy_data = get_request_data('language_privacy_form') ctx['language_privacy_form'] = forms.LanguagesPrivacyForm( language_privacy_data, instance=profile) ctx['skills_form'] = forms.SkillsForm(get_request_data('skills_form'), instance=profile) location_initial = { 'saveregion': True if profile.geo_region else False, 'savecity': True if profile.geo_city else False, 'lat': profile.lat, 'lng': profile.lng } ctx['location_form'] = forms.LocationForm( get_request_data('location_form'), instance=profile, initial=location_initial) ctx['contribution_form'] = forms.ContributionForm( get_request_data('contribution_form'), instance=profile) ctx['tshirt_form'] = forms.TshirtForm(get_request_data('tshirt_form'), instance=profile) ctx['groups_privacy_form'] = forms.GroupsPrivacyForm( get_request_data('groups_privacy_form'), instance=profile) ctx['irc_form'] = forms.IRCForm(get_request_data('irc_form'), instance=profile) ctx['developer_form'] = forms.DeveloperForm( get_request_data('developer_form'), instance=profile) ctx['email_privacy_form'] = forms.EmailPrivacyForm( get_request_data('email_privacy_form'), instance=profile) alternate_email_formset_data = get_request_data('alternate_email_formset') ctx['alternate_email_formset'] = forms.AlternateEmailFormset( alternate_email_formset_data, instance=profile, queryset=emails) forms_valid = True if request.POST: if not curr_sect: raise Http404 curr_forms = map(lambda x: ctx[x], sections[curr_sect]) forms_valid = all(map(lambda x: x.is_valid(), curr_forms)) if forms_valid: old_username = request.user.username for f in curr_forms: f.save() next_section = request.GET.get('next') next_url = urlparams(reverse('phonebook:profile_edit'), next_section) if curr_sect == 'registration_section': settings_url = reverse('phonebook:profile_edit') settings_link = '<a href="{0}">settings</a>'.format( settings_url) msg = _(u'Your registration is complete. ' u'Feel free to visit the {0} page to add ' u'additional information to your profile.'.format( settings_link)) messages.info(request, mark_safe(msg)) redeem_invite(profile, request.session.get('invite-code')) next_url = reverse('phonebook:profile_view', args=[user.username]) elif user.username != old_username: msg = _(u'You changed your username; ' u'please note your profile URL has also changed.') messages.info(request, _(msg)) return HttpResponseRedirect(next_url) ctx.update({ 'user_groups': user_groups, 'profile': request.user.userprofile, 'vouch_threshold': settings.CAN_VOUCH_THRESHOLD, 'mapbox_id': settings.MAPBOX_PROFILE_ID, 'apps': user.apiapp_set.filter(is_active=True), 'appsv2': profile.apps.filter(enabled=True), 'forms_valid': forms_valid }) return render(request, 'phonebook/edit_profile.html', ctx)
def edit_profile(request): """Edit user profile view.""" # Don't use request.user user = User.objects.get(pk=request.user.id) profile = user.userprofile user_groups = profile.groups.all().order_by('name') emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL) accounts_qs = ExternalAccount.objects.exclude(type=ExternalAccount.TYPE_EMAIL) sections = { 'registration_section': ['user_form', 'registration_form'], 'basic_section': ['user_form', 'basic_information_form'], 'groups_section': ['groups_privacy_form'], 'skills_section': ['skills_form'], 'email_section': ['email_privacy_form', 'alternate_email_formset'], 'languages_section': ['language_privacy_form', 'language_formset'], 'accounts_section': ['accounts_formset'], 'irc_section': ['irc_form'], 'location_section': ['location_form'], 'contribution_section': ['contribution_form'], 'tshirt_section': ['tshirt_form'], 'developer_section': ['developer_form'] } curr_sect = next((s for s in sections.keys() if s in request.POST), None) def get_request_data(form): if curr_sect and form in sections[curr_sect]: return request.POST return None ctx = {} ctx['user_form'] = forms.UserForm(get_request_data('user_form'), instance=user) ctx['registration_form'] = forms.RegisterForm(get_request_data('registration_form'), request.FILES or None, instance=profile) basic_information_data = get_request_data('basic_information_form') ctx['basic_information_form'] = forms.BasicInformationForm(basic_information_data, request.FILES or None, instance=profile) ctx['accounts_formset'] = forms.AccountsFormset(get_request_data('accounts_formset'), instance=profile, queryset=accounts_qs) ctx['language_formset'] = forms.LanguagesFormset(get_request_data('language_formset'), instance=profile, locale=request.locale) language_privacy_data = get_request_data('language_privacy_form') ctx['language_privacy_form'] = forms.LanguagesPrivacyForm(language_privacy_data, instance=profile) ctx['skills_form'] = forms.SkillsForm(get_request_data('skills_form'), instance=profile) location_initial = { 'saveregion': True if profile.geo_region else False, 'savecity': True if profile.geo_city else False, 'lat': profile.lat, 'lng': profile.lng } ctx['location_form'] = forms.LocationForm(get_request_data('location_form'), instance=profile, initial=location_initial) ctx['contribution_form'] = forms.ContributionForm(get_request_data('contribution_form'), instance=profile) ctx['tshirt_form'] = forms.TshirtForm(get_request_data('tshirt_form'), instance=profile) ctx['groups_privacy_form'] = forms.GroupsPrivacyForm(get_request_data('groups_privacy_form'), instance=profile) ctx['irc_form'] = forms.IRCForm(get_request_data('irc_form'), instance=profile) ctx['developer_form'] = forms.DeveloperForm(get_request_data('developer_form'), instance=profile) ctx['email_privacy_form'] = forms.EmailPrivacyForm(get_request_data('email_privacy_form'), instance=profile) alternate_email_formset_data = get_request_data('alternate_email_formset') ctx['alternate_email_formset'] = forms.AlternateEmailFormset(alternate_email_formset_data, instance=profile, queryset=emails) forms_valid = True if request.POST: if not curr_sect: raise Http404 curr_forms = map(lambda x: ctx[x], sections[curr_sect]) forms_valid = all(map(lambda x: x.is_valid(), curr_forms)) if forms_valid: old_username = request.user.username for f in curr_forms: f.save() # Spawn task to check for spam if not profile.can_vouch: params = { 'instance_id': profile.id, 'user_ip': request.META.get('REMOTE_ADDR'), 'user_agent': request.META.get('HTTP_USER_AGENT'), 'referrer': request.META.get('HTTP_REFERER'), 'comment_author': profile.full_name, 'comment_author_email': profile.email, 'comment_content': profile.bio } check_spam_account.delay(**params) next_section = request.GET.get('next') next_url = urlparams(reverse('phonebook:profile_edit'), next_section) if curr_sect == 'registration_section': settings_url = reverse('phonebook:profile_edit') settings_link = '<a href="{0}">settings</a>'.format(settings_url) msg = _(u'Your registration is complete. ' u'Feel free to visit the {0} page to add ' u'additional information to your profile.'.format(settings_link)) messages.info(request, mark_safe(msg)) redeem_invite(profile, request.session.get('invite-code')) next_url = reverse('phonebook:profile_view', args=[user.username]) elif user.username != old_username: msg = _(u'You changed your username; ' u'please note your profile URL has also changed.') messages.info(request, _(msg)) return HttpResponseRedirect(next_url) ctx.update({ 'user_groups': user_groups, 'profile': request.user.userprofile, 'vouch_threshold': settings.CAN_VOUCH_THRESHOLD, 'mapbox_id': settings.MAPBOX_PROFILE_ID, 'apps': user.apiapp_set.filter(is_active=True), 'appsv2': profile.apps.filter(enabled=True), 'forms_valid': forms_valid }) return render(request, 'phonebook/edit_profile.html', ctx)
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)
def edit_profile(request): """Edit user profile view.""" # Don't use 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')) emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL) accounts_qs = ExternalAccount.objects.exclude(type=ExternalAccount.TYPE_EMAIL) sections = { 'registration_section': ['user_form', 'registration_form'], 'basic_section': ['user_form', 'basic_information_form'], 'groups_section': ['groups_privacy_form'], 'skills_section': ['skills_form'], 'email_section': ['email_privacy_form', 'alternate_email_formset'], 'languages_section': ['language_privacy_form', 'language_formset'], 'accounts_section': ['accounts_formset'], 'irc_section': ['irc_form'], 'location_section': ['location_form'], 'contribution_section': ['contribution_form'], 'tshirt_section': ['tshirt_form'], 'developer_section': ['developer_form'] } curr_sect = next((s for s in sections.keys() if s in request.POST), None) def get_request_data(form): if curr_sect and form in sections[curr_sect]: return request.POST return None ctx = {} ctx['user_form'] = forms.UserForm(get_request_data('user_form'), instance=user) ctx['registration_form'] = forms.RegisterForm(get_request_data('registration_form'), request.FILES or None, instance=profile) basic_information_data = get_request_data('basic_information_form') ctx['basic_information_form'] = forms.BasicInformationForm(basic_information_data, request.FILES or None, instance=profile) ctx['accounts_formset'] = forms.AccountsFormset(get_request_data('accounts_formset'), instance=profile, queryset=accounts_qs) ctx['language_formset'] = forms.LanguagesFormset(get_request_data('language_formset'), instance=profile, locale=request.locale) language_privacy_data = get_request_data('language_privacy_form') ctx['language_privacy_form'] = forms.LanguagesPrivacyForm(language_privacy_data, instance=profile) ctx['skills_form'] = forms.SkillsForm(get_request_data('skills_form'), instance=profile, initial={'skills': user_skills}) location_initial = { 'saveregion': True if profile.geo_region else False, 'savecity': True if profile.geo_city else False, 'lat': profile.lat, 'lng': profile.lng } ctx['location_form'] = forms.LocationForm(get_request_data('location_form'), instance=profile, initial=location_initial) ctx['contribution_form'] = forms.ContributionForm(get_request_data('contribution_form'), instance=profile) ctx['tshirt_form'] = forms.TshirtForm(get_request_data('tshirt_form'), instance=profile) ctx['groups_privacy_form'] = forms.GroupsPrivacyForm(get_request_data('groups_privacy_form'), instance=profile) ctx['irc_form'] = forms.IRCForm(get_request_data('irc_form'), instance=profile) ctx['developer_form'] = forms.DeveloperForm(get_request_data('developer_form'), instance=profile) ctx['email_privacy_form'] = forms.EmailPrivacyForm(get_request_data('email_privacy_form'), instance=profile) alternate_email_formset_data = get_request_data('alternate_email_formset') ctx['alternate_email_formset'] = forms.AlternateEmailFormset(alternate_email_formset_data, instance=profile, queryset=emails) forms_valid = True if request.POST: if not curr_sect: raise Http404 curr_forms = map(lambda x: ctx[x], sections[curr_sect]) forms_valid = all(map(lambda x: x.is_valid(), curr_forms)) if forms_valid: old_username = request.user.username for f in curr_forms: f.save() if curr_sect == 'registration_section': redeem_invite(profile, request.session.get('invite-code')) elif user.username != old_username: msg = (u'You changed your username; ' u'please note your profile URL has also changed.') messages.info(request, _(msg)) next_section = request.GET.get('next') next_url = urlparams(reverse('phonebook:profile_edit'), next_section) return HttpResponseRedirect(next_url) ctx.update({ 'user_groups': user_groups, 'profile': request.user.userprofile, 'vouch_threshold': settings.CAN_VOUCH_THRESHOLD, 'mapbox_id': settings.MAPBOX_PROFILE_ID, 'apps': user.apiapp_set.filter(is_active=True), 'appsv2': profile.apps.filter(enabled=True), 'forms_valid': forms_valid }) return render(request, 'phonebook/edit_profile.html', ctx)
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)
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) queryset = ExternalAccount.objects.exclude(type=ExternalAccount.TYPE_EMAIL) accounts_formset = forms.AccountsFormset(request.POST or None, instance=profile, queryset=queryset) 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={ 'skills': user_skills, 'saveregion': True if profile.geo_region else False, 'savecity': True if profile.geo_city else False, 'lat': profile.lat, 'lng': profile.lng }) all_forms = [user_form, profile_form, accounts_formset, 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() 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: # Notify the user that their old profile URL won't work. messages.info( request, _(u'You changed your username; please note your ' u'profile URL has also changed.')) return redirect('phonebook:profile_view', user.username) data = dict(profile_form=profile_form, user_form=user_form, accounts_formset=accounts_formset, user_groups=user_groups, profile=request.user.userprofile, apps=user.apiapp_set.filter(is_active=True), language_formset=language_formset, vouch_threshold=settings.CAN_VOUCH_THRESHOLD, mapbox_id=settings.MAPBOX_PROFILE_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)
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, locale=request.locale, initial=dict(skills=user_skills)) 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() and language_formset.is_valid()): 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) # 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)