Exemple #1
0
    def post(self, action):

        # 只有超级管理员可以修改管理员信息
        if not self.current_user.is_super:
            raise tornado.web.HTTPError(403)

        if action == "add":
            admin = Admin()
            form = CreateAdminForm(self.arguments, obj=admin)
        else:
            admin = Admin.get_or_404(id=self.get_argument("id"))
            form = EditAdminForm(self.arguments, obj=admin)

        if form.validate() and self.validate_is_super(form, action, admin):
            self.logger.debug(form.manage_provinces.data)
            form.populate_obj(admin)

            if action == "add":
                admin.password = Admin.create_password(admin.password)

            elif action == "edit" and self.get_argument("newpassword", None):
                admin.password = Admin.create_password(
                    self.get_argument("newpassword", None))
                admin.password_changed = datetime.now()

            admin.email = admin.email if admin.email else None
            admin.save()

            if action == "add":
                self.flash("添加管理员成功", category="success")
            else:
                self.flash("修改管理员成功", category="success")

            self.redirect(self.reverse_url('admin_admins'))
            return

        self.validate_is_super(form, action, admin)

        self.render("admin/form.html",
                    form=form,
                    admin=admin,
                    action=action)
def main():

    settings = setting_from_object(local_settings)
    if settings.get('debug', False):
        options.logging = "debug"

    tornado.options.parse_command_line()

    if options.debug:
        settings['debug'] = True

    if options.cmd == 'createall':
        """Create all database tables"""

        create_app(settings)

        if not Sport.table_exists():
            Sport.create_table()

        if not User.table_exists():
            User.create_table()

        if not Team.table_exists():
            Team.create_table()

        if not TeamMemberGroup.table_exists():
            TeamMemberGroup.create_table()

        if not TeamMember.table_exists():
            TeamMember.create_table()

        if not Activity.table_exists():
            Activity.create_table()

        if not Admin.table_exists():
            Admin.create_table()

        if not Match.table_exists():
            Match.create_table()

        if not MatchStatus.table_exists():
            MatchStatus.create_table()

        models = find_subclasses(BaseModel)
        for model in models:
            if model._meta.db_table.startswith("__"):
                print(("table skip: " + model._meta.db_table))
            elif model.table_exists():
                print(('table exist: ' + model._meta.db_table))
            else:
                model.create_table()
                print(('table created: ' + model._meta.db_table))

        print('create all [ok]')

    elif options.cmd == 'createadmin':
        app = create_app(settings)
        Admin.create(username="******",
                     password=Admin.create_password("admin"),
                     mobile="17088888888",
                     email="*****@*****.**",
                     name="Admin",
                     is_superadmin=True,
                     state=1)

    elif options.cmd == 'createclient':
        app = create_app(settings)
        Client.create(name="ios",
                      key=create_token(32),
                      secret=create_token(32))

    elif options.cmd == 'run_as_wsgi':
        logging.info('server started. port %s' % options.port)

        import gevent.wsgi

        app = create_app(settings)

        # 转换成wsgi实例
        wsgi_app = tornado.wsgi.WSGIAdapter(app)

        http_server = gevent.wsgi.WSGIServer(('', options.port), wsgi_app)
        http_server.serve_forever()

    elif options.cmd == 'runserver':

        tornado.platform.asyncio.AsyncIOMainLoop().install()
        ioloop = asyncio.get_event_loop()

        app = create_app(settings)
        app.listen(options.port, xheaders=True)

        print("running...")
        ioloop.run_forever()
    elif options.cmd == "fix_notify":
        from fix_script.fix_match_start_nofity import fix_match_start_notify
        fix_match_start_notify()