Advertisement
ksieradzinski

Untitled

Feb 28th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. from flask import Flask, render_template, flash, redirect
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_login import UserMixin, LoginManager, login_user, login_required, logout_user
  4. from forms import RegisterForm, LoginForm
  5. from sqlalchemy.exc import IntegrityError
  6. from werkzeug.security import generate_password_hash, check_password_hash
  7.  
  8.  
  9. app = Flask(__name__)
  10. app.config["SECRET_KEY"] = "JSystems1234!"
  11. app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:Password!@localhost:5432/projekt"
  12.  
  13. db = SQLAlchemy(app)
  14.  
  15. login_manager = LoginManager()
  16. login_manager.init_app(app)
  17.  
  18.  
  19. class User(UserMixin, db.Model):
  20. id = db.Column(db.Integer, primary_key=True)
  21. email = db.Column(db.String(150), unique=True, nullable=False)
  22. password = db.Column(db.String(255), nullable=False)
  23. todos = db.relationship('Todo', backref='user')
  24.  
  25.  
  26. class Todo(db.Model):
  27. id = db.Column(db.Integer, primary_key=True)
  28. title = db.Column(db.String(300), nullable=False)
  29. due_date = db.Column(db.DateTime, nullable=True)
  30. completed = db.Column(db.Boolean, default=False)
  31. user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
  32.  
  33.  
  34. @login_manager.user_loader
  35. def load_user(user_id):
  36. return User.query.get(user_id)
  37.  
  38.  
  39. @app.route("/")
  40. def mainpage():
  41. return render_template("mainpage.html")
  42.  
  43. @app.route("/register", methods=["GET", "POST"])
  44. def register():
  45. register_form = RegisterForm()
  46. if register_form.validate_on_submit():
  47. email = register_form.email.data
  48. hashed_password = generate_password_hash(register_form.password.data)
  49.  
  50. try:
  51. new_user = User(email=email, password=hashed_password)
  52. db.session.add(new_user)
  53. db.session.commit()
  54. flash('Użytkownik został zarejestrowany', 'success')
  55. except IntegrityError:
  56. flash('Ten login jest już zajęty', 'error')
  57.  
  58. return redirect('/register')
  59.  
  60. return render_template("register.html", form=register_form)
  61.  
  62.  
  63. @app.route("/login", methods=["GET", "POST"])
  64. def login():
  65. login_form = LoginForm()
  66.  
  67. if login_form.validate_on_submit():
  68. email = login_form.email.data
  69. user = User.query.filter_by(email=email).first()
  70.  
  71. if user and check_password_hash(user.password, login_form.password.data):
  72. login_user(user)
  73. return redirect('/tasks')
  74. else:
  75. flash('Błędne dane logowania', 'error')
  76.  
  77. return render_template("login.html", form=login_form)
  78.  
  79.  
  80. @app.route('/logout')
  81. @login_required
  82. def logout():
  83. logout_user()
  84. return redirect('/login')
  85.  
  86.  
  87. @app.route('/tasks')
  88. @login_required
  89. def task_list():
  90. return "OK"
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement