You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #!/usr/bin/env python3
  2. from flask import Flask, render_template, session, request, abort, redirect, url_for
  3. from flask_sqlalchemy import SQLAlchemy
  4. import bcrypt
  5. db: SQLAlchemy = SQLAlchemy()
  6. app = Flask(__name__)
  7. app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
  8. app.secret_key = '98d31240f9fbe14c8083586db49c19c3a8d3f726'
  9. class Admin(db.Model):
  10. id = db.Column(db.Integer, primary_key=True)
  11. username = db.Column(db.String, unique=True, nullable=False)
  12. password = db.Column(db.String, nullable=False)
  13. query: db.Query
  14. @classmethod
  15. def generate_password(cls, pw: str):
  16. return bcrypt.hashpw(pw, bcrypt.gensalt(12))
  17. @classmethod
  18. def authenticate(cls, username: str, pw: str):
  19. user = Admin.query.filter_by(username=username).one_or_none()
  20. if user and bcrypt.checkpw(pw, user.password):
  21. session['admin'] = user.username
  22. return user
  23. else:
  24. return None
  25. @classmethod
  26. def exists(cls):
  27. user = Admin.query.one_or_none()
  28. return True if user else False
  29. @classmethod
  30. def authorize(cls):
  31. if not session.get('admin'):
  32. return redirect(url_for("admin_login"))
  33. class Chemical(db.Model):
  34. query: db.Query
  35. id = db.Column(db.Integer, primary_key=True)
  36. pubchem_cid = db.Column(db.Integer, nullable=False)
  37. name = db.Column(db.String, nullable=False)
  38. formula = db.Column(db.String, nullable=False)
  39. mass = db.Column(db.Float, nullable=False)
  40. mz = db.Column(db.Float, nullable=False)
  41. rt = db.Column(db.Float, nullable=False)
  42. # Error Handlers
  43. @app.errorhandler(404)
  44. def handler_404(msg):
  45. return render_template("errors/404.html")
  46. @app.errorhandler(403)
  47. def handler_403(msg):
  48. return render_template("errors/403.html")
  49. # Admin routes
  50. @app.route('/admin')
  51. def admin_root():
  52. if login := Admin.authorize():
  53. return login
  54. return render_template("admin.html", user=session.get("admin"))
  55. @app.route('/admin/create', methods=['GET', 'POST'])
  56. def admin_create():
  57. if Admin.exists():
  58. if login := Admin.authorize():
  59. return login
  60. if request.method == "GET":
  61. return render_template("register.html")
  62. else:
  63. username, pw = request.form.get('username'), request.form.get('password')
  64. if username is None or pw is None:
  65. return render_template("register.html", fail="Invalid Input.")
  66. elif db.session.execute(db.select(Admin).filter_by(username=username)).fetchone():
  67. return render_template("register.html", fail="Username already exists.")
  68. else:
  69. db.session.add(Admin(username=username, password=Admin.generate_password(pw)))
  70. db.session.commit()
  71. return render_template("register.html", success=True)
  72. @app.route('/admin/login', methods=['GET', 'POST'])
  73. def admin_login():
  74. if request.method == "POST":
  75. username, pw = request.form.get('username', ''), request.form.get('password', '')
  76. if Admin.authenticate(username, pw):
  77. return render_template("login.html", success=True)
  78. else:
  79. return render_template("login.html", fail="Could not authenticate.")
  80. else:
  81. return render_template("login.html")
  82. @app.route('/admin/logout', methods=['GET'])
  83. def admin_logout():
  84. session.pop('admin')
  85. return redirect(url_for('home'))
  86. @app.route("/")
  87. def home():
  88. if Admin.exists():
  89. return render_template("index.html")
  90. else:
  91. return redirect(url_for("admin_create"))
  92. @app.route("/search")
  93. def search():
  94. return "searching url"
  95. if __name__ == "__main__":
  96. db.init_app(app)
  97. with app.app_context():
  98. db.create_all()
  99. app.run(debug=True)