def post(self): form = LoginFrom(self.arguments) fail = False if form.validate(): admin = Admin.get_or_none(username=self.get_argument("username")) if admin and Admin.check_password(admin.password, self.get_argument("password")): remember_me = self.get_argument("remember", "off") if remember_me == "on": expires_days = 30 else: expires_days = None self.login(admin, expires_days) if self.next_url: self.redirect(self.next_url) else: self.redirect(self.reverse_url("admin_home")) return fail = True self.render("login.html", form=form, fail=fail)
def login(self, admin, expires_days=None): self.set_secure_cookie("admin", tornado.escape.json_encode({ "id": admin.id, "username": admin.username }), expires_days=expires_days) Admin.update(last_login=datetime.now()).where( Admin.id == admin.id).execute()
def get(self, action): # 只有超级管理员可以修改管理员信息 if not self.current_user.is_super: self.redirect(self.reverse_url("admin_admins")) if action == "add": admin = Admin() form = CreateAdminForm(obj=admin) else: admin = Admin.get_or_404(id=self.get_argument("id")) form = EditAdminForm(obj=admin) self.render("admin/form.html", form=form, admin=admin, action=action)
def get_current_user(self): admin = self.get_secure_cookie("admin") if not admin: return None try: admininfo = json.loads(admin.decode("utf-8")) except: admininfo = None if admininfo and admininfo.get("id", None): admin = Admin.get_or_none(id=admininfo['id']) if admin is not None: return admin
def get(self): keyword = self.get_argument("kw", "") query = Admin.select() if keyword: query = query.where( (Admin.name.contains(keyword) ) | (Admin.username.contains(keyword) ) | (Admin.email.contains(keyword) ) | (Admin.mobile == keyword) ) query = query.order_by(Admin.id.asc()) admins = self.paginate_query(query) self.render("admin/list.html", admins=admins )
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()
def validate_username(self, field): if Admin.select().where(Admin.username == field.data).count() > 0: raise ValidationError('用户名已存在')
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)