Exemplo n.º 1
0
def init(app):
    um = UserManager(app)
    db.init_app(app)
    admin = Admin(name='Chiki', base_template='base.html')

    admin.category_icon_classes = {
        u'运营': 'fa fa-hdd-o',
        u'日志': 'fa fa-database',
    }

    admin.add_view(UserView(um.models.User, name='用户'))
    admin.add_view(WeChatUserView(um.models.WeChatUser, name='微信用户'))
    admin.add_view(QQUserView(um.models.QQUser, name='QQ用户'))
    admin.add_view(WeiBoUser(um.models.WeiBoUser, name='微博用户'))

    # 日志
    admin.add_view(ItemView(Item, name='系统选项', category='日志'))
    admin.add_view(StatLogView(StatLog, name='统计日志', category='日志'))
    admin.add_view(TraceLogView(TraceLog, name='跟踪日志', category='日志'))

    admin.add_view(UserLogView(um.models.UserLog, name='用户日志', category='日志'))
    admin.add_view(
        PhoneCodeView(um.models.PhoneCode, name='手机验证码', category='日志'))
    admin.add_view(
        EmailCodeView(um.models.EmailCode, name='邮箱验证码', category='日志'))

    admin.init_app(app)
Exemplo n.º 2
0
def init(app):
    db.init_app(app)
    user = UserManager(app)
    user.init_wapis(wapi)
    user.init_web()
    wapi.init_app(app)
    media.init_app(app)

    @app.route("/test")
    def test():
        u = user.models.WeChatUser.objects(unionid="1223").first()
        login_user(u)
        return redirect(user.config.bind_url)
Exemplo n.º 3
0
def init(app):
    db.init_app(app)
    user = UserManager(app)
    user.init_wapis(wapi)
    user.init_web()
    wapi.init_app(app)
    media.init_app(app)

    @app.route('/test')
    def test():
        u = user.models.WeChatUser.objects(unionid='1223').first()
        login_user(u)
        return redirect(user.config.bind_url)
Exemplo n.º 4
0
Arquivo: web.py Projeto: OdayWu/chiki
def init(app):
    db.init_app(app)
    user = UserManager(app)
    user.init_wapis(wapi)
    user.init_web()
    wapi.init_app(app)
    media.init_app(app)

    @app.route('/test')
    def test():
        u = user.models.WeChatUser.objects(unionid='1212').first()
        if not u:
            u = user.models.WeChatUser(unionid='1212').save()
        login_user(u)
        return redirect(user.config.bind_url)


    @app.route('/')
    @login_required
    def index():
        return '123'
Exemplo n.º 5
0
def init(app):
    um = UserManager(app)
    db.init_app(app)
    admin = Admin(name="Chiki", base_template="base.html")

    admin.category_icon_classes = {u"运营": "fa fa-hdd-o", u"日志": "fa fa-database"}

    admin.add_view(UserView(um.models.User, name="用户"))
    admin.add_view(WeChatUserView(um.models.WeChatUser, name="微信用户"))
    admin.add_view(QQUserView(um.models.QQUser, name="QQ用户"))
    admin.add_view(WeiBoUser(um.models.WeiBoUser, name="微博用户"))

    # 日志
    admin.add_view(ItemView(Item, name="系统选项", category="日志"))
    admin.add_view(StatLogView(StatLog, name="统计日志", category="日志"))
    admin.add_view(TraceLogView(TraceLog, name="跟踪日志", category="日志"))

    admin.add_view(UserLogView(um.models.UserLog, name="用户日志", category="日志"))
    admin.add_view(PhoneCodeView(um.models.PhoneCode, name="手机验证码", category="日志"))
    admin.add_view(EmailCodeView(um.models.EmailCode, name="邮箱验证码", category="日志"))

    admin.init_app(app)
Exemplo n.º 6
0
def init(app):
    um = UserManager(app)
    db.init_app(app)
    um.init_apis(api)
    api.init_app(app)
