Skip to content

Commit 402abdd

Browse files
authored
Merge pull request #3 from TTWShell/refactor
refactor base db and schema
2 parents 4e77696 + b4df029 commit 402abdd

File tree

5 files changed

+19
-41
lines changed

5 files changed

+19
-41
lines changed

hobbit_core/flask_hobbit/__init__.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,25 @@
66
"""
77

88
from flask import Flask
9-
from flask_sqlalchemy import SQLAlchemy
10-
from flask_marshmallow import Marshmallow
119

1210

1311
class HobbitManager:
1412

15-
def __init__(self, app=None, db=None, ma=None, **kwargs):
13+
def __init__(self, app=None, **kwargs):
1614
"""
1715
app: The Flask application instance.
18-
db: An Object-Database Mapper instance such as SQLAlchemy.
19-
ma: The Marshmallow instance.
2016
"""
2117
self.app = app
2218
if app is not None:
23-
self.init_app(app, db, ma, **kwargs)
19+
self.init_app(app, **kwargs)
2420

25-
def init_app(self, app, db, ma, **kwargs):
21+
def init_app(self, app, **kwargs):
2622
if not isinstance(app, Flask):
2723
raise TypeError(
2824
'flask_hobbit.HobbitManager.init_app(): '
2925
'Parameter "app" is an instance of class "{}" '
3026
'instead of a subclass of class "flask.Flask".'.format(
3127
app.__class__.__name__))
3228

33-
if not isinstance(db, SQLAlchemy):
34-
raise TypeError('flask_hobbit be dependent on SQLAlchemy.')
35-
self.db = db
36-
37-
if ma and not isinstance(ma, Marshmallow):
38-
raise TypeError('flask_hobbit be dependent on Marshmallow.')
39-
self.ma = ma
40-
4129
# Bind Flask-Hobbit to app
4230
app.hobbit_manager = self

hobbit_core/flask_hobbit/db.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import enum
22

3-
from sqlalchemy.orm import relationship
4-
from flask import current_app
5-
6-
db = current_app.hobbit_manager.db
7-
8-
Column = db.Column
9-
Model = db.Model
10-
relationship = relationship
3+
from sqlalchemy import Integer, Column, ForeignKey, func, DateTime
114

125

136
class SurrogatePK:
147
__table_args__ = {'extend_existing': True}
158

16-
id = db.Column(db.Integer, primary_key=True)
9+
id = Column(Integer, primary_key=True)
1710
created_at = Column(
18-
db.DateTime, nullable=False, server_default=db.func.now())
11+
DateTime, nullable=False, server_default=func.now())
1912
updated_at = Column(
20-
db.DateTime, nullable=False, server_default=db.func.now(),
21-
onupdate=db.func.now())
13+
DateTime, nullable=False, server_default=func.now(),
14+
onupdate=func.now())
2215

2316
def __repr__(self):
2417
return '<{classname}({pk}:{label!r})>'.format(
2518
classname=type(self).__name__, pk=self.id, label=self.label or '')
2619

2720

2821
def reference_col(tablename, nullable=False, pk_name='id', **kwargs):
29-
return db.Column(
30-
db.ForeignKey('{0}.{1}'.format(tablename, pk_name)),
22+
return Column(
23+
ForeignKey('{0}.{1}'.format(tablename, pk_name)),
3124
nullable=nullable, **kwargs)
3225

3326

hobbit_core/flask_hobbit/schemas.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from flask import current_app
2-
from marshmallow import fields
1+
from marshmallow import Schema, fields
32

4-
ma = current_app.hobbit_manager.ma
53

6-
7-
class PagedSchema(ma.Schema):
4+
class PagedSchema(Schema):
85
total = fields.Int()
96
page = fields.Int(missing=1, default=1)
107
page_size = fields.Int(missing=10, default=10)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from hobbit_core.flask_hobbit.db import db, SurrogatePK
1+
from hobbit_core.flask_hobbit.db import SurrogatePK, Column
2+
from {{ project_name }}.exts import db
23

34

45
class User(SurrogatePK, db.Model):
5-
username = db.Column(db.String(20), unique=True, nullable=False, doc='用户名')
6-
nick = db.Column(db.String(20), unique=True, nullable=False, doc='昵称')
6+
username = Column(db.String(20), unique=True, nullable=False, doc='用户名')
7+
nick = Column(db.String(20), unique=True, nullable=False, doc='昵称')
78

hobbit_core/hobbit/static/bootstrap/shire/{{ project_name }}/run.py.jinja2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ from hobbit_core.flask_hobbit.err_handler import ErrHandler
66

77
from {{ project_name }}.exts import db, migrate, ma, hobbit
88
from {{ project_name }} import config
9+
from {{ project_name }} import views
910

1011

1112
def register_extensions(app):
1213
db.init_app(app)
1314
migrate.init_app(app, db)
1415
ma.init_app(app)
15-
hobbit.init_app(app, db, ma)
16+
hobbit.init_app(app)
1617

1718

1819
def register_blueprints(app):
19-
with app.app_context():
20-
from {{ project_name }} import views
21-
app.register_blueprint(views.bp, url_prefix='/api')
20+
app.register_blueprint(views.bp, url_prefix='/api')
2221

2322

2423
def register_error_handler(app):

0 commit comments

Comments
 (0)