Skip to content

Commit

Permalink
模型序列化
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkGao11520 committed Jun 16, 2018
1 parent 03a18d3 commit d84879e
Show file tree
Hide file tree
Showing 26 changed files with 209 additions and 243 deletions.
358 changes: 137 additions & 221 deletions .idea/workspace.xml

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@
create by gaowenfeng on
"""

from .app import Flask

__author__ = "gaowenfeng"


def register_blueprint(app):
from app.api.v1 import create_blueprint_v1
app.register_blueprint(create_blueprint_v1(), url_prefix='/v1')


def register_plug(app):
from app.models.base import db
db.init_app(app)
with app.app_context():
db.create_all()


def create_app():
app = Flask(__name__)
app.config.from_object('app.config.secure')
app.config.from_object('app.config.setting')

register_blueprint(app)
register_plug(app)
return app
Binary file modified app/__pycache__/app.cpython-36.pyc
Binary file not shown.
Binary file modified app/api/v1/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified app/api/v1/__pycache__/book.cpython-36.pyc
Binary file not shown.
Binary file added app/api/v1/__pycache__/client.cpython-36.pyc
Binary file not shown.
Binary file added app/api/v1/__pycache__/token.cpython-36.pyc
Binary file not shown.
Binary file modified app/api/v1/__pycache__/user.cpython-36.pyc
Binary file not shown.
6 changes: 1 addition & 5 deletions app/api/v1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
@auth.login_required
def get_user(uid):
user = User.query.get_or_404(uid)
r = {
'nickname': user.nickname,
'email': user.email
}
return jsonify(r), 200
return jsonify(user), 200
33 changes: 17 additions & 16 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
"""
create by gaowenfeng on
"""
from flask import Flask
from flask import Flask as _Flask
from flask.json import JSONEncoder as _JSONEncoder

from datetime import date

from app.libs.error_code import ServerError

__author__ = "gaowenfeng"


def register_blueprint(app):
from app.api.v1 import create_blueprint_v1
app.register_blueprint(create_blueprint_v1(), url_prefix='/v1')
class JSONEncoder(_JSONEncoder):

def default(self, o):
if hasattr(o, 'keys') and hasattr(o, '__getitem__'):
return dict(o)
# 兼容其他的序列化
if isinstance(o, date):
return o.strftime('%Y-%m-%d')
raise ServerError()


def register_plug(app):
from app.models.base import db
db.init_app(app)
with app.app_context():
db.create_all()
class Flask(_Flask):
json_encoder = JSONEncoder


def create_app():
app = Flask(__name__)
app.config.from_object('app.config.secure')
app.config.from_object('app.config.setting')

register_blueprint(app)
register_plug(app)
return app

Binary file modified app/config/__pycache__/secure.cpython-36.pyc
Binary file not shown.
Binary file modified app/config/__pycache__/setting.cpython-36.pyc
Binary file not shown.
Binary file added app/libs/__pycache__/enums.cpython-36.pyc
Binary file not shown.
Binary file modified app/libs/__pycache__/error_code.cpython-36.pyc
Binary file not shown.
Binary file modified app/libs/__pycache__/redprint.cpython-36.pyc
Binary file not shown.
Binary file added app/libs/__pycache__/token_auth.cpython-36.pyc
Binary file not shown.
Binary file added app/models/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added app/models/__pycache__/base.cpython-36.pyc
Binary file not shown.
Binary file added app/models/__pycache__/user.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions app/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Base(db.Model):
def __init__(self):
self.create_time = int(datetime.now().timestamp())

def __getitem__(self, item):
return getattr(self, item)

def set_attrs(self, attrs_dict):
for key, value in attrs_dict.items():
if hasattr(self, key) and key != 'id':
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class User(Base):
nickname = Column(String(24), nullable=False)
_password = Column('password', String(128))

def keys(self):
return ('id', 'email', 'nickname')

@property
def password(self):
return self._password
Expand Down
23 changes: 23 additions & 0 deletions app/test/test_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
create by gaowenfeng on
"""

__author__ = "gaowenfeng"


class Person:
name = 'gwf'
age = 18

def __init__(self):
self.gender = 'male'

def keys(self):
return ('name', 'age', 'gender')

def __getitem__(self, item):
return getattr(self, item)


o = Person()
print(dict(o))
Binary file added app/validators/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added app/validators/__pycache__/base.cpython-36.pyc
Binary file not shown.
Binary file added app/validators/__pycache__/forms.cpython-36.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion ginger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from werkzeug.exceptions import HTTPException

from app.app import create_app
from app import create_app
from app.libs.error_code import ServerError
from app.libs.errors import APIException

Expand Down

0 comments on commit d84879e

Please sign in to comment.