Skip to content

Commit bd0c5c6

Browse files
Isolate app with Blueprint
1 parent 5780ba5 commit bd0c5c6

16 files changed

+25
-17
lines changed

tvseries/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from flask import Flask
2-
from flask_sqlalchemy import SQLAlchemy
32

43
from tvseries import config
4+
from tvseries.ext import db
5+
from tvseries.core import core_blueprint
56

67
print("Problem: Hen and egg (python circular imports)")
78
app = Flask(__name__)
89
app.config.from_object(config.DevelopmentConfig)
9-
10-
db = SQLAlchemy(app)
11-
12-
from tvseries.core import views # noqa
10+
app.register_blueprint(core_blueprint, url_prefix='/')
11+
db.init_app(app)

tvseries/core/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import Blueprint
2+
3+
core_blueprint = Blueprint('core', __name__, template_folder='templates',
4+
static_folder='static', static_url_path='core')
5+
6+
from tvseries.core import views # noqa
7+
from tvseries.core import models # noqa

tvseries/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from tvseries import db
1+
from tvseries.ext import db
22

33

44
class TVSerie(db.Model):
File renamed without changes.
File renamed without changes.

tvseries/core/views.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33

44
from flask import render_template, url_for, redirect, request
55

6-
from tvseries import app, db
6+
from tvseries.ext import db
7+
from tvseries.core import core_blueprint
78
from tvseries.core.models import TVSerie
8-
# ou ....
9-
# from .models import TVSerie
109

1110

12-
@app.route('/')
11+
@core_blueprint.route('')
1312
def home(name=None):
14-
image_directory = os.path.join(app.static_folder, 'img')
13+
image_directory = os.path.join(core_blueprint.static_folder, 'img')
1514
image_filenames = os.listdir(image_directory)
1615
image = os.path.join('img', choice(image_filenames))
17-
img_url = url_for('static', filename=image)
16+
img_url = url_for('core.static', filename=image)
1817
series = TVSerie.query.all()
1918
return render_template('home.html', series=series, image=img_url)
2019

2120

22-
@app.route('/add', methods=['GET', 'POST'])
21+
@core_blueprint.route('add', methods=['GET', 'POST'])
2322
def add():
2423
if request.method == 'POST':
2524
name = request.form.to_dict().get('serie-name')

tvseries/ext.py

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

tvseries/templates/navbar.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
<span class="icon-bar"></span>
88
<span class="icon-bar"></span>
99
</button>
10-
<a class="navbar-brand" href="{{ url_for('home') }}">
10+
<a class="navbar-brand" href="{{ url_for('core.home') }}">
1111
<span class="glyphicon glyphicon-home"></span>
1212
</a>
1313
</div>
1414
<div class="collapse navbar-collapse">
1515
<ul class="nav navbar-nav">
16-
<li class="dropdown {% if url_for('add') in request.path %}active{% endif %}">
16+
<li class="dropdown {% if url_for('core.add') in request.path %}active{% endif %}">
1717
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Seriados <span class="caret"></span></a>
1818
<ul class="dropdown-menu" role="menu">
19-
<li><a href="{{ url_for('add') }}"><span class="glyphicon glyphicon-plus-sign"></span>&nbsp;&nbsp;Adicionar</a></li>
19+
<li><a href="{{ url_for('core.add') }}"><span class="glyphicon glyphicon-plus-sign"></span>&nbsp;&nbsp;Adicionar</a></li>
2020
</ul>
2121
</li>
2222
</ul>

tvseries/tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_navbar(self, db):
5050

5151
@pytest.fixture
5252
def db(self, app, request):
53-
from tvseries import db as db_test
53+
from tvseries.ext import db as db_test
5454

5555
def teardown():
5656
db_test.drop_all()

0 commit comments

Comments
 (0)