def icon(): form = IconForm() if form.validate_on_submit(): #生成随机的文件名 suffix = os.path.splitext(form.icon.data.filename)[1] filename = random_string() + suffix #保存文件 photos.save(form.icon.data, name=filename) #拼接完整的路径名 pathname = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], filename) #生成缩略图 img = Image.open(pathname) #设置图片大小 img.thumbnail((128, 128)) #重新保存 img.save(pathname) #删除原来的头像,默认的除外 if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) #保存到数据库 current_user.icon = filename db.session.add(current_user) flash('头像已修改') #获取头像的url img_url = photos.url(current_user.icon) return render_template('user/icon.html', form=form, img_url=img_url)
def change_icon(): form = UploadForm() if form.validate_on_submit(): # 获取后缀 suffix = os.path.splitext(form.icon.data.filename)[1] # 随机文件名 filename = random_string() + suffix photos.save(form.icon.data, name=filename) # 生成缩略图 pathname = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], filename) img = Image.open(pathname) img.thumbnail((128, 128)) img.save(pathname) # 删除原来的头像(不是默认头像时才需要删除) if current_user.icon != 'default.jpeg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) # 保存到数据库 current_user.icon = filename db.session.add(current_user) return redirect(url_for('user.change_icon')) img_url = photos.url(current_user.icon) return render_template('user/change_icon.html', form=form, img_url=img_url)
def change_icon(): form = Change_iconForm() if form.validate_on_submit(): # 获取文件后缀 suffix = os.path.splitext(form.icon.data.filename)[1] # 生成文件名 filename = random_string() + suffix # 保存图片 photos.save(form.icon.data, name=filename) # 文件保存路径 filepath = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], filename) # 生成缩略图 img = Image.open(filepath) img.thumbnail((200, 200)) img.save(filepath) # 删除原来的图片 if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) # 保存修改至数据库 current_user.icon = filename db.session.add(current_user) flash('头像已保存') return redirect(url_for('user.change_icon')) img_url = photos.url(current_user.icon) return render_template('user/change_icon.html', form=form, img_url=img_url)
def upload(): try: photo = request.files.get('photo') user_id = request.form.get('user_id') icon_type = int(request.form.get('icon_type')) token = request.form.get('token') # 提取文件后缀 suffix = os.path.splitext(photo.filename)[1] # 生成随机文件名 filename = random_string() + suffix photos.save(photo, name=filename) img_url = photos.url(filename) img_url = img_url.replace('_uploads', 'static') img_url = img_url.replace('photos', 'upload') # 判断数据库是否有该用户 user = Icon.query.filter_by(user_id=user_id).first() if user: # 删除原来的头像 os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], user.icon)) user.icon = filename user.confirmed = 1 else: u = Icon(user_id=user_id, icon_type=icon_type, icon=filename, token=token) db.session.add(u) data = {"url": img_url} data = json.dumps(data) return data except Exception as e: print(e) return ''
def icon(): form = IconForm() # 取出对象 user = current_user._get_current_object() # 判断数据是否合法 if form.validate_on_submit(): # 生成随机文件名 suffix = os.path.splitext(form.icon.data.filename)[1] filename = random_str() + suffix # 保存文件 photos.save(form.icon.data, name=filename) # 生成文件名的绝对路径 pathname = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], filename) # 生成缩略图 image = Image.open(pathname) # 修改大小 image.thumbnail((128, 128)) image.save(pathname) # 删除原头像 if user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], user.icon)) # 赋值给icon 属性 user.icon = filename db.session.add(user) # 提示用户修改成功 flash('头像修改成功') # 返回URL 路径 img_url = photos.url(user.icon) return render_template('users/icon.html', form=form, img_url=img_url)
def icon(): form = IconForm() if form.validate_on_submit(): # 获取上传的图片 suffix = os.path.splitext(form.icon.data.filename)[1] # 生成随机文件名 filename = random_string() + suffix # 保存文件 photos.save(form.icon.data, name=filename) # 生成缩略图 pathname = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], filename) img = Image.open(pathname) # 处理 img.thumbnail((128, 128)) # 再保存 img.save(pathname) # 之前老的图片要删除,除了默认图片 if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) # 把文件名存到数据库 current_user.icon = filename db.session.add(current_user) flash('头像上传成功') img_url = photos.url(current_user.icon) return render_template('user/icon.html', form=form, img_url=img_url)
def profile(): name = User.query.filter(User.username == session.get('username')).first() if name: img_url = photos.url(current_user.icon) users = User.query.all() return render_template('user/profile.html', img_url=img_url) else: flash('请到对应页面登录') session.pop('username') return redirect(url_for('main.index'))
def product_upload(): # print current_user.is_authenticated() # if not current_user.is_authenticated(): # return redirect(url_for('user.signin')) product_code = request.args.get('domain_code') product = ProcutInfo.query.filter(ProcutInfo.product_code == product_code).first() if product: product_form = ProductForm(product_code=product.product_code, product_name=product.product_name, detail_info=product.detail_info, guide_price=product.guide_price, introduce=product.introduce, pack_list=product.pack_list, phone_num=product.phone_num, csrf_enabled=False) form = product_form images= product.images else: form = ProductForm(request.values, csrf_enabled=False) images =None if form.validate_on_submit(): product = ProcutInfo() form.populate_obj(product) file = request.files['product_image'] filename = str(uuid.uuid4()) + '.jpg' photos.save(file, name=filename) url = photos.url(filename) image = ImageInfo() image.name = filename image.url = url product.images = [image] p = ProcutInfo.query.filter(ProcutInfo.product_code == product.product_code).first() if p is None: db.session.add(product) else: db.session.merge(product) fpath = photos.path(filename) f = open(fpath, 'rb') fdata = base64.b64encode(f.read()) rsp = upload_product_info(product, filename, fdata) print rsp if rsp[0] == 1: flash(u'资料上传成功', 'success') return redirect(url_for('site.index')) else: pass else: return render_template('/product/info_upload.html', form=form,images=images)
def change_icon(): form = UploadForm() if form.validate(): suffix = os.path.splitext(form.icon.data.filename)[1] filename = random_string()+suffix photos.save(form.icon.data,name=filename) pathname = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],filename) img = Image.open(pathname) img.thumbnail((128,128)) img.save(pathname) if current_user.icon != 'default.jpg': os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],current_user.icon)) current_user.icon = filename db.session.add(current_user) db.session.commit() flash('图片已经保存') img_url = photos.url(current_user.icon) return render_template('users/change_icon.html',form=form,img_url=img_url)
def updata_photo(): formData = request.form formData = str(formData) formData = reques_data(formData) icons = Icon.query.filter( Icon.user_id.in_(tuple([x['user'] for x in formData]))).all() for x in range(len(icons)): icons[x].confirmed = formData[x]['test'] db.session.add_all(icons) db.session.commit() users = Icon.query.filter_by(confirmed=2).all() # 将审核头像上传至网易云 def wyy_updata_user(accid, icon, token): for user in users: img_url = photos.url(user.icon) img_url = img_url.replace('_uploads', 'static') img_url = img_url.replace('photos', 'upload') print(img_url) print(wyy_updata_user(user.user_id, img_url)) return 'ok'
def profile(): s = current_user.username u = User.query.filter(User.username == s).first() try: idcard = u.idCard phonenumber = u.phonenumber sex = u.sex age = u.age except: idcard = None phonenumber = None sex = None age = None img_url = photos.url(current_user.icon) return render_template('user/profile.html', img_url=img_url, name=s, sex=sex, age=age, idcard=idcard, phonenumber=phonenumber)
def profile(): img_url = photos.url(current_user.icon) return render_template('user/profile.html', img_url=img_url)
def profile(): # 取出对象 user = current_user._get_current_object() img_url = photos.url(user.icon) return render_template('users/profile.html', img_url=img_url)
def upload_image(): if request.files and 'image' in request.files: filename = photos.save(request.files['image']) return success_response({'filename': filename, 'image_url': photos.url(filename)}) else: raise ValidationError('Invalid parameter')