open-law/app/__init__.py

150 lines
4.5 KiB
Python
Raw Normal View History

2023-04-20 13:10:16 +00:00
import os
2023-06-13 08:34:17 +00:00
import warnings
2023-06-21 13:22:14 +00:00
from uuid import uuid4
2023-04-20 13:10:16 +00:00
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from werkzeug.exceptions import HTTPException
from flask_migrate import Migrate
2023-05-25 12:44:23 +00:00
from flask_admin import Admin
2023-04-20 13:10:16 +00:00
from app.logger import log
# instantiate extensions
login_manager = LoginManager()
db = SQLAlchemy()
migration = Migrate()
def create_app(environment="development"):
from config import config
from app.views import (
main_blueprint,
auth_blueprint,
user_blueprint,
2023-04-24 06:57:38 +00:00
book_blueprint,
2023-04-21 11:40:20 +00:00
home_blueprint,
2023-05-09 14:56:29 +00:00
vote_blueprint,
2023-05-11 15:02:14 +00:00
approve_blueprint,
2023-05-12 13:49:28 +00:00
star_blueprint,
permissions_blueprint,
2023-05-23 12:52:49 +00:00
search_blueprint,
notifications_blueprint,
2023-04-20 13:10:16 +00:00
)
2023-05-25 12:44:23 +00:00
from app import models as m
2023-04-20 13:10:16 +00:00
# Instantiate app.
app = Flask(__name__)
# Set app config.
env = os.environ.get("APP_ENV", environment)
configuration = config(env)
app.config.from_object(configuration)
configuration.configure(app)
log(log.INFO, "Configuration: [%s]", configuration.ENV)
# Set up extensions.
db.init_app(app)
migration.init_app(app, db)
login_manager.init_app(app)
# Register blueprints.
app.register_blueprint(auth_blueprint)
app.register_blueprint(main_blueprint)
app.register_blueprint(user_blueprint)
2023-04-24 06:57:38 +00:00
app.register_blueprint(book_blueprint)
2023-04-21 11:40:20 +00:00
app.register_blueprint(home_blueprint)
2023-05-09 14:56:29 +00:00
app.register_blueprint(vote_blueprint)
2023-05-11 15:02:14 +00:00
app.register_blueprint(approve_blueprint)
2023-05-12 13:49:28 +00:00
app.register_blueprint(star_blueprint)
app.register_blueprint(permissions_blueprint)
2023-05-23 12:52:49 +00:00
app.register_blueprint(search_blueprint)
app.register_blueprint(notifications_blueprint)
2023-04-20 13:10:16 +00:00
# Set up flask login.
@login_manager.user_loader
def get_user(id):
2023-05-25 12:44:23 +00:00
return m.User.query.get(int(id))
2023-04-20 13:10:16 +00:00
login_manager.login_view = "auth.login"
login_manager.login_message_category = "info"
2023-05-25 12:44:23 +00:00
login_manager.anonymous_user = m.AnonymousUser
2023-04-20 13:10:16 +00:00
2023-04-28 11:41:29 +00:00
# Jinja globals
2023-05-19 08:08:52 +00:00
from app.controllers.jinja_globals import (
form_hidden_tag,
display_inline_elements,
2023-05-19 08:08:52 +00:00
build_qa_url_using_interpretation,
2023-05-30 11:48:28 +00:00
recursive_render,
has_permission,
2023-05-19 08:08:52 +00:00
)
2023-04-28 11:41:29 +00:00
2023-06-20 08:10:27 +00:00
app.jinja_env.globals["type"] = type
app.jinja_env.globals["m"] = m
2023-06-21 13:22:14 +00:00
app.jinja_env.globals["str"] = str
app.jinja_env.globals["uuid"] = uuid4
2023-06-20 08:10:27 +00:00
app.jinja_env.globals["Access"] = m.Permission.Access
app.jinja_env.globals["EntityType"] = m.Permission.Entity
2023-04-28 13:03:48 +00:00
app.jinja_env.globals["form_hidden_tag"] = form_hidden_tag
app.jinja_env.globals["display_inline_elements"] = display_inline_elements
2023-05-19 08:08:52 +00:00
app.jinja_env.globals["build_qa_url"] = build_qa_url_using_interpretation
2023-05-30 11:48:28 +00:00
app.jinja_env.globals["recursive_render"] = recursive_render
app.jinja_env.globals["has_permission"] = has_permission
2023-04-28 11:41:29 +00:00
2023-04-20 13:10:16 +00:00
# Error handlers.
@app.errorhandler(HTTPException)
def handle_http_error(exc):
return render_template("error.html", error=exc), exc.code
2023-05-25 12:44:23 +00:00
# flask admin
2023-06-08 11:35:40 +00:00
from app.controllers.admin import (
2023-05-25 12:44:23 +00:00
CustomAdminIndexView,
2023-06-08 11:35:40 +00:00
UsersView,
BooksView,
CollectionsView,
SectionsView,
InterpretationView,
CommentView,
TagView,
2023-06-09 13:36:09 +00:00
BookContributorView,
2023-05-25 12:44:23 +00:00
)
admin = Admin(
app,
name="Open Law Admin",
template_mode="bootstrap3",
index_view=CustomAdminIndexView(),
)
2023-06-12 08:21:07 +00:00
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Fields missing from ruleset", UserWarning)
for view in [
UsersView(m.User, db.session, name="User", endpoint="/user_"),
BooksView(m.Book, db.session, name="Book", endpoint="/book_"),
CollectionsView(
m.Collection, db.session, name="Collection", endpoint="/collection_"
),
SectionsView(m.Section, db.session, name="Section", endpoint="/section_"),
InterpretationView(
m.Interpretation,
db.session,
name="Interpretation",
endpoint="/interpretation_",
),
CommentView(m.Comment, db.session, name="Comment", endpoint="/comment_"),
TagView(m.Tag, db.session, name="Tag", endpoint="/tag_"),
BookContributorView(
m.BookContributor,
db.session,
name="BookContributor",
endpoint="/book_contributor_",
),
]:
admin.add_view(view)
2023-05-25 12:44:23 +00:00
2023-04-20 13:10:16 +00:00
return app