Skip to content

Commit 5adbab3

Browse files
system module with flask app factory, db, commands, blueprints and routes
1 parent bc06c34 commit 5adbab3

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

system/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
from flask import Flask
2-
3-
from system.routes import routes_blueprint
4-
5-
6-
app = Flask(__name__)
7-
app.config.from_object('system.config')
8-
app.register_blueprint(routes_blueprint)

system/app.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask import Flask
2+
3+
from system.db import db, migrate
4+
from system.blueprints import register_blueprints
5+
from system.commands import register_commands
6+
7+
8+
def create_app():
9+
app = Flask(__name__)
10+
app.url_map.strict_slashes = False
11+
app.config.from_object('system.config')
12+
13+
db.app = app
14+
db.init_app(app)
15+
migrate.init_app(app, db)
16+
register_blueprints(app)
17+
register_commands(app)
18+
19+
return app

system/blueprints.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from system.routes import system_blueprint
2+
3+
4+
def register_blueprints(app):
5+
with app.app_context():
6+
app.register_blueprint(system_blueprint)
7+
8+
from telegram_bot.routes import telegram_blueprint
9+
app.register_blueprint(telegram_blueprint, url_prefix='/telegram')

system/commands.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import click
2+
3+
from flask.cli import with_appcontext
4+
5+
import telegram_bot.polling
6+
import telegram_bot.webhook
7+
8+
9+
@click.command('polling')
10+
@with_appcontext
11+
def start_polling():
12+
telegram_bot.polling.start_polling()
13+
14+
15+
@click.command('webhook')
16+
@with_appcontext
17+
def start_webhook():
18+
telegram_bot.webhook.set_webhook()
19+
20+
21+
def register_commands(app):
22+
app.cli.add_command(start_polling)
23+
app.cli.add_command(start_webhook)

system/db.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
from flask_migrate import Migrate
3+
4+
5+
db = SQLAlchemy()
6+
migrate = Migrate()

system/routes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from flask import Blueprint, current_app
1+
from flask import Blueprint
22

33

4-
routes_blueprint = Blueprint('system_routes', __name__)
4+
system_blueprint = Blueprint('system_routes', __name__)
55

66

7-
@routes_blueprint.route('/')
7+
@system_blueprint.route('/')
88
def index():
99
return 'It works'

0 commit comments

Comments
 (0)