def add_picture(request):
    form = PictureForm(request.POST, request.FILES)
    print(form)
    if form.is_valid():
        print('VALID', request.POST)

        image = encode_picture(request.FILES['file'])
        miniature= resize_picture(image)
        size = picture_size(image)

        user = User.objects.get(pk=request.session.get('user'))
        pic = Picture(name_of_picture=form.cleaned_data['name_of_picture'],
                      pictures=image,
                      public=int(form.cleaned_data['check']),
                      picture_size=size
                      )
        pic.id_user = user
        pic.save()
        mini = Picture.objects.get(id=pic.id)
        mini_p = Picture(  # create a miniature picture
            id_pictures_id=mini.id,
            pictures=miniature,
            name_of_picture=mini.name_of_picture,
            id_user_id=mini.id_user_id,
            public=int(form.cleaned_data['check'])
        )
        mini_p.save()
    else:
        print("NOT VALID")
def add_picture(request):
    global user
    picture_form = PictureForm
    args = {}
    args.update(csrf(request))
    args['form'] = picture_form
    args['username'] = username(request.session.get('user'))

    if request.POST:
        file = request.FILES['file']
        form = PictureForm(request.POST, request.FILES)

        if form.is_valid():
            print('VALID')

            image, miniature = encode_picture(request.FILES['file'])
            user = User.objects.get(pk=request.session.get('user'))
            pic = Picture(name_of_picture=request.POST['name_of_picture'],
                          pictures=image)
            pic.id_user = user
            pic.save()
            mini = Picture.objects.get(id=pic.id)
            mini_p = Picture(  # create a miniature picture
                id_pictures_id=mini.id,
                pictures=miniature,
                name_of_picture=mini.name_of_picture,
                id_user_id=mini.id_user_id
            )
            mini_p.save()
        else:
            print("NOT VALID")
    else:
        return render_to_response('add_picture.html', args)
    return redirect('/profile/%s/' % user.id_users)