runserver.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: runserver.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-10-25 22:01:29 (CST)
  9. # Last Update: 星期五 2018-02-23 10:43:07 (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 getpass import getpass
  18. from forums import create_app
  19. from forums.extension import db, cache, search
  20. from forums.api.user.models import User
  21. import click
  22. import os
  23. import sys
  24. app = create_app('config')
  25. app.wsgi_app = ProxyFix(app.wsgi_app)
  26. cli = FlaskGroup(add_default_commands=False, create_app=lambda r: app)
  27. cli.add_command(run_command)
  28. try:
  29. from flask_migrate import Migrate
  30. migrate = Migrate(app, db)
  31. except ImportError:
  32. pass
  33. @cli.command('shell', short_help='Starts an interactive shell.')
  34. def shell_command():
  35. ctx = current_app.make_shell_context()
  36. interact(local=ctx)
  37. @cli.command()
  38. def runserver():
  39. app.run()
  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.models 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('pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.
  94. format(babel_conf, ' '.join(src_path)))
  95. os.system('pybabel init -i messages.pot -d translations -l {0}'.format(
  96. lang))
  97. os.unlink('messages.pot')
  98. @cli.command()
  99. def babel_update():
  100. babel_conf = "translations/babel.cfg"
  101. src_path = ["forums", "templates"]
  102. os.system('pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.
  103. format(babel_conf, ' '.join(src_path)))
  104. os.system('pybabel update -i messages.pot -d translations')
  105. os.unlink('messages.pot')
  106. @cli.command()
  107. def babel_compile():
  108. os.system('pybabel compile -d translations')
  109. @cli.command()
  110. @click.option('-u', '--username')
  111. def delete_user(username):
  112. user = User.query.filter_by(username=username).first()
  113. user.delete()
  114. @cli.command()
  115. @click.option('-u', '--username')
  116. def password_user(username):
  117. password = getpass('Password:')
  118. user = User.query.filter_by(username=username).first()
  119. user.set_password(password)
  120. user.save()
  121. @cli.command()
  122. @click.option('-u', '--username')
  123. @click.option('-e', '--email')
  124. @click.option('-w', '--password')
  125. def create_user(username, email, password):
  126. if username is None:
  127. username = input('Username(default admin):') or 'admin'
  128. if email is None:
  129. email = input('Email:')
  130. if password is None:
  131. password = getpass('Password:')
  132. user = User(
  133. username=username, email=email, is_superuser=True, is_confirmed=True)
  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()