Exemplo n.º 7
0
def init_app(init=None,
             config=None,
             pyfile=None,
             template_folder='templates',
             index=False,
             error=True,
             is_web=False,
             is_api=False):
    """ 创建应用 """

    app = Flask(__name__, template_folder=template_folder)
    if config:
        app.config.from_object(config)
    if pyfile:
        app.config.from_pyfile(pyfile)

    ENVVAR = app.config.get('ENVVAR')
    if ENVVAR and os.environ.get(ENVVAR):
        app.config.from_envvar(app.config['ENVVAR'])

    if app.debug:
        app.config.setdefault('DEBUG_TB_ENABLED', True)
        app.config.setdefault('DEBUG_TB_PANELS', DEBUG_TB_PANELS)
        app.config.setdefault('DEBUG_TB_INTERCEPT_REDIRECTS', False)

    DebugToolbarExtension(app)

    app.config.setdefault('SESSION_REFRESH_EACH_REQUEST', False)
    app.is_web = is_web
    app.is_api = is_api
    app.is_back = os.environ.get('CHIKI_BACK') == 'true'
    app.static_folder = app.config.get('STATIC_FOLDER')
    app.mail = Mail(app)

    def get_data_path(name):
        return os.path.abspath(
            os.path.join(app.config.get('DATA_FOLDER'), name))

    if app.config.get('USER_AGENT_LIMIT'):

        @app.before_request
        def before_request():
            if request.path == current_app.config.get('WEROBOT_ROLE'):
                return
            if not app.debug and 'micromessenger' not in request.headers[
                    'User-Agent'].lower():
                return error_msg('请用微信客户端扫一扫')

    app.get_data_path = get_data_path

    init_babel(app)
    init_redis(app)

    if app.config.get('SESSION_TYPE'):
        Session(app)

    init_jinja(app)
    init_logger(app)
    init_oauth(app)
    init_page(app)
    db.init_app(app)
    media.init_app(app)

    with app.app_context():
        cm.init_app(app)
        Choices.init()

    if callable(init):
        init(app)

    @app.context_processor
    def context_processor():
        return dict(Item=Item, Menu=Menu)

    if error:
        init_error_handler(app)

    if index:

        @app.route('/')
        def index():
            return redirect(app.config.get('INDEX_REDIRECT'))

    blueprint = Blueprint('chiki',
                          __name__,
                          template_folder=os.path.join(TEMPLATE_ROOT, 'chiki'))
    app.register_blueprint(blueprint)

    if app.is_back:

        @app.route('/chiki_back')
        def chiki_back():
            return 'true'

    with app.app_context():
        if hasattr(app, 'user_manager'):
            user = um.models.User.objects(id=100000).first()
            if not user:
                user = um.models.User(id=100000,
                                      phone='13888888888',
                                      password='******',
                                      nickname=app.config.get('SITE_NAME'))
                user.save()
            if not user.avatar and os.path.exists(
                    app.get_data_path('imgs/logo.jpg')):
                with open(app.get_data_path('imgs/logo.jpg')) as fd:
                    user.avatar = dict(stream=StringIO(fd.read()),
                                       format='jpg')
                user.save()

    return app
Exemplo n.º 8
0
def init(app):
    um = UserManager(app)
    db.init_app(app)
    um.init_apis(api)
    api.init_app(app)
