runserver.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016-2019 jianglin
  5. # File Name: runserver.py
  6. # Author: jianglin
  7. # Email: mail@honmaple.com
  8. # Created: 2016-10-25 22:01:29 (CST)
  9. # Last Update: Monday 2022-12-12 15:29:04 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import current_app
  14. from flask.cli import FlaskGroup, run_command
  15. from werkzeug.contrib.fixers import ProxyFix
  16. from code import interact
  17. from forums import create_app
  18. from forums.extension import db, cache, search
  19. from forums.api.user.db import User
  20. import click
  21. import os
  22. import sys
  23. app = create_app('config')
  24. app.wsgi_app = ProxyFix(app.wsgi_app)
  25. cli = FlaskGroup(add_default_commands=False, create_app=lambda r: app)
  26. cli.add_command(run_command)
  27. try:
  28. from flask_migrate import Migrate
  29. migrate = Migrate(app, db)
  30. except ImportError:
  31. pass
  32. @cli.command('shell', short_help='Starts an interactive shell.')
  33. def shell_command():
  34. ctx = current_app.make_shell_context()
  35. interact(local=ctx)
  36. @cli.command()
  37. @click.option('-p', '--port', default=8000)
  38. def runserver(port):
  39. app.run(port=port)
  40. @cli.command()
  41. def create_index():
  42. return search.create_index()
  43. @cli.command()
  44. def update_index():
  45. return search.create_index(update=True)
  46. @cli.command()
  47. def delete_index():
  48. return search.create_index(delete=True)
  49. @cli.command()
  50. def clear_cache():
  51. cache.clear()
  52. @cli.command()
  53. def init_perm():
  54. from forums.api.user.db import Group
  55. anonymous = Group.query.filter_by(name='anonymous').first()
  56. if not anonymous:
  57. anonymous = Group(name='anonymous')
  58. anonymous.save()
  59. logined = Group.query.filter_by(name='logined').first()
  60. if not logined:
  61. logined = Group(name='logined')
  62. logined.save()
  63. for rule in app.url_map.iter_rules():
  64. # print(rule.rule, rule.subdomain, rule.methods, rule.endpoint)
  65. print(rule.endpoint)
  66. methods = []
  67. for method in rule.methods:
  68. methods.append(method)
  69. method = 'get' if method in ['HEAD', 'OPTIONS'] else method.lower()
  70. if not rule.endpoint.startswith('admin'):
  71. anonymous.add_perm(
  72. method,
  73. rule.endpoint,
  74. description='anonymous组允许{}'.format(methods))
  75. logined.add_perm(
  76. method,
  77. rule.endpoint,
  78. description='logined组允许{}'.format(methods))
  79. @cli.command()
  80. def initdb():
  81. """
  82. Drops and re-creates the SQL schema
  83. """
  84. db.drop_all()
  85. db.configure_mappers()
  86. db.create_all()
  87. db.session.commit()
  88. @cli.command()
  89. @click.option('-l', '--lang', default='zh')
  90. def babel_init(lang):
  91. babel_conf = "translations/babel.cfg"
  92. src_path = ["forums", "templates"]
  93. os.system(
  94. 'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
  95. babel_conf, ' '.join(src_path)))
  96. os.system(
  97. 'pybabel init -i messages.pot -d translations -l {0}'.format(lang))
  98. os.unlink('messages.pot')
  99. @cli.command()
  100. def babel_update():
  101. babel_conf = "translations/babel.cfg"
  102. src_path = ["forums", "templates"]
  103. os.system(
  104. 'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
  105. babel_conf, ' '.join(src_path)))
  106. os.system('pybabel update -i messages.pot -d translations')
  107. os.unlink('messages.pot')
  108. @cli.command()
  109. def babel_compile():
  110. os.system('pybabel compile -d translations')
  111. @cli.command()
  112. @click.option('-u', '--username')
  113. def delete_user(username):
  114. user = User.query.filter_by(username=username).first()
  115. user.delete()
  116. @cli.command()
  117. @click.option('-u', '--username')
  118. @click.password_option('-p', '--password')
  119. def password_user(username, password):
  120. user = User.query.filter_by(username=username).first()
  121. user.set_password(password)
  122. user.save()
  123. @cli.command(short_help='Create user.')
  124. @click.option('-u', '--username', prompt=True, default="admin")
  125. @click.option('-e', '--email', prompt=True)
  126. @click.password_option('-p', '--password')
  127. def create_user(username, email, password):
  128. user = User(
  129. username=username,
  130. email=email,
  131. is_superuser=True,
  132. is_confirmed=True,
  133. )
  134. user.set_password(password)
  135. user.save()
  136. if __name__ == '__main__':
  137. if len(sys.argv) == 1:
  138. app.run()
  139. else:
  140. cli.main()