# along with Indico; if not, see <http://www.gnu.org/licenses/>. from __future__ import unicode_literals, absolute_import from inspect import getmro from flask_marshmallow import Marshmallow from flask_marshmallow.sqla import SchemaOpts from marshmallow import fields from marshmallow_enum import EnumField from marshmallow_sqlalchemy import ModelConverter from sqlalchemy.orm import ColumnProperty from indico.core.db.sqlalchemy import UTCDateTime, PyIntEnum mm = Marshmallow() class IndicoModelConverter(ModelConverter): SQLA_TYPE_MAPPING = ModelConverter.SQLA_TYPE_MAPPING.copy() SQLA_TYPE_MAPPING.update({ UTCDateTime: fields.DateTime, PyIntEnum: EnumField }) def _get_field_kwargs_for_property(self, prop): kwargs = super(IndicoModelConverter, self)._get_field_kwargs_for_property(prop) if isinstance(prop, ColumnProperty) and hasattr(prop.columns[0].type, 'marshmallow_get_field_kwargs'): kwargs.update(prop.columns[0].type.marshmallow_get_field_kwargs()) return kwargs
from inspect import getmro from flask_marshmallow import Marshmallow from flask_marshmallow.sqla import SchemaOpts from marshmallow import fields, post_dump, pre_load from marshmallow_enum import EnumField from marshmallow_sqlalchemy import ModelConverter from marshmallow_sqlalchemy import ModelSchema as MSQLAModelSchema from sqlalchemy.orm import ColumnProperty from webargs.flaskparser import parser as webargs_flask_parser from indico.core import signals from indico.core.db.sqlalchemy import PyIntEnum, UTCDateTime mm = Marshmallow() class IndicoModelConverter(ModelConverter): SQLA_TYPE_MAPPING = ModelConverter.SQLA_TYPE_MAPPING.copy() SQLA_TYPE_MAPPING.update({ UTCDateTime: fields.DateTime, PyIntEnum: EnumField }) def _get_field_kwargs_for_property(self, prop): kwargs = super(IndicoModelConverter, self)._get_field_kwargs_for_property(prop) if isinstance(prop, ColumnProperty) and hasattr(prop.columns[0].type, 'marshmallow_get_field_kwargs'): kwargs.update(prop.columns[0].type.marshmallow_get_field_kwargs()) return kwargs
@schema_view.setter def schema_view(self, schema_view): self.__apply_schema_view(schema_view) @property def field_names(self): return self.fields.keys() @classmethod def resolve_schema_name(cls, schema_view=None): schema_view_name = schema_view.name if isinstance(schema_view, SchemaView) else schema_view return ":".join(filter(None, [cls.__schema__ or cls.__name__.rsplit('Schema', 1)[0], schema_view_name])) ######################################## # Marshmallow ######################################## ma = Marshmallow() # Attach our classes to the Flask-Marshmallow extension ma.SchemaOpts = SchemaOpts ma.Enum = ma_ext_fields.Enum = Enum ma.Html = ma_ext_fields.Html = Html ma.Nested = ma_ext_fields.Nested = Nested ma.Method = ma_ext_fields.Method = Method ma.Function = ma_ext_fields.Function = Function ma.ExternalId = ma_ext_fields.ExternalId = ExternalId ma.NullableString = ma_ext_fields.NullableString = NullableString ma.NullableInteger = ma_ext_fields.NullableInteger = NullableInteger
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow import os app = Flask(__name__) basedir = os.path.abspath(os.path.dirname(__file__)) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'contact_model.sqlite') db = SQLAlchemy(app) ma = Marshmallow(app) from contact_model.app_v1 import app_v1_bp, api_v1 from contact_model.homepage import app_hp_bp, api_hp app.register_blueprint(app_v1_bp, url_prefix='/api/v1') app.register_blueprint(app_hp_bp)
from flask_sqlalchemy import SQLAlchemy as db from sqlalchemy.ext.declarative import declarative_base from passlib.apps import custom_app_context as pwd_context from flask_marshmallow import Marshmallow from flask import Flask import USER engine = Flask(__name__) engine.config['SQLALCHEMY_DATABASE_URI']="mysql://*****:*****@localhost" engine.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True base = db(engine) base.engine.execute("create database if not exists safeway") base.engine.execute("use safeway") ma=Marshmallow(engine) class User(base.Model): __tablename__ = 'users' def hash_passpword(self,password): self.password = pwd_context.encrypt(password) def verify_password(self,password): return pwd_context.verify(password,self.password) id = base.Column(base.Integer,primary_key=True) uname = base.Column(base.String(length=50)) fname = base.Column(base.String(length=50)) mname = base.Column(base.String(length=50)) lname = base.Column(base.String(length=50)) role = base.Column(base.String(length=10))
import os from flask import Flask from flask_migrate import Migrate from flask_marshmallow import Marshmallow from flexmeasures.data.config import configure_db_for, db from flexmeasures.data.transactional import after_request_exception_rollback_session ma: Marshmallow = Marshmallow() def register_at(app: Flask): # First configure the central db object and Alembic's migration tool configure_db_for(app) Migrate(app, db, directory=os.path.join(app.root_path, "data", "migrations")) global ma ma.init_app(app) app.teardown_request(after_request_exception_rollback_session)
import os from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow from flaskr.config import ProductionConfig, DevelopmentConfig, TestingConfig from flaskr.utils import cmd, bp, err, log, auth __version__ = (1, 0, 0, "dev") db = SQLAlchemy() ma = Marshmallow() def create_app(config=None): """创建APP实例""" app = Flask(__name__, instance_relative_config=True) # 加载配置信息 if config is None: FLASK_ENV = os.environ.get("FLASK_ENV") if FLASK_ENV == 'development': app.config.from_object(DevelopmentConfig()) elif FLASK_ENV == 'testing': app.config.from_object(TestingConfig()) else: app.config.from_object(ProductionConfig()) else: app.config.update(config) # 初始化 log.init_app(app) # 日志记录