Skip to content

Commit 391c3b8

Browse files
committed
split views and models
1 parent 7ad5738 commit 391c3b8

File tree

9 files changed

+59
-52
lines changed

9 files changed

+59
-52
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import bp

examples/flaskr/flaskr/auth/models.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from sqlalchemy.ext.hybrid import hybrid_property
2+
from werkzeug.security import check_password_hash
3+
from werkzeug.security import generate_password_hash
4+
5+
from flaskr import db
6+
7+
8+
class User(db.Model):
9+
id = db.Column(db.Integer, primary_key=True)
10+
username = db.Column(db.String, unique=True, nullable=False)
11+
_password = db.Column("password", db.String, nullable=False)
12+
13+
@hybrid_property
14+
def password(self):
15+
return self._password
16+
17+
@password.setter
18+
def password(self, value):
19+
"""Store the password as a hash for security."""
20+
self._password = generate_password_hash(value)
21+
22+
def check_password(self, value):
23+
return check_password_hash(self.password, value)

examples/flaskr/flaskr/auth.py renamed to examples/flaskr/flaskr/auth/views.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,9 @@
88
from flask import request
99
from flask import session
1010
from flask import url_for
11-
from sqlalchemy.ext.hybrid import hybrid_property
12-
from werkzeug.security import check_password_hash
13-
from werkzeug.security import generate_password_hash
1411

1512
from flaskr import db
16-
17-
18-
class User(db.Model):
19-
id = db.Column(db.Integer, primary_key=True)
20-
username = db.Column(db.String, unique=True, nullable=False)
21-
_password = db.Column("password", db.String, nullable=False)
22-
23-
@hybrid_property
24-
def password(self):
25-
return self._password
26-
27-
@password.setter
28-
def password(self, value):
29-
"""Store the password as a hash for security."""
30-
self._password = generate_password_hash(value)
31-
32-
def check_password(self, value):
33-
return check_password_hash(self.password, value)
34-
13+
from flaskr.auth.models import User
3514

3615
bp = Blueprint("auth", __name__, url_prefix="/auth")
3716

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import bp

examples/flaskr/flaskr/blog/models.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from flask import url_for
2+
3+
from flaskr import db
4+
from flaskr.auth.models import User
5+
6+
7+
class Post(db.Model):
8+
id = db.Column(db.Integer, primary_key=True)
9+
author_id = db.Column(db.ForeignKey(User.id), nullable=False)
10+
created = db.Column(
11+
db.DateTime, nullable=False, server_default=db.func.current_timestamp()
12+
)
13+
title = db.Column(db.String, nullable=False)
14+
body = db.Column(db.String, nullable=False)
15+
16+
# User object backed by author_id
17+
# lazy="joined" means the user is returned with the post in one query
18+
author = db.relationship(User, lazy="joined", backref="posts")
19+
20+
@property
21+
def update_url(self):
22+
return url_for("blog.update", id=self.id)
23+
24+
@property
25+
def delete_url(self):
26+
return url_for("blog.delete", id=self.id)

examples/flaskr/flaskr/blog.py renamed to examples/flaskr/flaskr/blog/views.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,8 @@
88
from werkzeug.exceptions import abort
99

1010
from flaskr import db
11-
from flaskr.auth import login_required
12-
from flaskr.auth import User
13-
14-
15-
class Post(db.Model):
16-
id = db.Column(db.Integer, primary_key=True)
17-
author_id = db.Column(db.ForeignKey(User.id), nullable=False)
18-
created = db.Column(
19-
db.DateTime, nullable=False, server_default=db.func.current_timestamp()
20-
)
21-
title = db.Column(db.String, nullable=False)
22-
body = db.Column(db.String, nullable=False)
23-
24-
# User object backed by author_id
25-
# lazy="joined" means the user is returned with the post in one query
26-
author = db.relationship(User, lazy="joined", backref="posts")
27-
28-
@property
29-
def update_url(self):
30-
return url_for("blog.update", id=self.id)
31-
32-
@property
33-
def delete_url(self):
34-
return url_for("blog.delete", id=self.id)
35-
11+
from flaskr.auth.views import login_required
12+
from flaskr.blog.models import Post
3613

3714
bp = Blueprint("blog", __name__)
3815

examples/flaskr/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from flaskr import create_app
77
from flaskr import db
88
from flaskr import init_db
9-
from flaskr.auth import User
10-
from flaskr.blog import Post
9+
from flaskr.auth.models import User
10+
from flaskr.blog.models import Post
1111

1212
_user1_pass = generate_password_hash("test")
1313
_user2_pass = generate_password_hash("other")

examples/flaskr/tests/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask import g
33
from flask import session
44

5-
from flaskr.auth import User
5+
from flaskr.auth.models import User
66

77

88
def test_register(client, app):

examples/flaskr/tests/test_blog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pytest
22

33
from flaskr import db
4-
from flaskr.auth import User
5-
from flaskr.blog import Post
4+
from flaskr.auth.models import User
5+
from flaskr.blog.models import Post
66

77

88
def test_index(client, auth):

0 commit comments

Comments
 (0)