-
Notifications
You must be signed in to change notification settings - Fork 366
/
__init__.py
161 lines (129 loc) · 5.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
import os
import click
from flask import Flask, render_template
from flask_login import current_user
from flask_wtf.csrf import CSRFError
from albumy.blueprints.admin import admin_bp
from albumy.blueprints.ajax import ajax_bp
from albumy.blueprints.auth import auth_bp
from albumy.blueprints.main import main_bp
from albumy.blueprints.user import user_bp
from albumy.extensions import bootstrap, db, login_manager, mail, dropzone, moment, whooshee, avatars, csrf
from albumy.models import Role, User, Photo, Tag, Follow, Notification, Comment, Collect, Permission
from albumy.settings import config
def create_app(config_name=None):
if config_name is None:
config_name = os.getenv('FLASK_CONFIG', 'development')
app = Flask('albumy')
app.config.from_object(config[config_name])
register_extensions(app)
register_blueprints(app)
register_commands(app)
register_errorhandlers(app)
register_shell_context(app)
register_template_context(app)
return app
def register_extensions(app):
bootstrap.init_app(app)
db.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
dropzone.init_app(app)
moment.init_app(app)
whooshee.init_app(app)
avatars.init_app(app)
csrf.init_app(app)
def register_blueprints(app):
app.register_blueprint(main_bp)
app.register_blueprint(user_bp, url_prefix='/user')
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(admin_bp, url_prefix='/admin')
app.register_blueprint(ajax_bp, url_prefix='/ajax')
def register_shell_context(app):
@app.shell_context_processor
def make_shell_context():
return dict(db=db, User=User, Photo=Photo, Tag=Tag,
Follow=Follow, Collect=Collect, Comment=Comment,
Notification=Notification)
def register_template_context(app):
@app.context_processor
def make_template_context():
if current_user.is_authenticated:
notification_count = Notification.query.with_parent(current_user).filter_by(is_read=False).count()
else:
notification_count = None
return dict(notification_count=notification_count)
def register_errorhandlers(app):
@app.errorhandler(400)
def bad_request(e):
return render_template('errors/400.html'), 400
@app.errorhandler(403)
def forbidden(e):
return render_template('errors/403.html'), 403
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'), 404
@app.errorhandler(413)
def request_entity_too_large(e):
return render_template('errors/413.html'), 413
@app.errorhandler(500)
def internal_server_error(e):
return render_template('errors/500.html'), 500
@app.errorhandler(CSRFError)
def handle_csrf_error(e):
return render_template('errors/400.html', description=e.description), 500
def register_commands(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()
def init():
"""Initialize Albumy."""
click.echo('Initializing the database...')
db.create_all()
click.echo('Initializing the roles and permissions...')
Role.init_role()
click.echo('Done.')
@app.cli.command()
@click.option('--user', default=10, help='Quantity of users, default is 10.')
@click.option('--follow', default=30, help='Quantity of follows, default is 30.')
@click.option('--photo', default=30, help='Quantity of photos, default is 30.')
@click.option('--tag', default=20, help='Quantity of tags, default is 20.')
@click.option('--collect', default=50, help='Quantity of collects, default is 50.')
@click.option('--comment', default=100, help='Quantity of comments, default is 100.')
def forge(user, follow, photo, tag, collect, comment):
"""Generate fake data."""
from albumy.fakes import fake_admin, fake_comment, fake_follow, fake_photo, fake_tag, fake_user, fake_collect
db.drop_all()
db.create_all()
click.echo('Initializing the roles and permissions...')
Role.init_role()
click.echo('Generating the administrator...')
fake_admin()
click.echo('Generating %d users...' % user)
fake_user(user)
click.echo('Generating %d follows...' % follow)
fake_follow(follow)
click.echo('Generating %d tags...' % tag)
fake_tag(tag)
click.echo('Generating %d photos...' % photo)
fake_photo(photo)
click.echo('Generating %d collects...' % photo)
fake_collect(collect)
click.echo('Generating %d comments...' % comment)
fake_comment(comment)
click.echo('Done.')