batman
This commit is contained in:
commit
345a801c40
33 changed files with 5499 additions and 0 deletions
5
app/auth/__init__.py
Normal file
5
app/auth/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('auth', __name__)
|
||||
|
||||
from app.auth import routes
|
BIN
app/auth/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
app/auth/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/auth/__pycache__/forms.cpython-313.pyc
Normal file
BIN
app/auth/__pycache__/forms.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/auth/__pycache__/routes.cpython-313.pyc
Normal file
BIN
app/auth/__pycache__/routes.cpython-313.pyc
Normal file
Binary file not shown.
21
app/auth/forms.py
Normal file
21
app/auth/forms.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField
|
||||
from wtforms.validators import DataRequired, Length, EqualTo, ValidationError
|
||||
from app.models.user import User
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
password = PasswordField('Password', validators=[DataRequired()])
|
||||
remember_me = BooleanField('Remember Me')
|
||||
submit = SubmitField('Sign In')
|
||||
|
||||
class SignupForm(FlaskForm):
|
||||
username = StringField('Username', validators=[DataRequired(), Length(min=3, max=64)])
|
||||
password = PasswordField('Password', validators=[DataRequired(), Length(min=8)])
|
||||
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.')
|
77
app/auth/routes.py
Normal file
77
app/auth/routes.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
from flask import render_template, request, redirect, url_for, flash, jsonify, session
|
||||
from flask_login import login_user, logout_user, current_user, login_required
|
||||
from app import db
|
||||
from app.models.user import User
|
||||
from app.auth import bp
|
||||
from app.auth.forms import LoginForm, SignupForm
|
||||
from app.models.document import Category
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
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', 'error')
|
||||
return redirect(url_for('auth.login'))
|
||||
|
||||
login_user(user, remember=form.remember_me.data)
|
||||
session.permanent = True
|
||||
|
||||
next_page = request.args.get('next')
|
||||
if not next_page or urlparse(next_page).netloc != '':
|
||||
next_page = url_for('main.index')
|
||||
|
||||
return redirect(next_page)
|
||||
|
||||
return render_template('auth/login.html', title='Sign In', form=form)
|
||||
|
||||
@bp.route('/signup', methods=['GET', 'POST'])
|
||||
def signup():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
form = SignupForm()
|
||||
if form.validate_on_submit():
|
||||
user = User(username=form.username.data)
|
||||
user.set_password(form.password.data)
|
||||
|
||||
db.session.add(user)
|
||||
db.session.flush() # Get the user ID for the next step
|
||||
|
||||
# Create root category for the user
|
||||
root_category = Category(
|
||||
name='My Documents',
|
||||
icon='mdi-folder',
|
||||
description='Default document category',
|
||||
user_id=user.id,
|
||||
is_root=True
|
||||
)
|
||||
db.session.add(root_category)
|
||||
db.session.commit()
|
||||
|
||||
flash('Your account has been created!', 'success')
|
||||
login_user(user)
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
return render_template('auth/signup.html', title='Sign Up', form=form)
|
||||
|
||||
@bp.route('/logout')
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for('auth.login'))
|
||||
|
||||
@bp.route('/settings', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def settings():
|
||||
if request.method == 'POST':
|
||||
if 'theme_color' in request.form:
|
||||
current_user.theme_color = request.form['theme_color']
|
||||
db.session.commit()
|
||||
flash('Settings updated!', 'success')
|
||||
|
||||
return render_template('auth/settings.html', title='Settings')
|
Loading…
Add table
Add a link
Reference in a new issue