123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # **************************************************************************
- # Copyright © 2016-2019 jianglin
- # File Name: runserver.py
- # Author: jianglin
- # Email: mail@honmaple.com
- # Created: 2016-10-25 22:01:29 (CST)
- # Last Update: Monday 2022-12-12 15:29:04 (CST)
- # By:
- # Description:
- # **************************************************************************
- from flask import current_app
- from flask.cli import FlaskGroup, run_command
- from werkzeug.contrib.fixers import ProxyFix
- from code import interact
- from forums import create_app
- from forums.extension import db, cache, search
- from forums.api.user.db import User
- import click
- import os
- import sys
- app = create_app('config')
- app.wsgi_app = ProxyFix(app.wsgi_app)
- cli = FlaskGroup(add_default_commands=False, create_app=lambda r: app)
- cli.add_command(run_command)
- try:
- from flask_migrate import Migrate
- migrate = Migrate(app, db)
- except ImportError:
- pass
- @cli.command('shell', short_help='Starts an interactive shell.')
- def shell_command():
- ctx = current_app.make_shell_context()
- interact(local=ctx)
- @cli.command()
- @click.option('-p', '--port', default=8000)
- def runserver(port):
- app.run(port=port)
- @cli.command()
- def create_index():
- return search.create_index()
- @cli.command()
- def update_index():
- return search.create_index(update=True)
- @cli.command()
- def delete_index():
- return search.create_index(delete=True)
- @cli.command()
- def clear_cache():
- cache.clear()
- @cli.command()
- def init_perm():
- from forums.api.user.db import Group
- anonymous = Group.query.filter_by(name='anonymous').first()
- if not anonymous:
- anonymous = Group(name='anonymous')
- anonymous.save()
- logined = Group.query.filter_by(name='logined').first()
- if not logined:
- logined = Group(name='logined')
- logined.save()
- for rule in app.url_map.iter_rules():
- # print(rule.rule, rule.subdomain, rule.methods, rule.endpoint)
- print(rule.endpoint)
- methods = []
- for method in rule.methods:
- methods.append(method)
- method = 'get' if method in ['HEAD', 'OPTIONS'] else method.lower()
- if not rule.endpoint.startswith('admin'):
- anonymous.add_perm(
- method,
- rule.endpoint,
- description='anonymous组允许{}'.format(methods))
- logined.add_perm(
- method,
- rule.endpoint,
- description='logined组允许{}'.format(methods))
- @cli.command()
- def initdb():
- """
- Drops and re-creates the SQL schema
- """
- db.drop_all()
- db.configure_mappers()
- db.create_all()
- db.session.commit()
- @cli.command()
- @click.option('-l', '--lang', default='zh')
- def babel_init(lang):
- babel_conf = "translations/babel.cfg"
- src_path = ["forums", "templates"]
- os.system(
- 'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
- babel_conf, ' '.join(src_path)))
- os.system(
- 'pybabel init -i messages.pot -d translations -l {0}'.format(lang))
- os.unlink('messages.pot')
- @cli.command()
- def babel_update():
- babel_conf = "translations/babel.cfg"
- src_path = ["forums", "templates"]
- os.system(
- 'pybabel extract -F {0} -k lazy_gettext -o messages.pot {1}'.format(
- babel_conf, ' '.join(src_path)))
- os.system('pybabel update -i messages.pot -d translations')
- os.unlink('messages.pot')
- @cli.command()
- def babel_compile():
- os.system('pybabel compile -d translations')
- @cli.command()
- @click.option('-u', '--username')
- def delete_user(username):
- user = User.query.filter_by(username=username).first()
- user.delete()
- @cli.command()
- @click.option('-u', '--username')
- @click.password_option('-p', '--password')
- def password_user(username, password):
- user = User.query.filter_by(username=username).first()
- user.set_password(password)
- user.save()
- @cli.command(short_help='Create user.')
- @click.option('-u', '--username', prompt=True, default="admin")
- @click.option('-e', '--email', prompt=True)
- @click.password_option('-p', '--password')
- def create_user(username, email, password):
- user = User(
- username=username,
- email=email,
- is_superuser=True,
- is_confirmed=True,
- )
- user.set_password(password)
- user.save()
- if __name__ == '__main__':
- if len(sys.argv) == 1:
- app.run()
- else:
- cli.main()
|