def edit_profile(request, template): """Edit user profile.""" try: user_profile = request.user.get_profile() except Profile.DoesNotExist: # TODO: Once we do user profile migrations, all users should have a # a profile. We can remove this fallback. user_profile = Profile.objects.create(user=request.user) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=user_profile) if form.is_valid(): user_profile = form.save() new_timezone = user_profile.timezone if request.session.get('timezone', None) != new_timezone: request.session['timezone'] = new_timezone return HttpResponseRedirect(reverse('users.profile', args=[request.user.id])) else: # request.method == 'GET' form = ProfileForm(instance=user_profile) # TODO: detect timezone automatically from client side, see # http://rocketscience.itteco.org/2010/03/13/automatic-users-timezone-determination-with-javascript-and-django-timezones/ return render(request, template, { 'form': form, 'profile': user_profile})
def edit_profile(request, username=None): """Edit user profile.""" # If a username is specified, we are editing somebody else's profile. user = None if username: username = username.replace(" ", "+") if username != request.user.username: try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404 # Make sure the auth'd user has permission: if not request.user.has_perm("users.change_profile"): return HttpResponseForbidden() if not user: user = request.user try: user_profile = Profile.objects.get(user=user) except Profile.DoesNotExist: # TODO: Once we do user profile migrations, all users should have a # a profile. We can remove this fallback. user_profile = Profile.objects.create(user=user) profile_form = ProfileForm(request.POST or None, request.FILES or None, instance=user_profile) user_form = UserForm(request.POST or None, instance=user_profile.user) if profile_form.is_valid() and user_form.is_valid(): user_profile = profile_form.save() user = user_form.save() new_timezone = user_profile.timezone if new_timezone: tz_changed = request.session.get("timezone", None) != new_timezone if tz_changed and user == request.user: request.session["timezone"] = new_timezone return HttpResponseRedirect( reverse("users.profile", args=[user.username])) # TODO: detect timezone automatically from client side, see msgs = messages.get_messages(request) fxa_messages = [ m.message for m in msgs if m.message.startswith("fxa_notification") ] return render( request, "users/edit_profile.html", { "profile_form": profile_form, "user_form": user_form, "profile": user_profile, "fxa_messages": fxa_messages, }, )
def edit_profile(request, username=None, template=None): """Edit user profile.""" # If a username is specified, we are editing somebody else's profile. if username is not None and username != request.user.username: try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404 # Make sure the auth'd user has permission: if not request.user.has_perm('users.change_profile'): return HttpResponseForbidden() else: user = request.user try: user_profile = Profile.objects.get(user=user) except Profile.DoesNotExist: # TODO: Once we do user profile migrations, all users should have a # a profile. We can remove this fallback. user_profile = Profile.objects.create(user=user) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=user_profile) if form.is_valid(): user_profile = form.save() new_timezone = user_profile.timezone tz_changed = request.session.get('timezone', None) != new_timezone if tz_changed and user == request.user: request.session['timezone'] = new_timezone return HttpResponseRedirect(reverse('users.profile', args=[user.username])) else: # request.method == 'GET' form = ProfileForm(instance=user_profile) # TODO: detect timezone automatically from client side, see # http://rocketscience.itteco.org/2010/03/13/automatic-users-timezone-determination-with-javascript-and-django-timezones/ # noqa return render(request, template, { 'form': form, 'profile': user_profile})
def edit_profile(request, template): """Edit user profile.""" try: user_profile = request.user.get_profile() except Profile.DoesNotExist: # TODO: Once we do user profile migrations, all users should have a # a profile. We can remove this fallback. user_profile = Profile.objects.create(user=request.user) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=user_profile) if form.is_valid(): user_profile = form.save() new_timezone = user_profile.timezone if request.session.get('timezone', None) != new_timezone: request.session['timezone'] = new_timezone return HttpResponseRedirect( reverse('users.profile', args=[request.user.id])) else: # request.method == 'GET' form = ProfileForm(instance=user_profile) # TODO: detect timezone automatically from client side, see # http://rocketscience.itteco.org/2010/03/13/automatic-users-timezone-determination-with-javascript-and-django-timezones/ # noqa return render(request, template, {'form': form, 'profile': user_profile})
class ProfileFormTestCase(TestCaseBase): form = ProfileForm() def setUp(self): self.form.cleaned_data = {} def test_facebook_pattern_attr(self): """Facebook field has the correct pattern attribute.""" fragment = pq(self.form.as_ul()) facebook = fragment('#id_facebook')[0] assert 'pattern' in facebook.attrib pattern = re.compile(facebook.attrib['pattern']) for url, match in FACEBOOK_URLS: eq_(bool(pattern.match(url)), match) def test_twitter_pattern_attr(self): """Twitter field has the correct pattern attribute.""" fragment = pq(self.form.as_ul()) twitter = fragment('#id_twitter')[0] assert 'pattern' in twitter.attrib pattern = re.compile(twitter.attrib['pattern']) for url, match in TWITTER_URLS: eq_(bool(pattern.match(url)), match) def test_clean_facebook(self): clean = lambda: self.form.clean_facebook() for url, match in FACEBOOK_URLS: self.form.cleaned_data['facebook'] = url if match: clean() # Should not raise. else: self.assertRaises(ValidationError, clean) def test_clean_twitter(self): clean = lambda: self.form.clean_twitter() for url, match in TWITTER_URLS: self.form.cleaned_data['twitter'] = url if match: clean() # Should not raise. else: self.assertRaises(ValidationError, clean)
def edit_profile(request, username=None): """Edit user profile.""" # If a username is specified, we are editing somebody else's profile. if username is not None and username != request.user.username: try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404 # Make sure the auth'd user has permission: if not request.user.has_perm('users.change_profile'): return HttpResponseForbidden() else: user = request.user try: user_profile = Profile.objects.get(user=user) except Profile.DoesNotExist: # TODO: Once we do user profile migrations, all users should have a # a profile. We can remove this fallback. user_profile = Profile.objects.create(user=user) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=user_profile) if form.is_valid(): user_profile = form.save() new_timezone = user_profile.timezone tz_changed = request.session.get('timezone', None) != new_timezone if tz_changed and user == request.user: request.session['timezone'] = new_timezone return HttpResponseRedirect( reverse('users.profile', args=[user.username])) else: # request.method == 'GET' form = ProfileForm(instance=user_profile) # TODO: detect timezone automatically from client side, see # http://rocketscience.itteco.org/2010/03/13/automatic-users-timezone-determination-with-javascript-and-django-timezones/ # noqa msgs = messages.get_messages(request) fxa_messages = [ m.message for m in msgs if m.message.startswith('fxa_notification') ] if not fxa_messages and not user_profile.is_fxa_migrated: fxa_messages.append('fxa_notification_deprecation_warning') return render(request, 'users/edit_profile.html', { 'form': form, 'profile': user_profile, 'fxa_messages': fxa_messages })
class ProfileFormTestCase(TestCaseBase): form = ProfileForm() def setUp(self): self.form.cleaned_data = {} def test_facebook_pattern_attr(self): """Facebook field has the correct pattern attribute.""" fragment = pq(self.form.as_ul()) facebook = fragment("#id_facebook")[0] assert "pattern" in facebook.attrib pattern = re.compile(facebook.attrib["pattern"]) for url, match in FACEBOOK_URLS: eq_(bool(pattern.match(url)), match) def test_clean_facebook(self): clean = self.form.clean_facebook for url, match in FACEBOOK_URLS: self.form.cleaned_data["facebook"] = url if match: clean() # Should not raise. else: self.assertRaises(ValidationError, clean)