views.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from flask import render_template, request, session
  5. #導入藍圖程序,用於構建路由
  6. from werkzeug.utils import redirect
  7. from . import main
  8. from manage import mqtt
  9. #導入db,用於操作數據庫
  10. from manage import db
  11. #導入實體類,用於操作數據庫
  12. from ..models import *
  13. import json
  14. from datetime import datetime as dt
  15. from sqlalchemy import text
  16. #主頁的訪問路徑
  17. @main.route('/')
  18. def main_index():
  19. #獲取登入信息
  20. if 'id' in session and 'uname' in session:
  21. username = session['uname']
  22. return render_template('pump_system.html',params=locals())
  23. else:
  24. return render_template('sign_in.html')
  25. #登入頁面的訪問路徑
  26. @main.route('/login', methods=['GET','POST'])
  27. def login_views():
  28. if request.method == 'GET':
  29. if 'id' in session and 'uname' in session:
  30. return redirect('/')
  31. else:
  32. return render_template('sign_in.html')
  33. else:
  34. #接收前端傳過來的資料
  35. username = request.form['username']
  36. password = request.form['password']
  37. #使用接收的用戶和密碼到資料庫中查詢
  38. user = User.query.filter_by(username=username, password=password).first()
  39. #如果用戶存在,將信息保存置session並重定向回首頁,否則重定向回登入頁
  40. if user:
  41. resp = redirect('/')
  42. #判斷是否有記住密碼
  43. if 'rem' in request.form:
  44. userID = str(user.userID)
  45. max_age = 60*60*24*365
  46. resp.set_cookie("username", username, max_age=max_age)
  47. resp.set_cookie("userID", userID, max_age=max_age)
  48. session['uname'] = user.username
  49. session['id'] = user.userID
  50. return resp
  51. else:
  52. errMsg = "Wrong login or password"
  53. return render_template('sign_in.html',errMsg=errMsg)
  54. #登入重設密碼的頁面
  55. @main.route('/reset_password', methods=['POST', 'GET'])
  56. def reset_password_views():
  57. if request.method == 'GET':
  58. if 'mail' in session:
  59. del session['mail']
  60. return render_template('reset_pwd1.html')
  61. else:
  62. #如果有id在session裡,代表從reset_pwd2過來的
  63. if "mail" in session:
  64. new_pwd = request.form['new_pwd']
  65. confirm_pwd = request.form['confirm_pwd']
  66. #判斷密碼是否一致
  67. if new_pwd == confirm_pwd:
  68. mail = session['mail']
  69. user = User.query.filter_by(mail=mail).first()
  70. user.password = new_pwd
  71. db.session.add(user)
  72. del session['mail']
  73. #修改完後回登入頁
  74. return redirect('/login')
  75. else:
  76. errMsg = "Passwords does not match"
  77. return render_template('reset_pwd2.html', errMsg=errMsg)
  78. email = request.form['email']
  79. user = User.query.filter_by(mail=email).first()
  80. if user:
  81. session['mail'] = user.mail
  82. return render_template('reset_pwd2.html')
  83. else:
  84. errMsg = "Wrong email.Please try again"
  85. return render_template('reset_pwd1.html', errMsg=errMsg)
  86. #註冊頁面的訪問路徑
  87. @main.route('/register',methods=['POST',"GET"])
  88. def register_views():
  89. if request.method == 'GET':
  90. return render_template('registration.html')
  91. else:
  92. #獲取文本框的值並賦值給user實體對象
  93. user = User()
  94. user.firstname = request.form['firstname']
  95. user.lastname = request.form['lastname']
  96. user.mail = request.form['email']
  97. user.phone = request.form['phone']
  98. user.username = request.form['username']
  99. user.password = request.form['password']
  100. user.status = 1
  101. #將數據保存進資料庫 - 註冊
  102. db.session.add(user)
  103. #手動提交,目的是為了獲取提交後的user的id
  104. db.session.commit()
  105. #當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  106. #完成登入的操作
  107. user = User.query.filter_by(username=user.username).first()
  108. session['id'] = user.userID
  109. session['uname'] = user.username
  110. return redirect('/')
  111. #驗證email訪問路徑
  112. @main.route('/check_email')
  113. def check_email_views():
  114. email = request.args['email']
  115. user = User.query.filter_by(mail=email).first()
  116. if user:
  117. result = {"errMsg":" "}
  118. else:
  119. result = {"pass":" "}
  120. return json.dumps(result)
  121. #驗證username訪問路徑
  122. @main.route('/check_username')
  123. def check_username_views():
  124. username = request.args['username']
  125. user = User.query.filter_by(username=username).first()
  126. if user:
  127. result = {"errMsg":" "}
  128. else:
  129. result = {"pass":" "}
  130. return json.dumps(result)
  131. @main.route('/pump_system', methods=['GET', 'POST'])
  132. def pump_views():
  133. if request.method == 'GET':
  134. username = session['uname']
  135. return render_template('pump_system.html', params=locals())
  136. else:
  137. pass
  138. @main.route('/drain_system', methods=['GET', 'POST'])
  139. def drain_views():
  140. if request.method == 'GET':
  141. username = session['uname']
  142. return render_template('drain_system.html', params=locals())
  143. else:
  144. pass
  145. @main.route('/hydration_system', methods=['GET', 'POST'])
  146. def hydration_views():
  147. if request.method == 'GET':
  148. username = session['uname']
  149. return render_template('hydration_system.html', params=locals())
  150. else:
  151. pass
  152. @main.route('/spray_system', methods=['GET', 'POST'])
  153. def spray_views():
  154. if request.method == 'GET':
  155. username = session['uname']
  156. return render_template('spray_system.html', params=locals())
  157. else:
  158. pass
  159. #退出的訪問路徑
  160. @main.route('/logout')
  161. def logout_views():
  162. if 'id' in session and 'uname' in session:
  163. del session['id']
  164. del session['uname']
  165. return redirect('/')