123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #主業務邏輯中的視圖和路由的定義
- import os
- import datetime
- from flask import render_template, request, session
- #導入藍圖程序,用於構建路由
- from werkzeug.utils import redirect
- from . import main
- from manage import mqtt
- #導入db,用於操作數據庫
- from manage import db
- #導入實體類,用於操作數據庫
- from ..models import *
- import json
- from datetime import datetime as dt
- from sqlalchemy import text
- #主頁的訪問路徑
- @main.route('/')
- def main_index():
- #獲取登入信息
- if 'id' in session and 'uname' in session:
- username = session['uname']
- return render_template('pump_system.html',params=locals())
- else:
- return render_template('sign_in.html')
- #登入頁面的訪問路徑
- @main.route('/login', methods=['GET','POST'])
- def login_views():
- if request.method == 'GET':
- if 'id' in session and 'uname' in session:
- return redirect('/')
- else:
- return render_template('sign_in.html')
- else:
- #接收前端傳過來的資料
- username = request.form['username']
- password = request.form['password']
- #使用接收的用戶和密碼到資料庫中查詢
- user = User.query.filter_by(username=username, password=password).first()
- #如果用戶存在,將信息保存置session並重定向回首頁,否則重定向回登入頁
- if user:
- resp = redirect('/')
- #判斷是否有記住密碼
- if 'rem' in request.form:
- userID = str(user.userID)
- max_age = 60*60*24*365
- resp.set_cookie("username", username, max_age=max_age)
- resp.set_cookie("userID", userID, max_age=max_age)
- session['uname'] = user.username
- session['id'] = user.userID
- return resp
- else:
- errMsg = "Wrong login or password"
- return render_template('sign_in.html',errMsg=errMsg)
- #登入重設密碼的頁面
- @main.route('/reset_password', methods=['POST', 'GET'])
- def reset_password_views():
- if request.method == 'GET':
- if 'mail' in session:
- del session['mail']
- return render_template('reset_pwd1.html')
- else:
- #如果有id在session裡,代表從reset_pwd2過來的
- if "mail" in session:
- new_pwd = request.form['new_pwd']
- confirm_pwd = request.form['confirm_pwd']
- #判斷密碼是否一致
- if new_pwd == confirm_pwd:
- mail = session['mail']
- user = User.query.filter_by(mail=mail).first()
- user.password = new_pwd
- db.session.add(user)
- del session['mail']
- #修改完後回登入頁
- return redirect('/login')
- else:
- errMsg = "Passwords does not match"
- return render_template('reset_pwd2.html', errMsg=errMsg)
- email = request.form['email']
- user = User.query.filter_by(mail=email).first()
- if user:
- session['mail'] = user.mail
- return render_template('reset_pwd2.html')
- else:
- errMsg = "Wrong email.Please try again"
- return render_template('reset_pwd1.html', errMsg=errMsg)
- #註冊頁面的訪問路徑
- @main.route('/register',methods=['POST',"GET"])
- def register_views():
- if request.method == 'GET':
- return render_template('registration.html')
- else:
- #獲取文本框的值並賦值給user實體對象
- user = User()
- user.firstname = request.form['firstname']
- user.lastname = request.form['lastname']
- user.mail = request.form['email']
- user.phone = request.form['phone']
- user.username = request.form['username']
- user.password = request.form['password']
- user.status = 1
- #將數據保存進資料庫 - 註冊
- db.session.add(user)
- #手動提交,目的是為了獲取提交後的user的id
- db.session.commit()
- #當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
- #完成登入的操作
- user = User.query.filter_by(username=user.username).first()
- session['id'] = user.userID
- session['uname'] = user.username
- return redirect('/')
- #驗證email訪問路徑
- @main.route('/check_email')
- def check_email_views():
- email = request.args['email']
- user = User.query.filter_by(mail=email).first()
- if user:
- result = {"errMsg":" "}
- else:
- result = {"pass":" "}
- return json.dumps(result)
- #驗證username訪問路徑
- @main.route('/check_username')
- def check_username_views():
- username = request.args['username']
- user = User.query.filter_by(username=username).first()
- if user:
- result = {"errMsg":" "}
- else:
- result = {"pass":" "}
- return json.dumps(result)
- @main.route('/pump_system', methods=['GET', 'POST'])
- def pump_views():
- if request.method == 'GET':
- username = session['uname']
- return render_template('pump_system.html', params=locals())
- else:
- pass
- @main.route('/drain_system', methods=['GET', 'POST'])
- def drain_views():
- if request.method == 'GET':
- username = session['uname']
- return render_template('drain_system.html', params=locals())
- else:
- pass
- @main.route('/hydration_system', methods=['GET', 'POST'])
- def hydration_views():
- if request.method == 'GET':
- username = session['uname']
- return render_template('hydration_system.html', params=locals())
- else:
- pass
- @main.route('/spray_system', methods=['GET', 'POST'])
- def spray_views():
- if request.method == 'GET':
- username = session['uname']
- return render_template('spray_system.html', params=locals())
- else:
- pass
- #退出的訪問路徑
- @main.route('/logout')
- def logout_views():
- if 'id' in session and 'uname' in session:
- del session['id']
- del session['uname']
- return redirect('/')
|