def favorite(pid): # 如果收藏了 就清除缓存 cache.clear() try: if current_user.is_favorite(pid): current_user.remove_favorite(pid) else: current_user.add_favorite(pid) return jsonify({'stat': 200}) except: return jsonify({'stat': 500})
def change_user_info(): form = User_info() if form.validate_on_submit(): u = current_user._get_current_object() u.username = form.data.get('username') u.sex = form.data.get('gender') u.age = form.data.get('age') db.session.add(u) cache.clear() flash('个人信息修改完成') return redirect(url_for('user.user_info')) return render_template('user/user_info_change.html', form=form)
def send_posts(): form = Posts() if form.validate_on_submit(): if current_user.is_authenticated: # 拿到实例化的对象 u = current_user._get_current_object() p = Post(content=form.content.data, user=u) db.session.add(p) flash('帖子发表成功') cache.clear() return redirect(url_for('main.index')) else: flash('还未登陆,请登录') return redirect(url_for('user.login')) return render_template('posts/send_posts.html', form=form)
def login(): form = Login() if form.validate_on_submit(): u = User.query.filter_by(username=form.username.data).first() if not u: flash('该用户不存在') elif not u.confirm: flash('未激活') elif u.checked_password_hash(form.password.data): flash('登陆成功') cache.clear() login_user(u, remember=form.remeber.data) return redirect(url_for('main.index')) else: flash('请输入正确的密码') return render_template('user/login.html', form=form)
def change_icon(): form = Icon() if form.validate_on_submit(): shuffix = os.path.splitext(form.file.data.filename)[-1] print(current_app.config['UPLOADED_PHOTOS_DEST']) while True: newname = new_name(shuffix) print(newname) 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 img.save( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], 's_' + 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)) current_user.icon = newname db.session.add(current_user) flash('头像上传成功') img_url = file.url(current_user.icon) cache.clear() return render_template('user/change_icon.html', form=form, img_url=img_url)
def logout(): cache.clear() logout_user() flash('退出成功') return redirect(url_for('main.index'))