mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	Restructure project
This commit is contained in:
		@@ -1,4 +1,3 @@
 | 
			
		||||
from app.models import User
 | 
			
		||||
from flask_wtf import FlaskForm
 | 
			
		||||
from wtforms import (
 | 
			
		||||
    BooleanField,
 | 
			
		||||
@@ -7,32 +6,45 @@ from wtforms import (
 | 
			
		||||
    SubmitField,
 | 
			
		||||
    ValidationError
 | 
			
		||||
)
 | 
			
		||||
from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, Length, Regexp
 | 
			
		||||
from wtforms.validators import InputRequired, Email, EqualTo, Length, Regexp
 | 
			
		||||
from app.models import User
 | 
			
		||||
from . import USERNAME_REGEX
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LoginForm(FlaskForm):
 | 
			
		||||
    user = StringField('Email or username', validators=[DataRequired()])
 | 
			
		||||
    password = PasswordField('Password', validators=[DataRequired()])
 | 
			
		||||
    remember_me = BooleanField('Keep me logged in')
 | 
			
		||||
    submit = SubmitField('Log In')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class RegistrationForm(FlaskForm):
 | 
			
		||||
    email = StringField('Email', validators=[DataRequired(), Email()])
 | 
			
		||||
    username = StringField('Username',
 | 
			
		||||
    email = StringField(
 | 
			
		||||
        'Email',
 | 
			
		||||
        validators=[InputRequired(), Email(), Length(max=254)]
 | 
			
		||||
    )
 | 
			
		||||
    username = StringField(
 | 
			
		||||
        'Username',
 | 
			
		||||
        validators=[
 | 
			
		||||
            InputRequired(),
 | 
			
		||||
            Length(1, 64),
 | 
			
		||||
            Length(max=64),
 | 
			
		||||
            Regexp(
 | 
			
		||||
                USERNAME_REGEX,
 | 
			
		||||
                message='Usernames must have only letters, numbers, dots or underscores'  # noqa
 | 
			
		||||
           )
 | 
			
		||||
                message=(
 | 
			
		||||
                    'Usernames must have only letters, numbers, dots or '
 | 
			
		||||
                    'underscores'
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
       ]
 | 
			
		||||
    )
 | 
			
		||||
    password = PasswordField('Password', validators=[DataRequired(), EqualTo('password_confirmation', message='Passwords must match')])
 | 
			
		||||
    password_confirmation = PasswordField('Password confirmation', validators=[DataRequired(), EqualTo('password', message='Passwords must match')])
 | 
			
		||||
    submit = SubmitField('Register')
 | 
			
		||||
    password = PasswordField(
 | 
			
		||||
        'Password',
 | 
			
		||||
        validators=[
 | 
			
		||||
            InputRequired(),
 | 
			
		||||
            EqualTo('password_2', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    password_2 = PasswordField(
 | 
			
		||||
        'Password confirmation',
 | 
			
		||||
        validators=[
 | 
			
		||||
            InputRequired(),
 | 
			
		||||
            EqualTo('password', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
    def validate_email(self, field):
 | 
			
		||||
        if User.query.filter_by(email=field.data.lower()).first():
 | 
			
		||||
@@ -43,12 +55,31 @@ class RegistrationForm(FlaskForm):
 | 
			
		||||
            raise ValidationError('Username already in use')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ResetPasswordForm(FlaskForm):
 | 
			
		||||
    password = PasswordField('New password', validators=[DataRequired(), EqualTo('password_confirmation', message='Passwords must match')])
 | 
			
		||||
    password_confirmation = PasswordField('Password confirmation', validators=[DataRequired(), EqualTo('password', message='Passwords must match')])
 | 
			
		||||
    submit = SubmitField('Reset Password')
 | 
			
		||||
class LoginForm(FlaskForm):
 | 
			
		||||
    user = StringField('Email or username', validators=[InputRequired()])
 | 
			
		||||
    password = PasswordField('Password', validators=[InputRequired()])
 | 
			
		||||
    remember_me = BooleanField('Keep me logged in')
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ResetPasswordRequestForm(FlaskForm):
 | 
			
		||||
    email = StringField('Email', validators=[DataRequired(), Email()])
 | 
			
		||||
    submit = SubmitField('Reset Password')
 | 
			
		||||
    email = StringField('Email', validators=[InputRequired(), Email()])
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ResetPasswordForm(FlaskForm):
 | 
			
		||||
    password = PasswordField(
 | 
			
		||||
        'New password',
 | 
			
		||||
        validators=[
 | 
			
		||||
            InputRequired(),
 | 
			
		||||
            EqualTo('password_2', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    password_2 = PasswordField(
 | 
			
		||||
        'New password confirmation',
 | 
			
		||||
        validators=[
 | 
			
		||||
            InputRequired(),
 | 
			
		||||
            EqualTo('password', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,5 @@
 | 
			
		||||
from app import db
 | 
			
		||||
from app.email import create_message, send
 | 
			
		||||
from app.models import User
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from flask import (
 | 
			
		||||
    abort,
 | 
			
		||||
    current_app,
 | 
			
		||||
    flash,
 | 
			
		||||
    redirect,
 | 
			
		||||
    render_template,
 | 
			
		||||
@@ -12,7 +7,9 @@ from flask import (
 | 
			
		||||
    url_for
 | 
			
		||||
)
 | 
			
		||||
from flask_login import current_user, login_user, login_required, logout_user
 | 
			
		||||
from sqlalchemy import or_
 | 
			
		||||
from app import db
 | 
			
		||||
from app.email import create_message, send
 | 
			
		||||
from app.models import User
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (
 | 
			
		||||
    LoginForm,
 | 
			
		||||
@@ -29,69 +26,32 @@ def before_request():
 | 
			
		||||
    unconfirmed view if user is unconfirmed.
 | 
			
		||||
    """
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        current_user.last_seen = datetime.utcnow()
 | 
			
		||||
        current_user.ping()
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        if (
 | 
			
		||||
            not current_user.confirmed
 | 
			
		||||
            and request.endpoint
 | 
			
		||||
            and request.blueprint != 'auth'
 | 
			
		||||
            and request.endpoint != 'static'
 | 
			
		||||
        ):
 | 
			
		||||
        if (not current_user.confirmed
 | 
			
		||||
                and request.endpoint
 | 
			
		||||
                and request.blueprint != 'auth'
 | 
			
		||||
                and request.endpoint != 'static'):
 | 
			
		||||
            return redirect(url_for('auth.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/login', methods=['GET', 'POST'])
 | 
			
		||||
def login():
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    form = LoginForm(prefix='login-form')
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        user = User.query.filter(
 | 
			
		||||
            or_(
 | 
			
		||||
                User.username == form.user.data,
 | 
			
		||||
                User.email == form.user.data.lower()
 | 
			
		||||
            )
 | 
			
		||||
        ).first()
 | 
			
		||||
        if user and user.verify_password(form.password.data):
 | 
			
		||||
            login_user(user, form.remember_me.data)
 | 
			
		||||
            next = request.args.get('next')
 | 
			
		||||
            if next is None or not next.startswith('/'):
 | 
			
		||||
                next = url_for('main.dashboard')
 | 
			
		||||
            return redirect(next)
 | 
			
		||||
        flash('Invalid email/username or password', category='error')
 | 
			
		||||
    return render_template('auth/login.html.j2', form=form, title='Log in')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/logout')
 | 
			
		||||
@login_required
 | 
			
		||||
def logout():
 | 
			
		||||
    logout_user()
 | 
			
		||||
    flash('You have been logged out')
 | 
			
		||||
    return redirect(url_for('main.index'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/register', methods=['GET', 'POST'])
 | 
			
		||||
def register():
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    form = RegistrationForm(prefix='registration-form')
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        user = User(
 | 
			
		||||
            email=form.email.data.lower(),
 | 
			
		||||
            password=form.password.data,
 | 
			
		||||
            username=form.username.data
 | 
			
		||||
        )
 | 
			
		||||
        db.session.add(user)
 | 
			
		||||
        db.session.flush(objects=[user])
 | 
			
		||||
        db.session.refresh(user)
 | 
			
		||||
        try:
 | 
			
		||||
            user.makedirs()
 | 
			
		||||
        except OSError as e:
 | 
			
		||||
            current_app.logger.error(e)
 | 
			
		||||
            db.session.rollback()
 | 
			
		||||
            user = User.create(
 | 
			
		||||
                email=form.email.data.lower(),
 | 
			
		||||
                password=form.password.data,
 | 
			
		||||
                username=form.username.data
 | 
			
		||||
            )
 | 
			
		||||
        except OSError:
 | 
			
		||||
            flash('Internal Server Error', category='error')
 | 
			
		||||
            abort(500)
 | 
			
		||||
        token = user.generate_confirm_user_token()
 | 
			
		||||
        flash(f'User "{user.username}" created')
 | 
			
		||||
        token = user.generate_confirm_token()
 | 
			
		||||
        msg = create_message(
 | 
			
		||||
            user.email,
 | 
			
		||||
            'Confirm Your Account',
 | 
			
		||||
@@ -110,36 +70,46 @@ def register():
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/confirm/<token>')
 | 
			
		||||
@bp.route('/login', methods=['GET', 'POST'])
 | 
			
		||||
def login():
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    form = LoginForm(prefix='login-form')
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        user = User.query.filter((User.email == form.user.data.lower()) | (User.username == form.user.data)).first()
 | 
			
		||||
        if user and user.verify_password(form.password.data):
 | 
			
		||||
            login_user(user, form.remember_me.data)
 | 
			
		||||
            next = request.args.get('next')
 | 
			
		||||
            if next is None or not next.startswith('/'):
 | 
			
		||||
                next = url_for('main.dashboard')
 | 
			
		||||
            flash('You have been logged in')
 | 
			
		||||
            return redirect(next)
 | 
			
		||||
        flash('Invalid email/username or password', category='error')
 | 
			
		||||
    return render_template('auth/login.html.j2', form=form, title='Log in')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/logout')
 | 
			
		||||
@login_required
 | 
			
		||||
def confirm(token):
 | 
			
		||||
    if current_user.confirmed:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    if current_user.confirm_user(token):
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('You have confirmed your account')
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    else:
 | 
			
		||||
        flash(
 | 
			
		||||
            'The confirmation link is invalid or has expired',
 | 
			
		||||
            category='error'
 | 
			
		||||
        )
 | 
			
		||||
        return redirect(url_for('.unconfirmed'))
 | 
			
		||||
def logout():
 | 
			
		||||
    logout_user()
 | 
			
		||||
    flash('You have been logged out')
 | 
			
		||||
    return redirect(url_for('main.index'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/unconfirmed')
 | 
			
		||||
@login_required
 | 
			
		||||
def unconfirmed():
 | 
			
		||||
    if current_user.is_anonymous:
 | 
			
		||||
        return redirect(url_for('main.index'))
 | 
			
		||||
    elif current_user.confirmed:
 | 
			
		||||
    if current_user.confirmed:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    return render_template('auth/unconfirmed.html.j2', title='Unconfirmed')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/confirm')
 | 
			
		||||
@login_required
 | 
			
		||||
def resend_confirmation():
 | 
			
		||||
    token = current_user.generate_confirm_user_token()
 | 
			
		||||
def confirm_request():
 | 
			
		||||
    if current_user.confirmed:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    token = current_user.generate_confirm_token()
 | 
			
		||||
    msg = create_message(
 | 
			
		||||
        current_user.email,
 | 
			
		||||
        'Confirm Your Account',
 | 
			
		||||
@@ -149,10 +119,23 @@ def resend_confirmation():
 | 
			
		||||
    )
 | 
			
		||||
    send(msg)
 | 
			
		||||
    flash('A new confirmation email has been sent to you by email')
 | 
			
		||||
    return redirect(url_for('auth.unconfirmed'))
 | 
			
		||||
    return redirect(url_for('.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/reset', methods=['GET', 'POST'])
 | 
			
		||||
@bp.route('/confirm/<token>')
 | 
			
		||||
@login_required
 | 
			
		||||
def confirm(token):
 | 
			
		||||
    if current_user.confirmed:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    if current_user.confirm(token):
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('You have confirmed your account')
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    flash('The confirmation link is invalid or has expired', category='error')
 | 
			
		||||
    return redirect(url_for('.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/reset_password', methods=['GET', 'POST'])
 | 
			
		||||
def reset_password_request():
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
@@ -160,7 +143,7 @@ def reset_password_request():
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        user = User.query.filter_by(email=form.email.data.lower()).first()
 | 
			
		||||
        if user is not None:
 | 
			
		||||
            token = user.generate_password_reset_token()
 | 
			
		||||
            token = user.generate_reset_password_token()
 | 
			
		||||
            msg = create_message(
 | 
			
		||||
                user.email,
 | 
			
		||||
                'Reset Your Password',
 | 
			
		||||
@@ -170,7 +153,8 @@ def reset_password_request():
 | 
			
		||||
            )
 | 
			
		||||
            send(msg)
 | 
			
		||||
        flash(
 | 
			
		||||
            'An email with instructions to reset your password has been sent to you'  # noqa
 | 
			
		||||
            'An email with instructions to reset your password has been sent '
 | 
			
		||||
            'to you'
 | 
			
		||||
        )
 | 
			
		||||
        return redirect(url_for('.login'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
@@ -180,7 +164,7 @@ def reset_password_request():
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/reset/<token>', methods=['GET', 'POST'])
 | 
			
		||||
@bp.route('/reset_password/<token>', methods=['GET', 'POST'])
 | 
			
		||||
def reset_password(token):
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
@@ -190,8 +174,7 @@ def reset_password(token):
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
            flash('Your password has been updated')
 | 
			
		||||
            return redirect(url_for('.login'))
 | 
			
		||||
        else:
 | 
			
		||||
            return redirect(url_for('main.index'))
 | 
			
		||||
        return redirect(url_for('main.index'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'auth/reset_password.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user