manager.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # *************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: db_create.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2016-02-11 13:34:38
  7. # *************************************************************************
  8. # !/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from flask_script import Manager
  11. from flask_migrate import Migrate, MigrateCommand
  12. from maple import app, db
  13. from maple.user.models import User, UserInfor, UserSetting, Role
  14. from getpass import getpass
  15. from werkzeug.security import generate_password_hash
  16. import os
  17. migrate = Migrate(app, db)
  18. manager = Manager(app)
  19. @manager.command
  20. def run():
  21. return app.run()
  22. @manager.command
  23. def init_db():
  24. """
  25. Drops and re-creates the SQL schema
  26. """
  27. db.drop_all()
  28. db.configure_mappers()
  29. db.create_all()
  30. db.session.commit()
  31. @manager.command
  32. def babel_init():
  33. pybabel = 'pybabel'
  34. os.system(pybabel +
  35. ' extract -F babel.cfg -k lazy_gettext -o messages.pot maple')
  36. os.system(pybabel + ' init -i messages.pot -d maple/translations -l zh')
  37. os.unlink('messages.pot')
  38. @manager.command
  39. def babel_update():
  40. pybabel = 'pybabel'
  41. os.system(pybabel +
  42. ' extract -F babel.cfg -k lazy_gettext -o messages.pot maple')
  43. os.system(pybabel + ' update -i messages.pot -d maple/translations')
  44. os.unlink('messages.pot')
  45. @manager.command
  46. def babel_compile():
  47. pybabel = 'pybabel'
  48. os.system(pybabel + ' compile -d maple/translations')
  49. @manager.option('-u', '--username', dest='username', default='admin')
  50. @manager.option('-e', '--email', dest='email')
  51. @manager.option('-w', '--password', dest='password')
  52. def create_user(username, email, password):
  53. if username == 'admin':
  54. username = input('Username(default admin):')
  55. if email is None:
  56. email = input('Email:')
  57. if password is None:
  58. password = getpass('Password:')
  59. user = User()
  60. user.username = username
  61. user.password = generate_password_hash(password)
  62. user.email = email
  63. user.is_superuser = True
  64. userinfor = UserInfor()
  65. user.infor = userinfor
  66. usersetting = UserSetting()
  67. user.setting = usersetting
  68. role = Role.query.filter_by(rolename='super').first()
  69. if role is None:
  70. role = Role()
  71. role.rolename = 'super'
  72. user.roles.append(role)
  73. db.session.add(user)
  74. db.session.commit()
  75. @manager.option('-h', '--host', dest='host', default='127.0.0.1')
  76. @manager.option('-p', '--port', dest='port', type=int, default=8000)
  77. @manager.option('-w', '--workers', dest='workers', type=int, default=2)
  78. def gunicorn(host, port, workers):
  79. """use gunicorn"""
  80. from gunicorn.app.base import Application
  81. class FlaskApplication(Application):
  82. def init(self, parser, opts, args):
  83. return {'bind': '{0}:{1}'.format(host, port), 'workers': workers}
  84. def load(self):
  85. return app
  86. application = FlaskApplication()
  87. return application.run()
  88. manager.add_command('db', MigrateCommand)
  89. if __name__ == '__main__':
  90. manager.run()