def upload(): form = Upload() if form.validate_on_submit(): icon = request.files.get('icon') # 获取上传对象 suffix = icon.filename.split('.')[-1] # 获取后缀 newName = random_filename(suffix) # 获取新的图片名称 # 保存图片 以新名称 file.save(icon, name=newName) delPath = current_app.config['UPLOADED_PHOTOS_DEST'] # 删除之前上传过的图片 if current_user.icon != 'default.png': # 如果不等于 证明之前有上传过头像 否则就是没有上传过新头像 os.remove(os.path.join(delPath, current_user.icon)) os.remove(os.path.join(delPath, 'b_' + current_user.icon)) os.remove(os.path.join(delPath, 'm_' + current_user.icon)) os.remove(os.path.join(delPath, 's_' + current_user.icon)) current_user.icon = newName # 更改当前对象的图片名称 db.session.add(current_user) # 更新到数据库中 db.session.commit() # 拼接图片路径 path = os.path.join(delPath, newName) # 进行头像的多次缩放 img_zoom(path) img_zoom(path, 'm_', 200, 200) img_zoom(path, 'b_', 300, 300) return render_template('owncenter/upload.html', form=form)
def change_password(): form = Icon() if form.validate_on_submit(): shuffix = os.path.splitext(form.file.data.filename)[-1] #生成随机的图片名 while True: newName = new_name(shuffix) if not os.path.exists( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newName)): break file.save(form.file.data, name=newName) #判断用户更改头像 原头像是否为默认 不是则将原图片删除 if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) #执行缩放 img = Image.open( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newName)) img.thumbnail((300, 300)) #保存新的图片名称为新的图片的s_newname img.save( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], 's_' + newName)) current_user.icon = newName db.session.add(current_user) flash('头像上传成功') img_url = file.url(current_user.icon) return render_template('user/change_icon.html', form=form, img_url=img_url)
def upload(): img_url = file.url('default.jpg') #获取默认头像的地址 form = UploadPhotos() #表单类实例化 if form.validate_on_submit(): photo = form.photo.data #获取上传的文件 suffix = photo.filename.split('.')[-1] #获取后缀 #在这个基础上完善 upload下存放图片 以每个人的username为名 存储当前用户的所有图片 #生成唯一的随机图片名 while True: newName = random_name(suffix) path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newName) if not os.path.exists(path): break #保存上传图片 file.save(photo, name=newName) #删除之前上传的图片 if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], 's_' + current_user.icon)) img_zoom(path) #进行缩放 current_user.icon = newName db.session.add(current_user) #把修改后的图片名 保存到表中 db.session.commit() img_url = file.url(current_user.icon) #获取上传后的图片地址 return render_template('owncenter/photo.html', form=form, img_url=img_url)
def upload(): form = Uploadedphotos() img_url = file.url('default.jpg') if form.validate_on_submit(): photo = form.photo.data suffix = photo.filename.split('.')[-1] while True: newname = random_name(suffix) path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname) if not os.path.exists(path): break file.save(photo, name=newname) if current_user.icon != 'default.jpg': os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], 's_' + current_user.icon)) img_zoom(path) current_user.icon = newname db.session.add(current_user) db.session.commit() img_url = file.url(current_user.icon) return render_template('owncenter/uploadphoto.html', form=form, img_url=img_url)
def upload_icon(): form = UploadPhotos() if current_user.icon != 'default.jpg': img_url = file.url(current_user.icon.split('_')[1]) else: img_url = file.url('default.jpg') if form.validate_on_submit(): photo = form.photo.data suffix = photo.filename.split('.')[-1] while True: newname = random_name(suffix) path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname) if not os.path.exists(path): break file.save(photo, name=newname) img = Image.open(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname)) if 1.3 < img.size[1]/img.size[0] < 1.5: if current_user.icon != 'default.jpg': os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon)) os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon.split('_')[1])) img_zoom(path) current_user.icon = 's_' + newname db.session.add(current_user) db.session.commit() img_url = file.url(current_user.icon.split('_')[1]) # print(img_url) flash('上传成功') else: os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname)) flash('图片尺寸比例必须为1.3-1.5') ############################################ c = Course.query.filter_by(pid=0) c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0)) c2 = Course.query.filter(and_(not_(Course.path.endswith(',')), Course.pid != 0)) return render_template('owncenter/icon.html', form=form, img_url=img_url,course=c,course1=c1,course2=c2)
def change_icon(): form = Icon() if form.validate_on_submit(): filename = form.icon.data.filename suffix = filename.split('.')[-1] #循环确保生成的图片名称唯一性 while True: filename = random_filename(suffix) path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],filename) if not os.path.exists(path): break file.save(form.icon.data,name=filename) #保存图片 #删除之前的图片 #原图一张 #在首页展示的时候 一张 大小为100 s_ if current_user.icon != 'default.jpg': os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],current_user.icon)) os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],'s_'+current_user.icon)) #执行缩放 img_zoom(path) current_user.icon = filename #数据保存到表中 db.session.add(current_user) #获取图片地址 img_url = file.url(current_user.icon) return render_template('user/icon.html',form=form,img_url=img_url)
def upload(): form = UploadPhotos() form1 = UploadInfo() # form2 = SubmitInfo() if form.validate_on_submit(): print(1111111111111111111111111111111111) photo = form.photo.data suffix = photo.filename.split('.')[-1] while True: newname = random_name(suffix) path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],newname) if not os.path.exists(path): break file.save(photo,name=newname) if current_user.icon != 'default.jpg': os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],current_user.icon)) img_zoom(path) current_user.icon = newname db.session.add(current_user) db.session.commit() img_url = file.url(current_user.icon) # print(img_url) flash('上传成功') # return render_template('owncenter/center.html', form=form,form1=form1,img_url=img_url) # if current_user.icon != 'default.jpg': img_url = file.url(current_user.icon) # else: # img_url = '#' u1 = User.query.filter_by(username=current_user.username).first() if form1.validate_on_submit(): if form1.sex.data == 'm': u1.sex = True u1.save() else: u1.sex = False u1.save() if form1.age.data != None: u1.age = form1.age.data u1.save() flash('修改成功') # if form1.sex.data == None: # print(form1.sex.data) # form1.sex.data = 'm' if u1.sex == True: form1.sex.data = 'm' else: form1.sex.data = 'w' form1.age.data = u1.age return render_template('owncenter/center.html',form=form,form1=form1,img_url=img_url)
def send_posts(): form = PostsForm() if form.validate_on_submit(): if current_user.is_authenticated: shuffix = os.path.splitext(form.file.data.filename)[-1] #生成随机的图片名 while True: newName = new_name(shuffix) if not os.path.exists( os.path.join( current_app.config['UPLOADED_PHOTOS_DEST'], newName)): break file.save(form.file.data, name=newName) #判断用户更改头像 原头像是否为默认 不是则将原图片删除 #执行缩放 img = Image.open( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newName)) img.thumbnail((300, 300)) #保存新的图片名称为新的图片的s_newname img.save( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], 's_' + newName)) #拿到真正实例化的user对象 u = current_user._get_current_object() file.save(form.file.data, name=newName) p = Posts(selfread=form.selfread.data, content=form.content.data, image=newName, user=u, title=form.title.data, director=form.director.data, characters=form.characters.data) db.session.add(p) flash('电影发表成功!!!') return redirect(url_for('main.index')) else: flash('您还没有登录 请前去登录在发表') return redirect(url_for('user.login')) return render_template('posts/send_posts.html', form=form)
def upload(): form = Upload() if form.validate_on_submit(): icon = request.files.get('icon') # 获取上传对象 suffix = icon.filename.split('.')[-1] # 获取后缀 newName = random_filename(suffix) # 获取新图片的名称 # 以新名称保存图片 file.save(icon, name=newName) delPath = current_app.config['UPLOADED_PHOTOS_DEST'] # 删除之前上传的图片 if current_user.icon != 'default.jpg': # 如果不等于,则证明上传了头像 os.remove(os.path.join(delPath, current_user.icon)) current_user.icon = newName # 更改当前对象的图片名称 db.session.add(current_user) # 更新到数据库中 db.session.commit() # 拼接图片路径 path = os.path.join(delPath, newName) # 进行缩放 img_zoom(path) return render_template('owncenter/upload.html', form=form)
def register(): form = Register() if form.validate_on_submit(): photos = request.files.get('icon') suffix = photos.filename.split('.')[-1] imgInfo = random_filename(suffix) filename = file.save(photos, name=imgInfo[1]) path = os.path.join(Config.UPLOADED_PHOTOS_DEST, filename) img_zoom(path) img_url = file.url(filename) u = User(username=form.username.data, password=form.userpass.data, email=form.email.data,icon=img_url) u.save() flash('恭喜注册成功') return redirect(url_for('user.login')) return render_template('user/register.html',form=form)