def add_category(): """ add a category to the database """ check_admin() add_category = True form = CategoriesForm() if form.validate_on_submit(): filename = request.files['image'] _, f_ext = os.path.splitext(filename.filename) name = form.name.data picture_fn = name + f_ext photos.save(filename, name=picture_fn) url = photos.url(picture_fn) category = Categories(category_name=form.name.data, category_image=url) try: db.session.add(category) db.session.commit() flash("You have successfully added a new category") gc.collect() except Exception as e: flash('Error: category name already exits. ') return redirect(url_for('admin.list_categories')) return render_template('admin/categories/category.html', action="Add", form=form, add_category=add_category, title="Add Categories")
def upload_photos(post_id): post = Post.query.filter_by(user_id=current_user.id, id=post_id, is_draft=True).first() # TODO: add raise here if post is None: return redirect(url_for("posts")) image_ids = [] if request.method == "POST": files = request.files.getlist("photos") if len(files) and files[0].filename: for image in request.files.getlist("photos"): image_id = str(uuid.uuid4()) image_id = ".".join( [image_id, image.filename.rsplit(".", 1)[1]]) image.filename = image_id photos.save(image) image_id = "/".join(["img", image_id]) image_ids.append(image_id) img_index_driver.set_image_ids(image_ids, post.id) post.number_of_photos = len(image_ids) post.is_draft = False db.session.add(post) db.session.commit() return redirect(url_for("posts")) return render_template("uploadphotos.html", post_id=post_id)
def price_get(): form = UploadForm() if form.validate_on_submit(): for filename in request.files.getlist('photo'): name = "Photo" + secure_filename(filename) photos.save(filename, name=name + '.') success = True else: success = False return render_template('addcar.html', title="Add car", form=form, success=success, predict_func=predict)
def edit_product(id): ''' edit product ''' check_admin() add_product = False product = Products.query.get_or_404(id) form = ProductsForm(obj=product) if form.validate_on_submit(): filename = request.files['image'] _, f_ext = os.path.splitext(filename.filename) name = form.name.data picture_fn = name + f_ext #get the name of the previous image previous_img_name = picture_fn photos.save(filename, name=picture_fn) url = photos.url(picture_fn) product.product_name = form.name.data product.product_price = form.price.data product.product_image = url product.product_description = form.description.data product.product_stock = form.stock.data db.session.commit() gc.collect() #remove the changed picture from the folder img_dir = Config.UPLOADED_PHOTOS_DEST + '/' os.remove(img_dir + previous_img_name) flash('You have successfully edited a product') #redirect to the products page redirect(url_for('admin.list_products')) form.name.data = product.product_name form.price.data = product.product_price form.image.data = product.product_image form.description.data = product.product_description form.stock.data = product.product_stock return render_template('admin/products/product.html', add_product=add_product, form=form, title="Edit Product")
def edit_by_id(id): user = session['user'] logged_in = True the_user = models.User.query.filter_by(username=user).first() the_settings = models.Settings.query.filter_by(user=the_user.id).first() display_name = the_settings.displayName post = models.Content.query.filter_by(id=id).first() if request.method == 'POST' and 'photo' in request.files: form = request.form pic = request.files['photo'] filename = photos.save(pic) user = session['user'] url = photos.url(filename) rec = models.Photo(filename=filename, user=user, url=url) db.session.add(rec) db.session.commit() if form is not None: res = posts.update_post_in_db(id, form["content"], url) print res else: print "nothing to update in db" return redirect(url_for('read')) return render_template('editPost.html', post=post, user=user, logged_in=logged_in, displayName=display_name)
def Upload(file): # filename = hashlib.md5(current_user.username + str(time.time())).hexdigest()[:10] # filename.update(hash.encode("utf-8")) # photo = photos.save(file, name=filename + '.') photo = photos.save(file) if photo: url = photos.url(photo) return url
def upload(): if request.method == 'POST' and 'image' in request.files: image = request.files['image'] if image.filename == 'blob': image.filename = 'blob.png' # flask-upload 的新文件名称会以点的后缀判断是否需要扩展名 new_name = uuid.uuid4().hex + '.' filename = photos.save(image, name=new_name) photos.config.base_url = '/static/uploads/file/' return jsonify(filename=photos.url(filename)) else: return jsonify(msg='error upload'), 400
def upload(): """ Upload a post Use Flask-Uploads for photo uploads :return: a redirect to write """ form = request.form print request.files if request.method == 'POST' and 'photo' in request.files: pic = request.files['photo'] filename = photos.save(pic) user = session['user'] url = photos.url(filename) rec = models.Photo(filename=filename, user=user, url=url) db.session.add(rec) db.session.commit() if form is not None: res = add_post_to_db(form['content'], url) print res else: print "nothing to add to db" return redirect(url_for('write'))