Exemplo n.º 9
0
Arquivo: app.py Projeto: endsh/chiki
def init_app(init=None,
             config=None,
             pyfile=None,
             template_folder='templates',
             index=False,
             error=True,
             is_web=False,
             is_api=False,
             manager=False):
    """ 创建应用 """
    app = Flask(__name__, template_folder=template_folder)
    if os.environ.get('LOGGER_DEBUG'):
        handler = logging.StreamHandler()
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(logging.Formatter(DEBUG_LOG_FORMAT))
        app.logger.addHandler(handler)
        app.logger.setLevel(logging.DEBUG)

    init_db(db)

    if config:
        app.config.from_object(config)
    if pyfile:
        app.config.from_pyfile(pyfile)

    ENVVAR = app.config.get('ENVVAR')
    if ENVVAR and os.environ.get(ENVVAR):
        env = os.environ.get(ENVVAR)
        for pyfile in env.split('|'):
            if pyfile.startswith('./'):
                pyfile = os.path.join(os.getcwd(), pyfile)
            app.logger.info('load config pyfile: %s' % pyfile)
            app.config.from_pyfile(pyfile)

    if app.debug:
        app.config.setdefault('DEBUG_TB_ENABLED', True)
        app.config.setdefault('DEBUG_TB_PANELS', DEBUG_TB_PANELS)
        app.config.setdefault('DEBUG_TB_INTERCEPT_REDIRECTS', False)

    DebugToolbarExtension(app)

    app.config.setdefault('SESSION_REFRESH_EACH_REQUEST', False)
    app.is_web = is_web
    app.is_api = is_api
    app.is_admin = not is_web and not is_api
    app.is_back = os.environ.get('CHIKI_BACK') == 'true'
    app.static_folder = app.config.get('STATIC_FOLDER')
    app.mail = Mail(app)

    def get_data_path(name):
        return os.path.abspath(
            os.path.join(app.config.get('DATA_FOLDER'), name))

    if app.config.get('USER_AGENT_LIMIT', False):

        @app.before_request
        def before_request():
            if request.path == current_app.config.get('WEROBOT_ROLE'):
                return

            ua = request.headers['User-Agent'].lower()
            if not app.debug and 'micromessenger' not in ua:
                return error_msg('请用微信客户端扫一扫')

    app.get_data_path = get_data_path

    init_babel(app)
    init_redis(app)

    if not app.config.get('CACHE_TYPE'):
        app.config['CACHE_TYPE'] = 'simple'
    cache.init_app(app)

    if app.config.get('SESSION_TYPE'):
        Session(app)

    init_jinja(app)
    init_logger(app)
    init_oauth(app)
    init_third(app)
    init_page(app)

    db.init_app(app)
    media.init_app(app)

    if app.is_admin and not manager:
        with app.app_context():
            cm.init_app(app)
            Choices.init()

    global inits
    for i in inits:
        i(app)

    if callable(init):
        init(app)

    @app.context_processor
    def context_processor():
        return dict(Item=Item, Menu=Menu, url_for=url_for, ImageItem=ImageItem)

    if error:
        init_error_handler(app)

    if index:

        @app.route('/')
        def index():
            return redirect(app.config.get('INDEX_REDIRECT'))

    blueprint = Blueprint('chiki',
                          __name__,
                          template_folder=os.path.join(TEMPLATE_ROOT, 'chiki'))
    app.register_blueprint(blueprint)

    if app.is_back:

        @app.route('/chiki_back')
        def chiki_back():
            return 'true'

    if app.is_admin and not manager:
        with app.app_context():
            if hasattr(app, 'user_manager'):
                user = um.models.User.objects(id=100000).first()
                if not user:
                    user = um.models.User(id=100000,
                                          phone='13888888888',
                                          password='******',
                                          nickname=app.config.get('SITE_NAME'))
                    user.tid = user.create_tid()
                    user.save()
                if not user.avatar and os.path.exists(
                        app.get_data_path('imgs/logo.jpg')):
                    with open(app.get_data_path('imgs/logo.jpg')) as fd:
                        user.avatar = dict(stream=StringIO(fd.read()),
                                           format='jpg')
                    user.save()

    @app.route('/1.gif')
    def gif():
        return send_file(DATA_ROOT + '/1.gif',
                         cache_timeout=0,
                         add_etags=False,
                         mimetype='image/gif')

    @app.route('/test/error')
    def test_error():
        raise ValueError('testing!!!')

    @app.route('/trace/log', methods=['POST'])
    def trace_log():
        user = None
        if current_user.is_authenticated():
            user = current_user.id

        if current_app.config.get('FAST_MODE') is True:
            return json_success()

        TraceLog(
            user=user,
            key=request.form.get('key', ''),
            tid=request.form.get('tid', ''),
            label=request.form.get('label', ''),
            value=request.form.get('value', ''),
        ).save()
        return json_success()

    @app.route('/complaint/choose')
    @login_required
    def complaint_choose():
        complaint = sorted(Complaint.TYPE.DICT.iteritems(),
                           key=lambda x: x[1],
                           reverse=True)
        return render_template('complaint/choose.html', type=complaint)

    @app.route('/complaint/desc/')
    @login_required
    def complaint_desc():
        types = request.args.get('type', '')
        return render_template('complaint/desc.html', types=types)

    @app.route('/complaint/refer', methods=['POST'])
    @login_required
    def complaint_refer():
        types = request.form.get('type', '')
        content = request.form.get('content', '')
        complaints = Complaint(
            user=current_user.id,
            content=content,
            type=types,
            active=True,
        )
        complaints.create()
        complaints.save()
        return json_success(msg='success', num=complaints.id)

    @app.route('/complaint/save/')
    @login_required
    def complaint_save():
        num = request.args.get('num', '')
        return render_template('complaint/refer.html', num=num)

    @app.route('/complaint/active')
    @login_required
    def complaint_active():
        complaints = Complaint.objects(user=current_user.id,
                                       active=True).first()
        if complaints:
            complaints.user.complaint = True
            complaints.user.save()
        return ''

    init_db(db)

    return app
