def profile_edit(request): """ GET: show profile edit form with current data POST: validate and store new data """ from profiles.forms import ProfileEditForm profile = get404(UserProfile, user=request.user) if request.method == 'POST': form = ProfileEditForm(request.POST) if form.is_valid(): user = request.user user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] up = UserProfile.objects.get(user=user) up.country = form.cleaned_data['country'] up.state = form.cleaned_data['state'] up.about = form.cleaned_data['about'] user.save() up.save() messages.success(request, 'Perfil actualizado satisfactoriamente') return HttpResponseRedirect(reverse('my-profile')) else: context = {'status_message': form.errors} return HttpResponse(status=400, content=simplejson.dumps(context)) data = { 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email, 'country': profile.country, 'about': profile.about, 'state': profile.state, } context = {'form': ProfileEditForm(data)} return Render('profile_edit.html', RequestContext(request, context))
def edit (request): try: profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: # profile creation failed profile = UserProfile.objects.create(user=request.user) base_form = ProfileEditForm(instance=profile) avatar_form = ProfileAvatarForm(instance=profile) if request.method == "POST": if 'base_form' in request.POST: base_form = ProfileEditForm(request.POST, instance=profile) if base_form.is_valid(): profile = base_form.save(commit=False) profile.save() action.send(request.user, verb=_(u"a modifié sa description")) elif 'avatar_form' in request.POST: avatar_form = ProfileAvatarForm(request.POST, request.FILES, instance=profile) if avatar_form.is_valid(): profile = avatar_form.save(commit=False) profile.save() action.send(request.user, verb=_(u"a modifié sa photo de profil")) return render(request, 'profiles/edit.html', {'base_form': base_form, 'avatar_form': avatar_form})
def test_profile_edit_form(self): """ Test profile edit form """ invalid_data_dicts = [ # Non-alphanumeric first_name. { 'data': {'first_name': 'kolia#', 'last_name': 'melnik', 'email': '*****@*****.**', 'phone': '380636787', 'birthday' '2009-01-01' 'description': 'description'}, 'error': ('first_name', [u"Enter a valid value."])}, # Non-alphanumeric last_name. { 'data': {'first_name': 'kolia', 'last_name': 'mel#nik', 'email': '*****@*****.**', 'phone': '380636787', 'birthday' '2009-01-01' 'description': 'description'}, 'error': ('last_name', [u"Enter a valid value."])}, # invalid email { 'data': {'first_name': 'kolia', 'last_name': 'melnik', 'email': 'fooexample.com', 'phone': '380636787', 'birthday' '2009-01-01' 'description': 'description'}, 'error': ('email', [u"Enter a valid e-mail address."])}, # invalid phone { 'data': {'first_name': 'kolia', 'last_name': 'melnik', 'email': '*****@*****.**', 'phone': '38063#6787', 'birthday' '2009-01-01' 'description': 'description'}, 'error': ('phone', [u"Enter a valid value."])}, ] for invalid_dict in invalid_data_dicts: form = ProfileEditForm(data=invalid_dict['data']) self.assert_equals(form.errors[invalid_dict['error'][0]], invalid_dict['error'][1]) form = ProfileEditForm(data={'first_name': 'kolia', 'last_name': 'melnik', 'email': '*****@*****.**', 'phone': '380636787', 'birthday': '2009-01-01', 'description': 'description'}) self.assert_true(form.is_valid())
def edit_profile(id=None): user = User.objects.get(id=id) profile = Profile.objects.get(id=user.profile.id) form = ProfileEditForm(instance=profile) return {'form': form}