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)
Exemple #3
0
def create_picture():
    picture = Picture()
    form = PictureForm(request.form, picture)
    if form.validate() and request.files['file']:            
        form.populate_obj(picture)            
        picture.data = request.files['file'].read()
        picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
        picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'Рисунок успешно добавлен', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/new.html', title=u'Добавить рисунок', form=form)
Exemple #4
0
def update_picture(id):
    key = db.Key(id)
    picture = Picture.get(key)
    form  = PictureForm(request.form, picture)
    if form.validate():
        form.populate_obj(picture)
        if request.files['file']:
            picture.data = request.files['file'].read()
            picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
            picture.content_type = request.files['file'].content_type                
        picture.put()
        flash(u'Рисунок успешно сохранен', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/edit.html', title=u'Редактировать рисунок', form=form, picture=picture)
Exemple #5
0
def create_picture():
    picture = Picture()
    form = PictureForm(request.form, picture)
    if form.validate() and request.files['file']:
        form.populate_obj(picture)
        picture.data = request.files['file'].read()
        picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
        picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'Рисунок успешно добавлен', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/new.html',
                               title=u'Добавить рисунок',
                               form=form)
Exemple #6
0
def edit_picture(id):
    key = db.Key(id)
    picture = Picture.get(key)
    form = PictureForm(request.form, picture)
    return render_template('admin/pictures/edit.html',
                           title=u'Редактировать рисунок',
                           form=form,
                           picture=picture)
Exemple #7
0
    def test_picture_form_with_mandatory(self):
        """ test all mandatory's """
        form = PictureForm(data={
            "title": 'test',
            "file_name": 'test',
            "description": 'test',
        })

        self.assertTrue(form.is_valid)
Exemple #8
0
    def test_picture_form_without_description(self):
        """ test missing description """
        form = PictureForm(data={
            "title": 'test',
            "file_name": 'test',
        })

        self.assertEqual(form.errors['description'][0],
                         "Ce champ est obligatoire.")
Exemple #9
0
def update_picture(id):
    key = db.Key(id)
    picture = Picture.get(key)
    form = PictureForm(request.form, picture)
    if form.validate():
        form.populate_obj(picture)
        if request.files['file']:
            picture.data = request.files['file'].read()
            picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
            picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'Рисунок успешно сохранен', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/edit.html',
                               title=u'Редактировать рисунок',
                               form=form,
                               picture=picture)
Exemple #10
0
    def test_picture_form_without_mandatory(self):
        """ test missing mandatory fields """
        form = PictureForm(
            data={
                "title": '',
                "file_name": '',
                "description": '',
                "camera": '',
                "lens": '',
                "place": '',
                "taken_date": '',
                "global_score": '',
                "upload_date": '',
                "categories": '',
                "user": ''
            })

        self.assertEqual(form.errors['title'][0], "Ce champ est obligatoire.")
Exemple #11
0
def new_picture():
    form = PictureForm()
    return render_template('admin/pictures/new.html',
                           title=u'Добавить рисунок',
                           form=form)