Exemplo n.º 10
0
Arquivo: app.py Projeto: endsh/chiki
def init_app(init=None, config=None, pyfile=None,
             template_folder='templates', index=False, error=True,
             is_web=False, is_api=False):
    """ 创建应用 """

    app = Flask(__name__, template_folder=template_folder)
    if config:
        app.config.from_object(config)
    if pyfile:
        app.config.from_pyfile(pyfile)

    ENVVAR = app.config.get('ENVVAR')
    if ENVVAR and os.environ.get(ENVVAR):
        app.config.from_envvar(app.config['ENVVAR'])

    if app.debug:
        app.config.setdefault('DEBUG_TB_ENABLED', True)
        app.config.setdefault('DEBUG_TB_PANELS', DEBUG_TB_PANELS)
        app.config.setdefault('DEBUG_TB_INTERCEPT_REDIRECTS', False)

    DebugToolbarExtension(app)

    app.config.setdefault('SESSION_REFRESH_EACH_REQUEST', False)
    app.is_web = is_web
    app.is_api = is_api
    app.is_back = os.environ.get('CHIKI_BACK') == 'true'
    app.static_folder = app.config.get('STATIC_FOLDER')
    app.mail = Mail(app)

    def get_data_path(name):
        return os.path.abspath(os.path.join(app.config.get('DATA_FOLDER'), name))

    if app.config.get('USER_AGENT_LIMIT'):
        @app.before_request
        def before_request():
            if request.path == current_app.config.get('WEROBOT_ROLE'):
                return
            if not app.debug and 'micromessenger' not in request.headers['User-Agent'].lower():
                return error_msg('请用微信客户端扫一扫')

    app.get_data_path = get_data_path

    init_babel(app)
    init_redis(app)

    if app.config.get('SESSION_TYPE'):
        Session(app)

    init_jinja(app)
    init_logger(app)
    init_oauth(app)
    init_third(app)
    init_page(app)
    db.init_app(app)
    media.init_app(app)

    with app.app_context():
        cm.init_app(app)
        Choices.init()

    if callable(init):
        init(app)

    @app.context_processor
    def context_processor():
        return dict(Item=Item, Menu=Menu)

    if error:
        init_error_handler(app)

    if index:
        @app.route('/')
        def index():
            return redirect(app.config.get('INDEX_REDIRECT'))

    blueprint = Blueprint('chiki', __name__,
                          template_folder=os.path.join(TEMPLATE_ROOT, 'chiki'))
    app.register_blueprint(blueprint)

    if app.is_back:
        @app.route('/chiki_back')
        def chiki_back():
            return 'true'

    with app.app_context():
        if hasattr(app, 'user_manager'):
            user = um.models.User.objects(id=100000).first()
            if not user:
                user = um.models.User(
                    id=100000, phone='13888888888', password='******',
                    nickname=app.config.get('SITE_NAME'))
                user.save()
            if not user.avatar and os.path.exists(app.get_data_path('imgs/logo.jpg')):
                with open(app.get_data_path('imgs/logo.jpg')) as fd:
                    user.avatar = dict(stream=StringIO(fd.read()), format='jpg')
                user.save()

    return app