-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YangXiao-logic
committed
Oct 2, 2021
1 parent
4e2c2b1
commit fd60f1c
Showing
22 changed files
with
419 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# idea | ||
.idea/ | ||
|
||
|
||
# Environments | ||
|
||
|
||
|
||
# Development | ||
*.db |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
import os | ||
|
||
from flask import Flask | ||
from todolist.blueprints.home import home_bp | ||
from todolist.extension import bootstrap | ||
from todolist.blueprints.auth import auth_bp | ||
from todolist.blueprints.todo import todo_bp | ||
from todolist.extension import bootstrap, db, login_manager, moment | ||
|
||
from todolist.setting import config | ||
|
||
import click | ||
|
||
|
||
def create_app(): | ||
def create_app(config_name=None): | ||
if config_name is None: | ||
config_name = os.getenv('FLASK_CONFIG', 'development') | ||
|
||
app = Flask('todolist') | ||
app.config.from_object(config[config_name]) | ||
register_extensions(app) | ||
register_blueprints(app) | ||
register_command(app) | ||
return app | ||
|
||
|
||
def register_extensions(app): | ||
bootstrap.init_app(app) | ||
db.init_app(app) | ||
login_manager.init_app(app) | ||
moment.init_app(app) | ||
|
||
|
||
def register_blueprints(app): | ||
app.register_blueprint(home_bp) | ||
app.register_blueprint(auth_bp, url_prefix='/auth') | ||
app.register_blueprint(todo_bp, url_prefix='/todo') | ||
|
||
|
||
def register_command(app): | ||
@app.cli.command() | ||
@click.option('--drop', is_flag=True, help='Create after drop.') | ||
def initdb(drop): | ||
"""Initialize the database.""" | ||
if drop: | ||
click.confirm('This operation will delete the database, do you want to continue?', abort=True) | ||
db.drop_all() | ||
click.echo('Drop tables.') | ||
db.create_all() | ||
click.echo('Initialized database.') | ||
|
||
@app.cli.command() | ||
@click.option('--user', default=5, help='Quantity of categories, default is 10.') | ||
@click.option('--category', default=30, help='Quantity of posts, default is 50.') | ||
@click.option('--task', default=100, help='Quantity of comments, default is 500.') | ||
def forge(user, category, task): | ||
"""Generate fake data.""" | ||
from todolist.fakes import fake_tasks, fake_users, fake_categories | ||
|
||
db.drop_all() | ||
db.create_all() | ||
|
||
click.echo('Generating %d users...' % user) | ||
fake_users(user) | ||
|
||
click.echo('Generating %d categories...' % category) | ||
fake_categories(category) | ||
|
||
click.echo('Generating %d tasks' % task) | ||
fake_tasks(task) | ||
|
||
click.echo('Done.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
from flask import render_template, flash, redirect, url_for, Blueprint | ||
from flask_login import login_user, logout_user, login_required, current_user | ||
|
||
from todolist.forms import RegisterForm, LoginForm | ||
from todolist.extension import db | ||
from todolist.models import User | ||
|
||
auth_bp = Blueprint('auth', __name__) | ||
|
||
|
||
@auth_bp.route('/login') | ||
@auth_bp.route('/register', methods=['GET', 'POST']) | ||
def register(): | ||
form = RegisterForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
new_user = User(username=username, password=password) | ||
db.session.add(new_user) | ||
db.session.commit() | ||
flash('New user registered.', 'Success') | ||
return redirect(url_for('auth.login')) | ||
return render_template('auth/register.html', form=form) | ||
|
||
|
||
@auth_bp.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
return render_template('../../../../../PycharmProjects/todolist/templates/auth/../templates/auth/login.html') | ||
if current_user.is_authenticated: | ||
return redirect(url_for('todo.app')) | ||
|
||
form = LoginForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
|
||
user = User.query.filter_by(username=username).first() | ||
if user.validate_password(form.password.data): | ||
login_user(user, remember=True) | ||
|
||
redirect(url_for('todo.app')) | ||
return render_template('auth/login.html', form=form) | ||
|
||
|
||
@auth_bp.route('/logon') | ||
def logon(): | ||
return render_template('../../../../../PycharmProjects/todolist/templates/auth/logon.html') | ||
return render_template('auth/logon.html') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
from flask_bootstrap import Bootstrap | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_login import LoginManager | ||
from flask_moment import Moment | ||
|
||
bootstrap = Bootstrap() | ||
db = SQLAlchemy() | ||
login_manager = LoginManager() | ||
moment = Moment() | ||
|
||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
from todolist.models import User | ||
user = User.query.get(int(user_id)) | ||
return user | ||
|
||
|
||
login_manager.login_view = 'auth.login' | ||
|
||
login_manager.login_message_category = 'warning' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import random | ||
|
||
from faker import Faker | ||
from todolist.extension import db | ||
from todolist.models import User, Category, Task | ||
|
||
fake = Faker() | ||
|
||
|
||
def fake_users(count=5): | ||
for i in range(count): | ||
user = User(username=fake.name(), password=fake.sentence()) | ||
db.session.add(user) | ||
db.session.commit() | ||
|
||
|
||
def fake_categories(count=30): | ||
for i in range(count): | ||
category = Category( | ||
name=fake.sentence(), | ||
user=User.query.get(random.randint(1, User.query.count())) | ||
) | ||
db.session.add(category) | ||
db.session.commit() | ||
|
||
|
||
def fake_tasks(count=100): | ||
for i in range(count): | ||
category=Category.query.get(random.randint(1, Category.query.count())) | ||
task = Task( | ||
content=fake.sentence(), | ||
category=category, | ||
user=category.user | ||
) | ||
db.session.add(task) | ||
db.session.commit() |
Oops, something went wrong.