def edit(request, display_name): """Edit user profile. Permission to edit user profile is granted to the user who owns the profile and all the users with permissions to edit profiles. Argument display_name should be lowered before queries because we allow case-insensitive profile urls. E.g. both /u/Giorgos and /u/giorgos are the same person. """ def date_joined_form_validation(form): """Convenience function to only validate datejoinedform when user has permissions. """ if request.user.has_perm('profiles.can_edit_profiles'): if form.is_valid(): return True return False return True user = get_object_or_404(User, userprofile__display_name__iexact=display_name) userform = forms.ChangeUserForm(request.POST or None, instance=user) profileform = forms.ChangeProfileForm(request.POST or None, instance=user.userprofile) datejoinedform = forms.ChangeDateJoinedForm(request.POST or None, instance=user.userprofile) if (userform.is_valid() and profileform.is_valid() and date_joined_form_validation(datejoinedform)): userform.save() profileform.save() if request.user.has_perm('profiles.can_edit_profiles'): # Update date joined datejoinedform.save() # Update groups. groups = {'Mentor': 'mentor_group', 'Admin': 'admin_group', 'Council': 'council_group', 'Rep': 'rep_group'} for group_db, group_html in groups.items(): if request.POST.get(group_html, None): user.groups.add(Group.objects.get(name=group_db)) else: user.groups.remove(Group.objects.get(name=group_db)) messages.success(request, 'Profile successfully edited.') if request.user == user: return redirect('profiles_view_my_profile') else: redirect_url = reverse('profiles_view_profile', kwargs={'display_name': user.userprofile.display_name}) return redirect(redirect_url) group_bits = map(lambda x: user.groups.filter(name=x).exists(), ['Admin', 'Council', 'Mentor', 'Rep']) pageuser = get_object_or_404(User, userprofile__display_name=display_name) functional_areas = map(int, profileform['functional_areas'].value()) return render(request, 'profiles_edit.html', {'userform': userform, 'profileform': profileform, 'datejoinedform': datejoinedform, 'pageuser': pageuser, 'group_bits': group_bits, 'range_years': range(1950, datetime.today().year - 11), 'functional_areas': functional_areas})
def edit(request, display_name): """Edit user profile. Permission to edit user profile is granted to the user who owns the profile and all the users with permissions to edit profiles. Argument display_name should be lowered before queries because we allow case-insensitive profile urls. E.g. both /u/Giorgos and /u/giorgos are the same person. """ def profile_date_form_validation(form): """Convenience function to only validate datejoinedform when user has permissions. """ if request.user.has_perm('profiles.can_edit_profiles'): if form.is_valid(): return True return False return True user = get_object_or_404(User, userprofile__display_name__iexact=display_name) userform = forms.ChangeUserForm(request.POST or None, instance=user) profileform = forms.ChangeProfileForm(request.POST or None, instance=user.userprofile, request=request) profile_date_form = forms.ChangeDatesForm(request.POST or None, instance=user.userprofile) if (userform.is_valid() and profileform.is_valid() and profile_date_form_validation(profile_date_form)): userform.save() profileform.save() if request.user.has_perm('profiles.can_edit_profiles'): # Update groups. groups = { 'Mentor': 'mentor_group', 'Admin': 'admin_group', 'Council': 'council_group', 'Rep': 'rep_group', 'Alumni': 'alumni_group', 'Review': 'review_group', 'Peers': 'peers_group', 'Resources': 'resources_group', 'Onboarding': 'onboarding_group' } for group_db, group_html in groups.items(): if Group.objects.filter(name=group_db).exists(): if request.POST.get(group_html, None): user.groups.add(Group.objects.get(name=group_db)) else: user.groups.remove(Group.objects.get(name=group_db)) # Update date fields profile_date_form.save() messages.success(request, 'Profile successfully edited.') statsd.incr('profiles.edit_profile') if request.user == user: return redirect('profiles_view_my_profile') else: redirect_url = reverse( 'profiles_view_profile', kwargs={'display_name': user.userprofile.display_name}) return redirect(redirect_url) else: # If forms are not valid and the fields are dirty, get a fresh copy # of the object. # This is needed when an invalid display_name is used. # Django tries to resolve the url based on this display_name, which # results in a NoReverseMatch error. See also bug: # https://bugzilla.mozilla.org/show_bug.cgi?id=1147541 user = User.objects.get(pk=user.id) group_bits = map(lambda x: user.groups.filter(name=x).exists(), [ 'Admin', 'Council', 'Mentor', 'Rep', 'Alumni', 'Review', 'Peers', 'Resources', 'Onboarding' ]) functional_areas = map(int, profileform['functional_areas'].value()) mobilising_skills = map(int, profileform['mobilising_skills'].value()) mobilising_interests = map(int, profileform['mobilising_interests'].value()) user_is_alumni = user.groups.filter(name='Alumni').exists() return render( request, 'profiles_edit.jinja', { 'userform': userform, 'profileform': profileform, 'profile_date_form': profile_date_form, 'pageuser': user, 'group_bits': group_bits, 'range_years': range(1950, now().date().year - 11), 'functional_areas': functional_areas, 'mobilising_skills': mobilising_skills, 'mobilising_interests': mobilising_interests, 'user_is_alumni': user_is_alumni })