def edit_profile(request): identify = request.POST.get('id', 1) try: profile = Profile.objects.get(id=identify) except ObjectDoesNotExist: profile = Profile.objects.first() form = ProfileForm(request.POST, request.FILES, instance=profile) logger.info(form) profile_to_json = { 'status': "error", 'image_src': profile.photo.url if profile and profile.photo else ' ' } if request.POST and form.is_valid(): try: form.save() profile_to_json['status'] = "success" except IntegrityError: profile_to_json['status'] = "error" return HttpResponse(json.dumps(profile_to_json), content_type="application/json") if request.POST and not form.is_valid(): return HttpResponse(json.dumps(profile_to_json), content_type="application/json") user_form = ProfileForm(instance=profile) context = dict(profile=profile, user_form=user_form) return render(request, 'hello/edit_profile.html', context)
def test_send_unvalid_post_data_update_profile(self): """ Testing not update profile unvalid data """ form_data = { 'id': 2, 'name': 'ad', 'bio': 'my', 'last_name': 'admin' * 21, 'date_of_birth': '1993-1121', 'email': '123', 'jabber': '123', 'skype': '12', } form = ProfileForm(data=form_data) # test if form is not valid self.assertFalse(form.is_valid()) self.assertIn(u'Ensure this value has at least 3 characters', str(form['name'].errors)) self.assertIn(u'Ensure this value has at most 100 characters', str(form['last_name'].errors)) self.assertIn(u'Enter a valid date', str(form['date_of_birth'].errors)) self.assertIn(u'Enter a valid email address', str(form['email'].errors)) self.assertIn(u'Enter a valid email address', str(form['jabber'].errors)) self.assertIn(u'Ensure this value has at least 3 characters', str(form['skype'].errors)) self.assertIn(u'Ensure this value has at least 3 characters', str(form['bio'].errors))
def test_send_no_post_data_update_profile(self): """ Testing not update profile unvalid data """ form = ProfileForm(data={}) self.assertIn(u'This field is required', str(form['id'].errors)) self.assertIn(u'This field is required', str(form['name'].errors)) self.assertIn(u'This field is required', str(form['last_name'].errors)) self.assertIn(u'This field is required', str(form['date_of_birth'].errors)) self.assertIn(u'This field is required', str(form['email'].errors)) self.assertIn(u'This field is required', str(form['jabber'].errors)) self.assertIn(u'This field is required', str(form['skype'].errors)) self.assertIn(u'This field is required', str(form['bio'].errors))
def update_profile(request): identify = request.POST.get('id') profile = Profile.objects.get(id=identify) form = ProfileForm(request.POST, request.FILES, instance=profile) logger.info(form) if form.is_valid(): form.save() profile = Profile.objects.get(id=int(identify)) profile_to_json = { 'status': "success", 'image_src': profile.photo.url if profile.photo else '' } else: profile_to_json = { 'status': "error", 'image_src': profile.photo.url if profile.photo else ' ' } return HttpResponse(json.dumps(profile_to_json), content_type="application/json")
def edit_profile(request): profile = Profile.objects.first() user_form = ProfileForm(instance=profile) context = dict(profile=profile, user_form=user_form) return render(request, 'hello/edit_profile.html', context)
def main(request): profile = Profile.objects.first() user_form = ProfileForm(instance=profile) context = {'profile': profile, 'user_form': user_form} return render(request, 'hello/index.html', context)