Download app/auth.py from Genos77/ParkinsonDetection: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/Genos77/ParkinsonDetection/resolve/main/app/auth.py
- Command line
-
hf download hf://Genos77/ParkinsonDetection/app/auth.py
-
curl -L -o auth.py https://huggingface.co/Genos77/ParkinsonDetection/resolve/main/app/auth.py
3.06 kB
| from flask import Blueprint, render_template, redirect, url_for, flash | |
| from flask_login import login_user, logout_user, current_user | |
| from app import db | |
| from app.models import User | |
| from flask_wtf import FlaskForm | |
| from wtforms import StringField, PasswordField, BooleanField, SubmitField | |
| from wtforms.validators import DataRequired, Email, EqualTo, ValidationError | |
| bp = Blueprint('auth', __name__) | |
| # --- Forms --- | |
| class LoginForm(FlaskForm): | |
| username = StringField('Username', validators=[DataRequired()]) | |
| password = PasswordField('Password', validators=[DataRequired()]) | |
| remember_me = BooleanField('Remember Me') | |
| submit = SubmitField('Sign In') | |
| class RegistrationForm(FlaskForm): | |
| username = StringField('Username', validators=[DataRequired()]) | |
| email = StringField('Email', validators=[DataRequired(), Email()]) | |
| password = PasswordField('Password', validators=[DataRequired()]) | |
| password2 = PasswordField( | |
| 'Repeat Password', validators=[DataRequired(), EqualTo('password')]) | |
| submit = SubmitField('Register') | |
| def validate_username(self, username): | |
| user = User.query.filter_by(username=username.data).first() | |
| if user is not None: | |
| raise ValidationError('Please use a different username.') | |
| def validate_email(self, email): | |
| user = User.query.filter_by(email=email.data).first() | |
| if user is not None: | |
| raise ValidationError('Please use a different email address.') | |
| # --- Routes --- | |
| def login(): | |
| if current_user.is_authenticated: | |
| return redirect(url_for('main.dashboard')) | |
| form = LoginForm() | |
| if form.validate_on_submit(): | |
| user = User.query.filter_by(username=form.username.data).first() | |
| if user is None or not user.check_password(form.password.data): | |
| flash('Invalid username or password') | |
| return redirect(url_for('auth.login')) | |
| login_user(user, remember=form.remember_me.data) | |
| if user.is_admin: | |
| return redirect(url_for('main.admin_dashboard')) | |
| return redirect(url_for('main.dashboard')) | |
| return render_template('auth/login.html', title='Sign In', form=form) | |
| def logout(): | |
| logout_user() | |
| return redirect(url_for('main.index')) | |
| def register(): | |
| if current_user.is_authenticated: | |
| return redirect(url_for('main.dashboard')) | |
| form = RegistrationForm() | |
| if form.validate_on_submit(): | |
| user = User(username=form.username.data, email=form.email.data) | |
| user.set_password(form.password.data) | |
| # Make the first registered user an admin | |
| if User.query.count() == 0: | |
| user.is_admin = True | |
| flash('Congratulations, you are now the admin!') | |
| db.session.add(user) | |
| db.session.commit() | |
| flash('Congratulations, you are now a registered user!') | |
| return redirect(url_for('auth.login')) | |
| return render_template('auth/register.html', title='Register', form=form) |