manager.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: manager.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-10-25 22:08:39 (CST)
  9. # Last Update:星期五 2018-01-05 00:35:45 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask_script import Manager
  14. from flask_migrate import Migrate, MigrateCommand
  15. from forums import create_app
  16. from forums.extension import db, cache
  17. from forums.api.user.models import User, UserInfo, UserSetting
  18. from getpass import getpass
  19. from datetime import datetime
  20. import os
  21. app = create_app('config')
  22. migrate = Migrate(app, db)
  23. manager = Manager(app)
  24. @manager.command
  25. def create_index():
  26. from forums.extension import search
  27. return search.create_index()
  28. @manager.command
  29. def update_index():
  30. from forums.extension import search
  31. return search.create_index(update=True)
  32. @manager.command
  33. def delete_index():
  34. from forums.extension import search
  35. return search.create_index(delete=True)
  36. @manager.command
  37. def clear_cache():
  38. with app.app_context():
  39. cache.clear()
  40. @manager.command
  41. def test_index():
  42. from forums.extension import search
  43. from forums.api.topic.models import Topic
  44. results = search.whoosh_search(Topic, '河海', ['title'], 1)
  45. print('results:')
  46. print(results)
  47. for i in results:
  48. print(i['title'])
  49. print(i.highlights("title")) # 高亮标题中的检索词
  50. @manager.command
  51. def runserver():
  52. return app.run()
  53. @manager.command
  54. def init_perm():
  55. from forums.api.user.models import Group
  56. anonymous = Group.query.filter_by(name='anonymous').first()
  57. if not anonymous:
  58. anonymous = Group(name='anonymous')
  59. anonymous.save()
  60. logined = Group.query.filter_by(name='logined').first()
  61. if not logined:
  62. logined = Group(name='logined')
  63. logined.save()
  64. for rule in app.url_map.iter_rules():
  65. # print(rule.rule, rule.subdomain, rule.methods, rule.endpoint)
  66. print(rule.endpoint)
  67. methods = []
  68. for method in rule.methods:
  69. methods.append(method)
  70. method = 'get' if method in ['HEAD', 'OPTIONS'] else method.lower()
  71. if not rule.endpoint.startswith('admin'):
  72. anonymous.add_perm(
  73. method,
  74. rule.endpoint,
  75. description='anonymous组允许{}'.format(methods))
  76. logined.add_perm(
  77. method,
  78. rule.endpoint,
  79. description='logined组允许{}'.format(methods))
  80. @manager.command
  81. def init_db():
  82. """
  83. Drops and re-creates the SQL schema
  84. """
  85. db.drop_all()
  86. db.configure_mappers()
  87. db.create_all()
  88. db.session.commit()
  89. @manager.command
  90. def babel_init():
  91. pybabel = 'pybabel'
  92. os.system(pybabel +
  93. ' extract -F babel.cfg -k lazy_gettext -o messages.pot forums')
  94. os.system(pybabel + ' init -i messages.pot -d translations -l zh')
  95. os.unlink('messages.pot')
  96. @manager.command
  97. def babel_update():
  98. pybabel = 'pybabel'
  99. os.system(
  100. pybabel +
  101. ' extract -F babel.cfg -k lazy_gettext -o messages.pot forums templates'
  102. )
  103. os.system(pybabel + ' update -i messages.pot -d translations')
  104. os.unlink('messages.pot')
  105. @manager.command
  106. def babel_compile():
  107. pybabel = 'pybabel'
  108. os.system(pybabel + ' compile -d translations')
  109. @manager.option('-u', '--username', dest='username')
  110. def delete_user(username):
  111. user = User.query.filter_by(username=username).first()
  112. user.delete()
  113. @manager.option('-u', '--username', dest='username')
  114. def password_user(username):
  115. password = getpass('Password:')
  116. user = User.query.filter_by(username=username).first()
  117. user.set_password(password)
  118. user.save()
  119. @manager.option('-u', '--username', dest='username')
  120. @manager.option('-e', '--email', dest='email')
  121. @manager.option('-w', '--password', dest='password')
  122. def create_user(username, email, password):
  123. if username is None:
  124. username = input('Username(default admin):') or 'admin'
  125. if email is None:
  126. email = input('Email:')
  127. if password is None:
  128. password = getpass('Password:')
  129. user = User(username=username, email=email)
  130. user.set_password(password)
  131. user.is_superuser = True
  132. user.is_confirmed = True
  133. user.save()
  134. @manager.option('-h', '--host', dest='host', default='127.0.0.1')
  135. @manager.option('-p', '--port', dest='port', type=int, default=8000)
  136. @manager.option('-w', '--workers', dest='workers', type=int, default=2)
  137. def gunicorn(host, port, workers):
  138. """use gunicorn"""
  139. from gunicorn.app.base import Application
  140. class FlaskApplication(Application):
  141. def init(self, parser, opts, args):
  142. return {'bind': '{0}:{1}'.format(host, port), 'workers': workers}
  143. def load(self):
  144. return app
  145. application = FlaskApplication()
  146. return application.run()
  147. manager.add_command('db', MigrateCommand)
  148. if __name__ == '__main__':
  149. manager.run()