def movie_edit(movie_id): form = MovieForm() movie = Movie.query.get_or_404(movie_id) form.url.validators = [] form.logo.validators = [] if form.validate_on_submit(): obj = Movie.query.filter_by(title=form.title.data).first() if obj and obj is not movie: flash("片名已存在", "fail") else: for k in form.data: if k in ["url", "logo"]: filename = secure_filename(getattr(form, k).data.filename) if filename: filename = handle_filename(filename) setattr(movie, k, filename) getattr(form, k).data.save(os.path.join(current_app["UPLOAD_DIR"], filename)) elif k not in ["submit", "csrf_token"]: setattr(movie, k, getattr(form, k).data) db.session.add(movie) db.session.commit() flash("修改成功", "succeed") return redirect(url_for("admin.movie_list")) for k in form.data: if k not in ["submit", "csrf_token"]: getattr(form, k).data = getattr(movie, k) return render_template("admin/movie_add.html", title="编辑电影", btn="编辑", form=form, movie=movie)
def preview_edit(preview_id): form = PreviewForm() preview = Preview.query.get_or_404(preview_id) form.logo.validators = [] if form.validate_on_submit(): obj = Preview.query.filter_by(title=form.title.data).first() if obj and obj is not preview: flash("预告已存在", "fail") else: if secure_filename(form.logo.data.filename): filename = handle_filename( secure_filename(form.logo.data.filename)) preview.logo = filename form.logo.data.save( os.path.join(current_app.config["UPLOAD_DIR"], filename)) preview.title = form.title.data db.session.add(preview) db.session.commit() flash("修改成功", "succeed") return redirect(url_for("admin.preview_list")) form.title.data = preview.title return render_template("admin/preview_add.html", title="编辑预告", btn="编辑", form=form, preview=preview)
def user(): form = UserDetailForm() user_obj = User.query.get(session.get("user_id")) if form.validate_on_submit(): obj = User.query.filter_by(phone=form.phone.data).first() obj_2 = User.query.filter_by(email=form.email.data).first() if obj and obj is not user_obj: flash("此手机已存在!", "fail") elif obj_2 and obj_2 is not user_obj: flash("此邮箱已存在!", "fail") else: for k in form.data: if k == "photo": filename = secure_filename(form.photo.data.filename) if filename: filename = handle_filename(filename) user.photo = filename form.photo.data.save( os.path.join(current_app.config["UPLOAD_DIR"], filename)) elif k not in ["submit", "csrf_token"]: setattr(user, k, getattr(form, k).data) db.session.add(user) db.session.commit() flash("修改资料", "succeed") return redirect(url_for("home.user")) for attr in ["email", "phone", "info"]: getattr(form, attr).data = getattr(user_obj, attr) return render_template("home/user.html", form=form, user=user_obj)
def movie_add(): form = MovieForm() if form.validate_on_submit(): title = form.title.data count = Movie.query.filter_by(title=title).count() if count: flash("此电影已存在", "fail") else: logo = handle_filename(secure_filename(form.logo.data.filename)) url = handle_filename(secure_filename(form.url.data.filename)) kwargs = {k: y for k, y in form.data.items() if k not in ["logo", "url", "submit", "csrf_token"]} movie = Movie(logo=logo, url=url, **kwargs) form.logo.data.save(os.path.join(current_app.config["UPLOAD_DIR"], logo)) form.url.data.save(os.path.join(current_app.config["UPLOAD_DIR"], url)) db.session.add(movie) db.session.commit() flash("添加电影成功", "succeed") return redirect(url_for("admin.movie_add")) return render_template("admin/movie_add.html", form=form, title="添加电影", btn="添加")
def preview_add(): form = PreviewForm() if form.validate_on_submit(): count = Preview.query.filter_by(title=form.title.data).count() if count: flash("此预告标题已经存在", "fail") else: filename = handle_filename(secure_filename( form.logo.data.filename)) preview = Preview(title=form.title.data, logo=filename) form.logo.data.save( os.path.join(current_app.config["UPLOAD_DIR"], filename)) db.session.add(preview) db.session.commit() flash("添加预告", "succeed") return redirect(url_for("admin.preview_add")) return render_template("admin/preview_add.html", title="添加预告", btn="添加", form=form)