from flask import Flask, request, render_template, session, redirect, jsonify from utils import query from utils.getHomeData import * from utils.getSearchData import * from utils.getTime_tData import * from utils.getRate_tData import * from utils.getMapData import * from utils.getType_tData import * from utils.getActor_tData import * from utils.getTableData import * from utils.getComments_cData import * import re app = Flask(__name__) app.secret_key = 'This is secret_key you know ?' @app.route('/login', methods=['GET', 'POST']) def login(): error_message = None if request.method == 'GET': return render_template('login.html') elif request.method == 'POST': session.clear() request.form = dict(request.form) def filter_fn(item): return request.form['email'] in item users = query.querys('select * from user where email = %s', [request.form['email']], 'select') filter_list = list(filter(filter_fn, users)) if len(filter_list): if request.form['password'] == filter_list[0][2]: session['email'] = request.form['email'] return redirect('/home') else: error_message = '密码错误!' else: error_message = '不存在该用户!' return render_template('login.html', error_message=error_message) @app.route('/loginout') def loginout(): session.clear() return redirect('/login') @app.route('/register', methods=['GET', 'POST']) def register(): if request.method == 'GET': return render_template('register.html') elif request.method == 'POST': request.form = dict(request.form) if request.form['password'] != request.form['passwordChecked']: return render_template('register.html', error_message='两次密码不符合!') def filter_fn(item): return request.form['email'] in item users = query.querys('select * from user', [], 'select') filter_list = list(filter(filter_fn, users)) if len(filter_list): return render_template('register.html', error_message='该用户已被注册!') else: query.querys('insert into user(email,password) values (%s,%s)', [request.form['email'], request.form['password']]) return redirect('/login') # @app.route('/login', methods=['GET', 'POST']) # # def login(): # if request.method == 'GET': # return render_template('login.html') # elif request.method == 'POST': # request.form = dict(request.form) # # def filter_fn(item): # return request.form['email'] in item # # users = query.querys('select * from user where email = %s', [request.form['email']], 'select') # filter_list = list(filter(filter_fn, users)) # # if len(filter_list): # if request.form['password'] == filter_list[0][2]: # session['email'] = request.form['email'] # return redirect('/home') # # return render_template('error.html', message='登陆成功!') # else: # return render_template('error.html', message='密码错误!') # else: # return render_template('error.html', message='不存在该用户!') # # # @app.route('/loginout') # def loginout(): # session.clear() # return redirect('/login') # # # @app.route('/register', methods=['GET', 'POST']) # def register(): # if request.method == 'GET': # return render_template('register.html') # elif request.method == 'POST': # request.form = dict(request.form) # if request.form['password'] != request.form['passwordChecked']: # return render_template('error.html', message='两次密码不符合!') # # def filter_fn(item): # return request.form['email'] in item # # users = query.querys('select * from user', [], 'select') # filter_list = list(filter(filter_fn, users)) # # if len(filter_list): # return render_template('error.html', message='该用户已被注册!') # else: # query.querys('insert into user(email,password) values (%s,%s)', # [request.form['email'], request.form['password']]) # return redirect('/login') # tableData = getTableData() # tableData = getTableDataByTablePage() @app.route('/home', methods=['GET', 'POST']) def home(): email = session.get('email') maxMovieLen, maxRate, maxCasts, maxCountry, maxTypes, maxLang = getHomeData() typeEcharData = getTypesEcharData() row, columns = getRateEcharData() tableData = getTableData() return render_template( 'index.html', email=email, maxMovieLen=maxMovieLen, maxRate=maxRate, maxCasts=maxCasts, maxCountry=maxCountry, maxTypes=maxTypes, maxLang=maxLang, typeEcharData=typeEcharData, row=row, columns=columns, tableData=tableData, ) # 电影预告片 @app.route('/movie/') def movie(movieName): movieUrl = getMovieUrlById(movieName) return render_template('movie.html', movieUrl=movieUrl) # @app.route('/movie/') # def movie(movieid): # movieUrl = getMovieUrlById(movieid) # print(movieUrl) # return render_template('movie.html', movieUrl=movieUrl) # 搜索 @app.route('/search/', methods=['GET', 'POST']) def search(movieId): email = session.get('email') if request.method == 'GET': resultData = getMovieDetailById(movieId) else: request.form = dict(request.form) resultData = getMovieDetailBySearchWord(request.form['searchWord']) # print(resultData) return render_template('search.html', email=email, resultData=resultData) # 时间 @app.route('/time_t') def time_t(): row, columns = getYearData() moveTimeDate = getMovieTimeData() email = session.get('email') return render_template('time_t.html', email=email, row=row, columns=columns, moveTimeDate=moveTimeDate) # 评分 @app.route('/rate_t/', methods=['GET', 'POST']) def rate_t(type): email = session.get('email') typeList = getAllTypes() # print(typeList) # print(type) row, columns = getAllRateDataByType(type) # print(row, columns) yearMenRow, yearMenColumns = getYearMeanData() if request.method == 'GET': startData, searchName = getStart('') else: request.form = dict(request.form) startData, searchName = getStart(request.form['searchIpt']) print(startData, searchName) return render_template( 'rate_t.html', email=email, typeList=typeList, type=type, row=row, columns=columns, startData=startData, searchName=searchName, yearMenRow=yearMenRow, yearMenColumns=yearMenColumns ) # 地图 @app.route('/map_t') def map_t(): email = session.get('email') row, columns = getMapData() # print(row, type(row)) # print(columns, type(columns)) langRow, langColumns = getLangData() return render_template('map_t.html', email=email, row=row, columns=columns, langRow=langRow, langColumns=langColumns) # 类型 @app.route('/type_t') def type_t(): email = session.get('email') typesData = getTypeData() return render_template('type_t.html', email=email, typesData=typesData) # 导演、演员 @app.route('/actor_t') def actor_t(): email = session.get('email') row, columns = getDirectorsDataTop20() rowCasts, columnsCasts = getCastsDataTop20() return render_template('actor_t.html', email=email, row=row, columns=columns, rowCasts=rowCasts, columnsCasts=columnsCasts) @app.route('/table/') def table(movieName): email = session.get('email') tableData = delMovieByMoiveName(movieName) # return redirect('/table/0') # tableData = getTableDataByTablePage() return render_template('table.html', email=email, tableData=tableData) # 评论词云图 @app.route('/comments_c', methods=['GET', 'POST']) def comments_c(): email = session.get('email') resSrc, searchName = '', '' # 初始化为空字符串 if request.method == 'POST': searchIpt = request.form['searchIpt'] resSrc, searchName = getCommentsImage(searchIpt) # 如果没有找到匹配项,resSrc 应该是空字符串,此时不渲染图片 return render_template('comments_c.html', email=email, resSrc=resSrc, searchName=searchName) # 标题词云图 @app.route('/title_c', methods=['GET', 'POST']) def title_c(): email = session.get('email') return render_template('title_c.html', email=email) # 简介词云图 @app.route('/summary_c', methods=['GET', 'POST']) def summary_c(): email = session.get('email') return render_template('summary_c.html', email=email) # 演员名词云图 @app.route('/casts_c', methods=['GET', 'POST']) def casts_c(): email = session.get('email') return render_template('casts_c.html', email=email) @app.route('/') def allRequest(): return redirect('/login') # 定义了一个 Flask 应用的全局前置请求处理器(before request handler) # 它会在每次请求到达 Flask 应用之前运行 @app.before_request def before_requre(): # 定义一个正则表达式模式,匹配以 '/static' 开头的路径 pat = re.compile(r'^/static') # 如果请求的路径匹配 '/static' 开头,则不进行后续的检查,直接返回 # 这意味着静态文件的请求不会被后续的认证逻辑所拦截 if re.search(pat, request.path): return # 如果请求的路径是 "/login",同样不进行后续的检查,直接返回 # 这允许用户直接访问登录页面,而不需要先登录 if request.path == "/login": return # 如果请求的路径是 '/register',同样不进行后续的检查,直接返回 # 假设 '/register' 是注册页面的路径,允许用户直接访问注册页面 if request.path == '/register': return # 尝试从 session 中获取用户的电子邮件地址 email = session.get('email') # 如果 session 中存在电子邮件地址(即用户已登录) if email: # 则不进行任何操作,返回 None 表示继续处理该请求 return None # 如果 session 中不存在电子邮件地址(即用户未登录) # 则重定向用户到登录页面 '/login' return redirect('/login') if __name__ == '__main__': app.run()