# from datetime import timedelta from flask import Flask, session from flask_migrate import Migrate, MigrateCommand # from flask_sqlalchemy import SQLAlchemy # from redis import StrictRedis # from flask_session import Session from flask_script import Manager # from config import DevelopConfig from info import create_app from flask import Blueprint # 利用模块创建app app = create_app("dev") # 创建管理器 mgr = Manager(app) # 创建迁移命令 mgr.add_command("mc", MigrateCommand) if __name__ == '__main__': mgr.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db, models # manage.py是程序启动的入口,只关心启动的相关参数以及内容,不关心具体该 # 通过指定配置名字创建对应的配置app,接收返回来的flask的app对象 # create_app 就类似于工厂方法,传不同参数,制造不同的小黄车 app = create_app("deverlopmentconfig") # 使用flask—script的命令格式操作flask,先连接一下 manager = Manager(app) # 将app 与 db 关联 Migrate(app, db) # 将迁移命令添加到manager中 manager.add_command("db", MigrateCommand) if __name__ == '__main__': manager.run()
from info import create_app, db from flask_migrate import Migrate, MigrateCommand from flask_script import Manager """ #迁移 $ python manage.py db init $ python manage.py db migrate -m"initial" $ python manage.py db upgrade from绝对路径时如果报错 则项目右键 Mark-Directory > Sources Root """ #迁移时须引入models文件 from info import models # 创建 app,并传入配置模式:dev(开发) / prod(生产) app = create_app('dev') #Flask-script manager = Manager(app) #数据库迁移 Migrate(app, db) manager.add_command('db', MigrateCommand) if __name__ == '__main__': #如果pycharm启动需要配置runserver manager.run()
from info import create_app, db, models #导入models是因为有了模型类才能创建数据表 from flask_script import Manager from flask_migrate import Migrate, MigrateCommand # 利用函数返回的的值app来解决问题 app = create_app("develop") #develop就是开发模式product就是线上模式 # 创建Manager对象 manager = Manager(app) # 创建Migrate对象 Migrate(app, db) # 添加db命令 manager.add_command("db", MigrateCommand) if __name__ == '__main__': manager.run()
import datetime import random from flask import current_app from flask_migrate import MigrateCommand from flask_script import Manager from info import create_app app = create_app('dev') # 创建应用,传入的参数决定环境的配置 # 创建管理器 mgr = Manager(app) # 添加迁移命令 mgr.add_command('mc', MigrateCommand) # 生成超级管理员 @mgr.option('-u', dest='username') @mgr.option('-p', dest='password') def create_superuser(username, password): if not all([username, password]): print('账号/密码不完整') return from info import db from info.models import User user = User() user.mobile = username user.password = password user.nick_name = username user.is_admin = True
# -*- coding:utf-8 -*- # Jason Mess from flask import Flask, session, current_app from flask_script import Manager from info import create_app, db from flask_migrate import Migrate, MigrateCommand import logging """ 单一职责的原则:manage.py 仅仅作为项目启动文件 整体业务逻辑都在info那里 """ """ 7: 创建manager管理类 """ app = create_app("development") manager = Manager(app) """ 初始化迁移对象 将迁移命令添加到管理对象中 """ Migrate(app, db) manager.add_command("db", MigrateCommand) @app.route('/') def hello_world(): """ session调整存储方式 没有调整之前,数据存在flask后端服务器,只是将session_id使用cookie的方式给了客户端 :return:
import logging from flask import session from flask_script import Manager from flask_migrate import MigrateCommand,Migrate from info import create_app,db from info.models import * # 通过传入不同配置创造出不同配置下的app实例,工厂方法 app = create_app('develop') # 6. 配置manager manager = Manager(app) # 7. 设置sql迁移 Migrate(app,db) manager.add_command('db',MigrateCommand) @manager.option("-n","--username",dest="username") @manager.option("-p","--password",dest="password") def createsuperuser(username,password): if not all([username,password]): print("参数不全") user = User() user.nick_name = username user.mobile = username user.password = password user.is_admin = 1 try: db.session.add(user) db.session.commit()
from flask_migrate import MigrateCommand from flask_script import Manager # 创建应用 from info import create_app app = create_app("pro") # 创建管理器 mgr = Manager(app) # 管理器生成迁移变量 mgr.add_command('mc',MigrateCommand) @app.route('/') def index(): return 'hello world' if __name__ == '__main__': mgr.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import db from info import create_app app = create_app('pro') manage = Manager(app) # mysql数据迁移 Migrate(app, db) manage.add_command('sql', MigrateCommand) @app.route("/") def index(): # redis_store.set('name','zhangsnan') from flask import session session['name'] = 'zd' return "index" if __name__ == '__main__': manage.run()
from flask_migrate import MigrateCommand, Migrate from flask_script import Manager from flask import Flask from info import create_app, db, models # 创建app app = create_app("Development") # type: Flask # 脚本管理flask以及添加迁移命令至脚本命令中 manager = Manager(app) Migrate(app, db) manager.add_command("db", MigrateCommand) if __name__ == '__main__': print(app.url_map) manager.run()
from info import create_app from flask_script import Manager app = create_app() manager = Manager(app) if __name__ == "__main__": manager.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand import pymysql from info import create_app, db, redis_store from info.models import * pymysql.install_as_MySQLdb() """ manager文件只去实现项目的启动和数据库的迁移,其他项目配置,app应用相关都应该抽取到专门的文件中 """ # 传入'development'就是开发环境 # 传入'prodution'就是生产环境 app = create_app('production') # 使得数据库具备迁移能力 Migrate(app, db) # 设置管理对象 manager = Manager(app) # 加入管理命令行 manager.add_command('db', MigrateCommand) # 装饰器定义的是创建管理员的命令行选项,以及对于的字段 @manager.option('-n', '-name', dest='name') @manager.option('-p', '-password', dest='password') def createsuperuser(name, password): """创建管理员用户""" if not all([name, password]): print('参数不足')
# from flask import Flask from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db # # app = Flask(__name__) # app.config.from_object(Config) # db = SQLAlchemy(app) # redis = StrictRedis(host = Config.REDIS_HOST,port = Config.REDIS_PORT) # # # CSRFProtect(app) # Session(app) # 脚本管理对象 app = create_app("unit") manager = Manager(app) # 数据库迁移 Migrate(app, db) # 数据库添加到manager manager.add_command("mysql", MigrateCommand) @app.route("/") def index(): # from flask import session # session["name"] = "haahhaha" return "ok page"
#coding=utf-8 import datetime import random from flask_migrate import Migrate, MigrateCommand from flask_script import Manager from info import create_app, db, models from info.models import User app = create_app('developmentConfig') # app = create_app('productionConfig') # 创建管理类对象 manager = Manager(app) Migrate(app, db) # 添加命令 manager.add_command('db', MigrateCommand) @manager.option('-n', '-name', dest='name') @manager.option('-p', '-password', dest='password') def createsuperuser(name, password): """创建管理员用户""" if not all([name, password]): print('参数不足') return from info.models import User user = User() user.mobile = name user.nick_name = name user.password = password user.is_admin = True
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db from config import DevelopmentConfig, ProductionConfig from info import models app = create_app(DevelopmentConfig) # manager启动程序 manager = Manager(app) # 数据库迁移 Migrate(app, db) manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from flask import current_app # 导入models的作用是让整个应用程序知道有models的存在 from info import create_app, db, models from info.models import User import os env = os.getenv("APP_ENV", "develop").lower() app = create_app(env) # 创建manager对象, 管理app manager = Manager(app) # 使用Migrate 关联app, db Migrate(app, db) # 给manager 添加一条操作命令 manager.add_command("db", MigrateCommand) # 定义方法,创建管理员对象 # @manager.option 给manager添加一个脚本运行的方法 # 参数1:在调用方法的时候传递的参数名称 # 参数2:对参数1的解释 # 参数3:目的参数,用户传递给形式参数使用 @manager.option('-u', '--username', dest='username') @manager.option('-p', '--password', dest='password') def create_superuser(username, password): # 1.创建用户对象
from flask.ext.migrate import Migrate, MigrateCommand from flask.ext.script import Manager from info import create_app, db from info import models from info.models import User config_name = 'develop' app = create_app(config_name) manager = Manager(app) Migrate(app, db) manager.add_command('mysql', MigrateCommand) # 创建管理员对象 @manager.option('-n', '--name', dest='name') @manager.option('-p', '--password', dest='password') def create_super_user(name, password): user = User() user.mobile = name user.nick_name = name user.password = password user.is_admin = True db.session.add(user) db.session.commit() if __name__ == '__main__': manager.run()
from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from info import create_app app, db, redis_store = create_app() class BaseModel(object): """模型基类,为每个模型补充创建时间与更新时间""" create_time = db.Column(db.DateTime, default=datetime.now) # 记录的创建时间 update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) # 记录的更新时间 # 用户收藏表,建立用户与其收藏新闻多对多的关系 tb_user_collection = db.Table( "info_user_collection", db.Column("user_id", db.Integer, db.ForeignKey("info_user.id"), primary_key=True), # 新闻编号 db.Column("news_id", db.Integer, db.ForeignKey("info_news.id"), primary_key=True), # 分类编号 db.Column("create_time", db.DateTime, default=datetime.now) # 收藏创建时间 ) tb_user_follows = db.Table( "info_user_fans",
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db from info import models app = create_app('development') manager = Manager(app) Migrate(app, db) manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run()
""" 项目基本配置与测试 -- 只用作最基本的运行工作 date: 18-11-7 下午7:54 """ from info import create_app, mysql_db from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import models from info.utils.common import add_test_users # 创建 app, 选择项目环境:development(开发) / production(上线) # app = create_app("development") app = create_app("production") # 使用终端脚本工具启动和管理Flask项目 manager = Manager(app) # 初始化数据库迁移模块 Migrate(app, mysql_db) # 给终端脚本工具新增数据迁移的相关命令 manager.add_command("db", MigrateCommand) @manager.option("-n", "--name", dest="name") @manager.option("-p", "--password", dest="password") def createsuperuser(name, password): """ 使用命令创建管理员用户 -- python manage.py createsuperuser -n 用户名 -p 密码 :param name: 用户名 :param password: 密码 :return: """
""" 1.集成配置类 2.集成sqlalchemy 3.集成redis,测试redis是否能写入 # 4.集成CSRFProtect 5.session保存到redis的配置 6.集成flask_script 7.集成flask_migrate """ from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db from info.models import * app = create_app("develop") # 6.集成flask_script manager = Manager(app) # 7.集成数据库迁移flask_migrate,在flask中对数据库迁移 Migrate(app, db) manager.add_command("db", MigrateCommand) if __name__ == '__main__': # print(app.url_map) manager.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from info import create_app, db, models # 通过指定的配置名字创建对应的app from info.models import User app = create_app("default") manager = Manager(app) # 将app与db进行关联 Migrate(app, db) # 将迁移命令添加到manager中 manager.add_command('db', MigrateCommand) # 创建管理员账户 @manager.option('-n', '-name', dest="name") @manager.option('-p', '-password', dest="password") def createsuperuser(name, password): if not all([name, password]): print("参数不足") user = User() user.nick_name = name user.mobile = name user.password = password user.is_admin = True try: db.session.add(user)