def movie_edit(id=None):
    path = app.config["UP_DIR"]
    form = forms.Add_Movie()
    movie_obj = models.Movie.query.filter_by(id=id).first_or_404()

    ##修改数据库文件
    if request.method == 'POST':
        movie_obj.name = form.name.data
        movie_obj.url = File(form.url.data, path).exit_filename(movie_obj.url)
        movie_obj.logo = File(form.logo.data,
                              path).exit_filename(movie_obj.logo)
        movie_obj.info = form.info.data
        movie_obj.score = form.score.data
        movie_obj.area = form.area.data
        movie_obj.length = form.length.data
        movie_obj.category_id = form.tag.data
        movie_obj.release_time = form.release_time.data
        mysql_db.session.add(movie_obj)
        mysql_db.session.commit()
        AddLog.add_oplog('编辑电影')
        return redirect(url_for('admin.movie_list', page=1))

    ##显示数据页面
    form.info.data = movie_obj.info
    form.logo.data = movie_obj.logo
    form.url.data = movie_obj.url
    form.score.data = movie_obj.score
    form.tag.data = movie_obj.category_id
    return render_template('admin/movie_edit.html',
                           movie_obj=movie_obj,
                           form=form)
def tag_add():
    form = forms.Add_Tag()
    if form.validate_on_submit():
        Tag = models.Category(title=form.data['title'])
        mysql_db.session.add(Tag)
        mysql_db.session.commit()
        flash('添加成功', category='add')
        AddLog.add_oplog('添加标签')
        return redirect(url_for('admin.tag_add'))
    return render_template('admin/add_tag.html', form=form)
def preview_del(id=None):
    path = app.config["UP_DIR"]
    pre_obj = models.Preview.query.filter_by(id=id).first_or_404()
    mysql_db.session.delete(pre_obj)
    mysql_db.session.commit()
    AddLog.add_oplog('删除预告')
    try:
        os.remove(os.path.join(path, pre_obj.logo))
    except FileNotFoundError as e:
        print(e.args)
        pass
    return redirect(url_for('admin.preview_list', page=1))
def tag_edit(id=None):
    print(request.path)
    tag_obj = models.Category.query.filter_by(id=id).first()
    if request.method == "POST":
        value = request.form.get('title')
        if value:
            tag_obj.title = value
            mysql_db.session.add(tag_obj)
            mysql_db.session.commit()
            AddLog.add_oplog('编辑标签')
            return redirect(url_for('admin.tag_list', page=1))
    return render_template('admin/tag_edit.html', tag_obj=tag_obj)
def login():
    form = forms.Admin()
    if form.validate_on_submit():
        user = models.Admin.query.filter_by(name=form.data['user']).first()
        if not user.check_pwd(form.data['pwd']):
            flash('密码错误')
            return redirect(url_for('admin.login'))
        session['user'] = form.data['user']
        session['id'] = user.id
        AddLog.add_admin_log()
        ##跳转判断
        next = request.args.get('next')
        if next:
            return redirect(next)
        return redirect(url_for('admin.index'))
    return render_template('admin/login.html', form=form)
def movie_add():
    form = forms.Add_Movie()
    path = app.config["UP_DIR"]
    if form.validate_on_submit():
        url = File(form.url.data, path)
        logo = File(form.logo.data, path)

        movie_obj = models.Movie(name=form.data['name'],
                                 url=url.safe_filename_save(),
                                 logo=logo.safe_filename_save(),
                                 info=form.data['info'],
                                 score=int(form.data['score']),
                                 playnum=0,
                                 area=form.data['area'],
                                 length=form.data['length'],
                                 category_id=form.data['tag'],
                                 release_time=form.data['release_time'])
        mysql_db.session.add(movie_obj)
        mysql_db.session.commit()
        AddLog.add_oplog('添加电影')
        return redirect(url_for('admin.movie_list', page=1))
    return render_template('admin/movie_add.html', form=form)
def user_del(id=None):
    user_obj = models.User.query.filter_by(id=id).first_or_404()
    mysql_db.session.delete(user_obj)
    mysql_db.session.commit()
    AddLog.add_oplog('删除用户')
    return redirect(url_for('admin.user_list', page=1))
def tag_del(id=None):
    tag_obj = models.Category.query.filter_by(id=id).first_or_404()
    mysql_db.session.delete(tag_obj)
    mysql_db.session.commit()
    AddLog.add_oplog('删除标签')
    return redirect(url_for('admin.tag_list', page=1))