Example #1
0
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)
Example #2
0
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_edit_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))
Example #4
0
def edit(request):
    """ My Profile Data Edit Page """

    my_data = Person.objects.get(pk=1)

    if request.user.is_authenticated():
        # for authenticated users
        if request.method == "POST":
            form = ProfileForm(request.POST, request.FILES, instance=my_data)
            resp = dict(ok=0)
            if form.is_valid():
                form.save()
                resp["ok"] = 1
                resp["image"] = my_data.get_img_url()
            else:
                errors = dict()
                for error in form.errors:
                    errors[error] = form.errors[error]
                resp["errors"] = errors
            return HttpResponse(json.dumps(resp))
        else:
            form = ProfileForm(instance=my_data)
            return render(request, "edit.html", dict(form=form))
    else:
        # for anonymous users
        return auth_views.login(request, template_name="edit.html", redirect_field_name="next")
 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))
Example #6
0
    def test_resize_image(self):
        """ Testing image save """
        person = Person.objects.get(pk=1)
        self.assertEqual(person.get_img_url(), '/static/img/no_image.png')
        photo = open('assets/img/no_image_test.png', 'rb')
        data = {
            'first_name': 'firstname',
            'last_name': 'lastname',
            'date_of_birth': '1991-01-01',
            'contacts': 'contacts',
            'bio': 'bio',
            'email': '*****@*****.**',
            'jabber': '*****@*****.**',
            'skype': 'skypeid'
        }
        photo = SimpleUploadedFile(photo.name,
                                   photo.read())
        form = ProfileForm(data, dict(photo=photo), instance=person)
        self.assertTrue(form.is_valid())
        form.save()
        person = Person.objects.get(pk=1)

        self.assertNotEqual(person.get_img_url(),
                            '/static/img/no_image.png')
        image_resized = Image.open(person.photo)
        self.assertLessEqual(image_resized.height, 200)
        self.assertLessEqual(image_resized.width, 200)
Example #7
0
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")
Example #8
0
 def test_blank_data(self):
     """
     testing form required fields
     """
     form = ProfileForm({})
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors,
                      {'name': ['This field is required.'],
                       'last_name': ['This field is required.'],
                       'date_of_birth': ['This field is required.'],
                       'email': ['This field is required.'],
                       'jabber': ['This field is required.'],
                       'skype': ['This field is required.'],
                       'other': ['This field is required.'],
                       'bio': ['This field is required.'],
                       })
Example #9
0
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")
Example #10
0
def edit_data(request):
    profile = Profile.objects.first()

    if request.method == "POST":
        form = ProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            profile = form.save(commit=False)
            profile.save()
            if request.is_ajax():
                return HttpResponse('OK')
            return redirect('apps.hello.views.profile_info')

        if request.is_ajax():
            errors_dict = {}
            if form.errors:
                for error in form.errors:
                    e = form.errors[error]
                    errors_dict[error] = e

            return HttpResponseBadRequest(json.dumps(errors_dict))

    else:
        form = ProfileForm(instance=profile)
    return render(request, 'hello/edit.html', {'form': form})
Example #11
0
    def test_form_save_and_resize_image(self):
        """
        testing form saving data and image resize
        """
        instance = Profile.objects.first()
        photo = open('assets/image/NoImage.png', 'rb')
        image = Image.open('assets/image/NoImage.png')
        self.assertGreater(image.height, 200)
        self.assertGreater(image.width, 200)

        form = ProfileForm({"id": 1,
                            "name": "Boris",
                            "last_name": "Marianenko",
                            "date_of_birth": date(1985, 1, 22),
                            "email": "*****@*****.**",
                            "jabber": "jabba",
                            "skype": "gts",
                            "other": "some other text",
                            "bio": "Nam dignissim at enim id ornare"},
                           {'photo': SimpleUploadedFile(photo.name,
                                                        photo.read())},
                           instance=instance)

        self.assertTrue(form.is_valid())
        form.save()
        profile = Profile.objects.first()
        # check database and form data equals
        for field in profile._meta.get_all_field_names():
            value = getattr(profile, field)
            if field != "photo":
                self.assertEqual(value, form.data[field])

        # check image resize
        image_resized = Image.open(profile.photo)
        self.assertLessEqual(image_resized.height, 200)
        self.assertLessEqual(image_resized.width, 200)
Example #12
0
    def test_send_valid_image_update_profile(self):
        """
        Testing valid image in profile form
        """

        profile = Profile.objects.get(id=1)

        # create test image
        test_image = Image.new('RGB', (1200, 1200))

        test_image_path = settings.BASE_DIR + \
            settings.MEDIA_URL + \
            'image_for_test.jpg'

        test_image.save(test_image_path)

        # get it
        valid_file = open(test_image_path)

        # test image width before put it in form
        image_width = Image.open(test_image_path).width
        self.assertEqual(image_width, 1200)

        form_data = {
            'id': 1,
            'name': 'ad2s',
            'last_name': 'admin',
            'date_of_birth': '1993-11-21',
            'email': '*****@*****.**',
            'jabber': '*****@*****.**',
            'skype': 'sgsfdf',
        }

        valid_file_name = valid_file.name

        # upload file
        photo_file = {
            'photo': SimpleUploadedFile(
                valid_file_name, valid_file.read())}

        valid_file.close()

        # put data in form
        form = ProfileForm(
            data=form_data,
            files=photo_file,
            instance=profile
        )
        form.save()

        # test if form is valid
        self.assertEqual(form.is_valid(), True)

        # saved image path
        new_image = settings.BASE_DIR + \
            settings.MEDIA_URL + 'images/' + \
            'image_for_test.jpg'

        # open saved image
        profile_image = Image.open(new_image)

        # test image width
        self.assertEqual(profile_image.width, 200)

        # test image height
        self.assertEqual(profile_image.height, 200)

        # delete image
        default_storage.delete(new_image)
        default_storage.delete(test_image_path)