def edit_category(): if not request.form['id'] == request.form[ 'name'] and Category.query.filter_by( name=request.form['name']).count(): flash('Category already exists') return redirect(request.form['currentURL']) c = Category.query.filter_by(name=request.form['id']).first() c_p = Painting.query.filter_by(category_name=c.name) c.name = request.form['name'] c.header = request.form['header'] c.description = request.form['description'] c.slug = slugify(request.form['name']) # Update foreign key relation for p in c_p: p.category_name = c.name # Check if thumbsize changed if c.thumbsize_large != (True if request.form.get('thumbsize') else False): c.thumbsize_large = not c.thumbsize_large # Update thumbnails c_p = Painting.query.filter_by(category_name=c.name) for p in c_p: Painting.makeThumbnail( os.path.join(app.config['MEDIA_ROOT'], 'image', p.filename), os.path.join(app.config['MEDIA_ROOT'], 'thumbnail', p.thumbname), c.thumbsize_large) db.session.commit() flash('success') return redirect(url_for('paintings', category_slug=c.slug))
def upload_painting(): # Collect info and initalize Painting image = request.files['file'] if not allowed_file(image.filename): flash(".jpg files only") return redirect(request.form['currentURL']) category = Category.query.filter_by(name=request.form['category']).first() new_painting = Painting( name=request.form['name'], category=category, medium=request.form['medium'], dimensions=request.form['dimensions'], year=request.form['year'], filename=secure_filename(image.filename), for_sale=True if request.form.get('for_sale') else False) # Upload File (Use new_painting filename since it would've updated for uniquness) image.save( os.path.join(app.config['MEDIA_ROOT'], 'image', new_painting.filename)) # Make thumbnail Painting.makeThumbnail( os.path.join(app.config['MEDIA_ROOT'], 'image', new_painting.filename), os.path.join(app.config['MEDIA_ROOT'], 'thumbnail', new_painting.thumbname), category.thumbsize_large if category else False, ) # Update database db.session.add(new_painting) db.session.commit() flash("success") return redirect(request.form['currentURL'])
def test_thumbnails_are_made_properly_for_tall_images(self): Painting.makeThumbnail( os.path.join(app.config['MEDIA_ROOT'], 'test_portrait_image.jpg'), os.path.join(app.config['MEDIA_ROOT'], 'test_portrait_thumb.jpg')) with open( os.path.join(app.config['MEDIA_ROOT'], 'test_portrait_thumb.jpg'), 'rb') as img_file: with Image.open(img_file) as portrait_thumb: _, _, width, height = portrait_thumb.getbbox() portrait_thumb.close() self.assertAlmostEqual(width, THUMBNAIL_SIZE, delta=1) self.assertAlmostEqual(height, THUMBNAIL_SIZE, delta=1)