def update_profile(): if 'avatar' not in request.files: return jsonify({"msg": "Avatar is required"}), 400 file = request.files['avatar'] # name = request.form.get("name") # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return jsonify({"msg": "Not Selected File"}), 400 if file and allowed_file(file.filename, ALLOWED_EXTENSIONS_IMGS): email = get_jwt_identity() user = User.query.filter_by(email=email).first() filename = secure_filename(file.filename) filename = "user_" + str(user.id) + "_" + filename file.save( os.path.join(app.config['UPLOAD_FOLDER'] + "/images", filename)) user.avatar = filename user.update() return jsonify({ "success": "Profile updated successfully!", "user": user.serialize() }), 200 #return redirect(url_for('uploaded_file', filename=filename)) return jsonify({"msg": "Image not allowed!"}), 400
def avatar_modify(): if request.method == 'POST': userid = session['userid'] try: file = request.files['file'] except Exception, e: message = {'message': u'图片上传域file错误', 'code': 133} return json.dumps(message, sort_keys=True) if file and utils.allowed_file(file.filename, app.config['ALLOWED_EXTENSIONS']): filename = secure_filename(file.filename) directoryname = os.path.join(app.config['UPLOAD_AVATAR'], utils.getdirectoryname()) if not os.path.exists(directoryname): os.makedirs(directoryname) os.chmod(directoryname, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO) # mode:777 newfilename = getfilename(filename) try: file.save(os.path.join(directoryname, newfilename)) except Exception, e: message = {'message': u'图片上传异常', 'code': 133} return json.dumps(message, sort_keys=True) avatar = '%s/%s' % (os.path.join(app.config['UPLOAD_AVATAR_SQL'], utils.getdirectoryname()), newfilename) queryparams = {'userid': userid, 'avatar': avatar} result = handle_user_avatar_modify(queryparams) if result: message = {'message': u'修改头像成功', 'code': 131} else: message = {'message': u'修改头像失败', 'code': 132}
def picture_post(): if request.method == 'POST': # 获取上传的图集id 和图片说明 galleryid = int(request.form['galleryid']) content = request.form['content'] # 获取图片域 try: file = request.files['file'] except Exception, e: message = {'message': u'图片上传域file错误', 'code': 133} return json.dumps(message, sort_keys=True) # 验证图片扩展名 if file and utils.allowed_file(file.filename, app.config['ALLOWED_EXTENSIONS']): # 中文名字会报错 filename = secure_filename(file.filename) # 获取以上传日期为命名的目录名 directoryname = os.path.join(app.config['UPLOAD_GALLERY'], utils.getdirectoryname()) # 如果日期目录不存在则新建,并赋予权限 if not os.path.exists(directoryname): os.makedirs(directoryname) os.chmod(directoryname, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO) # mode:777 # 获取新的文件名 newfilename = getfilename(filename) # 获取文件数存储名 orgin_pic = os.path.join(directoryname, newfilename) # 保存图片到硬盘上 try: file.save(orgin_pic) except Exception, e: message = {'message': u'图片上传异常', 'code': 133} return json.dumps(message, sort_keys=True) # 获取图片在硬盘上的目录名 dirname = os.path.join(app.config['UPLOAD_GALLERY_SQL'], utils.getdirectoryname()) # 获取文件数据库存储名 orgin_url = os.path.join(dirname, newfilename) # 裁剪图片 thumb_reust = utils.thumb_pic(orgin_pic, 300) if thumb_reust: # 组合数据库存储名 thumb_url = os.path.join(dirname, thumb_reust) else: message = {'message': u'图片上传异常', 'code': 133} return json.dumps(message, sort_keys=True) queryparams = {'galleryid': galleryid, 'orgin_url': orgin_url, 'thumb_url': thumb_url, 'content': content} # 将上传的图片地址信息写入数据库 message = handle_gallery_picture_post(queryparams)
def upload(): if request.method == 'POST': if 'table' not in request.form and 'buffer' not in request.form: flash('POST sem nome e tamanho do buffer!') return redirect(request.url) folder_name_hash_id = str(getrandbits(32)) upload_folder, temp_folder, out_folder = create_dir_structure( app, folder_name_hash_id) table_name = request.form['table'] buffer = int(request.form['buffer']) aggregate = request.form.get('aggregate') if 'file' not in request.files: flash('Sem parte arquivo!') return redirect(request.url) file = request.files['file'] filename = secure_filename(file.filename) if filename == '': flash('Arquivo não escolhido!') return redirect(request.url) if allowed_file(filename): file.save(os.path.join(upload_folder, filename)) with zipfile.ZipFile(os.path.join(upload_folder, filename), 'r') as zip_ref: zip_ref.extractall(upload_folder) os.remove(os.path.join(upload_folder, filename)) extract_kmz(upload_folder, temp_folder) sql_inserts = create_inserts(temp_folder, table_name, buffer) remove_dir(upload_folder) remove_dir(temp_folder) with open(out_folder + '/' + 'insert.sql', 'w+') as f: f.write('BEGIN;\n') f.write(create_table_sql(table_name, True)) f.write(sql_inserts) f.write(create_table_sql(table_name, False)) f.write(final_insert_sql(table_name, aggregate)) f.write('COMMIT;\n') return redirect('/uploads/' + folder_name_hash_id + '/out/' + 'insert.sql') return upload_view()
def teoria_imagenes(id): if request.method == "POST": if "multimedia" not in request.files: return jsonify({"msg":"Multimedia es requerido"}) teoria = Teoria.query.get(id) file = request.files['multimedia'] if file.filename == '': return jsonify({"msg": "No has seleccionado el archivo"}), 400 if file and allowed_file(file.filename, ALLOWED_EXTENSIONS_IMGS): filename = secure_filename(file.filename) filename = "teoria_" + str(teoria.id) + "_" + filename file.save(os.path.join(app.config['UPLOAD_FOLDER']+"/imagenes", filename)) teoria.multimedia = filename teoria.actualizar() return jsonify('Actualizado correctamente'), 200 return jsonify({"msg": "imagen de perfil no pudo guardarse"}), 400
def fotoperfil(): file = request.files['avatar'] if 'avatar' not in request.files: return jsonify({"msg": "Avatar es requerido"}), 401 if file.filename == '': return jsonify({"msg": "No seleccionaste el archivo"}), 401 if file and allowed_file(file.filename, ALLOWED_EXTENSIONS_IMGS): correo = get_jwt_identity() usuario = Usuario.query.filter_by( correo=correo ).first() filename = secure_filename(file.filename) filename = "usuario_" + str(usuario.id) + "_" + filename file.save(os.path.join(app.config['UPLOAD_FOLDER']+"/imagenes", filename)) usuario.avatar = filename usuario.actualizar() return jsonify({"msg": "imagen de perfil guardada satisfactoriamente", "usuario": usuario.serialize_con_rol()}), 200 return jsonify({"msg": "imagen de perfil no pudo guardarse"}), 400
def preguntas_imagenes(id=None): #------------------------------ METODO POST ------------------------------------------------ if request.method == "POST": if "imagen" not in request.files: return jsonify({"msg":"Imagen es requerido"}) imagen_pregunta = Imagen_pregunta() file = request.files['imagen'] if file.filename == '': return jsonify({"msg": "No has seleccionado el archivo"}), 400 encontrar_imagen = Imagen_pregunta.query.filter_by(id=id).first() if encontrar_imagen is not None: return jsonify({"msg": "Ese Id ya existe, por favor ingre en la url el id nuevo"}), 400 if file and allowed_file(file.filename, ALLOWED_EXTENSIONS_IMGS): filename = secure_filename(file.filename) filename = "pregunta_" + str(id) + "_" + filename file.save(os.path.join(app.config['UPLOAD_FOLDER']+"/imagenes", filename)) imagen_pregunta.imagen = filename imagen_pregunta.pregunta_id = request.form.get("pregunta_id", "") imagen_pregunta.guardar() return jsonify('Guardada correctamente'), 200 #------------------------------ METODO PUT ------------------------------------------------ elif request.method == "PUT": if "imagen" not in request.files: return jsonify({"msg":"Imagen es requerido"}) imagen_pregunta = Imagen_pregunta.query.get(id) file = request.files['imagen'] if file.filename == '': return jsonify({"msg": "No has seleccionado el arcihvo"}), 400 if file and allowed_file(file.filename, ALLOWED_EXTENSIONS_IMGS): filename = secure_filename(file.filename) filename = "pregunta_" + str(imagen_pregunta.id) + "_" + filename file.save(os.path.join(app.config['UPLOAD_FOLDER']+"/imagenes", filename)) imagen_pregunta.imagen = filename imagen_pregunta.pregunta_id = request.form.get("pregunta_id", "") imagen_pregunta.actualizar() return jsonify('Actualizado correctamente'), 200 #------------------------------ METODO DELETE ------------------------------------------------ elif request.method == "DELETE": imagen_pregunta = Imagen_pregunta.query.get(id) imagen_pregunta.borrar() return jsonify({"msg":"Borrado Satisfactoriamente"}), 201 #------------------------------ METODO GET ------------------------------------------------ elif request.method == "GET": if id is not None: imagen = Imagen_pregunta.query.get(id) if imagen: return jsonify(imagen.serialize()), 205 return jsonify({"msg": "Esta imagen No existe"}), 405 else: imagen = Imagen_pregunta.query.all() imagen = list(map(lambda imagenes: imagenes.serialize(), imagen)) return jsonify(imagen), 205